Programming4us
         
 
 
Windows Phone

Developing for Windows Phone and Xbox Live : Game Loop

12/5/2010 11:34:42 AM
This article has been nothing but text so far. Words, words, and rambling—that just isn’t exciting. Let’s get something on the screen!

Update and Draw

The basic flow of your game is to initialize everything, and then call Update and Draw continually until you exit the game. This is what is called the game loop. You’ve already seen it in action in this article, “Sprites and 2D Graphics,” without even realizing it most likely. To ensure you do realize it, let’s do a slightly more complex example.

First, you need to add the XnaLogo.png file to your content project from the accompanying CD. Because you are drawing this image much like before, you need to declare a texture variable to hold it. This time, though, also declare a position variable, as follows:

Texture2D texture;
Vector2 position;

Of course, you need to load that texture, so in your LoadContent method add this line:

texture = Content.Load<Texture2D>("XnaLogo");

You should probably also initialize that position to something! Add the following to the Initialize method:

position = Vector2.Zero;

Finally, because you need to actually draw the image, replace your Draw method with the following:

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(texture, position, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}

Running the project now shows you (as you might have guessed) the image in the upper left corner of the window. Because of the position variable in the Draw call, you are set up to get that thing moving! Add the following to the Update method:

position = new Vector2(position.X + (float)gameTime.ElapsedGameTime.TotalSeconds,
position.Y + (float)gameTime.ElapsedGameTime.TotalSeconds);


Running the project now has the image slowly moving down and to the right, and if you leave it running long enough, it eventually goes off screen! If you want the image to bounce around the screen, though, it would be complicated code. Do you add to the position or subtract? Instead, store your direction in a new variable:

Vector2 velocity;

Then, update the Initialize method to include:

velocity = new Vector2(30, 30);

Finally, change your update method to:

position += (velocity * (float)gameTime.ElapsedGameTime.TotalSeconds);

These updates provide faster movement and easier code, although the image still goes off screen if you let it. You need to detect if the image has gone off screen and make changes to ensure it “bounces” off the edges. This is the largest section of code you’ve seen so far, but it makes the image bounce around, so modify your Update call to this:

position += (velocity * (float)gameTime.ElapsedGameTime.TotalSeconds);
if (!GraphicsDevice.Viewport.Bounds.Contains(new Rectangle(
(int)position.X, (int)position.Y, texture.Width, texture.Height)))
{
bool negateX = false;
bool negateY = false;
// Update velocity based on where you crossed the border
if ((position.X < 0) || ((position.X + texture.Width) >
GraphicsDevice.Viewport.Width))
{
negateX = true;
}
if ((position.Y < 0) || ((position.Y + texture.Height) >
GraphicsDevice.Viewport.Height))
{
negateY = true;
}
// Move back to where you were before
position -= (velocity * (float)gameTime.ElapsedGameTime.TotalSeconds);

if (negateX) velocity.X *= -1;
if (negateY) velocity.Y *= -1;
// Finally do the correct update
position += (velocity * (float)gameTime.ElapsedGameTime.TotalSeconds);
}

Run the project now and you can see the image bounce around the screen! You simply check to see if the image is currently in the bounds of the viewport, and if it is not, check to see which edge it is currently over. Then, move it back to where it was before the update, swap the velocity axis of the sides you’ve crossed, and update the position again.
Other -----------------
- Developing for Windows Phone and Xbox Live : The Game Class
- Developing for Windows Phone and Xbox Live : What Is in a New Project?
- Windows Phone 7 : Deleting Pictures and Videos
- Windows Phone 7 : Personalizing the Pictures Hub
- Windows Phone 7 : Adding GPS Info to Pictures
- Windows Phone 7 : Creating a Favorites List
- Windows Phone 7 : Synching Pictures and Videos to Your PC
- Windows Phone 7 : Saving Pictures to the Web
- Windows Phone 7 : Saving Pictures to Your Phone
- Windows Phone 7 : Recording a Video
- Windows Phone 7 : Viewing Pictures and Videos
- Windows Phone 7 : Taking a Picture
- Developing for Windows Phone and Xbox Live : Sprites and 2D Graphics - Rendering Text
- Developing for Windows Phone and Xbox Live : Sprites and 2D Graphics - Spritebatch (part 3)
- Developing for Windows Phone and Xbox Live : Sprites and 2D Graphics - Spritebatch (part 2)
- Developing for Windows Phone and Xbox Live : Sprites and 2D Graphics - Spritebatch (part 1)
- Developing for Windows Phone and Xbox Live : Sprites and 2D Graphics - Show Me Something on Screen
- Windows Phone 7 : Working with SharePoint Documents
- Windows Phone 7 : Connecting to SharePoint
- Windows Phone 7 : Synching Notes to the Web
 
 
Most View
- Performing Administrative Tasks Using Central Administration (part 1) - Managing Web Applications
- Sharepoint 2007 : Track the Progress of a Workflow
- Windows Phone 7 : Getting Directions
- Windows 7: Managing Your Hardware with Device Manager (part 1)
- SharePoint 2010 : View/Add Notes to a Document or List Item
- .NET Debugging : Managed Heap and Garbage Collection
- SharePoint 2010 : Enforce Custom Validation on a Column
- Programming Windows Phone 7 : Color Themes
- Windows Server 2008 : Configuring IIS Security (part 6) - Understanding Handler Mappings
- Building Android Apps : Animation - Adding the New Entry Panel
Top 10
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 3) - Configuring Recipient Filtering
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 2)
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 1)
- Implementing Edge Services for an Exchange Server 2007 Environment : Installing and Configuring the Edge Transport Server Components
- What's New in SharePoint 2013 (part 7) - BCS
- What's New in SharePoint 2013 (part 6) - SEARCH
- What's New in SharePoint 2013 (part 6) - WEB CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 5) - ENTERPRISE CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 4) - WORKFLOWS
- What's New in SharePoint 2013 (part 3) - REMOTE EVENTS