Having just learned what C++ game variables are and that they store values that represent the objects in our games it is probably obvious that these values held by our variables will need to change as the game progresses. We can achieve this with C++ operators. Operators in C++ are just like mathematical operators and many of them are the same symbol. So if you coped with junior school math you will have no problem with this tutorial. Let’s start by looking at a list of some of the frequently used C++ operators.
[widgets_on_pages id=”udemy_advert_cpp_1″][widgets_on_pages id=”udemy_code_details”]
C++ operators
Here is a list of some of the most useful C++ operators that allow us to do things with our game’s variables. The assignment operator which we have already seen ( =): This makes the variable to the left of the operator the same as the value to the right. For example look at other ways we might use the C++ assignment operator:
// Player gets a new high score hiScore = score;
or
// Set the score to 100 score = 100;
The addition operator ( +): This adds the values on either side of the operator. It is usually used in conjunction with the assignment operator, such as:
// Add to the score when an alien is shot score = aliensShot + wavesCleared;
or
// Add 100 to whatever the score currently is score = score + 100;
[widgets_on_pages id=”bcgp_cfgp_gpp”]
Notice that it is perfectly acceptable to use the same variable on both sides of an operator.
The subtraction operator (
-): This subtracts the value on the right side of the operator from the value on the left. It is usually used in conjunction with the assignment operator, perhaps:
// Uh oh lost a life lives = lives - 1;
or
// How many aliens left at end of game aliensRemaining = aliensRemaining - numberAliesDestroyed;
The division operator ( /): This 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 the value in swordLevel hitPoints = hitPoints / swordLevel;
or
// Give player something back for recycling a block recycledValueOfBlock = originalValue / .9;
Obviously, in the previous example, the variable
recycledValueOfBlock will need to be of the type
float to be multiplied by the value .9.
The multiplication operator (
*): Perhaps unsurprisingly, multiplies variables and numbers, such as:
// answer is equal to 100 - of course answer = 10 * 10;
or
// biggerAnswer = 1000 - duh biggerAnswer = 10 * 10 * 10;
As an aside have you ever wondered how C++ got its name? C++ is an extension of the C language. It’s inventor Bjarne Stroustrup originally called it “C with classes” but the name evolved. If you are interested read the C++ story.
The increment operator ( ++): This is a really neat way to add 1 to the value of one of our game’s variables.
This:
// Add one to myVariable myVariable = myVariable + 1;
is the same as this:
// Much neater and quicker myVariable ++;
[widgets_on_pages id=”udemy_advert_cpp_2″][widgets_on_pages id=”udemy_code_details”]
The decrement operator (
--): You guessed it, a really neat way to subtract 1 from something.
playerHealth = playerHealth -1;
is the same as:
playerHealth --;
What next
Perhaps surprisingly we know enough C++ to actually get some game-like action in a real C++ program. If you would like to practice what you have learned in this and the previous tutorial about game variables try the mini-project C++ game variables demo which uses the SFML C++ game library. In the next tutorial, Checking for conditions in our game we will try using some declarations, assignments, and operators. When we bundle these elements together into some meaningful syntax, we call it an expression.
for the score=highscore part instead of after score=100 could it be score=hiscore
Hi Leigh,
Are you asking if that syntax would work or are you saying there is a mistake in the tutorial? Or something else? Sorry to be vague, not sure what you’re asking.
If you mean would the syntax work? Yes it would.
Thanks for your comment.
i appreciate the tutorials john, its very useful. thanks to you ill be successful at something. GAMES!!!!
Hi there,
My pleasure and thanks for taking the time to comment. I look forward to seeing your achievements!
Keep it up man your teaching success
Just keep learning and don’t stop. Nobody can stop you except you.
yeah that’s what I meant cheers
You can also write:
aliensRemaining -= aliensDestroyed;
hitPoints /= swordLevel;
instead of:
aliensRemaining = aliensRemaining – aliensDestroyed;
hitPoints = hitPoints / swordLevel;
etc.
Thanks for the good tip.
i’m sick of python so i’m learning c++ and this is the first thing i found, love it, the best part is the detail, and how you explain it.
Have you thought of making videos that go through the tutorial?