0
\$\begingroup\$

How can I disable pause() in game_loop() once game_over() function is called?

game loop

def game_loop():
    global playerX, playerY, playerX_change, playerY_change, bulletX, bulletY, bulletY_change, bullet_state
    global score

    # background music
    mixer.music.load('bg music.mp3')
    mixer.music.play(-1)

    running = True

    while running:

        # set background image(bg)
        bg = pygame.image.load('bg 2.PNG')
        screen.blit(bg, (0, 0))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False
                if event.key == pygame.K_p:
                    pause()
                    game_loop()

            # movement of player L, R , U & D
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    playerX_change -= 7

                if event.key == pygame.K_RIGHT:
                    playerX_change += 7

                if event.key == pygame.K_UP:
                    playerY_change -= 7

                if event.key == pygame.K_DOWN:
                    playerY_change += 7

                # update bullet position when shift in player position + bullet sound
                if event.key == pygame.K_SPACE:
                    if bullet_state == "ready":
                        bullet_sound = mixer.Sound('bullet.wav')
                        bullet_sound.play()
                        bulletX = playerX
                        fire_bullet(bulletX, bulletY)

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    playerX_change = 0

                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    playerY_change = 0

        playerX += playerX_change
        # for player stay inside game window (L & R)
        if playerX >= 650:  # R
            playerX = 650
        elif playerX <= 20:  # L
            playerX = 20

        playerY += playerY_change
        # for player stay inside game window (U & D)
        if playerY >= 370:  # D
            playerY = 370
        if playerY <= 350:  # U
            playerY = 350

        # for enemies to stay inside game window & enemies movement
        for i in range(no_of_enemies):
            # game over
            if enemyY[i] >= 340:
                for j in range(no_of_enemies):
                    enemyY[j] = 1000
                game_over()
                break

            enemyX[i] += enemyX_change[i]
            if enemyX[i] >= 720:  # R
                enemyX_change[i] -= 5
                enemyY[i] += enemyY_change[i]
            elif enemyX[i] <= 20:  # L
                enemyX_change[i] += 5
                enemyY[i] += enemyY_change[i]

            # collision for every enemy
            result = collision(enemyX[i], enemyY[i], bulletX, bulletY)
            if result:
                enemy_to_blast(enemyX[i], enemyY[i])
                explosion_sound = mixer.Sound('explosion.wav')
                explosion_sound.play()
                score += 1
                bulletY = 445
                bullet_state = "ready"
                enemyX[i] = random.randint(20, 720)
                enemyY[i] = random.randint(20, 150)

            # call enemy func
            enemy(enemyX[i], enemyY[i], i)

        # bullet movement
        if bullet_state == "fire":
            fire_bullet(bulletX, bulletY)
            bulletY -= bulletY_change
            # re-appearance of bullet at start point
            if bulletY <= 0:  # U
                bulletY = 445
                bullet_state = "ready"

        # call funcs 'player' & 'score'
        player(playerX, playerY)
        display_score()
        display_pause()

        pygame.display.update()

pause()

def pause():
    global running

    # background music for paused screen
    mixer.music.load('start.mp3')
    mixer.music.play(-1)

    paused = True

    while paused:

        txt_on_screen(paused_text_font_1, "PROJECT ANNIHILATION", 0, (-120), white, black)
        txt_on_screen(paused_text_font_2, "0N HOLD", 0, (-70), red, black)
        txt_on_screen(paused_text_font_3, " RESUME >> esc ", 0, 0, black, light_purple)
        txt_on_screen(start_text_font_3, " SETTINGS >> s ", 0, 50, black, light_purple)
        txt_on_screen(start_text_font_4, " QUIT >> Delete ", 0, 100, black, light_purple)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                paused, running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:  # RESUME GAME
                    paused = False
                    mixer.music.stop()
                    break
                if event.key == pygame.K_DELETE:
                    paused, running = False
                # if event.type == pygame.K_s >>>>> volume settings
        pygame.display.update()

game over

def game_over():
    global bullet_state
    # centering game-over text
    txt_on_screen(game_over_font, "GAME-OVER!", 0, 0, light_purple, None)
    # centering game-over 'instructions' text
    txt_on_screen(below_txt_font, "Press 'Delete' to QUIT, 'r' to RESTART", 0, 50, white, black)

    # game-over sound effect
    end_sound = mixer.Sound('end.wav')
    end_sound.play()
    mixer.music.set_volume(0)

    # unable shooting bullets
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bullet_state = "ready"
            if event.key == pygame.K_r:
                game_loop()
    
\$\endgroup\$
0
\$\begingroup\$

You call game_over() inside game_loop(), and game_loop() again inside game_over(), and I think that could eventually reach an OverFlowError if you restart the game too many times (though I am not 100% sure).

What if you did something similar to pause() and write a while loop inside the game_over() function that leads you back to game_loop() once it's over? That way you would also disable pause() from being called.

e.g.

def game_over():
    global bullet_state
    # centering game-over text
    txt_on_screen(game_over_font, "GAME-OVER!", 0, 0, light_purple, None)
    # centering game-over 'instructions' text
    txt_on_screen(below_txt_font, "Press 'Delete' to QUIT, 'r' to RESTART", 0, 50, white, black)

    # game-over sound effect
    end_sound = mixer.Sound('end.wav')
    end_sound.play()
    mixer.music.set_volume(0)

    game_over = True
    while game_over:
        # unable shooting bullets
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    bullet_state = "ready"
                if event.key == pygame.K_r:
                    game_over = False

```
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.