ProgramEnvironment: vs2010 and xNa game studio 4.0 plug-ins, which can be downloaded.
Http://xbox.create.msdn.com/zh-CN/resources/downloads, This is the author's
1. Create a project
Select windows game (4.0) and create
There will be two projects, the above project is responsible for code implementation, the following content contains all the resources of the project, image sound model
First, let's take a look at the one (MOVE) project. There is a game1 and program file under it to open game1.
/// <Summary> /// All games are inherited from Microsoft. xNa. Framework. Game. /// </Summary> Public Class Game1: Microsoft. xNa. Framework. Game { // It provides access to PC, xbox360, and WP7 graphics devices. Graphicsdevicemanager graphics; // The rendering wizard can be understood as a 2D, 3D image. Spritebatch; Public Game1 () {graphics = New Graphicsdevicemanager ( This ); Content. rootdirectory = " Content " ;} /// <Summary> /// Initializing Various data and methods is similar to initialize () of. net. Here is the first step of program execution. /// </Summary> Protected Override Void Initialize (){ // Todo: add your initialization logic here Base . Initialize ();} /// <Summary> /// Load various resources here /// </Summary> 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 } /// <Summary> /// Release resources /// </Summary> Protected Override Void Unloadcontent (){ // Todo: unload any non contentmanager content here } /// <Summary> /// Here, a refresh action is performed. The default value is 60 times per second, which is equivalent to the dynamic /// </Summary> /// <Param name = "gametime"> Provides a snapshot of timing values. </Param> Protected Override Void Update (gametime ){ // Allows the game to exit If (Gamepad. getstate (playerindex. One). Buttons. Back = Buttonstate. Pressed) This . Exit (); // Todo: add your update logic here Base . Update (gametime );} /// <Summary> /// This is the same as update. It is dynamically refreshed, but it is recommended to do less processing here. All processing and logic should be put in update. /// </Summary> /// <Param name = "gametime"> Provides a snapshot of timing values. </Param> Protected Override Void Draw (gametime) {graphicsdevice. Clear (color. cornflowerblue ); // Todo: add your drawing code here Base . Draw (gametime );}}
The lifecycle of an xNa instance is:Initialize ()-> Loadcontent ()-> Update () and draw () loop-> unloadcontet ()
Then I add an image. You can right-click it or copy it directly and include it in the project.
Attach Images
Protected Override VoidLoadcontent (){//Create a new spritebatch, which can be used to draw textures.Spritebatch =NewSpritebatch (graphicsdevice); dog= Content. Load <texture2d> (@"Image/dog");//Todo: Use this. content to load your game content here}
In the draw function, draw the loaded image.
Protected Override VoidDraw (gametime) {graphicsdevice. Clear (color. cornflowerblue );//Todo: add your drawing code hereSpritebatch. Begin (); spritebatch. Draw (dog, vector2.zero,Null, Color. White );Spritebatch. End ();Base. Draw (gametime );}
Run the command to view the image.
Then, we want to let the image work. In the program, we define the initial position of an image and the running speed of an image.
Vector2 beginposition =Vector2.zero;FloatSpeed = 3f;
Add judgment in update
Protected Override Void Update (gametime ){ // Allows the game to exit If (Gamepad. getstate (playerindex. One). Buttons. Back = Buttonstate. Pressed) This . Exit (); // Todo: add your update logic here Beginposition. x + = Speed; If (Beginposition. x> window. clientbounds. Width-dog. Width | Beginposition. x < 0 ) Speed * =- 1 ; Base . Update (gametime );}
In the draw Function
protected override void draw (gametime)
{< br> graphicsdevice. clear (color. cornflowerblue);
// todo: add your drawing code here
spritebatch. begin ();