首页 > 解决方案 > Pygame window closes right after

问题描述

import pygame

WIDTH, HEIGHT = 1100, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game")

WHITE = (255, 255, 255)

def main():
    run = True
    while run:        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        WIN.fill(WHITE)
        pygame.display.update()
    
    pygame.quit()

if __name__ == "__GAME!__":
    main()

When I run this code, the pygame window closes right after while using Virtual Studio Code. While using Jupyter notebook it runs but goes unresponsive after 2-3 seconds. Both of them do not display any errors though.

标签: pythonpygame

解决方案


我不确定你为什么使用

if __name__ == "__GAME!__"

但是,如果我将其更改为

if __name__ == "__main__"

然后它工作。


推荐阅读