<< 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 phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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