Professional Documents
Culture Documents
Follow these steps to create a simple spritebased game by using XNA Game Studio. This sample code works on all platforms, including Windows Phone.
Note
A sprite is a simple 2D graphic such as a bitmap that is displayed on the screen using a call to SpriteBatch.Draw.
Step 1: Install Your Software
Step 2: Create a New Project
Step 3: View the Code
Step 4: Add a Sprite
Step 5: Make the Sprite Move and Bounce
Step 6: Explore!
In the Properties window below Solution Explorer, look for the "Asset Name" property. Note the name; you'll use it in your code to load the graphic so it can be displayed in your
game.
5. If the Properties window is not visible, press F4, or click the View menu, and then click Properties Window.
Now, you must write code that loads and displays the sprite on the screen.
6. Back in the Code view of your game, find the LoadContent method, and add the following lines in and above the method so it looks similar to this:
C#
//Thisisatexturewecanrender.
Texture2DmyTexture;
//Setthecoordinatestodrawthespriteat.
Vector2spritePosition=Vector2.Zero;
//Storesomeinformationaboutthesprite'smotion.
Vector2spriteSpeed=newVector2(50.0f,50.0f);
protectedoverridevoidLoadContent()
{
//CreateanewSpriteBatch,whichcanbeusedtodrawtextures.
spriteBatch=newSpriteBatch(GraphicsDevice);
myTexture=Content.Load<Texture2D>("mytexture");
}
The Content property of the parent Game class offers the ContentManager class through which your game assets can be loaded.
Make sure the call to Content.Load is using the "Asset Name" you saw in the Properties window in the previous step. This code will load and prepare your graphic to be drawn, and
will reload your graphic if the graphics device is reset such as in the case of the game window being resized.
7. Now, add code to the Draw loop so it looks like this:
C#
protectedoverridevoidDraw(GameTimegameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
//Drawthesprite.
spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend);
spriteBatch.Draw(myTexture,spritePosition,Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
This code draws the sprite on the screen each frame.
Notice the parameter passed by the Begin method, BlendState.AlphaBlend. This parameter tells the Draw method to use the alpha channel of the source color to create a
transparency effect so that the destination color appears through the source color.
8. Build and run your game.
The sprite appears.
Now, it's time to give it some motion.
spritePosition.X=MinX;
}
if(spritePosition.Y>MaxY)
{
spriteSpeed.Y*=1;
spritePosition.Y=MaxY;
}
elseif(spritePosition.Y<MinY)
{
spriteSpeed.Y*=1;
spritePosition.Y=MinY;
}
}
This adds a little bit of logic that will move the sprite around each frame and cause the sprite to change direction if it hits the edges of the game window.
Build and run your game.
The sprite moves across the screen and changes direction when it encounters the edges of the game window.
Step 6: Explore!
From here, you can do just about anything.
Here are some more ideas to extend this sample:
Add a second sprite, and use BoundingBox objects to detect collisions.
Use Keyboard, Mouse, or GamePad to make the sprite respond to movements of an input device. See Overview of User Input and Input Devices.
Create some audio events so that the sprite makes sounds as it moves. See Adding a Sound File XACT and Playing Sounds from an XACT Project.
Get more ideas and resources at App Hub.
See Also
Software Install Requirements
Getting Started with XNA Game Studio Development
Community Additions
Code in VB.net
Protected Overrides Sub UpdateByVal gameTime As GameTime
If GamePad.GetStatePlayerIndex.One.Buttons.Back = ButtonState.Pressed Then
Me.Exit
End If
spriteposition += spritespeed * gameTime.ElapsedGameTime.TotalSeconds
No, this is not correct. You should call UpdateSpritegameTime inside the Update method look for the Add your update logic here comment.
Petr Kadlec
10/18/2012
ERROR
Sir,
What is the reason for this and send me the remedy for this error.
Umasankar3259
8/16/2012
"Content" node
in my version of visual studio the "Content" node is actually called "<name of the project file>Content Content". So if my project was called "WindowsGame3" the folder would be called
"WindowsGame3Content Content".
chenjesu
2/2/2012
2015 Microsoft