Final code of
Game1.cs
/*
content created by -uditha sampath bandara
udithamail@yahoo.com
uditha.wordpress.com
*/
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 menu_example
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 :
Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager
graphics;
SpriteBatch
spriteBatch;
SpriteBatch
myspitebatch;
SpriteFont
myfont;//add spite batch and spite font as explained
in tutorial 01-hello world
Texture2D
mytexture, mytexture2;//creating texture object
float
Position = 0.0f;//initializing up down position
float
Position2 = 0.0f;//initializing left right position
GamePadState
state;
enum Mygamemode
{
start,
game,
exit
}
//define game modes start,game,exit
Mygamemode
mygame = Mygamemode.start;//assign start mode as initial mode
public
Game1()
{
graphics = new
GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to
before starting to run.
/// This is where it can query for any required services and
load any non-graphic
/// related content.
Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected
override void
Initialize()
{
// TODO:
Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place
to load
/// all of your content.
/// </summary>
protected
override void
LoadContent()
{
// Create
a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
myspitebatch = new SpriteBatch(graphics.GraphicsDevice);//setting spite batch for graphic device
myfont = Content.Load<SpriteFont>("Arial");
//loading font
mytexture = Content.Load<Texture2D>("pic");//loding image
mytexture2 = Content.Load<Texture2D>("cursor");//loding cursor image
}
/// <summary>
/// UnloadContent will be called once per game and is the
place to unload
/// all content.
/// </summary>
protected
override void
UnloadContent()
{
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing
audio.
/// </summary>
/// <param
name="gameTime">Provides a
snapshot of timing values.</param>
protected
override void
Update(GameTime gameTime)
{
state = GamePad.GetState(PlayerIndex.One);
//capturing
the plyer one XBOX360 Controller state
// Allows
the game to exit
if
(GamePad.GetState(PlayerIndex.One).Buttons.Back
== ButtonState.Pressed)
this.Exit();
// Move
400 pixels each second
float
moveFactorPerSecond = 2000 *
(float)gameTime.ElapsedRealTime.TotalMilliseconds
/ 1000.0f;
if
(mygame == Mygamemode.game)//chack whether you are in the game mode
{
//Direction
Pad
if
(state.DPad.Up == ButtonState.Pressed)
{
Position -=
moveFactorPerSecond*200;
}
if
(state.DPad.Down== ButtonState.Pressed)
{
Position +=
moveFactorPerSecond * 200;
}
if
(state.DPad.Left == ButtonState.Pressed)
{
Position2 -=
moveFactorPerSecond * 200;
}
if
(state.DPad.Right == ButtonState.Pressed)
{
Position2 +=
moveFactorPerSecond * 200;
}
}
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param
name="gameTime">Provides a
snapshot of timing values.</param>
protected
override void
Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
if
(mygame == Mygamemode.start) //chack whether you are in the start mode
{
myspitebatch.Begin();
myspitebatch.DrawString(myfont,
"Welcome to the demo ", new Vector2(300.0f,
30.0f), Color.YellowGreen);
myspitebatch.DrawString(myfont,
"Press A to continue..... ", new Vector2(300.0f,
80.0f), Color.YellowGreen);
myspitebatch.End();
if
(state.Buttons.A==ButtonState.Pressed)//chack whather user press A while he/she in the start mode
mygame = Mygamemode.game; //if(true)
assign game mode
}
if
(mygame == Mygamemode.game)//chack whether you are in the game mode
{
myspitebatch.Begin();//start process
Vector2
position = new Vector2(200.0f
+ Position2, 200.0f + Position);//setting position with
variables (Position1) and (Position2)
//those
variables change the position of the image according to the key pressing
myspitebatch.Draw(mytexture,
position, Color.White);//drawing the image
myspitebatch.DrawString(myfont,
"Use D Pad to move the image", new Vector2(100.0f,
10.0f), Color.Gold);//drawing
text on the screan
myspitebatch.DrawString(myfont,
"Press Start to exit demo", new Vector2(100.0f,
30.0f), Color.Gold);//drawing
text on the screan
myspitebatch.End();//end process
if
(state.Buttons.Start== ButtonState.Pressed)//chack whather user press start while he/she in the start
mode
mygame = Mygamemode.exit; //if(true)
assign exit mode
}
if
(mygame == Mygamemode.exit) //chack whether you are in the exit mode
{
myspitebatch.Begin();
myspitebatch.DrawString(myfont,
"Goodbuy from the demo ", new Vector2(300.0f,
30.0f), Color.YellowGreen);
myspitebatch.DrawString(myfont,
"Press B to exit..... ", new Vector2(300.0f,
80.0f), Color.YellowGreen);
myspitebatch.End();
if
(state.Buttons.B== ButtonState.Pressed)//chack whather user press b while he/she in the start mode
this.Exit(); //if(true)
assign game mode
}
base.Draw(gameTime);
}
}
}
This is the end of creating a Menu system in a XBOX360 game
tutorial.
Also source project is attached with this article. http://digit.lk/files/menu_example-xbox.rar
Form the next article onward you`ll able to read about the
latest gaming news around the world.