1

So I have made I game using pygame, the system I am using makes it so that the game() function is where the actual gameplay happens, I'm trying to make a health bar for the enemy you face but the if the function is not updating the health bar and the button doesn't even seem to be pressed as the 'click' sound effect is not playing any insight into why this may be the case would be greatly appreciated, I would also like to mention that as shown at the bottom I have made it so that the health total is shown when you hover over the health bar and this doesn't change with when clicking attack.

def game():
        running = True
        stopmusic()
        pmusic("assets\\music\\battlemusic.mp3")
        while running:
                screen.fill((0, 0, 0))
                sewer = pygame.image.load('assets\\backgrounds\\sewer.png')
                screen.blit(sewer,(0,0))
                mx, my = pygame.mouse.get_pos()
                turn = 0
                mob()
                monster_health()

                button_attack = pygame.Rect(150, 500, 200, 50)
                button_potion = pygame.Rect(150, 600, 200, 50)
                button_spell     = pygame.Rect(450, 500, 200, 50)
                button_suicide = pygame.Rect(450, 600, 200, 50)
                healthbar_outline = pygame.Rect(45,45, 1190, 60)

                healthbar = pygame.Rect(50,50, (how_much_health_bar), 50)


                if button_attack.collidepoint((mx, my)):
                    if click:
                        global damage_done
                        pygame.mixer.Sound.play(click_sound)
                        damage_done = random.randint(1000,10000)
                        monster_health()
                        print(damage_done)
                        healthbar = pygame.draw.Rect(50,50, (how_much_health_bar), 50)
                        return(healthbar)


                if healthbar.collidepoint((mx, my)):
                        pygame.draw.rect(screen, (255, 0, 0), healthbar)
                        draw_text((display_health), font, (255, 255, 255), screen, 550, 60)
                else:
                        pygame.draw.rect(screen, (255, 0, 0), healthbar)
  • 1
    Where is you event loop? See [PyGame window not responding after a few seconds](https://stackoverflow.com/questions/20165492/pygame-window-not-responding-after-a-few-seconds/61409221#61409221). Where do you set `click`? What do you expect from `return(healthbar)` in the middle of the application loop? – Rabbid76 Oct 13 '20 at 21:14
  • oh sorry click is defined earlier in the code as 'click = False' and I'm not overly sure what return was meant to do just something I was testing out to see if that fixed the issue. – IHateCoding Oct 13 '20 at 21:20

1 Answers1

1
             click = False
                for event in pygame.event.get():
                        if event.type == QUIT:
                                pygame.quit()
                                sys.exit()
                        if event.type == KEYDOWN:
                                stopmusic()
                                pmusic("assets\\music\\backgroundmusic.mp3")
                                if event.key == K_ESCAPE:
                                       running = False

this was the old code but I had forgotten to add the click if statement at the bottom the previous code relied on.

                click = False
                for event in pygame.event.get():
                        if event.type == QUIT:
                                pygame.quit()
                                sys.exit()
                        if event.type == KEYDOWN:
                                stopmusic()
                                pmusic("assets\\music\\backgroundmusic.mp3")
                                if event.key == K_ESCAPE:
                                       running = False
                        if event.type == MOUSEBUTTONDOWN:
                                if event.button == 1:
                                        click = True