0

I am creating a trading game in Python, and want to know how to implement turns without pausing the gameloop. I know that I will have to change the way movement is implemented, but how would I do that?

Note: code can be reached here (May be old): http://pastebin.com/rZbCXk5i

Pip
  • 4,387
  • 4
  • 23
  • 31
  • I would use threads, did you try? – IssamLaradji Nov 29 '13 at 00:36
  • What do you mean by "implement turns"? You want the user to be able to rotate his direction by using the arrow keys, and move continuously forward, instead of moving in the direction of each key press? – abarnert Nov 29 '13 at 00:37
  • @abarnert No turns as in only one person can move at a time and if its not your turn you can go, like a board game – Serial Nov 29 '13 at 00:40
  • Exactly, Mr. friend @ChristianCareaga – Pip Nov 29 '13 at 00:42
  • 1
    why don't you just check if its the players turn each update and if it isn't it cant move, then once the other player or whatever is done moving make it so you can move and the other player cant – Serial Nov 29 '13 at 00:45
  • @ChristianCareaga I tried that, but that way, it still pauses the whole loop – Pip Nov 29 '13 at 00:53
  • 1
    You should put the player movement in the player class instead of the main loop and then update the player in the main loop – Serial Nov 29 '13 at 00:57
  • how would I do that? There's a move() method already, what else should I do? – Pip Nov 29 '13 at 00:59
  • @ChristianCareaga come to the GD SE chat... http://chat.stackexchange.com/rooms/19/game-development – Pip Nov 29 '13 at 01:02
  • @TheProgramm3r you need to reorganize the code to have less code in main loop and more code in classes and functions. For example you could move keys checking and `if player.atPort == True: ...` into player class. – furas Nov 29 '13 at 01:38

1 Answers1

2

This is usually done with something called a game state machine

What that is, is extremely simple. I can show you with an example.

def main_game_loop():
    if state == "player_turn":
        # logic for player's turn
    elif state == "enemy_turn":
        # logic for enemy's turn
    # they can also be used for other things, such as where you are in the game
    elif state == "paused":
        # pause logic etc etc
Azeirah
  • 6,176
  • 6
  • 25
  • 44
  • +1. However, at the end of the logic of each player turn you should make sure you change the state of the game, i.e. the turn. – Juan Gallostra Nov 29 '13 at 11:56