为什么我的 PyGame 应用程序根本不运行?

Ita*_*tay 2 python macos pygame

我有一个简单的 Pygame 程序:

#!/usr/bin/env python

import pygame
from pygame.locals import *

pygame.init()

win = pygame.display.set_mode((400,400))
pygame.display.set_caption("My first game")
Run Code Online (Sandbox Code Playgroud)

但每次我尝试运行它时,我都会得到以下信息:

pygame 2.0.0 (SDL 2.0.12, python 3.8.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
Run Code Online (Sandbox Code Playgroud)

然后什么也没有发生。为什么我无法运行这个程序?

Rab*_*d76 5

您的应用程序运行良好。但是,您还没有实现应用程序循环:

import pygame
from pygame.locals import *

pygame.init()

win = pygame.display.set_mode((400,400))
pygame.display.set_caption("My first game")
clock = pygame.time.Clock()

run = True
while run:

    # handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # update game objects
    # [...]

    # clear display
    win.fill((0, 0, 0))

    # draw game objects
    # [...]

    # update display
    pygame.display.flip()

    # limit frames per second
    clock.tick(60) 

pygame.quit()
Run Code Online (Sandbox Code Playgroud)

典型的 PyGame 应用程序循环必须:

repl.it/@Rabbid76/PyGame-MinimalApplicationLoop另请参见事件和应用程序循环