Book Image

Windows Phone 7 XNA Cookbook

By : Zheng Yang
Book Image

Windows Phone 7 XNA Cookbook

By: Zheng Yang

Overview of this book

Developing games for Windows Phone 7, a new mobile platform, is your big chance to impact the world of mobile games. The XNA 4.0 for Windows Phone 7 integrates a lot of capabilities from software and hardware for you to create incredible games. The next generation of mobile games will be built by you. Windows Phone 7 XNA Cookbook is the best choice for you to make a game on Windows Phone 7. The book helps you to master the indispensable techniques to create your games using XNA 4.0. From the basics such as animating a 2D sprite and interacting with the customized graphical user interface to the more challenging such as 3D graphic rendering and collision detection. This comprehensive cookbook covers all the essential areas of XNA game development for Windows Phone 7, such as approaches to control the sensors, gestures and typical kinds of cameras. We also have recipes for sprite animation, texture rendering, and graphical user interface development that will give you a powerful tool to work with 2D effects. After this we move onto the more juicy stuff with recipes covering 3D graphic rendering and collision detection, and major ways to improve your loading efficiency. You will also work with Xbox live so you can take your game global. Finally, no mobile game development book would be complete without a look at performance optimization to make your games run faster. Windows Phone 7 XNA Cookbook will equip you with the firepower to rock the game world.
Table of Contents (17 chapters)
Windows Phone 7 XNA Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Creating your first Windows Phone XNA application


After installing the Visual Studio 2010 and Windows Phone Developer Tools, I am sure you cannot wait to begin your first Windows Phone XNA application. In this section, you will start the charming journey, learn about the basic and important code of XNA, and how this code works. It is easy to get started.

How to do it...

  1. 1. Once the XNA Game Studio 4.0 is successfully installed, click the top-left menu item File | New | Project. The XNA and Windows Phone Game Project Template will show up in the New Project window, as shown in the following screenshot:

  2. 2. In the New Project pop-up window, select Visual C# from the left-hand side pane, and then choose the Window Phone Game project template.

  3. 3. Next, give a Name, Location, and Solution name to the project. Solution name will be the same as your project name by default.

  4. 4. Finally, click OK to let Visual Studio 2010 automatically create the Windows Phone Game project for you.

  5. 5. The generated Windows Phone Game project WindowPhoneGame1 automatically has the main game functionalities; the other generated associate project WindowsPhoneGame1Content is responsible for the game content:

How it works...

The basic methods in Game1 class residing in Game1.cs of your first Windows Phone Game project WindowsPhoneGame1 are very useful. They are clear and easy to understand according to the method name and comments. This template is your first gift, and you will find new ways here to speed up your development. The complete game skeleton presents several significant methods:

  • Game1() constructor method: The constructor, called before the Initialize method, is a typical one found in any class and, therefore, has the same meaning used to set default values to required elements. For example, instantiating the graphics device manager, define the game frame rate, and so on.

  • Initialize() method: Sets default and preliminary values to your game, queries and initializes user-based information, such as starting positions and frame updating rate.

  • LoadContent() method: Loading all game content may include images, sprite sheet, sounds, models, and so on. In XNA game development, all artwork loading should be done in the LoadContent() method before the Update() and Draw() methods.

  • UnloadContent() method: Unloads all game content and content managers, a controller of all contents with loading and unloading when the objects used in your game need specific disposing or unloading.

  • Update() method: This method is very important when your game is running. It performs ongoing game logic such as calculating current positions, physics, collisions, and states; collecting the input information from various input devices; updating animations. Note that at this stage you only decide upon the current frame to display. Drawing is not performed because all drawing should be done by the Draw() method. It updates the camera, refers to the update animation note, plays audios, and so on. The Update() method updates the game logic, which will make your game more fun depending on the interaction with the game data, such as player life, experience value, and score.

  • Draw() method: As the method name implies, in this method your work is to render all the graphics, including 2D and 3D views, onto the screen to make the game data visible, so that players can experience the real game world.

For the recipes in the following chapters, we will be revising this code many times from different perspectives. When you build the project and run it, you will see an emulator window with a solid blue background by default. Your first XNA Windows Phone Game is done, although it is just a blank screen as shown in the following screenshot. Isn't it easy?