gpt4 book ai didi

python - 为什么pygame窗口动画只在我移动光标时起作用

转载 作者:行者123 更新时间:2023-12-04 03:55:25 25 4
gpt4 key购买 nike

我正在按照本指南制作 flappy bird 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

最佳答案

这是Indentation的问题.在应用程序循环而不是事件循环中绘制场景:

# 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()

注意,事件循环只在事件发生时执行,而应用程序循环是连续执行的。

关于python - 为什么pygame窗口动画只在我移动光标时起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63982853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com