Beginning C++ Game Programming
上QQ阅读APP看书,第一时间看更新

Manipulating variables

At this point, we know exactly what variables are, the main types they can be, and how to declare and initialize them. We still can't do that much with them, however. We need to manipulate our variables; add them; take them away; and multiply, divide, and test them.

First, we will deal with how we can manipulate them and then we will look at how and why we test them.

C++ arithmetic and assignment operators

In order to manipulate variables, C++ has a range of arithmetic operators and assignment operators. Fortunately, most arithmetic and assignment operators are quite intuitive to use and those that aren't are quite easy to explain. To get us started, let's look at a table of arithmetic operators, followed by a table of assignment operators, all of which we will regularly use throughout this book:

And now for the assignment operators:

Important note

Technically, all of these operators, except for =, --, and ++, are called compound assignment operators because they comprise more than one operator.

Now that we have seen a good range of arithmetic and assignment operators, we can actually look at how we can manipulate our variables by combining operators, variables, and values to form expressions.

Getting things done with expressions

Expressions are the result of combining variables, operators, and values. Using expressions, we can arrive at a result. Furthermore, as we will soon see, we can use an expression in a test. These tests can be used to decide what our code should do next. First, let's look at some simple expressions we might see in our game code. Here is one example of a simple expression:

// Player gets a new high score

hiScore = score;

In the preceding code, the value held in the score variable is used to change the value in the hiScore variable. The two variables now hold the same value, but note that they are still separate and distinct variables (places in memory). This would likely be just what we need when the player beats a high score. Here is another example:

// Set the score to 100

score = 100;

Let's take a look at the addition operator, which used in conjunction with the assignment operator:

// Add to the score when an alien is shot

score = aliensShot + wavesCleared;

In the preceding code, the values held by aliensShot and wavesCleared are added together using the addition operator and then the result of the addition is assigned to the score variable. Now, let's take a look at the following code:

// Add 100 to whatever the score currently is

score = score + 100;

Note that it is perfectly acceptable to use the same variable on both sides of an operator. In the preceding code, 100 is added to the value held by the score variable and then this new value is then assigned back into score.

Look at the subtraction operator in conjunction with the assignment operator. The following code subtracts the value on the right-hand side of the subtraction operator from the value on the left. It is usually used in conjunction with the assignment operator, perhaps like so:

// Uh oh lost a life

lives = lives - 1;

It can also be used like this:

// How many aliens left at end of game

aliensRemaining = aliensTotal - aliensDestroyed;

Next, we will see how we might use the division operator. The following code divides the number on the left by the number on the right. Again, it is usually used with the assignment operator, like this:

// Make the remaining hit points lower based on swordLevel

hitPoints = hitPoints / swordLevel;

It can also be used like this:

// Give something, but not everything, back for recycling a block

recycledValueOfBlock = originalValue / .9f;

Obviously, in the previous example, the recycledValueOfBlock variable will need to be of the float type to accurately store the answer to a calculation like that.

Perhaps unsurprisingly, we could use the multiplication operator like this:

// answer is equal to 100, of course

answer = 10 * 10;

It can also be used like this:

// biggerAnswer = 1000, of course

biggerAnswer = 10 * 10 * 10;

Important note

As a side note, have you ever wondered how C++ got its name? C++ is an extension of the C language. Its inventor, Bjarne Stroustrup, originally called it "C with classes", but the name evolved. If you are interested, you can read the story of C++ at http://www.cplusplus.com/info/history/.

Now, let's look at the increment operator in action. This is a neat way to add 1 to the value of one of our game's variables.

Take a look at the following code:

// Add one to myVariable

myVariable = myVariable + 1;

The preceding code gives the same result as the following code:

// Much neater and quicker

myVariable ++;

The decrement operator, --, is, you guessed it, a quick way to subtract 1 from something, like so:

playerHealth = playerHealth -1;

This is the same as doing the following:

playerHealth --;

Let's look at a few more operators in action and then we can get back to building the Timber!!! game. The addition, subtraction, multiplication, and division operators each have a related operator that combines their primary function (adding, subtracting, and so on) with assignment. They allow us to use more concise code when we want to perform the primary function of the operator, followed by assignment. Have a look at the four examples (one for each operator) that follow:

someVariable = 10;

// Multiply the variable by 10 and put the answer

// back in the variable

someVariable *= 10;

// someVariable now equals 100

// Divide someVariable by 5 put the answer back

// into the variable

someVariable /= 5;

// someVariable now equals 20

// Add 3 to someVariable and put the answer back

// into the variable

someVariable += 3;

// someVariable now equals 23

// Take 25 from someVariable and put the answer back

// into the variable

someVariable -= 25;

// someVariable now equals -2

In the preceding four examples, we can see that the *=, /=, +=, and -= operators can be used to shorten the syntax when we want to use one of the four arithmetic operators followed by an assignment. We will do this quite a bit throughout this book.

It's time to add some more sprites to our game.