改成这样(图片路径自己改回来):
import pygame
import sys
size = width, height = 800, 600
screen = pygame.display.set_mode(size)
# 加载图片背景
game_background = pygame.image.load(r'E:\Python\项目\飞机大战\images\background.png')
# 绘制 surface(背景图片) 对象
screen.blit(game_background, (0, 0))
# 加载我方飞机素材
our_aircraft = pygame.image.load(r'E:\Python\项目\飞机大战\images\me1.png')
# 获得我方飞机的矩形范围
our_aircraft_rect = our_aircraft.get_rect()
# 初始化我方飞机位置
our_aircraft_rect = our_aircraft_rect.move(400, 400)
# 绘制 surface(我方飞机) 对象
screen.blit(our_aircraft, our_aircraft_rect)
# 画面刷新
pygame.display.flip()
# 用判断发生点击事件是否点击到我方飞机
click_event = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if our_aircraft_rect.left < event.pos[0] < \
our_aircraft_rect.right and our_aircraft_rect.top < event.pos[1] < our_aircraft_rect.bottom:
click_event = True
if event.type == pygame.MOUSEBUTTONUP:
click_event = False
if event.type == pygame.MOUSEMOTION and click_event:
if click_event:
our_aircraft_rect = our_aircraft_rect.move(event.rel[0], event.rel[1])
if our_aircraft_rect.top < 0:
our_aircraft_rect.top = 0
elif our_aircraft_rect.bottom > height:
our_aircraft_rect.bottom = height
if our_aircraft_rect.left < 0:
print(1)
our_aircraft_rect.left = 0
elif our_aircraft_rect.right > width:
our_aircraft_rect.right = width
# 重绘窗口
screen.blit(game_background, (0, 0))
screen.blit(our_aircraft, our_aircraft_rect)
# 更新窗口
pygame.display.update()
|