0

As is says in the title after I select a button moving the code onto the second if statement the whole screen just freezes and stops responding im struggling to see what the issue is any insights into what could be causing this to freeze? I'm almost certain the issue isn't with the functions i haven't included as they are all very simple and shouldn't change anything that could freeze the code although if you think they could be the issue ill add them in the comments.

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

                                        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)


                                        combat_log = pygame.Rect(680, 150, 500, 450)

                                        click = False

                                        for event in pygame.event.get():
                                                # this will make it so that you can press escape to exit this funciton
                                                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
                                                if event.type == pygame.MOUSEBUTTONDOWN:
                                                        if event.button == 1:
                                                                if button_attack.collidepoint((mx, my)):
                                                                        pygame.mixer.Sound.play(click_sound)
                                                                        update_healthbar()
                                                                        print(damage_done)
                                                                        healthbar = pygame.Rect(50,50, (how_much_health_bar), 50)
                                                                        move_complete = True



                                        if button_potion.collidepoint((mx, my)):
                                                if click:
                                                        pygame.mixer.Sound.play(click_sound)

                                        if button_spell.collidepoint((mx, my)):
                                                if click:
                                                        pygame.mixer.Sound.play(click_sound)

                                        if button_suicide.collidepoint((mx, my)):
                                                if click:
                                                        pygame.mixer.Sound.play(click_sound)



                                        #this draws the boxs on the game window
                                        pygame.draw.rect(screen, (0, 128, 128), healthbar_outline)
                                        pygame.draw.rect(screen, (180, 0, 0), healthbar)
                                        pygame.draw.rect(screen, (145, 39, 143), combat_log)


                                        #this will show the monsters health total
                                        pygame.draw.rect(screen, (255, 0, 0), healthbar)
                                        if healthbar.collidepoint((mx, my)):
                                                draw_text((display_health), font, (255, 255, 255), screen, 550, 60)
                                        else:
                                                pygame.draw.rect(screen, (255, 0, 0), healthbar)

                                        if healthbar_outline.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)


                                        #this makes the buttons glow as you hover over them
                                        if button_attack.collidepoint((mx, my)):
                                                pygame.draw.rect(screen, (255, 0, 0), button_attack)
                                        else:
                                                pygame.draw.rect(screen, (180, 0, 0), button_attack)
                                        draw_text('attack', font, (255, 255, 255), screen, 150, 500)

                                        if button_potion.collidepoint((mx, my)):
                                                pygame.draw.rect(screen, (255, 0, 0), button_potion)
                                        else:
                                                pygame.draw.rect(screen, (180, 0, 0), button_potion)
                                        draw_text('potion', font, (255, 255, 255), screen, 150, 600)

                                        if button_spell.collidepoint((mx, my)):
                                                pygame.draw.rect(screen, (255, 0, 0), button_spell)
                                        else:
                                                pygame.draw.rect(screen, (180, 0, 0), button_spell)
                                        draw_text('spell', font, (255, 255, 255), screen, 450, 500)
                                        if button_suicide.collidepoint((mx, my)):
                                                pygame.draw.rect(screen, (255, 0, 0), button_suicide)
                                        else:
                                                pygame.draw.rect(screen, (180, 0, 0), button_suicide)
                                        draw_text('suicide', font, (255, 255, 255), screen, 450, 600)

                                        turn = 2
                                        print(turn)
                                        pygame.display.update()
                                        mainClock.tick(60)
                                except:
                                        print("please just work")


                if turn == 2:
                    try:
                        turn = 1
                        print(turn)
                        pygame.display.update()
                        mainClock.tick(60)
                    except:
                        print("please just work")
  • You should stick to the tradition of using 4 spaces indents in Python, that would make your code fit in the window and would make it easier to read... – Thierry Lathuille Oct 16 '20 at 18:34

1 Answers1

0

The window freezes because in the second case you don't have an event loop. See pygame.event.get() respectively pygame.event.pump():

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.

In the second case, implement a similar event loop as in the first case. For instance:

for event in pygame.event.get():
    # this will make it so that you can press escape to exit this funciton
    if event.type == QUIT:
        pygame.quit()
        sys.exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174