Chapter 4.  Loops, Arrays, Switch, Enumerations, and Functions – Implementing Game Mechanics

This chapter probably has more C++ information than any other chapter in the book. It is packed with fundamental concepts that will move our understanding on enormously. It will also begin to shed light on some of the murky areas that we have been skipping over a little bit, such as functions and game loops.

Once we have explored a whole list of C++ language necessities we will then use everything we know to make the main game mechanic, the tree branches, move. By the end of this chapter we will be ready for the final phase and the completion of Timber!!!.

We will be looking at the following topics:

  • Loops
  • Arrays
  • Making decisions with switch
  • Enumerations
  • Getting started with functions
  • Creating and moving the tree branches

Loops

In programming, we often need to do the same thing more than once. The obvious example that we have seen so far is our game loop. With all the code stripped out, our game loop looks like this:

while (window.isOpen()) 
{      
 
} 

There are a few different types of loop and we will look at the most commonly used. The correct term for this type of loop is a while loop.

while loops

The while loop is quite straightforward. Think back to the if statements and their expressions that evaluated to either true or false. We can use the exact same combination of operators and variables in the conditional expression of our while loops.

As with if statements, if the expression is true the code executes. The difference in comparison a while loop, however, is that the C++ code within it will continue to execute until the condition is false. Take a look at this code:

int numberOfZombies = 100; 
 
while(numberOfZombies > 0) 
{ 
   // Player kills a zombie 
   numberOfZombies--; 
 
   // numberOfZombies decreases each pass through the loop 
} 
 
// numberOfZOmbies is no longer greater than 0 

This is what is happening in the previous code. Outside of the while loop, int numberOfZombies is declared and initialized to 100. Then the while loop begins. Its conditional expression is numberOfZombies > 0. Therefore the while loop will continue looping through the code in its body until the condition evaluates to false. This means that the code above will execute 100 times.

On the first pass through the loop, numberOfZombies equals 100 then 99 then 98 and so on. But once numberOfZOmbies is equal to zero, it is of course, no longer greater than zero. Then the code will break out of the while loop and continue to run, after the closing curly brace.

Just like an if statement, it is possible that the while loop will not execute even once. Take a look at this:

int availableCoins = 10; 
 
while(availableCoins > 10) 
{ 
   // more code here. 
   // Won't run unless availableCoins is greater than 10 
} 

Moreover, there is no limit to the complexity of the expression or the amount of code that can go in the loop body. Consider this hypothetical variation of a game loop:

int playerLives = 3; 
int alienShips = 10; 
 
while(playerLives !=0 && alienShips !=0 ) 
{ 
   // Handle input 
   // Update the scene 
   // Draw the scene 
} 
 
// continue here when either playerLives or alienShips equals 0 

The previous while loop would continue to execute until either playerLives or alienShips was equal to zero. As soon as one of those conditions occurred, the expression would evaluate to false, and the program would continue to execute from the first line of code after the while loop.

It is worth noticing that, once the body of the loop has been entered, it will always complete at least once, even if the expression evaluates to false, part way through, as it is not tested again until the code tries to start another pass. For example:

int x = 1; 
 
while(x > 0) 
{ 
   x--; 
   // x is now 0 so the condition is false 
   // But this line still runs 
   // and this one 
   // and me! 
} 
 
// Now I'm done! 

The previous loop body will execute once. We can also set up a while loop that will run forever, unsurprisingly called an infinite loop. Here is an example:

int y = 0; 
 
while(true) 
{ 
   y++; // Bigger... Bigger... 
} 

If you find the above loop confusing, just think of it literally. A loop executes when its condition is true. Well, true is always true and will therefore keep executing.

Breaking out of a while loop

We might use an infinite loop so that we can decide when to exit the loop from within its body, rather than in the expression. We would do this by using the break keyword, when we are ready to leave the loop body. Perhaps it would look like this:

int z = 0; 
 
while(true) 
{ 
   z++; // Bigger... Bigger... 
   break; // No you're not 
    
   // Code doesn't reach here 
} 

You might also have been able to guess that we can combine any of the C++ decision making tools such as if, else, and another we will learn shortly, switch, within our while loops and other loop types as well. Consider this example:

int x = 0; 
int max = 10; 
 
while(true) 
{ 
   x++; // Bigger... Bigger... 
 
   if(x == max)
   {     
     break;   
   } // No you're not 
 
   // code reaches here only until x = 10 
} 

We could go on for a long time looking at the various permutations of C++ while loops, but at some point we want to get back to making games. So let's move on to another type of loop.

for loops

The for loop has a slightly more complicated syntax than a while loop, because it takes three parts to set one up. Have a look at the code first then we will break it apart:

for(int x = 0; x < 100; x ++) 
{ 
   // Something that needs to happen 100 times goes here 
} 

Here is what all the parts of the for loop condition do.

for(declaration and initialization; condition; change before each iteration)

To clarify further, here is a table to explain all of the three key parts as they appear in the previous for loop example.

Part

Description

Declaration and initialization

We create a new int variable i and initialize it to 0

Condition

Just like the other loops, it refers to the condition that must be true for the loop to execute

Change after each pass through loop

In the example, x ++ means that 1 is added/incremented to x on each pass

We can vary for loops to do many more things. Here is another simple example that counts down from 10:

for(int i = 10; i > 0; i--) 
{ 
   // countdown 
} 
 
// blast off 

The for loop takes control of initialization, condition evaluation, and the control variable upon itself. We will use for loops in our game, later this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.106.237