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
Skill Level Beginner
1. C++, SFML, Visual Studio, and Starting the First Game
2. Variables, Operators, and Decisions
3. C++ Strings, SFML Time, Player Input, and HUD
4. Loops, Arrays, Switch, Enumerations, and Functions
5. Collisions, Sound, and End Conditions
6. Object-Oriented Programming, Classes, and SFML Views
7. C ++ References, Sprite Sheets, and Vertex Arrays
8. Pointers, the Standard Template Library, and Texture Management
9. Collision Detection, Pickups, and Bullets
10. Layering Views and Implementing the HUD
Updating the HUD each frame3m 24s
11. Sound Effects, File I/O, and Finishing the Game
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.