1

I am writing a test game with PyGame and have a turn/phase based system (Like in Yu-Gi-Oh!) however the problem I am having is when I want to increase the Turn Counter (on the Draw Phases) it keeps incrementing it indefinitely until I press the mouse click again to release the mouse hold. Is there a way to break the incrementing and go straight to the next phase right after it increments once? I assume the problem has to do with the game loop itself refreshing so rapidly. Maybe I could freeze the game loop? Another alternative would be to automatically (not by mouse click) shift from turn to turn but I would still have to press a button to shift into the next phase. Any Ideas? Could I possibly increment this outside of the game loop?

#The Game Phases (outside of the game loop): 
phases = "Coin Flip","Your Draw Phase", "Your Standby Phase", "Your Main Phase 1", "Your Battle Phase", "Your Main Phase 2", "Your End Phase", "Their Draw Phase", "Their Standby Phase", "Their Main Phase 1", "Their Battle Phase", "Their Main Phase 2", "Their End Phase" #enemy phases


#Code Inside the game loop: 
        #If phase = A Draw Phase then the Turn Counter increments.
        if phasecounter == 1: 
            turncounter += 1

        elif phasecounter == 7:
            turncounter += 1

        #A Mouse Click that can will change the Phase on the Screen (displayed by Text). 
        if event.type == pygame.MOUSEBUTTONDOWN:
                phasecounter += 1

            
            
       #Loops the Phases. 
        if phasecounter > 12: 
                    phasecounter = 1

        currentphase = phases[phasecounter]

2 Answers2

1

Add a flag to your game loop to track the mouse button click. Set the flag on button down, and unset on button up. Set the variables on the button down event.

Try this update:

    mousedown = False  # above game loop

    #A Mouse Click that can will change the Phase on the Screen (displayed by Text). 
    if event.type == pygame.MOUSEBUTTONUP:
        mousedown = False
        
    if event.type == pygame.MOUSEBUTTONDOWN and not mousedown: # don't repeat
        mousedown = True
        phasecounter += 1
        if phasecounter in [1,7]:
            turncounter += 1
        if phasecounter > 12: 
            phasecounter = 1

    currentphase = phases[phasecounter]
Mike67
  • 11,175
  • 2
  • 7
  • 15
1

Thanks to Mike67, his solution did lend greatly to the problem. However it was still only incrementing every 2 turns. My solution was to make the phases without the "Your" or "Their" and just make a boolean to keep track of whose turn it was:

whoseturn = 0 #0 = your turn; 1 = their turn
phases = ["Next Turn","Draw Phase", "Standby Phase", "Main Phase 1", "Battle Phase", "Main Phase 2", "End Phase", "Draw Phase", "Standby Phase", "Main Phase 1", "Battle Phase", "Main Phase 2", "End Phase"]

And then make the Turn Counter on in

if event.type == pygame.MOUSEBUTTONDOWN and not mousedown: # don't repeat
                mousedown = True
                mousedown = False
                phasecounter += 1
                if phasecounter == 0:
                    turncounter += 1
                if phasecounter == 7:
                    turncounter += 1
                    
                if phasecounter == 7: 
                    phasecounter = 0
                    
                
                    

        currentphase = phases[phasecounter]

So it only had to increment once instead of twice per list. Now it increments perfectly on the "Next Turn".