首页 > 解决方案 > 为什么我的 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")

但是每次我尝试运行它时,我都会得到这个:

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

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

标签: pythonmacospygame

解决方案


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

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

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

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


推荐阅读