Bright Hub
 
Matthew Casperson's Hubfolio

SDL Programming Tutorial - Bosses

Article by Matthew Casperson (4,883 pts )
Published on Sep 16, 2009

In this tutorial we look at adding bosses to the levels.

Every good shoot’em’up needs a boss. A boss will be a standard enemy class, but unlike a standard enemy the boss will be created by the EnemyFactory directly from the EnemyManager, rather than being created through an enemy wave. This is because each box will be only one ship, and will be created at the end of randomly selected levels.

BossOne.h / BossOne.cpp

BossOne is a good example of how to create a slightly more intelligent enemy. It moves out from the side of the screen and then begins moving up and down the screen, firing off shots at predetermined intervals. All this logic exists in the EnterFrame function.

First the boss moves out from the side of the screen. [code]

Then it begins moving up and down. [code]

The boss graphics has 7 guns, so the boss will shoot 7 times starting from the top gun and working down. The yOffset variable simply refers to the distance from the top of the boss image where the guns are placed. [code]

Once the seventh shot has been fired we pause for effect before starting the firing again. [code]

EnemyManager.h / EnemyManager.cpp

The boss has a one in three chance of being sent in at the end of the level. When the EnemyManager is first created it determines if this is going to be a boss level. If bossAdded is false then a boss will be added at the end of the level. If it is set to true the EnemyManager assumes that the boss has already been created, so setting it to true in the constructor means that no boss will be created for this level. [code]

When the level is finished the EnemyManager checks to see if a boss should be added. If so, a call to the EnemyFactory is made to create a random boss. The EnemyManager then waits for the boss to be destroyed before ending the level. [code]

EnemyFactory.h

Unlike the standard enemies, the bosses are created directly by the EnemyManager, rather than being created through an enemy wave. The CreateRandomBoss function creates a new instance of the BossOne class (via the CreateBoss1 function). If additional bosses are created you would want to assign bosses to levels much like the enemy waves, but since we only have one boss we just create it directly. [code]

Player.h / Player.cpp

Because no enemies could shoot the player has not had to deal with collisions between itself and enemy weapons. Since the boss can now shoot we need to call the Damage function, inheretied from the DamageableObject class, when a collision with an enemy weapon is detected. [code]

As we get towards the end of this tutorial series you can see how easy it is to add new functionality. With simple changes to 3 classes, and one additional class for the new boss, we have implemented some end of level bosses.

Download the source code here.

Read more in the SDL Programming Tutorial series

Related Files

Images

Search More About:

 
Follow Matthew Casperson
Receive weekly updates from Matthew Casperson
 
Bright Hub - Science & Technology Articles, Buyer's Guides, How-To Tips and Software Reviews
About Bright Hub | Contact Us | Advertise with Us | Become a Writer | RSS | Site Map | Terms of Use | Privacy Policy | Copyright Policy
©2009 Bright Hub Inc. All rights reserved. Page copy protected against web site content infringement by Copyscape