Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not very experienced, but for what it's worth, the method I use is as follows. It does not recognise explicit 'states' or 'scenes' as such, but rather it relies on the adding (and removal) of discrete items into my game world. Each such item may have its own key handlers, and knows how and when to create other such items.</p> <p>A specific example:</p> <p>GameItem is a subclass for all items that can be put into the world. It is a simple collection of attributes, with no behaviour. It is subclassed by items in the game world like Bush, Player, etc, and also by HUD items like ScoreDisplay and MainMenu.</p> <p>World is just a collection of GameItems.</p> <p>My 'update' function iterates through all items in the world, calling their update method if they have one.</p> <p>My 'draw' function similarly iterates through all items in the world, drawing each of them. (many of them might be drawn en-masse by simply calling something like pyglet's Batch.draw)</p> <p>Right at application start-up, one of the first items I add to the world is a MainMenu object. It has an on_key_down method.</p> <p>World.add looks at the item being added. If an item has an on_key_down method, then it adds this handler to the pyglet key handlers stack. (Similarly, it undoes this in World.remove) (Actually, on reflection, I guess world.add raises an event when it adds an item. If the application's keyboard handler module is interested, then on recieving one of these events it then does the adding of the item's key hander, but this is kinda incidental to the question)</p> <p>The MainMenu.on_key_handler method looks at the pressed key, and if it's the key to start the game, then it makes a series of calls:</p> <pre><code># Do everything needed to start the game # For dramatic pacing, any of these might be scheduled to be # called in a second or so, using pyglet.clock.schedule_once world.add(player) camera.to_follow(player) world.add(scoredisplay) # Finally, remove the main menu from the world # This will stop showing it on screen # and it will remove its keyboard event handler world.remove_item(self) </code></pre> <p>This is just a simple example, but hopefully you can see how, instead of starting the game, the mainmenu could instead display secondary menus by adding them to the world, or any such thing.</p> <p>Once in the game, you can change 'scenes' by simply calling 'world.remove' on all items that you no longer want visible, and calling 'world.add' on all the items in the new scene.</p> <p>An example of a game that uses this technique is previous pyweek entry SinisterDucks: <a href="http://code.google.com/p/brokenspell/" rel="nofollow">http://code.google.com/p/brokenspell/</a> (although, to be fair, it does not feature distinct 'scenes' during gameplay. It merely usesthis technique to manage menus, game over screen, etc)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload