4
\$\begingroup\$

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?

\$\endgroup\$

2 Answers 2

9
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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 :) \$\endgroup\$
    – Straightfw
    Commented May 22, 2014 at 16:42
2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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! \$\endgroup\$
    – Straightfw
    Commented May 22, 2014 at 16:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .