Collision Detection: Game Over
Without collision detection, the game is pretty easy to play. Luckily, adding simple collision detection into the game is fairly easy as well. The XNA Framework Rectangle objects contain functionality that let you check whether they're intersecting another Rectangle object. You'll use this functionality and add it to the
Update method to check whether a hazard has collided with the car. When a collision does occur, you switch the current game state to
Crash.
After a crash occurs, the game will be over—but you need a way to allow the user to play the game again or exit, and you should probably tell them how to do that.
Go ahead and add the following code to the
Update method. This code replaces the existing
UpdateHazard call.
if (mHazardPosition.Intersects(mCarPosition) == false)
{
UpdateHazard(gameTime);
}
else
{
mCurrentState = State.Crash;
}
This code checks for a collision by using the Rectangle object's
Intersects method. When a collision is detected, the current state of the game changes to
Crash to reflect this.
You also want to add the following lines of code within the
Crash case of the
switch block in the
Update method.
if (aCurrentKeyboardState.IsKeyDown(Keys.Enter) == true ||
aCurrentGamePadState.Buttons.Start == ButtonState.Pressed)
{
StartGame();
}
This code checks to see whether the user has pressed Enter or the Start button. Pressing either causes a call to the
StartGame method, which restarts the game.
Now that the code responds to game state in the
Update method, you need to do the same in the
Draw method.
Add the following code just after the line of code that draws the current number of hazards that the car has passed.
if (mCurrentState == State.Crash)
{
mSpriteBatch.DrawString(mFont, "Crash!",
new Vector2(5, 200), Color.White, 0,
new Vector2(0, 0), 1.0f,
SpriteEffects.None, 0);
mSpriteBatch.DrawString(mFont,
"'Enter' to play again.",
new Vector2(5, 230),
Color.White, 0, new Vector2(0, 0), 1.0f,
SpriteEffects.None, 0);
mSpriteBatch.DrawString(mFont,
"'Escape' to exit.",
new Vector2(5, 260),
Color.White, 0, new Vector2(0, 0), 1.0f,
SpriteEffects.None, 0);
}
 | |
Figure 11: Running the final build of the game project. |
This code displays some informational text to the player—but only if the current state of the game is Crash. Players then know how to exit the game or replay.
That's it. Now you can build the project for the last time and play the game (see
Figure 11).
You should now have a functional game. Sure, you can make some enhancements to make it a little prettier, a bit more challenging and more interesting, but I'll leave that to your very capable minds.
XNA Game Studio Express is a perfect fit for a game developer. With a small learning curve, low (or no) cost, a thriving community, and excellent support from Microsoft, if you are interested in giving game development a shot, this is your chance. By making it intuitive and easier to get a game started and developers developing quicker, Microsoft has opened the floodgates for creativity and risktaking in game development. I'm excited to see what the community does with this and just where XNA game development might take the future of gaming.
In April 2006 Microsoft refreshed XNA Game Studio Express and included even more features, building font support into XNA Game Studio Express and making it easier to share game projects without packaging up the source code. I expect that Microsoft will continue to add features and enhancements to XNA Game Studio Express, and I expect the XNA team will continue to solicit feedback in the forums for problems and ask for enhancement requests and set priorities.
Useful Links
Along with the
Creators Club (which is basically the central XNA hang-out site), I suggest you check out several prominent XNA sites run by what most would consider the grandfathers of the XNA community. Here are some of my favorites: