📜 ⬆️ ⬇️

Splash! - development of the game for Windows Phone 7. Part I

How fast and easy can you create a full-fledged game for a modern phone running Windows Phone 7? What is needed for this? And what obstacles are waiting for the developer on this path?

We will try to answer all these questions in the process of creating a small game. And so that everything is real - it will be not just an example, but a full-fledged application that we will send to the Windows Phone Marketplace.

Thus, in this article we will look at the full cycle of creating a game - from the emergence of an idea, to the publication of a game in the Marketplace, without losing a single detail of this process.

Idea of ​​the game


We will not clone other games, but we will invent something new, interesting and colorful - especially for a phone with an accelerometer and a touch screen.
')
The user has access to one ball (white) on the playing field. The movements of this ball can be controlled with an accelerometer by tilting the phone in different directions, as if the ball is rolling in a rectangular box.

Colored balls appear on the field and strive for a white ball to color it.
The user can destroy the colored balls by clicking on them with their fingers. When you click on them, the balls turn into color blots (blots then gradually disappear). Instead of destroyed balls, new ones constantly appear. For each destroyed ball a certain amount of points is given (depending on the level).

The level is replaced by a set of points. With each level, the speed and number of colored balls increases. The number of levels is not limited.

The goal of the player is to score as many points as possible.

To force the player to use the accelerometer (and not just crush the balls with his fingers), add a special ball (black) to the game, which cannot be crushed until it turns into a color one after some time.

To implement this game will be enough 2D-graphics. In addition, you will need to play sounds and interact with the accelerometer and touch screen.

Development environment


To create applications for Windows Phone 7, we need very little: Visual C # 2010 Express and Windows Phone Developer Tools. Both products are completely free.
Go to the Microsoft website, download and install these tools: http://www.microsoft.com/express/Downloads . Windows Phone Developer Tools does not want to be installed on Windows XP, but if you want, you can do this: http://www.brucebarrera.com/how-to-install-windows-phone-7-developer-tools-on-windows-xp . But the phone emulator most likely will not start on Windows XP, so it’s better to install Windows 7 anyway.

If you already have Visual Studio 2010, it is better to put Windows Phone Developer Tools on it, since Visual C # 2010 Express is noticeably inferior to the studio in terms of convenience.

Choice of technology: XNA vs Silverlight


vs

After installing Windows Phone Developer Tools, we have the opportunity to create new types of projects in our studio: Windows Phone Application and Windows Phone Game.

Which one is better for us?

The first project is an application on Silverlight for Windows Phone 7. Accordingly, almost all classes available in regular Silverlight are available in it. There is XAML and controls, designer is available in the studio, etc.

The second project is an application on XNA 4.0. It already has no controls, but there is a game loop and access to fast hardware graphics features.

Judging by the name and description of the technology, we need exactly XNA. But maybe Silverlight is also good for writing games? However, the simplest test with Silverlight for Windows Phone immediately detects the limitation of this technology - a hundred of simultaneously moving small pictures put the phone into a slide show. We try to do the same on XNA - everything works quickly, even with a lot more pictures.

So we will make the game on XNA. It is a pity that there are no controls there - but so far, it seems, they are not particularly needed.

Meet XNA


Create a Windows Phone Game project in the studio. He appears immediately with the game class:
public class Game1 : Microsoft.Xna.Framework.Game 
Immediately determine how the user will keep the phone during the game. We assume that it will be more convenient to hold the phone in one hand (by tilting the phone and controlling the white ball), and with the other hand - click on the colored balls, turning them into blots. So, we need a portrait mode of the game. Here we set it up in the game constructor:
 public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; graphics.SupportedOrientations = DisplayOrientation.Portrait; graphics.PreferredBackBufferWidth = 480; graphics.PreferredBackBufferHeight = 800; graphics.IsFullScreen = true; } 

Further, in this class, two functions are of the greatest interest to us: Update and Draw.
 protected override void Update(GameTime 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); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: Add your drawing code here base.Draw(gameTime); } 

The first function is designed to handle game logic. In it, we will produce data about the actions of the user (from the accelerometer and touch screen) and the calculation of new positions of all game objects.

The second function is intended solely for drawing game objects on the screen.
It should be noted that the total execution time of these two functions should not exceed one iteration of the game cycle. On the phone it is about 33 milliseconds, since the frequency of the game cycle is 30 frames per second. If the execution time of the functions exceeds this value, the game will slow down, skip frames and, even worse, skip user actions.

That is, it turns out that the game is actually a real-time application. But how will it respond to the actions of the Garbage Collector? It is best to avoid his sudden intervention, so that we will carefully work with objects and not create them in thousands at each iteration. In general, we don’t need to create them often - after all, we will have relatively long-lived objects of the game world, and we will use structures or references to already existing objects as function parameters. And in this situation, the Garbage Collector can not stop us.

Let's try to compile and run the application. On some computers, it displays a message that XNA cannot be started in the emulator. In this case, you can make a project Windows Game and attach files there (Add As Link) from our project Windows Phone Game. That is, the same XNA code will work both for the phone and for Windows (and for the XBOX 360 too).
Finally, a window filled with purple appears - the result of the GraphicsDevice.Clear function (Color.CornflowerBlue).

In the class of the game is already present object class SpriteBatch. Actually, this object is all that we need when drawing. It contains all the necessary methods for working with 2D graphics and text.

But so far we lack something very important - game content.

Content


Gaming content is the various resources that are necessary for the game: textures (pictures with which we will draw), fonts (we will need to display text) and sounds.

Together with the Windows Phone Game project, another project is immediately created, the title of which contains the word Content. In this project we need to add files with pictures and sounds.
In addition, in the same project, you must specify all the fonts that we will use to display the text.

Downloading content in the program is very simple - using the Content.Load (assetName) function. The Content property is available in the game class. As assetName, you must pass the name of the resource that we added the project with content. As a rule, this is just a filename without an extension. As T you need to specify the type of content that we need: Texture2D, SpriteFont or SoundEffect.

Downloading content is not a very fast operation, so we’ll act very simply - we’ll download all the content immediately when the game starts and we will use it as needed.

Now all the preparations have been completed, and you can start making the game. This process will be described in more detail in the second part. Link to the second part

Source: https://habr.com/ru/post/113009/


All Articles