Variables are the way that C++ games store and manipulate values. If you want to know how much health the player has, then you need a variable. Is the game over, or is it still playing? That's also a variable. So, first learn about variables before procee
- [John] Hi and welcome to Section Two: Variables, Operators, and Decisions: Animating Sprites. In this section, we will do quite a bit more drawing on the screen, and to achieve this, we will need to learn some of the basics of C++. Here's what we're going to do. We'll learn all about C++ variables, see how to manipulate the values stored in the variables, add a static tree ready for the player to chop, draw and animate a bee and three clouds, take a look at random numbers, learn making decisions with if and else, learn about timing and moving clouds and bee.
Now, we move on to the first video of this section, that is C++ Variables. In this video, we are going to take a look at different variables, constants, user defined types, and declaring and initializing variables. Variables are the way that our C++ games store and manipulate values. If you want to know how much health the player has, then we need a variable. Perhaps you want to know how many zombies are left in the current wave. That is a variable, as well. If you need to remember the name of the player who got a particular high score, you guessed it, we need a variable for that.
Is the game over or still playing? Yep, that's a variable, too. Variables are named identifiers to locations in memory, so we might name a variable "NumberOfZombies" and that variable could refer to a place in the memory that stores a value representing the number of zombies that are left in the current wave. The way that computer system addresses locations in memory is complex. Programming languages use variables to give a human friendly way to manage our data in memory.
Our brief discussion about variables implies that there must be different types of variable. Types of Variable. There are a wide variety of C++ variable types. It would easily be possible to spend an entire section discussing them. This is a table of the most commonly used types. Then we will look how to actually use each of these variable types. The compiler must be told what type of variable is, so that it can allocate the right amount of memory for it. It is good practice to use the best and most appropriate type for each and every variable you use.
For example, you need a floating point number with just five significant digits. The compiler won't complain if you store it as a double, however, if you tried to store a float or a double in an int, it will change the value to fit the int. Constants. Sometimes we need to make sure that a value can never be changed. To achieve this, we can declare and initialize a constant using the "const" keyword. It is conventional to declare constants in all uppercase. The value of the preceding constants can never be altered.
User Defined Types. User defined types are way more advanced than the types we have just seen. When we talk about user defined types in C++, we are usually talking about classes. We briefly talked about classes and their related objects in the previous section. We can write code in a separate file, sometimes in two separate files. From these, we will then be able to declare, initialize, and use them. We will discuss more about this in later sections. Declaring and Initializing Variables.
So far, we know that variables are for storing the data values that our games need in order to work. For example, a variable could represent the number of lives a player has or that player's name. We also know that there is a wide selection of different types of values that these variables can represent, such as; int, float, boolean, and so on. Of course, what we haven't seen yet is how we would actually go about using a variable. There are two stages for creating and preparing a new variable. The stages are called "Declaration" and "Initialization".
Declaring Variables. We can declare variables in C++ like this. So, here, player's score is variable of integer type. In second example, player initials is a character variable. Similarly, value pi is a float type. And, is alive is a boolean type. Initializing Variables. Now, we have declared the variables with meaningful names. We can initialize those same variables with appropriate values like this. After initializing each variable, we have to write semicolon declaring and initializing in one step.
When it suits us, we can combine the declaration and initialization steps into one. We just have to write the type before the variable. If you want to see a complete list of C++ types, then check this webpage. You get to know more about C++ data types. Declaring and initializing user defined types. We've already seen examples of how we declare and initialize some SFML defined types. Because the way we can create define these types, classes is so flexible, the way we declare and initialize them is also highly varied.
Here are a couple of reminders for declaring and initializing user defined types from the previous section. In this line, we create an object called "vm" of type "VideoMode" and initialize it with two integer values, 1280 and 720. In this line, we create an object called "textureBackground" of type "Texture", but we don't do any initialization. Note that it is possible, in fact very likely, that even though we are not suggesting any specific values with which to initialize textureBackground, some variables may be set up internally.
Whether or not an object needs or has the option of giving initialization values, at this point, is entirely dependent on how the class is coded and is almost infinitely flexible. Cool. That marks the end of this video. In this video, we have learned about C++ variables. Great! In the next video, we'll take a look at manipulating variables.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- Creating the main game loop
- Drawing the game background
- Handling errors
- Manipulating C++ variables
- Randomizing numbers
- Making decisions with if, else, and switch
- Moving game objects
- Pausing and restarting games
- Adding scoring and messages
- Using class enumerations and arrays
- Adding sound effects
- Object-oriented programming in C++
- C++ references
- Texture management
- Collision detection
- Implementing the HUD
- Finishing the game