<< 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 defense mechanism
Chinaza Reply
what is defense mechanisms
Chinaza
I'm interested in biological psychology and cognitive psychology
Tanya Reply
what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
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