Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame应用程序没有';Don’不要第一次就结束_Python_Pygame - Fatal编程技术网

Python Pygame应用程序没有';Don’不要第一次就结束

Python Pygame应用程序没有';Don’不要第一次就结束,python,pygame,Python,Pygame,我正在pygame中构建一个小游戏,我想要一个退出的函数。但是,它需要多次单击才能退出,并且也不一致。另外,windows退出功能正在重新启动程序,下面是处理退出的代码部分 if isKill: pygame.mixer.music.stop() gameover = myfont.render("Press R to Respawn", False, (255, 255, 255)) rect = gameover.g

我正在pygame中构建一个小游戏,我想要一个退出的函数。但是,它需要多次单击才能退出,并且也不一致。另外,windows退出功能正在重新启动程序,下面是处理退出的代码部分

    if isKill:
        pygame.mixer.music.stop()
        gameover = myfont.render("Press R to Respawn", False, (255, 255, 255))
        rect = gameover.get_rect()
        rect.center = screen.get_rect().center
        screen.blit(gameover, rect)
        if event.type == KEYDOWN:
            if event.key == K_r:
                gameloop()


*gameloop()是整个脚本

而不是
事件。键入
,您可以使用pygame内置事件
退出

请参阅以下代码

running = True
while running:
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False
        #Can use pygame.quit() in place of running=False if the above line doesn't work.
循环是gameloop的开始。gameloop的内容应该在其中。

不要调用while循环中的
gameloop()
函数

在游戏循环中,应始终保持循环在一个级别运行。在您的代码中,重生实际上冻结了当前级别,并在较低级别重新运行游戏。这就是为什么退出游戏需要几个退出命令

当玩家重生时,重置游戏变量,然后继续游戏循环

更新类似以下内容的代码:

if isKill:  # game is over
    pygame.mixer.music.stop()
    gameover = myfont.render("Press R to Respawn", False, (255, 255, 255))
    rect = gameover.get_rect()
    rect.center = screen.get_rect().center
    screen.blit(gameover, rect)
    if event.type == KEYDOWN:
        if event.key == K_r:
            #gameloop() # remove this
            dospawn()  # initialize\reset game variables here (can use same function at game start)
            isKill = False  # start new game
    continue  # skip rest of game process

可能使用
sys.exit
停止程序。 尝试:


你是从
gameloop()
中调用
gameloop()
吗?是的,我在gameloop()中调用它
if isKill:  # game is over
    pygame.mixer.music.stop()
    gameover = myfont.render("Press R to Respawn", False, (255, 255, 255))
    rect = gameover.get_rect()
    rect.center = screen.get_rect().center
    screen.blit(gameover, rect)
    if event.type == KEYDOWN:
        if event.key == K_r:
            #gameloop() # remove this
            dospawn()  # initialize\reset game variables here (can use same function at game start)
            isKill = False  # start new game
    continue  # skip rest of game process
import sys
import pygame

while 1:

    #code

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()