How to Write 2D games with XNA on WP7 (2)

Source: Internet
Author: User

Prepare to write a series of articles on XNA designing 2D games. This is the first chapter.

 

1. hello world in XNA

 

1.1 create an XNA Game Development Project

When we are new to a program, we always love to write a hello world to try it out. Writing hello world is the easiest way to understand a language or framework. So how can we use the xna game framework in wp7 (windows phone 7) to output hello world on the screen.

In fact, Microsoft's development tools are very friendly to developers. Open vs2010 Express for Windows Phone, select File> New Project, and create XNAGameSample, 1-1:

Figure 1-1

 

So we can see a solution like 1-2:

Figure 1-2

For this Solution, we can see two projects in Solution Explorer on the right:

XNAGameSample and XNAGameSampleContent.

On the hard disk stored in our project, we can see the directory structure shown in Figure 1-3:

Figure 1-3

 

The XNAGameSampleContent project is used to manage game resource files. resources such as textures, models, and sounds will be managed here.

XNAGameSample is our main project.

In the XNAGameSample project, we open the game1.cs file, which is the main portal for game operation, 1-4:

Figure 1-4

 

There are five methods in Game1.cs: Initialize, LoadContent, UnloadContent, Update, and Draw.

So what do these five methods play in the game? In the subsequent sections, we will understand the respective uses of these five methods.

 

1.2 updateAnd draw

From the function name, we can guess that LoadContent is responsible for loading game resources. Draw is used to Draw game interfaces. So what about Update? Literally, we can see the meaning of "Update". What is the Update in this method?

A simple game scenario. If we draw a soldier with a shield in hand and ask him to go to the right from the left side of the mobile phone screen, what should we do.

First, we will use LoadContent to load all the texture resources used by this soldier. Then, use the Draw method to Draw the texture to the position specified on the screen. However, we need to move the child soldier from the left to the right. Frame Animation changes are also involved during the running process. We need to handle these problems in Update. To put it simply, we need to change the coordinates and positions of the soldiers and the texture resources used for the transformation in the Update.

We need to know that the Update and Draw methods are continuously called at a certain interval during game operation so that we can see continuous animations. The flowchart is described as follows:

When we write XNA games on WP7, we need to understand a very important concept: What is the update rate? In general, the number of times Draw and Update are called in one second.

Only by clarifying this problem can we control the playback speed of 2D game animations. The default Update rate of xna in WP7 is 30fps. That is to say, Draw and Update are called 30 times in 1 second. 1-4. Expand the Game1 function and you can see the following code:

Public Game1 ()

{
Graphics = new GraphicsDeviceManager (this );
Content. RootDirectory = "Content ";
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan. FromTicks (333333 );
}

The English comment "Frame rate is 30 fps by default for Windows Phone." indicates that the default FPS for Windows Phone is 30.

 

1.3 LoadContent and UnloadContent

In section 1.2, We have briefly introduced the functions of LoadContent and UnloadContent. If we need to output a piece of text in WP7, we need to use LoadContent, in XNA, we often have access to the SpriteFont object. This object is used to control the output font. In XNA, we need to use an XML file to control the font style. 1-5. Create a New folder "Fonts" in the XNAGameSampleContent project. Then, right-click the Fonts folder created in the XNAGameSampleContent project and choose Add, then select sprite font and change Name to default. spritefont, and click add.

 

Figure 1-5

Then, 1-6, we modify the content of default. spritefont:

 

Figure 1-6

After completing the above preparations, we add a new object font in Game1.cs and add the following code in the LoadContent function:

Font = Content. Load <SpriteFont> ("Fonts/default ");

1-7:

Figure 1-7

 

Then write the following code in the Draw function:

SpriteBatch. Begin ();
SpriteBatch. DrawString (font, "Hello world", new Vector2 (200,100), Color. Black );
SpriteBatch. End ();

 

1-8:

Figure 1-8

Then, we only need to select "Window Phone 7 Emulator" on the rightmost side of the toolbar and press F5 to run this small demo in simulator mode, 1-9:

 

Figure 1-9

So how can we debug it on a real machine? We will discuss it in the next chapter.

1.4Debug and deploy XNA Projects

First, we need to install Zune, and our wp7 mobile phone also needs to be unlocked. To unlock a WP7 instance, you must have a WP7 developer account. Registration address: http://create.msdn.com /. 1-10:

Tips: If you have a. edu student email address and are certified by dreamSpark, you can avoid the annual fee of $99.

Figure 1-10

After we have these ready, we connect the data line to the WP7 real machine. If WP7 is identified, Zune will be automatically started. 1-11:

Figure 1-11

Then, select "Windows Phone 7 Device" on the rightmost side of the vs2010 Express for Windows Phone toolbar ". Press F5. After a few seconds, the XAP files generated after the project compilation will be deployed to the real machine for running.

Demo source code:/Files/wangergo/XNAGameSample1.rar

 

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.