Revised: Sun May 08 09:19:08 CDT 2016
This page is part of a Book titled XNA Game Studio .
Table of Contents
- Table of Contents
- Preface
- General background information
- Preview
-
Discussion and sample code
-
Making the sprite move
- The game loop
- Distance to move
- How often is the Update method called?
-
The overridden Update method
- Xbox 360 code
- Test for sprite out of bounds horizontally
- Test for a collision of the sprite with an edge
- Test for sprite out of bounds vertically
- Change the current position of the sprite
- Move to the right and down
- Reverse the direction of motion
- No visible movement at this time
- The remainder of the overridden Update method
- Using the Debug class
-
Making the sprite move
- Run the program
- Run my program
- Summary
- Miscellaneous
- Complete program listing
Preface
This module is one in a collection of modules designed primarily for teaching GAME 1343 Game and Simulation Programming I at Austin Community College in Austin, TX. These modules are intended tosupplement and not to replace the textbook.
An earlier module titled Getting Started provided information on how to get started programming with Microsoft's XNA Game Studio.
Viewing tip
I recommend that you open another copy of this module in a separate browser window and use the following links to easily find and view the Figuresand Listings while you are reading about them.
Figures
Listings
- Listing 1 . Skeleton code for a new Windows Game project.
- Listing 2 . Declare variables that specify the incremental distance to move.
- Listing 3 . Beginning of the overridden Update method.
- Listing 4 . Test for sprite out of bounds horizontally.
- Listing 5 . Test for sprite out of bounds vertically.
- Listing 6 . Change the current position of the sprite.
- Listing 7 . The remainder of the overridden Update method.
- Listing 8 . Overridden LoadContent method.
- Listing 9 . The class named Game1 for the project named XNA0120Proj.
General background information
The earlier module titled Xna0118-The XNA Framework and the Game Class used a very simple XNA program to teach many of the details regarding the incorporation ofthe XNA framework into the object-oriented C# programming language.
Skeleton code
When you create a new Windows Game project using Visual C#, a source code file named Game1.cs is automatically created and opened in the editor window. The file contains skeleton code for a windows gamebased on XNA. Listing 1 shows the skeleton code contained in the file named Game1 (with most of the comments deleted for brevity).
Listing 1 . Skeleton code for a new Windows Game project.
using System;
using System.Collections.Generic;using System.Linq;
using Microsoft.Xna.Framework;using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;namespace WindowsGame2
{public class Game1 : Microsoft.Xna.Framework.Game
{GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;public Game1()
{graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";}
protected override void Initialize(){
// TODO: Add your initialization logic herebase.Initialize();
}protected override void LoadContent()
{// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);// TODO: use this.Content to load your game content here
}protected override void UnloadContent()
{// TODO: Unload any non ContentManager content here
}protected override void Update(GameTime gameTime)
{// Allows the game to exit
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)this.Exit();
// TODO: Add your update logic herebase.Update(gameTime);
}protected override void Draw(GameTime gameTime)
{GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code herebase.Draw(gameTime);
}}
}