【问题标题】:Pygame window closes instantlyPygame 窗口立即关闭
【发布时间】:2022-02-09 13:49:34
【问题描述】:

我的 pygame 窗口在我运行代码后立即关闭且没有任何错误
这是我的代码:

import pygame
pygame.init()   # initialises pygame

win = pygame.display.set_mode((500, 500))   # width, height

pygame.display.set_caption("Project_01")

如果有人可以提供帮助,请提前感谢您:)

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    参见Why is my PyGame application not running at all? 您的应用程序将在窗口创建后立即退出。您必须实现一个应用程序循环。您必须实现应用程序循环以防止窗口立即关闭:

    import pygame 
    
    pygame.init() # initialises pygame
    win = pygame.display.set_mode((500, 500)) # width, height
    pygame.display.set_caption("Project_01")
    
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(100)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        win.fill((0, 0, 0))
    
        # draw scene
        # [...]
    
        pygame.display.flip()
    
    pygame.quit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 2020-11-08
      相关资源
      最近更新 更多