I'm trying to make a 2-player Connect 4 game, but I'm having trouble wrapping my head around some of the concepts related to incorporating event handlers while progressing with the game.

On the one hand, I have my windows:
One is a control window used for entering player information, and has an exit button.
One is the actual game window used for drawing on and interaction with the player.
One is a popup that asks the player a question that they must answer correctly in order to take that turn.

On the other hand, I have my actual game logic. A player's turn could progress as follows:

//create question window with question and await an answer (game state != WAITING_FOR_ANSWER)
//question window is destroyed after the question
//if not correct, move onto the next player's turn
//if correct, await their move (by clicking on the board, state != WAITING_FOR_MOVE)
//then check for win/tie
//then perform end-of-game function / change state if needed
//move onto next player's turn (same function)

//all this time, game window redraws when game has started and needs to change

Meanwhile, my game window draws and responds to clicks depending on the game state. My general method is:

//change state to something like WAITING_FOR_MOVE
//window sees state is now this, and responds to clicks
//window changes state back when move is made
//function continues

The problems arise when trying to get all of this running concurrently. If I try as is, the question window for the first player's turn comes up after clicking the "Start Game: button, but then all windows freeze up. I would imagine this is due to the logic not actually ending, as it requires the window to give it a reason to stop.

I tried wrapping the very first nextTurn() call (in the "Start Game" button handler) in a new thread, but it causes the same thing to happen, except the control window responds when clicking the exit button.

How should I be dealing with program logic and window event handlers at the same time?

I'm also pretty sure I should be accessing the state differently than just checking it until it's the desired result. Would I need a mutex for this?

  • \$\begingroup\$ Does this help somehow? mvps.org/directx/articles/writing_the_game_loop.htm \$\endgroup\$ May 4, 2012 at 4:44
  • \$\begingroup\$ @PetrAbdulin, I can sort of see it working, but the way it is at the moment, my nextTurn function calls itself to move on. Must I replace that function with a state-based switch executing from the main loop? I already have one for the windows, of course. I do find that article pretty good though. I might take a look through some other things on that site. \$\endgroup\$
    – chris
    May 4, 2012 at 4:47

1 Answer 1


I don't know where you got the idea of doing it like this, but it doesn't sound very game design like at all. For a simple game like this you shouldn't need multiple windows and multiple threads. Also events aren't commonly used in game development because they break the flow of your game loop.

Anyway why not try something like this:

Build a game loop that can have 3 states like you had before -Waiting for answer -Waiting for move -Turn finalizer

In each of these states the game loop just keeps running at 60fps, there is no waiting whatsoever, the states just influence what is being drawn to the screen and what input is accepted,

Here is a very crude example that should get you started. Keep in mind that you should never wait on something in your game loop, the goal of your game loop is run again as fast as possible. The behavior of your game loop is completely dependent on the game state. Handle your input via polling and change state when certain criteria are met.

        case GameStates.WaitingForAnswer:
                state = GamesStates.WaitingForMove;

        case GameStates.WaitingForMove:
                state = GameStates.TurnFinalizer;

        case GameStates.TurnFinalizer:
            time = time + elapsedtime;
            if(time > 5)
                state = GameStates.WaitingForAnswer;

        exiting = true;
  • \$\begingroup\$ I'll be trying it. I'll be back once I rewrite my code :p \$\endgroup\$
    – chris
    May 4, 2012 at 10:38
  • \$\begingroup\$ For your curiosity, I got this idea from not actually having found a good game programming logic book to read. I don't really have the time to do that for this thing, but afterwards I sure would like a nice book on the concepts involved in game programming. \$\endgroup\$
    – chris
    May 4, 2012 at 18:54
  • \$\begingroup\$ Please please don't do the "always running at 60fps" nonsense. People play games on laptops, tablets, phones, and other mobile devices. Naive programmers that eat up as much CPU as possible in games that aren't constantly animating or running real-time game logic can easily make games that drain a battery faster than is even remotely reasonable. \$\endgroup\$ May 5, 2012 at 18:32
  • \$\begingroup\$ You're really missing the point of this example where I try to keep everything as simple as possible and am not talking about mobile devices which require some extra thought. Though even on mobile devices running at a fixed frame rate (usually 30fps) isn't that bad, if your game loop isn't doing a lot the CPU will just idle a little each frame. \$\endgroup\$
    – Roy T.
    May 5, 2012 at 20:36
  • \$\begingroup\$ I switched over to the game state and the problems I've had were solved. Just a few subtle things now. Is there a well-accepted book on the logic behind games? I'd really like to read one/some when I have the time. \$\endgroup\$
    – chris
    May 7, 2012 at 16:21

You must log in to answer this question.

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