<< Chapter < Page
  Xna game studio     Page 7 / 13
Chapter >> Page >

There is nothing unusual about the code that was added to the Move method, so it shouldn't require further explanation beyond the embedded comments.

End of discussion of the Sprite class

There were no additional changes made to the Sprite class other than those identified above, so that will end the discussion of the Sprite class.

The Game1 class for XNA0132Proj

General information

This project demonstrates how to write a simple 2D arcade style game.

Ten spiders try to make it from the top to the bottom of a spider web.

The bottom of the web is guarded by two large ladybugs that are intent on eating the spiders.

If a ladybug collides with a spider, the spider gets eaten by the ladybug and removed from the population of spiders. If the player plays long enough, all ofthe spiders should eventually be eaten.

The objective of the game

The objective of the game is for the player to cause as many spiders as possible to be eaten by the ladybugs before they make it to the bottom of thegame window.

Ladybug control

The ladybugs can be moved by the player using either the keyboard or the mouse. Pressing the arrow keys moves one of the ladybugs. Holding down the A keyand pressing the arrow keys moves the other ladybug.

The player can drag one of the ladybugs with the mouse pointer by pressing the left mouse button. The player can drag the other ladybug by pressing theright mouse button.

Spiders are recycled

Even though the game begins with only ten spiders, those that are not eaten before they reach the bottom of the game window are recycled. If a spider makesit from the top to the bottom of the game window without being eaten, the spider wraps around and appears back at the top of the game window ready to start thetrip over.

Keeping score

A counter keeps track of the number of spiders that cross the bottom of the game window. On-screen text displays that number as the game progresses as shownin Figure 1 . Note, however, that if two or more spiders cross the bottom edge of the game window during the same iteration of the game loop, only one will becounted. In hindsight, therefore, the elapsed time required to cause the ladybugs to eat all of the spiders might be a better scorekeeping mechanism thancounting spider crossings.

Modifying the program to keep score on the basis of elapsed time might make a good student project.

Code for the Game1 Class

Listing 6 shows the beginning of the Game1 class along with the constructor in abbreviated form. By abbreviated form, I mean that much ofthe code is very similar to code that I have explained in earlier modules so I deleted that code from Listing 6 for brevity. You can view all of the code for the Game1 class in Listing 14 .

Listing 6 . Abbreviated beginning of the Game1 Class for the XNA0132Proj project.

namespace XNA0132Proj { public class Game1 : Microsoft.Xna.Framework.Game {GraphicsDeviceManager graphics; SpriteBatch spriteBatch;//code deleted for brevity //The following variable is used to count the number// of spiders that make it past the ladybugs and // reach the bottom of the game window.int spiderCount = 0; SpriteFont Font1;//font for on-screen textVector2 FontPos;//position of on-screen text //-------------------------------------------------//public Game1() {//constructor //code deleted for brevity}//end constructor

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Xna game studio. OpenStax CNX. Feb 28, 2014 Download for free at https://legacy.cnx.org/content/col11634/1.6
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Xna game studio' conversation and receive update notifications?

Ask