我是 pygame 的初学者,我一直在学习如何创建 Flappy Bird 游戏。我在 FlappyBird/assets
文件夹中找到了一张图片,如下所示。
这样做之后,我添加了代码以在屏幕上显示图像:
# Pygame flappy bird program
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((288, 512))
bg_surface = pygame.image.load('assets/background-day.png').convert()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.blit(bg_surface, (0, 0))
但是最后还是显示了部分画面,如下图:
这是为什么呢?对我的代码的任何帮助或评论将不胜感激。
最佳答案
您错过了通过 pygame.display.update()
更新显示或 pygame.display.flip()
:
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((288, 512))
bg_surface = pygame.image.load('assets/background-day.png').convert()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.blit(bg_surface, (0, 0))
pygame.display.flip()
您实际上是在 Surface
上绘图目的。如果您在与 PyGame 显示相关联的 Surface 上绘图,这不会立即显示在显示中。当显示更新为 pygame.display.update()
时,更改变得可见。或 pygame.display.flip()
.
This will update the contents of the entire display.
典型的 PyGame 应用程序循环必须:
- 通过
pygame.event.pump()
处理事件或pygame.event.get()
. - 根据输入事件和时间(分别为帧)更新对象的游戏状态和位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象) - 通过
pygame.display.update()
更新显示或pygame.display.flip()
关于python - 为什么屏幕不是背景表面不会出现在屏幕上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64566867/