Chapter 8. Creating Alien Attack
The framework has come on in leaps and bounds and we are almost ready to make our first game. We are going to create a simple 2D sidescrolling shooter in the vein of the classic '80's and '90's shooter games such as R-Type or Pulstar. However, the game will not be set in space. Aliens have attacked earth and only you and your weaponized helicopter can stop them. One level of fast-paced action is available in the source code downloads and this chapter will cover the steps taken to create it. Here is a screenshot of the game we will be creating:
And another slightly more hectic shot:
There are still a few things that the framework must handle before we can create this game. These additions include:
By the end of the chapter you will have a good understanding of how this game was built using the framework and you will have the ability to continue and improve it. In this chapter, we will cover:
Using the SDL_mixer extension for sound
The SDL_mixer extension has its own Mercurial repository that can be used to grab the latest source for the extension. It is located at http://hg.libsdl.org/SDL_mixer. The TortoiseHg application can again be used to clone the extension's Mercurial repository. Follow these steps to build the library:
Open up TortoiseHg and press CTRL+SHIFT+N to start cloning a new repository.
Type http://hg.libsdl.org/SDL_mixer into the source box.
The Destination will be C:\SDL2_mixer
.
Hit Clone and wait for completion.
Navigate to C:\SDL2_mixer\VisualC\
and open SDL_mixer.vcproj
in Visual Studio 2010.
As long as the x64 folder outlined in Chapter 2, Drawing in SDL was created, the project will convert with no issues.
We are going to build the library without MP3 support as we are not going to need it, and also it does not work particularly well with SDL 2.0.
Add MP3_MUSIC_DISABLED
to the Preprocessor Definitions in the project properties, which can be found by navigating...
Setting up the basic game objects
The majority of the work that went into creating Alien Attack was done in the object classes, while almost everything else was already being handled by manager classes in the framework. Here are the most important changes:
The GameObject
base class has a lot more to it than it previously did.
Most objects in the game fire bullets and they all pretty much need to be checked for collisions against bullets as well; the bottom line—bullets are important in Alien Attack. The game has a dedicated BulletHandler
class that handles the creation, destruction, updating, and rendering of bullets.
There are two types of bullets in the game, PlayerBullet
and EnemyBullet
, both of which are handled in the same
BulletManager
class. Both of the bullet classes are declared and defined in Bullet.h
:
With so many bullets flying around and having the Enemy
objects to check collisions against, it is important that there be a separate class that does this collision checking for us. This way we know where to look if we decide we want to implement a new way of checking for collisions or optimize the current code. The Collision.h
file contains a static method that checks for collisions between two SDL_Rect
objects:
Alien Attack is a pretty robust game at the moment; we highly recommend looking through the source code and becoming familiar with every aspect of it. Once you have a good understanding of most of the areas of the game, it is a lot easier to see where certain areas could be enhanced. Here are some ideas that could be added to improve the game:
Bullets could be created at the start of a level and stored in an object pool; so rather than creating and deleting bullets all the time they can be pulled from and put back into the object pool. The main advantage of this approach is that the creation and destruction of objects can be quite expensive when it comes to performance. Eliminating this while the game is running could give a real performance boost.
Collision detection could be optimized further, possibly through the addition of a Quadtree
to stop unnecessary collision checks.
The source code has a few areas that use string comparisons to check types. This can be a bit...
The framework has been successfully used to create a game—Alien Attack. Throughout this chapter, the most important parts of the game were covered, along with a short explanation of why they were designed in such a way. With the source code for this game available, there is now a great project to start practicing with.