Python Forum

Full Version: Pausing without freezing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have created a interactive game using pygame, I want like a pause before the computer takes its turn. I can only do that with pygame.time.wait(), but this freezes the interactivity for the duration. I am wondering if anyone here has a idea of how I can overcome my problem. Any help is appreciated
You can have a flag somewhere like

enemy_turn = False
 
while game_loop:

    if enemy_turn == True 
        enemy_update()
        enemy_turn = False
Then when your player's turn is done put enemy_turn = True.

However you want to do this is up to you. Just have a tag that switches once the player is finished it's turn, then have it switch back when the enemy is done it's turn.
Definitely do not use pygame.time.wait.
You do have 3 other options.
  1. Use pygame.time.get_ticks()
  2. Use delta. delta = clock.tick(fps)
  3. Use pygame.time.set_timer(eventid, millisecond, once)

Example get ticks
class Timer:
    def __init__(self, ticks, interval, callback):
        self.tick = ticks + interval
        self.interval = interval
        self.callback = callback

    def update(self, ticks):
        if ticks > self.tick:
            self.tick += self.interval
            self.callback(self)
Example delta countdown.
class DeltaTimer:
    def __init__(self, milliseconds, callback):
        self.milliseconds = milliseconds
        self.countdown = milliseconds
        self.callback = callback

    def update(self, delta):
        self.countdown -= delta
        if self.countdown <= 0:
            self.countdown += self.milliseconds
            self.callback(self)
Example pygame Timer. Limited to 31.
pause_timer = pygame.USEREVENT + 1
# Just call this after your move.
pygame.time.set_timer(pause_timer, milliseconds, True)

# Event loop
for event in pygame.event.get():
    if event.type == pause_timer:
        # computer move
Thank You, that's really helpful