Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-04 05:34
7.2%
Getting Started with XBOX 360 game development using Microsoft XNA. Part VI.

by Uditha Bandara

 

This month we`ll discuss about the code and steps to create a Menu system in a XBOX360 game.

Main menu Screen

 

A menu system consists with 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 for creating a menu system in a XNA game.

 

In the Game1.cs file initialize these variables.

 

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; //Crteating a XBOX360 gamepad state

 

 

 

Using enum we can define the different game states.Then we set 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 for loading the asset

 

 

 

 

       

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

              

 

 

 

 

Then in the Update() method you can chack for keyboard and mouse inputs.

 

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)//check 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;

 

               

}

 

          

 }

 

In the Draw () method you can see the Changes by drawing the images.

 

 

if (mygame == Mygamemode.start) //check 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)//check whether user press A while he/she in the start mode

                    mygame = Mygamemode.game;            //if(true) assign game mode

 

 

            }

 

            if (mygame == Mygamemode.game)//check 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)//check whether user press start while he/she in the start mode

                    mygame = Mygamemode.exit;      //if(true) assign exit mode

 

 

            }

 

            if (mygame == Mygamemode.exit) //check 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)//check whether user press b 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;

 

      

        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.

 


Previous Article