Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Introducing XNA Game Studio Express : Page 6

Microsoft's XNA Game Studio Express gives you powerful game tools, and the ability to create games for both Windows and Xbox 360.


advertisement
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:



George Clingerman is relatively new to the development scene. He took a short course in BASIC on Apple IIgs in high school and did not re-visit the computer world again until his freshman year of college in 1996. A lot had changed. George managed to catch up (some) and graduated with a math/computer science major in 2000. He has worked as a software developer ever since, and continues to refine his development skills and play catch up with the "old school" developers. George currently works with Visual Basic, ASP.NET, and SQL building Windows Forms and Web software for the construction industry by day, and dabbles with game development at night. George runs an XNA community site, where he creates tutorials for beginning 2D game development with XNA Game Studio Express. For this and other contributions to the XNA community, George was awarded a Microsoft MVP award for XNA.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date