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

Do the same for the other ladybug object

The code in Listing 10 also does essentially the same thing, but applies the operation to the ladybug object referred to by index [1]in the list of ladybugs.

Methods of the Mouse class

In addition to the GetState method mentioned earlier, the Mouse class has one other static method that is not inherited from the Object class. It is named SetPosition , and it

"Sets the position of the mouse cursor relative to the upper-left corner of the window."

You saw this method used to place the mouse cursor in the center of the game window in Listing 7 .

The end of the overridden Update method

Listing 10 also signals the end of the overridden Update method.

The overridden Game.Draw method

The overridden Game.Draw method is shown in its entirety in Listing 11 .

Listing 11 . The overridden Game.Draw method.

protected override void Draw(GameTime gameTime) { spriteBatch.Begin();spiderWeb.Draw(spriteBatch);//draw background //Draw all spiders.for(int cnt = 0;cnt<spiders.Count;cnt++) { spiders[cnt].Draw(spriteBatch); }//end for loop//Draw all ladybugs. for(int cnt = 0;cnt<ladybugs.Count;cnt++) { ladybugs[cnt].Draw(spriteBatch); }//end for loop//Draw the output text. string output = "";if(spiderCount == 0){ output = "Congratulations. No spiders made it to"+ " the bottom."; }else{output = "Oops, " + spiderCount + " or more " + "spiders made it to the bottom.";}//end else// Find the center of the string Vector2 FontOrigin =Font1.MeasureString(output) / 2; // Draw the stringspriteBatch.DrawString(Font1, output,FontPos, Color.Yellow,0,//angle FontOrigin,1.0f,//scale SpriteEffects.None,0.0f);//layer depth spriteBatch.End();base.Draw(gameTime); }//end Draw method//-------------------------------------------------// }//end class}//end namespace

The MeasureString method

Other than the MeasureString method of the SpriteFont class, there is nothing in Listing 11 that I haven't explained before.

The MeasureString method

"Returns the height and width of a given string as a Vector2."

Overloaded division operator

The statement that declares and initializes the variable named FontOrigin in Listing 11 is very interesting. Recall that a Vector2 object has two fields, X and Y. Note that the code in Listing 11 divides the object by 2. I didn't say that the code extracts the fields and divides them by 2. I said that the code dividesthe object by 2. This must mean that the division operator (/) is overloaded by the Vector2 class such that dividing a Vector2 object by a scalar value divides the fields by the scalar value.

If you know what I am talking about when I talk about operator overloading, that is good. If not, don't worry about it. That topic is far beyond thetechnical requirements for the introductory XNA game programming course that I teach.

The end of the program

Listing 11 signals the end of the overridden Game.Draw method, the end of the Game1 class, and the end of the program.

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