3

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()
The_spider
  • 1,202
  • 1
  • 8
  • 18
Nabil
  • 31
  • 1
  • Did you paste the complete "player.py" file's code ? It is not a class then ? If it was a class, in your game class you could just instanciate it like `player = Player()` then you could access it's health from game by simply doing `player.health` or write a function in the class for example `is_dead()` and in your game class you could just do something like `if player.is_dead(): self.game_over()` – Anto Sep 02 '22 at 07:46
  • class Player(Entity): def __init__(self, pos, groups, obstacle_sprites, create_attack, destroy_attack, create_magic): But when i instance it in my main file and Game class i have this error "player = Player() TypeError: Player.__init__() missing 6 required positional arguments: 'pos', 'groups', 'obstacle_sprites', 'create_attack', 'destroy_attack', and 'create_magic'" – Nabil Sep 02 '22 at 16:09
  • Please edit this code in the question, such that we can see its proper indentation. – The_spider Sep 04 '22 at 14:38
  • Do you really need all those arguments ( @Nabil ) ? Player class `__init__(self)` function often doesn't even need any arguments (beside "self") in most games. – Anto Sep 05 '22 at 07:55

0 Answers0