Bright Hub
 
Matthew Casperson's Hubfolio

SDL Programming Tutorial - Levels

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

In this article we look at defining a level, and allowing the player to move between levels.

Recapping how the enemies are added to the level, the EnemyManager creates enemy waves, and the enemy waves create the enemies. By allowing any wave to create any enemy, and giving the EnemyManager the ability to randomly select from a range of enemy waves, we have created a very flexible system of randomly generating levels. The only problem is that the only way to finish a level to this point has been for the player to die, which drops us back to the main menu.

In order for levels to be defined we need to set some condition that signals the end of the level, at which point the player will progress to the next level. This condition could be almost anything, like number of enemies destroyed, score reached, or time spent in the level. For this tutorial we will use the time spent in the level as an indication as to when the level should end. So after a specified amount of time no more enemies will be created and the player will move onto the next level.

ApplicationManager.h / ApplicationManager.cpp

The ApplicationManager will take care of recording the current level, and also implementing a new state called “LevelSummary” that will display a screen in between the levels. In addition the ApplicationManager will also have to record the players weapon and weaponLevel between levels, otherwise the player would start with the default weapon for every new level.

To achieve this the ApplicationManager gains 3 new properties: playerWeaponLevel, playerWeapon and currentLevel, to record the level and the players weapons. [code] The ChangePlayerWeapon, GetPlayerWeapon and GetPlayerWeaponLevel functions allow the player to find out which weapons to fire, and also to change weapons when a powerup is picked up. [code]

The StartLevelSummary and ShutdownLevelSummary functions will be called when the level summary screen is displayed at the end of the level, and when it is removed before the next level. [code]

The StartLevelSummary and ShutdownLevelSummary functions need to be registered against the new LevelSummary state to ensure they are called when the state is changed. [code]

The logic for these functions is quite simple, incrementing the currentLevel property and creating a new LevelSummary object for the splash screen when the LevelSummary state has been entered, and calling ShutdownAll on the EngineManager to remove the LevelSummary object when the state is exited. [code]

The StartMainMenu function has been modified slightly to reset the players score, current level and weapons when the main menu is displayed, which essentially resets the game back to the initial state. [code]

The last change that is made is that the background layers are modified depending on the current level. There are now 3 background layer collections – one for the day, a purple one for the evening/morning, and a dark one for night time. As the player progresses through the levels these background will be shown in sequence. [code]

You will also note that the EnemyManager constructor now gets the currentLevel, instead of always being constructed with a level of 1. [code]

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
©2010 Bright Hub Inc. All rights reserved. Page copy protected against web site content infringement by Copyscape