加载具有不同背景的场景时出现错误

bug in loading scenes with different background

我想在我的游戏 SpaceInvaders 中添加一个暂停菜单,背景是游戏的最后一帧,但是当我加载暂停场景时,我的游戏没有在背景中显示游戏

the background game looks like this

the first time I load pause it shows this

and then whenever i load pause it behaves normally like this

我的游戏有 3 个文件:

1)游戏文件

2) 暂停文件

3)init文件(连接以上两个)


游戏文件

import pygame
from __init__ import __INIT__

pygame.display.set_caption("Endless")
pygame.init()
screen=pygame.display.set_mode((1920/2,1080/2),pygame.RESIZABLE)

def menu():
    running=True

    while running:
        screen.blit(pygame.image.load("docs/bg.png"),(0,0))
        
        for event in pygame.event.get():
        
            if event.type==pygame.QUIT:
                running=False
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    __INIT__("pause")

        pygame.display.update()
menu()

暂停文件

import pygame
from __init__ import __INIT__

pygame.display.set_caption("Pause")
pygame.init()
screen=pygame.display.set_mode((1920/2,1080/2),pygame.RESIZABLE) 

def PAUSE():
    running=True

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

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    __INIT__("menu")
                if event.key == pygame.K_h:
                    from __init__ import hello
                    hello()

        screen.blit(pygame.image.load("docs/D_effect.png"),(0,0))
        pygame.display.update()
PAUSE()

初始化文件

def __INIT__(a):
    if a=="pause":
        from pause import PAUSE
        PAUSE()
    if a=="menu":
        from endless import menu
        menu()

我不知道是什么导致了这个问题,因为它只出现过一次,我是初学者,这可能是一个愚蠢的错误,如果发生这种情况,我很抱歉

无论如何,如果您发现我的问题难以理解,请运行 Space_invaders/Space_invaders/endless.py 中的无尽文件 此处提供 https://drive.google.com/file/d/1FUhGNdXp4CgYcr9PAfey-6ElZhVYMKem/view?usp=sharing

问题是递归。 menu 调用 PAUSEPAUSE 调用 menu:

menu
  |
   -> PAUSE
        |
         -> menu
             |
              -> PAUSE
                   |
                    ...

您根本不需要额外的 PAUSE 应用程序循环。只需添加 use game_state 变量。例如:

import pygame

pygame.display.set_caption("Endless")
pygame.init()
screen=pygame.display.set_mode((1920/2,1080/2),pygame.RESIZABLE)

def menu():

    bg = pygame.image.load("docs/bg.png")
    bg_pause = pygame.image.load("docs/D_effect.png")

    game_state = 'running'
    while game_state != 'quit':

        if game_state == 'pause':
            screen.blit(bg_pause, (0,0))
        else:
            screen.blit(bg, (0,0))
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_state = 'quit'
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    if game_state == 'running':
                        game_state = 'pause'
                    else:
                        game_state = 'running' 

        if game_state == 'running':
            # draw game scene
            # [...]
            pass

        pygame.display.update()

    pygame.quit()
    quit()
    
menu()

不要在每一帧都加载背景图片。这导致滞后。 pygame.image.load 是一个非常耗时的过程,图像文件必须从卷中加载并解码。在应用程序循环之前加载图像并在循环中使用它们。