Home > Mobile >  bug in loading scenes with different background
bug in loading scenes with different background

Time:12-29

I am trying to add a pause menu in my game SpaceInvaders with the last frame of the game in the background but when I load the pause scene my game does not show the game in the background

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

my game has 3 files :

1)game file

2)pause file

3)init file (that connects above two)


Game file

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()

Pause file

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()

init file

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

I don't know what is causing this issue because it only does it one time I am a beginner it might be a silly mistake I am sorry if that happens

in any case, if u find it difficult to understand my problem please run the endless file in Space_invaders/Space_invaders/endless.py provided here https://drive.google.com/file/d/1xkSKcptJpuY9mZhQtl6mwU1Hh3p2L5j4/view?usp=sharing

CodePudding user response:

The problem is recursion. menu calls PAUSE, PAUSE calls menu:

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

You don't need the extra application loop for PAUSE at all. Just add use game_state variable. e.g.:

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()

Do not load the background images in every frame. This causes lagging. pygame.image.load is a very time consuming process, the image file has to be loaded from the volume and decoded. Load the images before the application loop and use them in the loop.

  • Related