XNA Games: Hello XNA

Source: Internet
Author: User
Tags current time set time time interval
The following creates a simple Windows Phone 7 XNA program, just a hello XNA text, moving from the upper left corner of the screen to the lower right corner, using this example to start programming Windows Phone 7 XNA game.

After you create a new project, you can see a project project structure, as shown in the figure.

The content item is a resource file that represents the game, and the resources for the game need to be added in the content directory.

Take a look at Game1.cs, the main class.

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.Input.Touch;

Using Microsoft.Xna.Framework.Media; namespace Helloworldxna {//<summary>/////</summary> public class Game1:micro Soft. 

        Xna.Framework.Game {Graphicsdevicemanager graphics;//for managing graphics devices SpriteBatch spritebatch;//Games graphics devices to draw 2D objects Spritefont spritefont1;//font Viewport viewport;//form Vector2 textsize;//font size two-dimensional vector Vector2
            textposition;//Font Location Public Game1 () {graphics = new Graphicsdevicemanager (this);

            Content.rootdirectory = "Content";
            Frame rate is a FPS by default for Windows Phone. TargetelapsedtimE = Timespan.fromticks (333333);
            Extend battery life under lock.
        Inactivesleeptime = Timespan.fromseconds (1); }///<summary>///Games pre-run some initialization///</summary> protected override void Initia Lize () {//Add some initialization processing base for the game here.
        Initialize (); }///<summary>//Load game resources, loadcontent only once///</summary> protected Overrid
            e void Loadcontent () {//Create a new SpriteBatch, which can is used to draw textures.

            SpriteBatch = new SpriteBatch (graphicsdevice); Todo:use this. Content to load your game content here viewport = this. Graphicsdevice.viewport;//gets the game's form spriteFont1 = this. Content.load<spritefont> ("SpriteFont1");//Load font resource textSize = spritefont1.measurestring ("hello,xna!");
        /returns the height and width of the character a textposition = new Vector2 (0, 0);

      }  <summary>///Game exit, Recycle resources///</summary> protected override void Unloadcontent () {//Todo:unload any non contentmanager content here}//<summary>// /Draw pre-game update processing//</summary>//<param name= "GameTime" > The current Time object of the game </param> Protect ed override void Update (GameTime GameTime) {//allows the game to exit if (Gamepad.getsta Te (Playerindex.one). Buttons.back = = buttonstate.pressed) this.

            Exit (); Todo:add Your update logic here if (Textposition.x < viewport.
                Height) {TEXTPOSITION.Y + = ten * (float) gameTime.ElapsedGameTime.TotalSeconds;
            Textposition.x + = (float) gameTime.ElapsedGameTime.TotalSeconds;
       } else {TEXTPOSITION.Y-= 10000 * (float) gameTime.ElapsedGameTime.TotalSeconds;         Textposition.x-= 20000 * (float) gameTime.ElapsedGameTime.TotalSeconds; } base.
        Update (GameTime); }///<summary>///Draw games///</summary>//<param name= "GameTime" > Game when Former time object </param> protected override void Draw (GameTime GameTime) {graphicsdevice.clear (Col Or.

            Cornflowerblue);
            Todo:add your drawing code here Spritebatch.begin ();
            Spritebatch.drawstring (SpriteFont1, "hello,xna!", TextPosition, color.red);

            Spritebatch.end (); Base.
        Draw (GameTime); }
    }
}

The effect of the operation is as follows:

For a new XNA for WP7 project, you can see the main functions in its project file, which are: Initialize (), Loadcontent (), Update (), Draw (), Then you can see the basic architecture of XNA Development game:

Loading: It is used to load all the resources that ensure the game can run properly, and this state runs only before the game starts in the whole system, that is to say, the whole game is run only once in the life cycle.

Update: This state is driven by the time interval you set, recalculating the status of each character in the game at each interval, as well as the game score and various game logic. The default is 30 times per second (frames), and if the set interval is too large, the game may not start.

Draw: This state is also driven by the set time interval, which is used to draw various changes to the display settings.

For XNA Resources, when a new project is created, a content project is created together, and all resources (images, sounds, videos, fonts, 3D shapes, textures, etc.) are loaded into the project, and is compiled into the. xnb format of the XNA binary file, so that it can be loaded into the game and called.

The life cycle of the XNA game is shown in the figure:

1) Graphicsdevicemanager: The type mentioned in the XNA Class Library introduction is very important. It provides a way for developers to manage the graphics resources of the target device. Simply called an interface to the video card, the GraphicsDevice property of the object represents the video card of the current target device.

2) SpriteBatch: The main purpose of this object is to draw text and 2D images. In the game development process, its role is very important, because the game needs most of the picture, text and sound material resources. All images that need to be displayed in the game must be drawn through the draw method in the SpriteBatch object.

3) Initialize (): This method is used to initialize the variables and objects of the game program. It allows the game to do some initialization work before running. You can query for any required services and load graphics-independent content here. For example, graphics devices, game score settings, and so on.

4) Loadcontent (): When the initialization is complete, the program enters Loadcontent, which is used to load the game's footage, including the game's modes, sounds, images, etc. This method is only called once in a game and is where all the footage is loaded.

5) Update (): This method is equivalent to FrameMove in Direct3D, which is simply to modify the current picture on the graph. It is mainly used for the game to perform some logic, such as: Refresh the screen, check collisions, update scores, detect game progress, collect input data and play audio and so on.

6) Draw (): This method is equivalent to the paint or OnPaint of Windows programming, and is the method of automatic drawing of the system.

Game program after the execution of Loadcontent, began to enter a game loop, no matter what language to write the game program, all need a game loop, which is the biggest difference between the game program and the application. In the XNA Game Project, the game loop consists mainly of update and draw two methods.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.