Pygame - 为什么我的游戏会重复而不是关闭?

Pygame - why my game repeats instead of closing?

首先,很抱歉我的菜鸟问题。

所以我正在努力学习 pygame,为此我创建了一个“"game"”,只要有人点击关闭按钮它就会退出("x" 按钮”)在 window 的顶角,但整个游戏会自行重新启动。 我使用 "Game = False" 来指出游戏需要关闭,因为游戏所做的一切都位于仅以 "Game = True" 开头的循环中,并且在脚本末尾(循环结束后)有一个 "pygame.quit()" 和一个 "sys.exit()"。这工作正常,但最近我添加了以下几行:

if x < 0 or x > display_width - spacecore_width:
            game_over()  

它们应该调用一个 "game_over" 函数,该函数通过调用覆盖整个游戏的另一个函数(循环位于该函数内部)来重置游戏。但是当有人点击 "x" 按钮退出游戏时,这不应该发生,我不知道为什么当这样的事情完成时也会发生这种情况(除了按预期工作,如果 "x < 0 or x > display_width - spacecore_width".

我已经知道如何避免它(我只需要将 "Game = False" 替换为那些退出命令)但我想首先知道为什么会出现这个问题。

完整代码如下:

import pygame, sys, time

pygame.init()

display_width = 800
display_height = 600

DISPLAY = pygame.display.set_mode((display_width,display_height))

pygame.display.set_caption("Não enconste nas bordas da tela") 

WHITE = (255,255,255)
RED = (255,0,0) 

SPACECORE = pygame.image.load("spacecore.png")

spacecore_width = 100

clock = pygame.time.Clock()


def text_and_rect(text,font):
    text_surface = font.render(text,True,RED)
    return text_surface, text_surface.get_rect()


def message_display(text):
    font = pygame.font.Font("freesansbold.ttf",50)
    text_surf, text_rect = text_and_rect(text,font)
    text_rect.center = ((display_width/2),(display_height/2))
    DISPLAY.blit(text_surf,text_rect) 
    pygame.display.update()
    time.sleep(2)   



def game_over():
    message_display("Você morreu de morte morrida")
    game_loop()


def player(x,y):
    DISPLAY.blit(SPACECORE,(x,y))



def game_loop():
    Game = True

    x = display_width * 0.5
    y = display_height * 0.8

    mod_x = mod_y = 0



    while Game == True:
        pygame.display.update()
        DISPLAY.fill(WHITE) 



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


            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    mod_x = -5
                if event.key == pygame.K_RIGHT:
                    mod_x = 5
                if event.key == pygame.K_UP:
                    mod_y = -5
                if event.key == pygame.K_DOWN:
                    mod_y = 5


            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    mod_x = 0
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    mod_y = 0

        x += mod_x
        y += mod_y

        clock.tick(60) 

        player(x,y)

        if x < 0 or x > display_width - spacecore_width:
            game_over()  



game_loop()
pygame.quit()
sys.exit() 

确保您正在跳出循环,然后再次调用循环函数:

while Game == True:

    # some here code

    if x < 0 or x > display_width - spacecore_width:
        break # leave while loop

 # proceed elsewhere
game_over()

或者在 game_over 中将流量切回到 game_loop:

def game_over():
    message_display("Você morreu de morte morrida")
    # game_loop() # removed

目前你永远不会在游戏结束时跳出循环 - 你只是以相互递归的方式调用其他函数(这又会激活另一个循环),实际上这永远不会结束任何游戏。

此外,如果您收到退出事件,您可能希望 break 退出 for 循环:

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