# 1。0 加载背景图片
# 2。0 加载飞机图片
# 3,0 监控鼠标键盘操作并且控制飞机移动
import random
import pygame
import sys
def main():
# 1,创建窗口
screen = pygame.display.set_mode((480,852),0,32)
# 2.创建一个背景图片: 1.load 导入图片 2.blit 放到屏幕上
background = pygame.image.load('background.png')
plane = pygame.image.load('hero1.png')
planex = 210
planey = 300
while True:
# event 事件,我们对电脑的每一种操作,例如按键,按鼠标,移动鼠标
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
# 往左
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
planex -= 5
if planex < -20:
planex = 5
elif event.key == pygame.K_d:
planex += 5
# # 修改飞机的x坐标
# planey += 1
# planex += 1
screen.blit(background,(0,0)) # 把背景图片放在窗口上
screen.blit(plane,(planex,planey)) # 确定飞机的位置,控制飞机的坐标
pygame.display.update()
if __name__ == '__main__':
main()
来源:CSDN
作者:少儿编程侯老师
链接:https://blog.csdn.net/houlaos/article/details/103848216