Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a rough schematic of a class structure that might work for you:</p> <pre><code>class Game(object): "This class contains the Scene which is the current scene the user is look ing at." def __init__(self): self.current_level = 0 self.current_screen = MainMenu(self) def load(self): "Load progress from disk" pass def save(self): "Save progress to disk" pass def clearCurrentScreen(self): self.current_screen.clear() self.window.remove_handlers() def startCurrentScreen(self): self.window.set_handler("on_key_press", self.current_screen.on_key_press) # etc self.current_screen.start() def gotoNextLevel(self): "called from within LevelPlayer when the player beats the level" self.clearCurrentScreen() self.current_level += 1 self.current_screen = LevelPlayer(self, game, self.current_level) self.startCurrentScreen() def startPlaying(self): "called by the main menu when the user selects an option" self.clearCurrentScreen() self.current_screen = LevelPlayer(self, game, self.current_level) self.startCurrentScreen() def execute(self): self.window = pyglet.window.Window() self.startCurrentScene() pyglet.app.run() class Screen(object): def __init__(self): pass def start(): pass def clear(): "delete all graphical objects on screen, batches, groups, etc. Clear all state in pyglet." pass def on_key_press(self, key, etc): pass def on_draw(self): pass # etc class LevelPlayer(Screen): "This class contains all your game logic. This is the class that enables the user to play through a level." def __init__(self, game, level_to_play): pass # be sure to implement methods from Screen class MainMenu(Screen): "This class presents the title screen and options for new game or continue." def __init__(self, game): self.game = game def handleNewGame(self): self.game.startPlaying() def handleContinue(self): self.game.load() self.game.startPlaying() # be sure to implement methods from Screen game = Game() game.execute() </code></pre> <p>So you have a Game class who owns the window and that manages which Screen is displayed to the user. Here I use "Screen" to mean what the user is interacting with, for example a MainMenu, or a LevelPlayer. The key here is the clear() method of Screen, which you should implement to delete all the sprites, media, groups, and batches that you were displaying. You also have to remove the window handlers on clear and set them on start.</p> <p>You can see this solution in action here: <a href="https://github.com/superjoe30/lemming/tree/master/lemming" rel="nofollow">https://github.com/superjoe30/lemming/tree/master/lemming</a></p>
 

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