Game hangs up (2 while loops)

I am trying to build a small snake game.
And I was trying to do a turn with 2 while loops ( the first is while(!WindowShouldClose() and the second is in the game logic) but when I try to access the second while loop it hangs up and does nothing (at least on the screen).
Would really appreciate if someone could tell me why it happens. :blush:

PS While debugging and pressing “Step into” in the while loop the snake.x is updating

The outer loop is being drawn for each “frame” (every 16ms approx if you’re doing 60fps).

This depends on the Begin/Clear/End drawing commands wrapping around the frame to tell it when to start and when to finish for each frame.

If you run an inner loop in that, it will continue to execute inside the current frame and won’t let the frame finish, display itself, and advance to the next one - hence appearing stuck.

Also whilst you are inside a current frame, all of the input has already been read and determined (I would highly assume), you only get to check that when this frame started, whether any of those keys were being pressed. But their state won’t change again until the next frame - something which is being prevented from being reached.

Instead of an inner loop, you’ll probably just want to use more conditional logic (if’s) to determine what to do inside a single frame, given those conditions being met.

Things like user input, delta time, and so on, won’t change inside a frame or main engine loop.

Your inner while loop is an infinite loop when you are not pressing any keys other than A.

It should be an IF statement instead.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms