python - 为什么 pygame 窗口动画仅在我移动光标时起作用
问题描述
我正在按照本指南https://www.youtube.com/watch?v=UZg49z76cLw&t=1309s制作飞扬的鸟 ,但屏幕仅在我移动光标时才会更新有谁知道如何解决这个问题
import pygame, sys
def draw_floor():
screen.blit(floor_surface, (floor_animation, 400))
screen.blit(floor_surface, (floor_animation + 275,400))
pygame.init()
screen = pygame.display.set_mode((275,512))
clock = pygame.time.Clock()
bg_surface = pygame.image.load('C:/Users/cuerv/Downloads/flappy-bird-assets-master/flappy-bird-assets-master/sprites/background-day.png').convert()
floor_surface = pygame.image.load('C:/Users/cuerv/Downloads/flappy-bird-assets-master/flappy-bird-assets-master/sprites/base.png').convert()
floor_animation = 0
bird_surface = pygame.image.load('C:/Users/cuerv/Downloads/flappy-bird-assets-master/flappy-bird-assets-master/sprites/bluebird-midflap.png').convert()
bird_rect = bird_surface.get_rect(center = (100,256))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.blit(bg_surface, (0, 0))
screen.blit(bird_surface, (bird_rect))
floor_animation -= 1
draw_floor()
if floor_animation <= -275:
floor_animation = 0
screen.blit(floor_surface, (floor_animation, 400))
pygame.display.update()
clock.tick(120)
enter code here
解决方案
它是缩进的问题。在应用程序循环而不是事件循环中绘制场景:
# application loop
while True:
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#<--| INDENTATION
screen.blit(bg_surface, (0, 0))
screen.blit(bird_surface, (bird_rect))
floor_animation -= 1
draw_floor()
if floor_animation <= -275:
floor_animation = 0
screen.blit(floor_surface, (floor_animation, 400))
pygame.display.update()
注意,事件循环仅在事件发生时执行,而应用程序循环是连续执行的。
推荐阅读
- php - 如何使用模块 11 算法计算和获取数字
- javascript - 如何使用 DJANGO python 在 POSTGRESQL 数据库中保存 JSON 数组数据?
- javascript - 如何修复我的 Blazor 服务器应用程序文件下载过程?
- aws-sdk-go - AWS golang sdk v2 描述安全组新手问题
- php - 如何删除 Woo 商务订单列表中的货币?
- server-sent-events - SSE(服务器发送事件)“重试”选项应该如何工作?
- flutter - 应用程序在加载正确的屏幕之前加载错误的屏幕
- python - 解析位置失败的错误消息
- google-apps-script - 将格式化文本从 Google 表格转换为 Google 文档
- sql - 使用 ALTER 添加新列后,数据错误地加载到 Hive 分区表中