This month we`ll discuss about the code and steps to create a Menu system in a PC game.
Main menu Screen
A menu system consists of different set of states like main menu, options, and help. In this tutorial we are going to discuss about using three main states. Start, Game, Exit.
For the Game state you need to add some images to show the user inputs.
You can add those files in to the content folder.

Then you are able to write a code to create a menu system in a XNA game.
In the Game1.cs file initialize these variables.
KeyboardState mykeyboardstate;
//create a keyboardstate object to get the state of the keyboard
MouseState mymousesatate;//get the state of the mouse
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
float mousex = 0.0f;//initilizing mousex position
float mousey = 0.0f;//initializing mousey position
Using enum we can define the different game states.Then we sets the initial stage as start state.
enum Mygamemode
{
start,
game,
exit
} //define game modes start,game,exit
Mygamemode mygame = Mygamemode.start;//assign start mode as initial mode
Now in the LoadContent() method write code to load the assest
Window.Title = Using menu system;
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
Then in the Update() method you can chack for keyboard and mouse inputs.
mykeyboardstate = Keyboard.GetState();//capturing the state
mymousesatate = Mouse.GetState();//capturing mouse state
mousex = (float)mymousesatate.X;//getting the x position of the mouse
mousey = (float)mymousesatate.Y;//getting the y position of the mouse
// Move 400 pixels each second
float moveFactorPerSecond = 80 *
(float)gameTime.ElapsedRealTime.TotalMilliseconds / 1000.0f;
//chage the position according to the key pressed up,down,left,right
if (mykeyboardstate.IsKeyDown(Keys.Up))
Position -= moveFactorPerSecond;
if (mykeyboardstate.IsKeyDown(Keys.Down))
Position += moveFactorPerSecond;
if (mykeyboardstate.IsKeyDown(Keys.Left))
Position2 -= moveFactorPerSecond;
if (mykeyboardstate.IsKeyDown(Keys.Right))
Position2 += moveFactorPerSecond;
In the Draw () method you can see the Changes by drawing the images.
if (mygame == Mygamemode.start) //chack whether you are in the start mode
{
myspitebatch.Begin();
myspitebatch.DrawString(myfont, Welcome to the demo, new Vector2(30.0f, 30.0f), Color.YellowGreen);
myspitebatch.DrawString(myfont, Press Enter to continue....., new Vector2(30.0f, 80.0f), Color.YellowGreen);
myspitebatch.End();
if (mykeyboardstate.IsKeyDown(Keys.Enter))//chack whather user press enter key 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 + (int)Position2, 200.0f + ((int)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 Arrowkeys to move the image&use mouse to point, new Vector2(10.0f, 10.0f), Color.Gold);//drawing text on the screan
myspitebatch.DrawString(myfont, Use Esc to exit demo, new Vector2(10.0f, 30.0f), Color.Gold);//drawing text on the screan
myspitebatch.Draw(mytexture2, new Vector2(mousex, mousey), Color.White);
//drawing the cursor image, acording to the mouse position
myspitebatch.End();//end process
if (mykeyboardstate.IsKeyDown(Keys.Escape))//chack whather user press escape key 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(30.0f, 30.0f), Color.YellowGreen);
myspitebatch.DrawString(myfont, Press Q to exit....., new Vector2(30.0f, 80.0f), Color.YellowGreen);
myspitebatch.End();
if (mykeyboardstate.IsKeyDown(Keys.Q))//chack whather user press enter key while he/she in the start mode
this.Exit(); //if(true) assign game mode
}
Now you can run the project by pressing F5 or by clicking the ‘run’ button.

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;
KeyboardState mykeyboardstate;//create a keyboardstate object to get the state of the keyboard
MouseState mymousesatate;//get the state of the mouse
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
float mousex = 0.0f;//initilizing mousex position
float mousey = 0.0f;//initializing mousey position
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);
Window.Title = Using menu system;
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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
mykeyboardstate = Keyboard.GetState();//capturing the state
mymousesatate = Mouse.GetState();//capturing mouse state
mousex = (float)mymousesatate.X;//getting the x position of the mouse
mousey = (float)mymousesatate.Y;//getting the y position of the mouse
// Move 400 pixels each second
float moveFactorPerSecond = 80 *
(float)gameTime.ElapsedRealTime.TotalMilliseconds / 1000.0f;
//chage the position according to the key pressed up,down,left,right
if (mykeyboardstate.IsKeyDown(Keys.Up))
Position -= moveFactorPerSecond;
if (mykeyboardstate.IsKeyDown(Keys.Down))
Position += moveFactorPerSecond;
if (mykeyboardstate.IsKeyDown(Keys.Left))
Position2 -= moveFactorPerSecond;
if (mykeyboardstate.IsKeyDown(Keys.Right))
Position2 += moveFactorPerSecond;
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(30.0f, 30.0f), Color.YellowGreen);
myspitebatch.DrawString(myfont, Press Enter to continue....., new Vector2(30.0f, 80.0f), Color.YellowGreen);
myspitebatch.End();
if (mykeyboardstate.IsKeyDown(Keys.Enter))//chack whather user press enter key 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 + (int)Position2, 200.0f + ((int)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 Arrowkeys to move the image&use mouse to point, new Vector2(10.0f, 10.0f), Color.Gold);//drawing text on the screan
myspitebatch.DrawString(myfont, Use Esc to exit demo, new Vector2(10.0f, 30.0f), Color.Gold);//drawing text on the screan
myspitebatch.Draw(mytexture2, new Vector2(mousex, mousey), Color.White);
//drawing the cursor image, acording to the mouse position
myspitebatch.End();//end process
if (mykeyboardstate.IsKeyDown(Keys.Escape))//chack whather user press escape key 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(30.0f, 30.0f), Color.YellowGreen);
myspitebatch.DrawString(myfont, Press Q to exit....., new Vector2(30.0f, 80.0f), Color.YellowGreen);
myspitebatch.End();
if (mykeyboardstate.IsKeyDown(Keys.Q))//chack whather user press enter key 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 PC game tutorial.
Also source project is attached with this article. http://digit.lk/files/GameDev-menu_example.rar
Form the next tutorial you`ll learn about working with MP3 music files in a XNA game.
Previous Article
Post new comment