界面、图片加载、按键事件
import pygame # pygame2d游戏
import time
from pygame.locals import *
def main(): # 01
screen = pygame.display.set_mode((480, 852), 0, 32) # 1窗口
background = pygame.image.load("./feiji/background.png") # 2背景
hero = pygame.image.load("./feiji/hero1.png") # 7
x = 210#10
y = 700#10
while True: # 5死循环,一直重复
screen.blit(background, (0, 0)) # 3背景贴到窗口00处
# screen.blit(hero,(210,700))#8飞机位置
screen.blit(hero, (x, y))#9
pygame.display.update() # 4显示贴的效果
for event in pygame.event.get():#11
if event.type==QUIT:#按下叉号
print("exit")
exit()#退出程序,循环结束是break
elif event.type==KEYDOWN:#按下键盘键
if event.key==K_a or event.key==K_LEFT:
print("left")
x-=5#12
elif event.key==K_d or event.key==K_RIGHT:
print("right")
x+=5#13
elif event.key==K_SPACE:
print("space")
time.sleep(0.01) # 6电脑配置不高的话,内存占用会很大,所以延时显示可以减少内存消耗
if __name__ == '__main__': # 0
main()