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 full game. In here I will tell you how to add a 3Dmodel to the game and make it move with arrow keys.
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 backgorund to the game. yuo 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
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.
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";
}
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
model = Content.Load<Model>("Helicopter");
}
{
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);
}
{
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();
}
}
}
}
No comments:
Post a Comment