from pgzero.constants import keys
from ..actors.buttons import TextButton
-from .base import Scene, ChangeSceneEvent
+from ..actors.navigator import ActorNavigator
+from .base import Scene, ChangeSceneEvent, QuitEvent
+
+
class MenuScene(Scene):
super().__init__()
self._title = self.actors.add(TextButton("Main Menu"))
self._title.pos = (300, 200)
- self._level = self.actors.add(TextButton("Level"))
- self._level.pos = (300, 300)
- self._credits = self.actors.add(TextButton("Credits"))
- self._credits.pos = (300, 400)
+ self._nav = ActorNavigator()
+ self._menu = [
+ TextButton("Play", action=self.change_to_level),
+ TextButton("Credits", action=self.change_to_credits),
+ TextButton("Quit", action=self.quit),
+ ]
+ for i, item in enumerate(self._menu):
+ item.pos = (300, 300 + (i * 100))
+ self.actors.add(item)
+ self._nav.connect(
+ self._menu, [keys.DOWN, keys.RIGHT], [keys.UP, keys.LEFT],
+ wrap=True)
+ self._nav.current.select()
+
+ def change_to_level(self):
+ from .level import LevelScene
+ return [ChangeSceneEvent(LevelScene('level1'))]
+
+ def change_to_credits(self):
+ from .credits import CreditsScene
+ return [ChangeSceneEvent(CreditsScene())]
+
+ def quit(self):
+ return [QuitEvent()]
def on_key_down(self, key, mod, unicode):
- if key == keys.C:
- from .credits import CreditsScene
- return [ChangeSceneEvent(CreditsScene())]
- if key == keys.L:
- from .level import LevelScene
- return [ChangeSceneEvent(LevelScene('level1'))]
+ if key == keys.RETURN:
+ current = self._nav.current
+ if current is not None:
+ return current.action()
+ else:
+ prev, current = self._nav.follow(key)
+ if prev is not None:
+ prev.deselect()
+ if current is not None:
+ current.select()