This is the final phase of the first project. By the end of this chapter you will have your first completed game. Once you have Timber!!! up-and-running, be sure to read the last section of this chapter as it will suggest ways to make the game better. We will be looking at the following topics:
Let's add the code for the player's 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 ax sprite to chop with, and a log sprite that can whiz away each time the player chops.
Notice that after the spritePlayer
object we also declare a side
variable, playerSide,
to keep track of where the player is currently standing. Furthermore, we add some extra variables for the spriteLog
object, including, logSpeedX
, logSpeedY
, and logActive
to store how fast the log will move, and whether it is currently moving. The spriteAxe
also has two related float
constant variables to remember where the ideal pixel position is on both the left and the right.
Add this next block of code just before the while(window.isOpen())
code as we have done so often before. Note that all the code in this next listing is new, not just the highlighted code. I haven't provided any extra context for the next block of code as while(window.isOpen())
should be easy to identify. The highlighted code is the code we have just specifically discussed.
Add the entirety of this code, just before the while(window.isOpen())
line, and make a mental note of the highlighted lines we have briefly discussed. It will make the rest of the chapter's code easier to understand:
// Prepare the player Texture texturePlayer; texturePlayer.loadFromFile("graphics/player.png"); Sprite spritePlayer; spritePlayer.setTexture(texturePlayer); spritePlayer.setPosition(580, 720); // The player starts on the left side playerSide = side::LEFT; // Prepare the gravestone Texture textureRIP; textureRIP.loadFromFile("graphics/rip.png"); Sprite spriteRIP; spriteRIP.setTexture(textureRIP); spriteRIP.setPosition(600, 860); // Prepare the axe Texture textureAxe; textureAxe.loadFromFile("graphics/axe.png"); Sprite spriteAxe; spriteAxe.setTexture(textureAxe); spriteAxe.setPosition(700, 830); // Line the axe up with the tree const float AXE_POSITION_LEFT = 700; const float AXE_POSITION_RIGHT = 1075; // Prepare the flying log Texture textureLog; textureLog.loadFromFile("graphics/log.png"); Sprite spriteLog; spriteLog.setTexture(textureLog); spriteLog.setPosition(810, 720); // Some other useful log related variables bool logActive = false; float logSpeedX = 1000; float logSpeedY = -1500;
Now we can draw all our new sprites.
13.59.199.250