Basic Platformer Features
- Platformer Game Design
- Starter Kit Assets
- Exploring the Basic Classes
- Basic Modifications to the Platformer Starter Kit
Platformer Game Design
The design of Platformer should be familiar to all gamers. You must avoid the enemies and reach the level exit before time expires (collecting gems along the way). If you complete the level with time remaining, that time is converted to a point bonus, and it is added to your current score. You lose a life if you run into an enemy, fall off a ledge, or run out of time before collecting all gems and reaching the exit.
Figure 1. Level 2 of the Platformer Starter Kit
The game world is composed of individual levels where each level is automatically constructed, using several square tile types, from an existing text file. This text file uses a set of symbols to map out the level, locating the start and exit points, enemy starting positions, gem locations and tile locations (including their type). For a complete listing of the level file format, see Advanced Platformer Features.
Three tile types are implemented by Platformer and represent the ledges and impassable areas of a level.
|Impassable||Players and enemies cannot pass through the tile from any direction.|
|Passable||Players and enemies can pass through the tile freely. The main purpose of this tile type is to provide decoration to the level – decoration such as jungle foliage, rocks, and other scenery.|
Platform tiles behave like passable tiles except that players and enemies can stand on (or in the case of the player character, fall onto) the top edge of the tile without falling.
This is implemented by checking for collision between the character's bounding rectangle and the top edge of the platform tile. If collision occurs, the affected character's Y velocity (that is, the character's speed when falling) is forced to 0. If the character stood, or fell onto, a passable tile, no collision checking is performed. This causes the character to continue falling until the character reaches an impassable tile or passes the bottom edge of the screen. For more information on collision checking and game world physics, see Advanced Platformer Features.
In addition to tiles, there are several special objects that represent special locations or objects in the game world.
|Level Start||The player character begins the level at this location, facing to the right.|
|Gem||An object that players collect for points and level completion. The player must collect all gems and reach the exit before time expires to complete the level.|
|Level Exit||The location the player must reach to complete the level before time expires. Failure to reach the exit (and collect all gems) forces the player to lose a life and begin at the Level Start location.|
The player character can run and jump. The player is affected by a simple gravity implementation, and can run off platforms without jumping. When falling, the character is halted by any collision with a platform-type tile. If the player character is moving and not under direct control of the player, simple pseudo-drag is implemented. This gradually stops the player character instead of causing an instant stop.
Initially, Platformer implements a single enemy type. This enemy slowly walks back and forth along a platform, automatically turning around at the platform's edge. The player character cannot kill the enemy and is immediately killed by the enemy upon contact.
Starter Kit Assets
Assets in the various content projects are platform-dependent. For Windows and Xbox 360 projects, the HighResolutionContent content project is used. It contains fonts, level backgrounds, higher-resolution tile textures (64x48) and single-animation sprite sheets. Each sprite list is a strip of single frames (96×96) that provide animation when displayed sequentially. For instance, the Celebrate.png asset is a series of frames of a celebration by the player character. The entire set of animation lists for a specific character (either enemy or player) are kept in a separate directory.
Zune projects use a lower resolution set of these level backgrounds, tile textures, fonts, and single-frame sprite lists. For example, tile texture resolutions are 32×32 and sprite lists contain frames that are half the size of their higher-resolution counterpart, 48×48.
In addition to the HighResolutionContent and LowResolutionContent content projects, all projects use an additional content project, SharedContent. This content project contains the sound effects for the game. It includes sound effects used when the player character collects a gem, reaches the level exit, or dies by falling or enemy contact. Unlike texture files, sound files are shared between all three platforms. Having a separate set of lower fidelity sound effects for the Zune would not be worth the effort and loss in quality when compared to the size saved when Platformer is installed.
Shared projects (such as SharedContent) are the default in XNA Game Studio, but it is also possible to set up platform-specific game content projects. It's easier to implement if you do a little planning before adding new projects to your solution. For more information on sharing game assets between projects, see Platform-Specific Content Projects.
Exploring the Basic Classes
The Platformer starter kit can be pretty overwhelming the first time you see it. However, it can be broken down into more manageable pieces, based on the complexity of the game object and its usage by the Platformer game. In this section, we'll explore Platformer's basic classes and structures. We'll talk about their purpose and design and point out features that can be modified or expanded on. For information on more complex classes, see Intermediate Platformer Features and Advanced Platformer Features.
Gem Class and Circle Structure
Gem class implementation is located in Gem.cs. Important methods include:
- Loads a grayscale gem-shaped texture.
- Updates the gem height at regular intervals during gameplay. This method is used to cause all gems in the level to oscillate up and down in relation to each other. The motion is implemented with a sine curve over time. In addition, the X-coordinate of the gem is used to produce a nice syncopated pattern.
- Draws the gem using the specified color shade. The default color is yellow.
- Plays a sound indicating retrieval by the player. This is an great place to modify the default behavior of gem collection!
Properties include the parent
Level, its position in world space, and a bounding circle.
The bounding circle (implemented by the
Circle structure, located in Circle.cs) is used to determine if the player is colliding with the gem. If the player collides with the gem, it is removed from the gem collection of the parent
Level and points are added to the player's score. The most important method of
Intersects. This method checks for intersection with a rectangle (used to represent the bounding area of other game objects). This method is called by
Level.UpdateGems and if it returns true, the gem is removed and the
OnGemCollected method is called. For more information on
OnX methods, see Changing the Behavior of Existing Game Events.
Gems are the only objects that use a bounding circle. All other objects use a bounding rectangle.
The default animation of gems is based on a sine wave. They slowly rise and fall as a group during gameplay. The
Gem.Update method controls this animation.
|You can easily modify this behavior by changing the default height and speed of the animation.|
Tile Structure and TileCollision Enumeration
Tile structure implementation is located in Tile.cs. It has a constructor, but its main purpose is storing the properties of a level tile.
Properties include the tile dimensions (width and height), the collision behavior of the tile object, and the texture used when drawing the tile object.
The collision behavior (stored in the
Collision property) is the most important property of the
Tile structure. This property determines what kind of collision detection is done, if any. For more information on supported tile types, see Game World.
TileCollision enumeration lists all possible collision behaviors for a tile and is located in Tile.cs.
Basic Modifications to the Platformer Starter Kit
Changing the Behavior of Existing Game Events
|Modifying default behaviors in the game is as easy as modifying the related |
Platformer handles important events in the game using methods that follow an
OnX naming convention, where X is an important event name. For instance, when the player character collects a gem,
OnCollected is ultimately called. The complete list of methods called before
Gem.OnCollected is as follows:
When a gem is collected, the default behavior is to play a "gem collected" sound (GemCollected.wma). If you wanted to change this behavior, there are two places you should focus on: the
Gem.OnCollected methods. Add (or remove existing) code to do different things. For instance, to create a "cursed" gem (subtracts points from the player's total when collected), modify the
Level.OnCollected method to subtract
Gem.PointValue form the total instead of adding to it.