Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to create a turn-based game in pyGame but hit a wall when trying to properly handle the main game loop. So I have something like this:

def loop(self):
    while self.stategame==1:
        for event in pygame.event.get():
            if event.type == QUIT \
            or (event.type == KEYDOWN and event.key ==    K_ESCAPE):
                self.stategame = 0

        if self.isPlayerTurn:
            self.events()
        else:
            self.enemyTurn()
        self.draw_map()

        pygame.display.flip()

The enemyTurn() function here is called multiple times after each player's move causing the enemies to move multiple tiles before the player does anything again. I'm a beginner in gamedev do I don't know how to overcome this and, in the first place, why does it hapen. Isn't the code supposed to be analyzed from top to bottom like in regular Python so that the enemyTurn() is called just once and then the game goes back to waiting for user input? How can I solve this problem and call the enemyTurn() just once each time the player makes a move?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

Isn't the code supposed to be analyzed from top to bottom like in regular Python so that the enemyTurn() is called just once and then the game goes back to waiting for user input?

The code is executed just like normal code; your assertion here is correct except that your code doesn't have anywhere that "waits for user input."

The function pygame.event.get() gets all messages in the event queue and removes them. The documentation does not mention that the function will block if empty (indeed, that's probably what wait is for.

So your loop never waits for user input, it simply gets any available input, and even if there wasn't any, goes on to either call self.events() or self.enemyTurn(), depending on the state of self.playerTurn.

It sounds like self.enemyTurn() doesn't necessarily always toggle that variable, and so you end up calling enemyTurn several times in a row; make sure you toggle playerTurn when appropriate.

share|improve this answer
    
Right you are, self.enemyTurn() didn't toggle the isPlayerTurn properly. Dunno how I overlooked it cause I even had some debugging labels set up to show me this specific value. Thanks a lot :) –  Straightfw May 22 at 16:42

Just change the turn after the enemy moves:

    if self.isPlayerTurn:
        self.events()
    else:
        self.enemyTurn()
        self.isPlayerTurn = True

Also, your main loop seems to lack a clock.Tick() to avoid using CPU all the time.

share|improve this answer
    
The player toggling is inside enemyTurn(), the problem lied in the fact that it wasn't always toggled properly :) Thanks a lot for the clock.Tick() suggestion, though! –  Straightfw May 22 at 16:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.