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

The variable declarations near the center of Listing 6 are new to this module.

The overridden LoadContent method

An abbreviated listing of the LoadContent method is shown in Listing 7 .

Listing 7 . Abbreviated overridden LoadContent method.

protected override void Update(GameTime gameTime) { spriteBatch = new SpriteBatch(GraphicsDevice);Font1 = Content.Load<SpriteFont>("Kootenay"); IsMouseVisible = true;//make mouse visible//code deleted for brevity //Position the on-screen text.FontPos = new Vector2(windowWidth / 2, 50); //Position the mouse pointer in the center of the// game window. Mouse.SetPosition(windowWidth / 2,windowHeight /2);}//end LoadContent

The font family

To begin with, note that this program displays text on the screen using the font family named Kootenay (see Figure 3 for a reference to this font-family name).

Make the mouse pointer visible

By default, the mouse pointer is not visible when it is in the game window. The code in Listing 7 sets the inherited variable named IsMouseVisible to true, which causes the mouse pointer to be visible.

Position the on-screen text

The code in Listing 7 causes the text to be centered horizontally, 50 pixels down from the top of the game window as shown in Figure 1 .

Set the initial position of the mouse pointer

The code in Listing 7 causes the initial position of the mouse pointer to be centered in the game window. (I will have more to say aboutthis later.)

End of the LoadContent method

Listing 7 also signals the end of the overridden LoadContent method.

The overridden Update method

An abbreviated listing of the overridden Update method begins in Listing 8 .

Listing 8 . Beginning of an abbreviated overridden Update method.

protected override void Update(GameTime gameTime) { //code deleted for brevity//Check to see if any spiders have made it to the // bottom edge.for(int cnt = 0;cnt<spiders.Count;cnt++) { if(spiders[cnt].Edge == 3) //One or more made it to the bottom edge.spiderCount += 1; }//end for loop

The code in in Listing 8 counts the number of spiders that make it to the bottom and collide with the bottom edge of the game window.

Moving the ladybug objects using the keyboard

The code required to service the keyboard is long, tedious, and repetitive, but is not complicated. That code is shown in Listing 9 .

Listing 9 . Service the keyboard.

//The following code is used to move one or the // other of two ladybugs using the arrow keys.// Press only the arrow keys to move one of the // ladybugs. Press the A plus the arrow keys// to move the other ladybug. //The ladybugs cannot be moved outside the game// window using the keyboard. //While an arrow key is pressed, the ladybug moves//five pixels per call to the Update method. //Get the state of the keyboard.KeyboardState keyboardState = Keyboard.GetState(); //Execute moves on one ladybug with arrow keys plus// the A or a key. if(keyboardState.IsKeyDown(Keys.Left)&&(keyboardState.IsKeyDown(Keys.A))&&(ladybugs[0].Position.X>0)) { ladybugs[0].Position = new Vector2( ladybugs[0].Position.X - 5, ladybugs[0].Position.Y); }//end ifif(keyboardState.IsKeyDown(Keys.Right)&&(keyboardState.IsKeyDown(Keys.A))&&(ladybugs[0].Position.X<(windowWidth - ladybugs[1].Image.Width))) { ladybugs[0].Position = new Vector2( ladybugs[0].Position.X + 5, ladybugs[0].Position.Y); }//end ifif(keyboardState.IsKeyDown(Keys.Up)&&(keyboardState.IsKeyDown(Keys.A))&&(ladybugs[0].Position.Y>0)) { ladybugs[0].Position = new Vector2( ladybugs[0].Position.X, ladybugs[0].Position.Y - 5); }//end ifif(keyboardState.IsKeyDown(Keys.Down)&&(keyboardState.IsKeyDown(Keys.A))&&(ladybugs[0].Position.Y<(windowHeight - ladybugs[1].Image.Height))) { ladybugs[0].Position = new Vector2( ladybugs[0].Position.X, ladybugs[0].Position.Y + 5); }//end if//Execute moves on the other ladybug with arrow // keys pressed but the A key not pressed.if(keyboardState.IsKeyDown(Keys.Left)&&!(keyboardState.IsKeyDown(Keys.A))&&(ladybugs[1].Position.X>0)) { ladybugs[1].Position = new Vector2( ladybugs[1].Position.X - 5, ladybugs[1].Position.Y); }//end ifif(keyboardState.IsKeyDown(Keys.Right)&&!(keyboardState.IsKeyDown(Keys.A))&&(ladybugs[1].Position.X<(windowWidth - ladybugs[1].Image.Width))) { ladybugs[1].Position = new Vector2( ladybugs[1].Position.X + 5, ladybugs[1].Position.Y); }//end ifif(keyboardState.IsKeyDown(Keys.Up)&&!(keyboardState.IsKeyDown(Keys.A))&&(ladybugs[1].Position.Y>0)) { ladybugs[1].Position = new Vector2( ladybugs[1].Position.X, ladybugs[1].Position.Y - 5); }//end ifif(keyboardState.IsKeyDown(Keys.Down)&&!(keyboardState.IsKeyDown(Keys.A))&&(ladybugs[1].Position.Y<(windowHeight - ladybugs[1].Image.Height))) { ladybugs[1].Position = new Vector2( ladybugs[1].Position.X, ladybugs[1].Position.Y + 5); }//end if

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