Hey duckblue,
It's a very reasonable question, and I suspect that the reason you've
seen few definitive, straightforward answers is that it can be a
fiddly issue, and there is no single right way to do it. I'd guess
that mostly, everyone is inventing their own solution to this, with a
few common themes, no doubt.
Richard's suggestion of of looking at how other people do it is an
excellent one. You might get some benefit from looking at the old
entries to the 'PyWeek' competitions, there are many small (and hence
understandable) projects there to base your ideas on.
For what it's worth, here are some vague, arm-waving suggestions to
get you started.
In your draw event handler, you currently just draw the menu.
Presumably, you have (or intend to write) a different draw event
handler function that would draw the game graphics. One simple way to
let the game transition from menu to game is something like the
following:
menu = None
gameWorld = None
@window.event
def on_draw():
window.clear()
if gameWorld:
gameWorld.draw(window)
if menu:
menu.draw(window)
Then when your application starts up, you set 'menu' to be an object
with a draw method, which draws the menu, and you keep 'gameWorld' set
as None.
When the game starts (eg. the user selects 'start game' from the
menu), then you will set 'menu' to None, and set 'gameWorld' to an
object. The gameWorld object will have a 'draw()' member, which knows
how to draw the game.
Similarly, you could change the key_press handler to something like:
# a dictionary mapping keys to actions
menuKeyActions = {
key.UP: lambda: menu.move(-1),
key.DOWN: lambda: menu.move(1),
key.ENTER: lambda: menu.select(),
}
keyActions = menuKeyActions
@window.event
def on_key_press(symbol, modifiers):
if symbol in keyActions:
# call the action defined for this key symbol
keyActions[symbol]()
And then when the user presses ENTER on the 'start game' menu item,
you call:
def start_game():
menu = None
gameWorld = GameWorld() # create game world
keyActions = gameKeyActions
where 'gameKeyActions' is a second dictionary of keys mapped to game
actions.
Make any sense?
Jonathan
On Oct 10, 11:56 pm, duckblue <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I've followed the tutuorials and I've made a menu class and game
> class. Both work independently, but I don't know how to go from
> showing the menu to playing the game.
>
> Here's how I'm loading the menu:
>
> __docformat__ = 'restructuredtext'
> import math
> import pyglet
> from pyglet.window import key
> from pyglet.window import mouse
> import Menu
> menu_class = Menu
> menu_items = ['Start Game','Options','Exit']
> menu = menu_class(menu_items)
>
> window = pyglet.window.Window(800, 600)
> window.clear()
>
> @window.event
> def on_draw():
> window.clear()
> menu.display_item(window)
>
> @window.event
> def on_key_press(symbol, modifiers):
> if symbol == key.UP:
> menu.move(-1)
> menu.display_item(window)
> if symbol == key.DOWN:
> menu.move(1)
> menu.display_item(window)
> if symbol == key.ENTER:
> print "go to game"
>
> pyglet.app.run()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---