【问题标题】:Why is my PyGame application not running at all?为什么我的 PyGame 应用程序根本没有运行?
【发布时间】:2021-03-23 15:39:09
【问题描述】:

我有一个简单的 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

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

【问题讨论】:

    标签: python macos pygame


    【解决方案1】:

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

    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 另见Event and application loop

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多