简体   繁体   English

pygame 模块 - window 立即关闭

[英]pygame module - window instantly is closing

I already checked other topics and I had tested these solutions and without any pos results.. I need to refresh topic with question.我已经检查了其他主题并且我已经测试了这些解决方案并且没有任何 pos 结果。我需要用问题刷新主题。 Where I have an error?我在哪里有错误?

import pygame

pygame.init()

WIDTH, HEIGHT = 1000, 800
window = pygame.display.set_mode((WIDTH, HEIGHT))

def run(window, width, height):
    run = True
    clock = pygame.time.Clock()
    fps = 60

    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                break

        clock.tick(fps)
    
        pygame.quit()

if __name__ == "_main_":
    run(window, WIDTH, HEIGHT)

Change from single underscore to double underscores around main here:main周围的单下划线更改为双下划线:

if __name__ == "_main_":

It should be它应该是

if __name__ == "__main__":

The quit invocation should not be on the while block but after it. quit 调用不应该在 while 块上,而是在它之后。 Otherwise it quits immediately.否则它会立即退出。 Therefore:所以:

def run(window, width, height):
    run = True
    clock = pygame.time.Clock()
    fps = 60

    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                break

        clock.tick(fps)
    
    pygame.quit()

It is a matter of Indentation .这是Indentation的问题。 The window is closing because you are calling pygame.quit() in the application loop. window 正在关闭,因为您在应用程序循环中调用pygame.quit() You need to call pygame.quit() after the application loop.您需要在应用程序循环后调用pygame.quit()
The name of thetop level code environment is "__main__" , but not "_main_" : 顶级代码环境的名称是"__main__" ,而不是"_main_"

def run(window, width, height):
    run = True
    clock = pygame.time.Clock()
    fps = 60

    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        clock.tick(fps)
    
    # INDETATION
    #<--|
    pygame.quit()

if __name__ == "__main__":           # <-- 
    run(window, WIDTH, HEIGHT)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM