Sign in to follow this  

Main Menu view, Game view.. How to put them together?

Recommended Posts

I'm making a tetris game in Python/Pygame.. so far, I've got the tetris part working. I can play tetris in the console. (it's just a 2-D list of numbers right now) I'm confused on how I should approach changing from the Main Menu view, to the Game view and vice versa.. Can someone point me to the right direction?

Share this post

Link to post
Share on other sites
I'm not sure exactly how your console game is set up, but here's an easy approach I use in graphical games.

Declare an int called "iAppState" somewhere where you'll be able to access it easily (as a global variable, or in your App/Game class if you wrote one). If you're not familiar with Hungarian notation, the "i" simply indicates it's an int-- if you don't use Hungarian notation, name it however you like. This variable will keep track of the state of the application, as the name indicates.

Then, in some header that you have your often-used defines (#define), define these and whichever else come to mind:

#define MODE_TITLE 1
#define MODE_GAME 2
#define MODE_HIGHSCORE 3 // (optional, but it's an idea)

Whenever you change from the title screen to the game, or from the game to the title screen, or from the game to the high score list, etc., set iAppState to whatever the new state is.

Then, in your game loop, you'll want to have a switch statement to handle all possible iAppMode states.

switch (iAppMode)
// do your game logic here
// do your title screen logic here

You should have similar in the drawing part of your game loop as well.

That's all there is to it, but this technique is suited best for simple games. For large-scale games, you may want to go so far as to write a GameState class full of variables regarding the state of the game (are we on the title screen? is the game paused? how many players are currently playing?).

Share this post

Link to post
Share on other sites
I tended towards having a stack of states, held within a game state manager class. The states were capable of pushing and poping themselves and other states. I just then updated the top state in the stack every frame.

So for example, before the start of the game loop, I pushed a main menu state to the stack. When the user pressed the new game button, the menu class pushed the game state. When it was done, the game state simply poped itself to return to the menu.

I am not sure if it is the best method, but I liked it better than having a if statement within the game loop itself.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this