为什么我的Pygame窗口只显示几秒钟?

2024-04-24 15:42:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我是PyGame新手(通常是PyGame),我只是想打开一个窗口。这就是我现在所追求的。我的代码是:

import pygame
pygame.init()

win = pygame.display.set_mode((600, 600))

pygame.display.set_caption('First Game')

我在Pycharm和pygame1.9.4中使用python3.7.0。在


Tags: 代码importgameinitmodedisplaypygamewin
2条回答

因为你需要先把它放在一个循环中(这样它就可以刷新它里面的任何东西),这样它就会一直保持下去,直到有东西改变了循环的条件。在

# Event loop (HERE YOU PUT IT).
    while 1:
        for event in pygame.event.get():
            if event.type == QUIT:
                return

        screen.blit(background, (0, 0))
        pygame.display.flip()


if __name__ == '__main__': main()

{Python的下一步是开始游戏^的循环:

import pygame
pygame.init()

win = pygame.display.set_mode((600, 600))

pygame.display.set_caption('First Game')

clock = pygame.time.Clock()    # Determine FPS (frames-per-second)

crashed = False

# Game loop
while not crashed:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

        print(event)

    pygame.display.update()
    clock.tick(60)

相关问题 更多 >