pygame.display.flip()似乎无法正常工作?

时间:2018-12-10 01:40:39

标签: python-3.x pygame

我一直在从事一个项目,当我测试它时,这发生了。经过一些测试,我很快意识到pygame的显示翻转部分代码有问题。我真的看不到这里有什么问题,所以我希望你们中的一个能做到。

import pygame
pygame.init()
screen = pygame.display.set_mode((200, 200))
img = pygame.image.load("test.png")
while 1:
    screen.fill((0, 0, 0))
    screen.blit(img, (0, 0))
    pygame.display.flip()
    pygame.time.delay(10)

现在,结果是空白的白色屏幕,该屏幕为200 x200。我希望有人在这里看到问题所在。另外,我的png在这里无关紧要,因为我只用fill(black)就能得到相同的结果,所以我希望有人知道。

1 个答案:

答案 0 :(得分:2)

在这里耦合事物:

1)我建议使用pygame时钟而不是time.delay。使用时钟设置每秒的帧数以运行代码,其中time.delay只是等待并等待延迟。

2)Pygame是事件驱动的,因此即使您还没有事件,也需要检查事件。否则,它将被解释为无限循环并锁定。有关更详细的说明:click here

3)我将提供一种可以设置为false的标志来退出游戏循环,这样程序就可以自然终止

import pygame

pygame.init()
screen = pygame.display.set_mode((200, 200))
img = pygame.image.load("test.png")
clock = pygame.time.Clock()
game_running = True
while game_running:
    # evaluate the pygame event
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False  # or anything else to quit the main loop

    screen.fill((0, 0, 0))
    screen.blit(img, (0, 0))
    pygame.display.flip()
    clock.tick(60)