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

Questions & Answers

what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
yes that's correct
I think
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
analytical skills graphene is prepared to kill any type viruses .
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?