Chapter 6.  Object-Oriented Programming, Classes, and SFML Views

This is the longest chapter of the book. There is a fair amount of theory, but the theory will give us the knowledge to start using Object-Oriented Programming (OOP) to powerful effect. Furthermore, we will not waste any time in putting that theory to good use. Before we explore C++ OOP, we will find out about and plan our next game project.

This is what we will do in the following chapter:

  • Plan the Zombie Arena game
  • Learn about OOP and classes
  • Code the Player class
  • Learn about the SFML View class
  • Build the Zombie Arena game engine
  • Put the Player class to work

Planning and starting the Zombie Arena game

At this point, if you haven't already, I suggest you go and watch a video of Over 9,000 Zombies (http://store.steampowered.com/app/273500/) and Crimson Land (http://store.steampowered.com/app/262830/).

Our game will obviously not be as in-depth or advanced as either of the examples but we will have the same basic set of features and game mechanics:

  • A Heads-up Display (HUD) that shows details such as score, high score, bullets in clip,total bullets left, player health, and zombies left to kill
  • The player will shoot zombies while frantically running away from them
  • Move around a scrolling world using the  W, A, S, and D keys while aiming the gun using the mouse
  • In between each level, choose a level up that will affect the way the game needs to be played to succeed
  • Collect pick-ups to restore health and ammunition
  • Each wave brings more zombies and a bigger arena

There will be three types of zombie to splatter. They will have different attributes such as appearance, health, and speed. We will call them chasers, bloaters, and crawlers. Take a look at this annotated screenshot of the game to see some of the features in action and the components and assets that make up the game:

Planning and starting the Zombie Arena game

Here is a little bit more information about each of the numbered points:

  1. The SCORE and HI SCORE. These, along with the other parts of the HUD, will be drawn in a separate layer, known as a View. The hi-score will be saved and loaded to a file.
  2. This is a texture that will build a wall around the arena. This texture is contained in a single graphic called a sprite-sheet, along with the other background textures (3, 5, and 6).
  3. The first of two mud textures from the sprite-sheet.
  4. This is an ammo pick-up. When the player gets this they will be given more ammunition. There is a health pick-up as well. The players can choose to upgrade these pick-ups in between waves of zombies.
  5. A grass texture, also from the sprite-sheet.
  6. The second mud texture from the sprite-sheet.
  7. A blood splat where there used to be a zombie.
  8. The bottom part of the HUD. From left to right there is an icon to represent ammo, the number of bullets in the clip, the number of spare bullets, a health bar, the current wave of zombies, and the number of zombies remaining in this wave.
  9. The player character.
  10. A crosshair which the player aims with the mouse.
  11. A slow-moving but strong bloater zombie
  12. A slightly faster-moving but weaker crawler zombie. There is also a chaser zombie who is very fast and weak. Unfortunately, I couldn't manage to get one in the screenshot before they were all killed.

We have a lot to do and new C++ skills to learn. Let's start by creating a new project.

Creating a project from the template

Creating a new project is now extremely easy. Just follow these straightforward steps in Visual Studio:

  1. Select File | New Project from the main menu.
  2. Make sure that Visual C++ is selected in the left-hand menu and then select HelloSFML from the list of presented options. This next image should make this clear:

    Creating a project from the template

  3. In the Name: field, type ZombieArena and also make sure that the Create directory for solution option is checked. Now click OK.
  4. Now we need to copy the SFML .dll files into the main project directory. My main project directory is D:Visual Studio StuffProjects ZombieArenaombieArena. This folder was created by Visual Studio in the previous step. If you put your Projects folder somewhere else then perform this step there instead. The files we need to copy in to the Projects folder are located in your SFMLin folder. Open a window for each of the two locations and highlight the required .dll files.

Now copy and paste the highlighted files into the project. The project is now set up and ready to go.

The project assets

The assets in this project are more numerous and diverse than the previous game. The assets include:

  • A font for the writing on the screen
  • Sound effects for different actions such as shooting, reloading, or getting hit by a zombie

All the graphics for the character, zombies, background, and sound required for the game are included in the download bundle. They can be found in the Chapter 6/graphics, and Chapter 6/sound folders, respectively.

The font that is required has not been supplied. This is because I wanted to avoid any possible ambiguity regarding the license. This will not cause a problem though, as I will show you exactly where and how to choose and download fonts for yourself.

Although I will provide either the assets themselves or information on where to get them, you might like to create and acquire them for yourself.

Exploring the assets

The graphical assets make up the parts of the scene that is our Zombie Arena game. Take a look at the graphical assets and it should be clear where in our game they will be used:

Exploring the assets

What might be less obvious, however, is the background_sheet.png which contains four different images. This is the sprite-sheet I mentioned previously, and we will see how we can use one to save memory and increase the speed of our game in Chapter 7, C++ References, Sprite Sheets, and Vertex Arrays.

The sound files are all .wav format. These are files which contain the sound effects that we will play at certain events throughout the game. They are:

  • hit.wav: A sound that plays when a zombie comes into contact with the player
  • pickup.wav: A sound that plays when the player touches (collects) a health boost (pick-up)
  • powerup.wav: A sound  that plays when the player chooses an attribute to increase (power-up) in between each wave of zombies
  • reload.wav: A satisfying click to let the player know they have loaded a fresh clip of ammunition
  • reload_failed.wav: A less satisfying sound that indicates failure to load new bullets
  • shoot.wav: A shooting sound
  • splat.wav: A sound like a zombie being hit by a bullet

Adding the assets to the project

Once you have decided which assets you will use, it is time to add them to the project. These next instructions will assume you are using all the assets supplied in the book's download bundle. Where you are using your own, simply replace the appropriate sound or graphic file with your own, using exactly the same file name that is used in this book:

  1. Browse to D:Visual Studio StuffProjectsombieArenaombieArena.
  2. Create three new folders within this folder and name them graphics, sound, and fonts.
  3. From the download bundle, copy the entire contents of Chapter 6/graphics into the D:Visual Studio StuffProjectsombieArenaombieArenagraphics folder.
  4. From the download bundle, copy the entire contents of Chapter 6/sound into the D:Visual Studio StuffProjectsombieArenaombieArenasound folder.
  5. Now visit http://www.1001freefonts.com/zombie_control.font in your web browser and download the Zombie Control font.

Extract the contents of the zipped download and add the zombiecontrol.ttf file to the D:Visual Studio StuffProjectsombieArenaombieArenafonts folder. Now it's time to learn some more C++, so we can start writing the code for Zombie Arena.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.43.124