How can I show a Game over screenand stop the game when the health reaches 0 in pygame?

Viewed 48

So, I have a problem in my python code. I made a 2d game by following this tutorial on youtube. I finished the tutorial, but the maker of the video doesn't show how to make a function to die and display a game over screen. As such, I searched on the internet how to make a Game Over screen and I made it. But now I have to implement that when self.health <= 0, the Game Over screen shows up. I have a file player.py with the health of the player, but how can I connect this with the main.py file to display the Game over screen when the health reaches 0?

Thanks to the one who will help me!

main.py:

class Game:
    
    def __init__(self):

        # general setup
        pygame.init()
        self.screen = pygame.display.set_mode((WIDTH, HEIGTH))
        pygame.display.set_caption('Zelda')
        self.clock = pygame.time.Clock()

        self.level = Level()
        self.playing = False

    def run(self):
        main_sound = pygame.mixer.Sound('../audio/zelda.ogg')
        main_sound.set_volume(0.4)
        main_sound.play(loops=-1)

        while not self.playing:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_m:
                        self.level.toggle_menu()
                if self.playing == True:
                    main_sound.stop()
                    pygame.display.update()
                    self.game_over()

            self.screen.fill(WATER_COLOR)
            self.level.run()
            pygame.display.update()
            self.clock.tick(FPS)

    def game_over(self):
        Over_sound = pygame.mixer.Sound('../audio/Retro-Game.ogg')
        Over_sound.set_volume(0.5)
        Over_sound.play()

        while True:
            Game_Over_MOUSE_POS = pygame.mouse.get_pos()

            SCREEN.fill("black")

            Game_Over_TEXT = get_font(90).render(
                "Game Over", True, "Red")
            Game_Over_RECT = Game_Over_TEXT.get_rect(center=(640, 260))
            SCREEN.blit(Game_Over_TEXT, Game_Over_RECT)

            Game_Over_BACK = Button(image=None, pos=(640, 460),
                                    text_input="Main menu", font=get_font(75), base_color="White", hovering_color="Green")

            Game_Over_BACK.changeColor(Game_Over_MOUSE_POS)
            Game_Over_BACK.update(SCREEN)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if Game_Over_BACK.checkForInput(Game_Over_MOUSE_POS):
                        Over_sound.stop()
                        main_menu()

            pygame.display.update()

if __name__ == '__main__':
    while True:
        game = Game()
        game.run()
        game.game_over()

player.py:

import pygame
from settings import *
from support import import_folder
from entity import Entity

    self.stats = {'health': 20, 'energy': 60,
                  'attack': 10, 'magic': 4, 'speed': 5}
    self.health = self.stats['health']

def death(self):
    if self.health <= 0:
        self.playing = True
    else:
        self.playing = False

def update(self):
    self.input()
    self.cooldowns()
    self.get_status()
    self.animate()
    self.move(self.stats['speed'])
    self.energy_recovery()
    self.death()
0 Answers
Related