XNa games: Hello xNa

Source: Internet
Author: User

Create a simple Windows Phone 7 xNaProgramIt is just a hello xNa text, which moves from the upper left corner of the screen to the lower right corner. This example is used to start game programming for Windows Phone 7 xNa.

After creating a project, you can see this project structure ,.

The content project indicates the resource file of the game. The resources of the game must be added under the content directory.

Let's take a look at the main class game1.cs.

 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>
/// Main subjects of the game
/// </Summary>
Public Class Game1: Microsoft. xNa. Framework. Game
{
Graphicsdevicemanager graphics; // Used to manage graphics Devices
Spritebatch; // The graphics devices of the game draw 2D objects.

Spritefont spritefont1; // Font
Viewport; // Form
Vector2 textsize; // Font size 2D Vector
Vector2 textposition; // Font position

Public Game1 ()
{
Graphics = New Graphicsdevicemanager ( This );
Content. rootdirectory = " Content " ;

// Frame rate is 30 FPS by default for Windows Phone.
Targetelapsedtime = Timespan. fromticks ( 333333 );

// Extend battery life under lock.
Inactivesleeptime = Timespan. fromseconds ( 1 );
}

/// <Summary>
/// Game initialization before running
/// </Summary>
Protected Override Void Initialize ()
{
// Add some game initialization operations here
Base . Initialize ();
}

/// <Summary>
/// Load game resources. loadcontent is executed only once.
/// </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
Viewport = This . Graphicsdevice. viewport; // Get the game form
Spritefont1 = This . Content. Load < Spritefont > ( " Spritefont1 " ); // Load font Resources
Textsize = Spritefont1.measurestring ( " Hello, xNa! " ); // Returns the height and width of a character.
Textposition = New Vector2 ( 0 , 0 );
}

/// <Summary>
/// Withdraw the game and reclaim Resources
/// </Summary>
Protected Override Void Unloadcontent ()
{
// Todo: unload any non contentmanager content here
}

/// <Summary>
/// Update Processing before creating a game
/// </Summary>
/// <Param name = "gametime"> Current Time object of the game </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
If (Textposition. x < Viewport. Height)
{
Textposition. Y + = 10 * ( Float ) Gametime. elapsedgametime. totalseconds;
Textposition. x + = 20 * ( Float ) Gametime. elapsedgametime. totalseconds;
}
Else
{
Textposition. Y -= 10000 * ( Float ) Gametime. elapsedgametime. totalseconds;
Textposition. x -= 20000 * ( Float ) Gametime. elapsedgametime. totalseconds;
}

Base . Update (gametime );
}

/// <Summary>
/// Draw game
/// </Summary>
/// <Param name = "gametime"> Current Time object of the game </Param>
Protected Override Void Draw (gametime)
{
Graphicsdevice. Clear (color. cornflowerblue );

// Todo: add your drawing code here
Spritebatch. Begin ();
Spritebatch. drawstring (spritefont1, " Hello, xNa! " , Textposition, color. Red );
Spritebatch. End ();

Base . Draw (gametime );
}
}
}

The running result is as follows:

To create an xNa for WP7 project, you can see that the project files include initialize (), loadcontent (), update (), and draw () from these main functions, we can see a basic architecture of xNa game development:

Loading: it is used to load all resources that ensure the normal operation of the game. This state is only run before the game starts in the system, that is, the game only runs once in its lifecycle.

Update: this status is driven by the set interval. During each interval, the status of each role in the game is recalculated, as well as the game score and various game logic. The default value is 30 frames per second. If the interval is too large, the game may fail to start.

Draw: this status is also driven by the set time interval, used to draw various changes to the display settings.

For xNa resources, a content project is created when a new project is created, and all resources (images, sounds, videos, fonts, 3D shapes, textures, etc.) are created) are loaded into this project and compiled. the xNa binary file in xNb format can be loaded into the game and called.

 

XNa game lifecycle:

 

1) graphicsdevicemanager: This type is very important as mentioned in the xNa class library introduction. It provides developers with methods to manage the graphics card resources of the target device. In short, it calls an interface of the video card. The graphicsdevice attribute of this object represents the video card of the current target device.

2) spritebatch: This object is mainly used to draw text and 2D images. In the game development process, its role is very important, because the game requires most of the image, text, sound and other material resources. All pictures 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 perform initialization before running. You can query any required services and load graph-independent content here. For example, video card devices and game score settings.

4) loadcontent (): After initialization, the program enters loadcontent. This method is used to load game materials, including game modes, sounds, and images. This method is called only once in a game and is the place where all materials are loaded.

5) Update (): This method is equivalent to framemove in direct3d. In short, it is used to modify the current image on the graph. It is mainly used for game execution logic, such as refresh the screen, check collision, update scores, detect game processes, collect input data, and play audio.

6) Draw (): This method is equivalent to Windows-programmed paint or onpaint. It is an automatic drawing method.

After the game program executes loadcontent, it starts to enter a game loop. No matter what language the game program is written, it requires a game loop, which is also the biggest difference between the game program and the application program. In the xNa game project, the game loop mainly consists of the update and draw 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.