打飞机代码:显示、控制玩具飞机-面向过程
import pygame
from pygame.locals import *
'''
3. 使用面向过程的方式来显示一个飞机,并控制其左右移动
'''
if __name__ == "__main__":
screen = pygame.display.set_mode((480,890),0,32)
background = pygame.image.load("./feiji/background.png").convert()
hero = pygame.image.load("./feiji/hero.gif").convert()
x=0
y=0
while True:
screen.blit(background,(0,0))
screen.blit(hero,(x,y))
for event in pygame.event.get():
if event.type == QUIT:
print("exit")
exit()
elif event.type == KEYDOWN:
if event.key == K_a or event.key == K_LEFT:
print('left')
x-=5
elif event.key == K_d or event.key == K_RIGHT:
print('right')
x+=5
elif event.key == K_SPACE:
print('space')
pygame.display.update()