看红色代码
# 游戏时钟
clock = pygame.time.Clock()
# 定义画面中各组件的初始位置
plane_rect = pygame.Rect(0, 168, 64, 64)
bg_rect = pygame.Rect(0, 10, 600, 400)
foreground_rect = pygame.Rect(0, 302, 1018, 98)
foreground_light_rect = pygame.Rect(580, 302, 1018, 98)
pygame.key.set_repeat(1, 1) # 表示每隔1毫秒发送一个pygame.KEYDOWN,事件间隔1毫秒
# 无限循环,窗口不关闭,(游戏循环)
while True:
# 一秒钟60帧
clock.tick(60)
# 修改初始位置的x,y坐标
foreground_rect.x -= 1
# plane_rect.x += 1
bg_rect.x -= 1
# 获取事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
# 直接退出系统
exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN :
plane_rect.y += 5
if plane_rect.bottom >=464 :
plane_rect.y = 0
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP :
plane_rect.y -= 5
if plane_rect.bottom <= 0:
plane_rect.y = 400
# break
surface.blit(bg, bg_rect)
surface.blit(plane1, plane_rect)
surface.blit(foreground, foreground_rect)
# 屏幕更新才能看到图片
if foreground_rect.x == -438:
foreground_rect.x = 0
if bg_rect.x == -638:
bg_rect.x = 0
pygame.display.update()
print(event)