In the last two tutorials, I wrote about how to write a working Neat-based code. In this tutorial I’m going to show you how to use Neat menus. Here I assume you have a project based on NeatStarter.

Alright, we’re going to show our menu in a new screen. To do that, we first need to create a new empty screen:

public class SampleMenuScreen : Screen
    {
        public SampleMenuScreen(NeatGame Game)
            : base(Game)
        {
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Activate()
        {
            base.Activate();
        }

        public override void Behave(GameTime gameTime)
        {
            base.Behave(gameTime);
        }

        public override void HandleInput(GameTime gameTime)
        {
            base.HandleInput(gameTime);
        }

        public override void Render(GameTime gameTime)
        {
            base.Render(gameTime);
        }
    }

Now let’s hook this screen into our main game class. Add this line to your AddScreens() method:

Screens["samplemenu"] = new SampleMenuScreen(this);

Okay, let’s get back to our screen code. We are going to use the MenuSystem class inside the Neat.MenuSystem namespace, so be sure to add the using Neat.MenuSystem; line to the top of your code. Then create an object based on the MenuSystem class inside your screen:

MenuSystem menu;

And add this line in your Initialize method:

menu = new MenuSystem(game, new Vector2(game.GameWidth/2f, game.GameHeight/2f), game.GetFont("menufont"));

The first parameter in MenuSystem’s constructor method is our NeatGame, the second parameter is the position of the menu, and the third is the font we’re going to use.

Now that we have created our menu, let’s add items to it using the AddItem(…) function and then enable it. Add these lines in your Initialize() method:

var newGameItem = menu.AddItem("New Game");
var optionsItem = menu.AddItem("Options");
var exitItem = menu.AddItem("Exit Game");
menu.Enable();

And to bring our menu to life, we should update and render it each frame:

public override void Behave(GameTime gameTime)
        {
            menu.Update(gameTime);
            base.Behave(gameTime);
        }

public override void Render(GameTime gameTime)
        {
            menu.Render(gameTime);
            base.Render(gameTime);
        }

Our menu is now ready. Build and run the project, then open the console and type in e_show samplemenu. bingo!

neat_tut_3_menu

Alright, now let us make some changes to the menu. We don’t have a game yet, so let’s go ahead and disable the New Game item. To do that, we can edit the line in which we added the New Game item as below:

menu.AddItem("New Game",false);

Because New Game is the first item in the menu, we should change our default selected item. Add this below the AddItem lines:

menu.SelectedItem = 1;

Now let’s change the color of the Exit Game item to Red. Insert this line after the line that adds the Exit Game to the menu:

exitItem.Forecolor = Color.Red;

Okay, at this point our menu looks fine, but doesn’t do anything. We want the Options and Exit Game items to work:

optionsItem.OnSelectScript = “e_show optionsmenu”;
exitItem.OnSelectScript = “quit”;

Because Neat already has an Options menu built-in, we’re going to use that. Here, The OnSelectScript property is used to run a line of script.

neat_tut_3_menu_final

Here’s the complete code of this tutorial:

Game1.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Neat;

namespace NeatStarter
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class NeatStarterGame : NeatGame
    {
        public NeatStarterGame()
        {
            GameWidth = 800;
            GameHeight = 600;
            GameBackGroundColor = Color.CornflowerBlue;
            FullScreen = false;
        }

        protected override void Initialize()
        {
            base.Initialize();
            HasConsole = true;
            ConsoleKey = Keys.OemTilde;
            Graphics.ApplyChanges();
        }

        public override void FirstTime()
        {
            ActivateScreen("samplemenu");
            base.FirstTime();
        }

        public override void AddScreens()
        {
            base.AddScreens();
            Screens["mainmenu"] = new StartScreen(this);
            Screens["samplemenu"] = new SampleMenuScreen(this));
        }
    }
}

SampleMenuScreen.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Neat;
using Neat.MenuSystem;
using Neat.EasyMenus;
using Neat.GUI;
using Neat.Mathematics;
using Neat.Graphics;

namespace NeatStarter
{
    public class SampleMenuScreen : Screen
    {
        MenuSystem menu;

        public SampleMenuScreen(NeatGame Game)
            : base(Game)
        {
        }

        public override void Initialize()
        {
            menu = new MenuSystem(game, new Vector2(game.GameWidth/2f, game.GameHeight/2f), game.GetFont("menufont"));
            var newGameItem = menu.AddItem("New Game",false);
            var optionsItem = menu.AddItem("Options");
            var exitItem = menu.AddItem("Exit Game");
optionsItem.OnSelectScript = “e_show optionsmenu”;
exitItem.OnSelectScript = “quit”;

menu.SelectedItem = 1; exitItem.Forecolor = Color.Red; menu.Enable(); base.Initialize(); } public override void Behave(GameTime gameTime) { menu.Update(gameTime); base.Behave(gameTime); } public override void Render(GameTime gameTime) { menu.Render(gameTime); base.Render(gameTime); } } }

Last edited May 29, 2012 at 1:46 PM by saeedoo, version 4

Comments

No comments yet.