6
\$\begingroup\$

I'm working on a Pong clone in XNA. Gameplay-wise, I have it where I want it to be. I want to add a title screen and some other screens to it like a menu, as well as a screen for the Winning/Losing results.

I've tried the Game State Management Example on the App Hub site, but It's very complicated and I haven't been able to make sense of it. Is there a simpler way?

I'm hoping for a solution that can be used in other projects too.

Plus I'd like to know how to actually create menu items (basically, how do I display the different options on it, and highlight them, etc).

\$\endgroup\$
3
  • \$\begingroup\$ Something about menu screen can be also found in XNA 3.0 Game programming recipes - riemers.net/eng/Recipes30/chap3.php - if you don't have the book, you can have at least code :) \$\endgroup\$
    – zacharmarz
    Sep 19, 2011 at 17:31
  • 2
    \$\begingroup\$ The game state management example is a really good way to do it. I'd recommend trying to get your head around it, worked a treat in our game. \$\endgroup\$ Sep 21, 2011 at 10:26
  • \$\begingroup\$ Does anyone have the link to the Game State Management example on App Hub? \$\endgroup\$
    – ashes999
    Jan 27, 2013 at 3:18

5 Answers 5

6
\$\begingroup\$

I'd recommend continuing to battle it out learning the sample. Once you groc how to implement it, you get all the nice things it does (screen fades, pause control, etc.) for nothing.

\$\endgroup\$
2
  • \$\begingroup\$ Microsoft doesn't really give much info on the code itself. Are there any other places that might? \$\endgroup\$
    – Slateboard
    Sep 19, 2011 at 16:06
  • \$\begingroup\$ Is there anything in particular that's proving troublesome? I found the player index stuff a little verbose, same with the transition stuff. In high level terms it's just a stack of Screens (which are simply classes with a Draw and Update method called by ScreenManager that can be added to / cleared by a call to Load in the ScreenManager. Load uses a 'params' argument so you can send it as many new screens as you want. ScreenManager stores Screens as a list and iterates over them on Update. \$\endgroup\$ Apr 10, 2012 at 10:29
4
\$\begingroup\$

I would seriously consider sticking it out with the game state management example. It is a great framework for your first game. If you were designing one that can be re-used, with menu items etc. you'd probably end up with something similar.

The basic idea is to have a stack of screens (GameScreen is the base screen class, The ScreenManager is the 'stack' manager in the sample i think).

i.e. start with a title screen. Pressing a button pushes the main menu screen onto the stack.

While any screen is transitioning on, render it as such (menu items can use the transition amount to decided where to render, so they appear to slide on-screen).

Always direct input to only the top screen, even while transitioning on (so fast menu selectors can easily get into the game while the menus are transitioning on).

Once the top screen is fully transitioned, only render that screen.

So during gameplay you could end up with the following on the screen stack:

TitleScreen, MainMenuScreen, GamePlayScreen, PauseMenuScreen.

In that state, the pause menu gets rendered, and that's the screen the player controls. To 'exit' the screen, simply pop it off of the stack (or start it transitioning off), then it's the gameplay screen that gets input and is rendered.

\$\endgroup\$
3
\$\begingroup\$

maybe this method not the best, but for pong game could be enough, and is easy to understand.

Make a enum with your states:

 enum States { Menu, Playing, Pause, Lost, Win, Intro }

In your update method:

 void Update(gameTime time)
 { 
     switch (State)
     {
        case States.Intro: UpdateIntro(gametime); break;
        case States.Menu: UpdateMenu(gametime); break;
        case States.Playing: UpdatePlaying(gametime); break;
        ....
     }  
 }

In your draw method is the same but with DrawMethods.

I'd recommend that the State variable is a property this way:

 States State { 
    get {return _state;} 
    set { 
      if (_state!=value) {
          OnStateChanged(_state, value);
          _state=value;
      }
    }
 }

 void OnStateChanged(States prev, States next)
 {
      if (prev == States.Menu && next == States.Playing)
      {
          ResetGame();
      }
 }
\$\endgroup\$
2
\$\begingroup\$

I'm using this example myself right now - I found it easiest to recreate the classes myself, then take chunks of code over a bit at a time to see exactly how each method / class communicates with the rest. Now I've got all the pieces in play and an understanding of what they're doing. It's really the best example of screen management out there.

\$\endgroup\$
0
1
\$\begingroup\$

Since it wasn't mentioned in the question (nor the answers) -- the original, and best Game State Management class can be downloaded and learned from here - http://xbox.create.msdn.com/en-US/education/catalog/sample/game_state_management

It's applicable in both XNA 4.0 and MonoGame 3.2, Enjoy.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .