Friday, October 9, 2009

We all love to play games. Have you ever thought of creating a game? So let’s to do it. It is not that hard.
Let’s create a game using XNA. First step is to get a new project go to File --> New -->Project --> XNA Game studio 3.0 --> Windows
Game(3.0)

Then you will get a code like this where we can create a fu
ll game. In here I will tell you how to add a 3Dmodel to the game and make it move with arrow keys.
First of all if you just debug your code without adding anything this is what you get.




This is the place we are going to put our whole game to.
Before the game loads the company logo and the game menu gets loaded. Here are some screen shots of a simple game I created.






I will definitely give an example for creati
ng the game menu in my next post. For now let's start
coding with our 3D model.
First you need to get the 3D model. So right click on the content in the solution explorer and go to ADD then select Existing Item and select the 3D model you want to add to the code. If you want to add any texture to the code let’ say you want to add a background to the game. you can do it by adding it the same way as the 3D model.

And add this line to initialize the texture.
Texture2D background;
Then you need to load it. Put this inside LoadContent() method.
background= Content.Load("backg
round");
Finally you need to draw it. So add these lines to the Draw() method in your code.
spriteBatch.Begin();
spriteBatch.Draw(background, viewportRect, Color.White);
spriteBatch.End();
This will draw the background image according to the size of the screen.
Now let’s get back to work. Using this code you can move a 3D model with arrow keys. You might have played a lot of games where you move objects with arrow keys. So by improving this code you can create a simple game. So go ahead with your ideas.


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.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace MovingObject
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Model model;
float f = 0.01f;
float a, b;
float f1 = 0.01f;
private Matrix world = Matrix.CreateTranslation(new Vector3(10, 0, 0));
private Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 10), new Vector3(0, 0, 0), Vector3.UnitY);
private Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(100), 800f / 600f, 0.1f, 100f);
Vector3 position;
GamePadState previousGamePadState = GamePad.GetState(PlayerIndex.One);
KeyboardState previousKeyboardState = Keyboard.GetState();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
model = Content.Load("Helicopter");
}
protected override void UnloadContent()
{
base.UnloadContent();
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
KeyboardState keyboardState = Keyboard.GetState();
position += new Vector3(0.01f, 0.01f, 0.01f);
if (keyboardState.IsKeyDown(Keys.Left))
{
f = f - 0.1f;
a = 10 + f;
world = Matrix.CreateTranslation(new Vector3(10 + f, b, 0));
}
if (keyboardState.IsKeyDown(Keys.Right))
{
f = f + 0.1f;
a = 10 + f;
world = Matrix.CreateTranslation(new Vector3(10 + f, b, 0));
}
if (keyboardState.IsKeyDown(Keys.Up))
{
f1 = f1 + 0.1f;
b = 0 + f1;
world = Matrix.CreateTranslation(new Vector3(a, 0 + f1, 0));
}
if (keyboardState.IsKeyDown(Keys.Down))
{
f1 = f1 - 0.1f;
b = f1;
world = Matrix.CreateTranslation(new Vector3(a, 0 + f1, 0));
}
base.Update(gameTime);
}protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, view, projection);
base.Draw(gameTime);
}
private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = world;
effect.View = view;
effect.Projection = projection;
graphics.GraphicsDevice.RenderState.DepthBufferEnable = true;
}
mesh.Draw();
}
}
}
}

If you did this you will see a 3D model in the screen and you will be able to move it here and there with arrow keys. So I'm looking forward to tell you about how to create a simple game.