Saturday, October 31, 2009

CREATE 2D GAME YOURSELF TUTORIAL 1

After successfully installing Visual Studio .net and Microsoft XNA Game Studio, we can start to create Window base game or XBOX360 base game our self.


1st step:

All programs-> Microsoft XNA Game Studio

After open XNA Game studio go to
File->New->Project or (ctr+shift+N)
Now it selects XNA Game Studio under Visual C#. And select Windows Game check figure 1.














Figure 1 show that selects Windows Game.

In here you should provide the name for the game which going to create and should provide the location to save the project after that press OK button.
2nd step:
Now you can see Solution Explorer. Check Figure 2.


















Figure 2

Now it is better to know about the Game.cs file. By clicking on that you can see it has basically Five methods already. Now on here we can create game using that already created methods.

1) Initialize() -(Provide Game initial information)
2) LoadContent()- (Using that load resource files such as images,videos,sound files,font and so on)
3) UnloadContent()-(Using that we can unload resource which we load using LoadContent() method)
4) Update(GameTime gameTime) –(use to update action are goes here)
5) Draw(GameTime gameTime) – (Use to draw resource which we added)
Now I think you have some idea about how the game create procedure is going. Let’s check how to add image to our Game project.
How to add image to Game Project

Before you can start drawing backgrounds, characters, or so on having in your game to the screen, you will need to add these images to your game project. The image types you can add that are supported by the Content Pipeline are .bmp, .jpg, .png and a few texture file formats. Adding the images to your project makes them available to the XNA framework's Content Importer. This will compile them as ".xnb" files when you build your game and make them accessible to the Content Importer in the code for loading and unloading.

3rd step:

To add image to game project, Right click on Content and should provide existing items. Check figure 3.



















Figure 3.

Now you can brows and able to provide image to your game project. After providing image path now you can see the image which you added is added to Content Pipeline. Check Figure 4.











Figure 4

Now you have added image. And next go to Game.cs and should provide added images reference. For that should use Texture2D mickymouse2D;

Next using above added reference we must load that image to our game project for that go to LoadContent() methods and add following line mickymouse2D = Content.Load("Mickymouse"); in here “Mickymouse” is the image name which I alredy added to Content Pipe. Now I have already loaded image to my game. Now my next step is draw that load image on my screen. To do that I must use Draw(GameTime gameTime) method. And add following few line on it.

Rectangle mickymouse2DPosition = new Rectangle(100, 100, 100, 100);
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
spriteBatch.Draw(mickymouse2D, mickymouse2DPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);

in here I provide the position which my image should draw. Now press play button and you can see the image is drawn on your game screen. Check figure 5.















Figure 5

Lets check complete source code.

using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;

namespace MyGame1
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D mickymouse2D;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
base.Initialize();
}

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
mickymouse2D = Content.Load("Mickymouse");
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
Rectangle mickymouse2DPosition = new Rectangle(100, 100, 100, 100);
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
spriteBatch.Draw(mickymouse2D, mickymouse2DPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}

Now you can add image to your games screen.

Your Comments are highly appreciated; it will be improving my knowledge also.

0 comments:

About This Blog

  © Blogger templates 'Neuronic' by Ourblogtemplates.com 2008

Back to TOP