Game loops

Boltgreywing

Senior Member
I am looking to understand which of these loops would work in a game loop design strategy. What are the advantages and disadvantages of each approach?

//This will be a working pong game.

//It will feature a single paddle, one ball, and three walls.

//It will be played from the left.

//The ball will increase in speed with each time it hits the paddle.

//There is only one life in this game.

//Points are recieved each time the ball hits a wall or the paddle itself.

//The object is to keep the ball above the paddle as long as possible.

//Newer version will have enemies and walls that will emerge from the right wall.

//They will try to attack the player but can only be defeated by the ball.

//Which of these ones should I use?

//Choice A:
for( ; ;)
{

}

//Choice B:
while(true)
{

}

//Choice C:
do
{

}
while(true);

 
So, no real difference in B and C, usually you would not use a for, since that is for iteration. while, and do..while are essentially the same.

For those interested in what a game loop is, in games that are not web based, the game is ran via a main loop, that loop repeatedly updates the screen with other processing (such as input, movement of items on screen, etc).

 
So, no real difference in B and C, usually you would not use a for, since that is for iteration. while, and do..while are essentially the same.

For those interested in what a game loop is, in games that are not web based, the game is ran via a main loop, that loop repeatedly updates the screen with other processing (such as input, movement of items on screen, etc).
@Digital: There actually is a difference between b and c. If you put an if statement in the middle that sets a variable that is looked at the bottom then it will exit on that iteration. However if you use the while loop and an if statement is in the middle that sets a variable that is used in the top expression you need to wait until it runs through whole portion of the loop before it returns to the top.

For example suppose you have this:

while(running) #Line 1

{ #Line 2

  if(input == "quit") #Line 3

     set running = false; #Line 4

} #Line 5

#Line 6 exits loop

If you run through the loop and the input is quit you will then need till wait till it hits line 5 before it returns back to line 1 to jump to line 6.

In the do while loop example:

do #Line 1

{ #Line 2

if(input == "quit") #Line 3

   set running = false; #Line 4

} #Line 5

while(running); #Line 6

#Line 7 exits loop

In this case once you hit line 3 you have already executed half of the statements and so now you only need to hit line 6 to exit which then jumps down to line 7. This saves the compiler time since you don't have to jump back to the top again.

 
Last edited by a moderator:
Back
Top