As you now move closer to running the game. Add the code for the player's sprite, as well as a few more sprites and textures, and then draw them.
- [Instructor] Hi and welcome to section five, collisions, sound, and end conditions, making the game playable. In this section, we will start with preparing and drawing the player and other sprites. Then we will move to handling the player's input and death. At the end, we will take a look at simple sound FX and improving the game and the code. Now let's start with the first video of this section, preparing and drawing the player and other sprites. In this video, we are going to take a look at the code to prepare and draw the player as well as other sprites such as axe, gravestone, log, et cetera.
Let's add the code for the player sprite as well as a few more sprites and textures at the same time. This next, quite large, block of code, also adds a gravestone sprite for when the player gets squished, an axe sprite to chop with, and a log sprite that can whiz away each time the player chops. Add this block of code just before the while window is open. Code as we have done so often before. First we create an object of type texture, called texturePlayer. Then we load a graphic into texture from player.png graphics file.
Then we declare an object of type Sprite called SpritePlayer. Next we associate text player with SpritePlayer. Finally we set the position of the player using coordinates 580 and 720. Notice that after the SpritePlayer object, we also declare a side variable, playerSide, to keep track of where the player is currently standing. This means player is currently standing on left. Preparing the gravestone is similar to preparing the player. First we create an object of type texture called textureRIP. Then we load a graphic from rip.png graphics file.
Next we create an object called spriteRIP and associate textureRIP and spriteRIP. And lastly, we set the position of the gravestone. For preparing axe, we create textureAxe, load the axe graphics, create spriteAxe and after associating it with textureAxe, set the position of axe. The sprite axe also has two related float constant variables to remember where the ideal pixel position is on both the left and the right. Just as we have prepared the player, gravestone, and axe, we prepare the flying log.
Furthermore, we add some extra variables for spriteLog object, including logSpeedX, logSpeedY, and logActive. LogActive shows whether it is currently moving or not. LogSpeedX and logSpeedY will decide how fast the log will move. Now we can draw all our new sprites. Before we add the code to move the player and use all our new sprites, let's draw them. Try this so that as we add code to update, change, move the sprites, we will be able to see what is happening.
Add this highlighted code to draw the four new sprites which are sprite player, axe, flying log, and gravestone. Now run the game and you will see our new sprites in the scene. Wow, we are really close to a working game now. In this video, we have learned preparing and drawing the player and other sprites. Cool. In the next video, we'll take a look at handling player's input.
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