From 243e8924ca1375cbef82367620948a19ee55649a Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Wed, 2 Mar 2016 23:53:26 +0200 Subject: [PATCH] Make roach management buttons print things. --- koperkapel/scenes/roaches.py | 81 +++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 29 deletions(-) diff --git a/koperkapel/scenes/roaches.py b/koperkapel/scenes/roaches.py index b5c2715..34f0267 100644 --- a/koperkapel/scenes/roaches.py +++ b/koperkapel/scenes/roaches.py @@ -1,8 +1,8 @@ """ Roach management scene. """ -from pgzero.constants import keys +from pgzero.constants import keys, mouse from pgzero.actor import Actor -from ..actors.buttons import TextButton +from ..actors.buttons import TextButton, ImageButton from ..constants import WIDTH, HEIGHT from .base import Scene, ChangeSceneEvent @@ -13,10 +13,10 @@ TOOLBAR_MID_Y = HEIGHT * 1 // 2 BUTTON_INSET = (20, 20) -def inset_button(pos, dx, dy): +def inset_button(pos, d): return ( - pos[0] + dx * BUTTON_INSET[0], - pos[1] + dy * BUTTON_INSET[1]) + pos[0] + d[0] * BUTTON_INSET[0], + pos[1] + d[1] * BUTTON_INSET[1]) class RoachesScene(Scene): @@ -43,31 +43,47 @@ class RoachesScene(Scene): Actor("roach_management/inventory_pad", anchor=("left", "top"))) self._inventory_pad.pos = (TOOLBAR_LEFT_X, TOOLBAR_MID_Y) + def _add_button(self, name, anchor, inset, pos, action): + button = self._buttons.add( + ImageButton(name, anchor=anchor, action=action)) + button.pos = inset_button(pos, inset) + return button + def _init_buttons(self): - self._roach_left = self._buttons.add( - Actor("roach_management/left_button", anchor=("left", "bottom"))) - self._roach_left.pos = inset_button( - self._roach_pad.bottomleft, 1, -1) - - self._roach_right = self._buttons.add( - Actor("roach_management/right_button", anchor=("right", "bottom"))) - self._roach_right.pos = inset_button( - self._roach_pad.bottomright, -1, -1) - - self._inventory_left = self._buttons.add( - Actor("roach_management/left_button", anchor=("left", "bottom"))) - self._inventory_left.pos = inset_button( - self._inventory_pad.bottomleft, 1, -1) - - self._inventory_right = self._buttons.add( - Actor("roach_management/right_button", anchor=("right", "bottom"))) - self._inventory_right.pos = inset_button( - self._inventory_pad.bottomright, -1, -1) - - self._road_eject = self._buttons.add( - Actor("roach_management/eject_button", anchor=("right", "top"))) - self._road_eject.pos = inset_button( - (TOOLBAR_LEFT_X, TOOLBAR_TOP_Y), -1, 1) + self._add_button( + "roach_management/left_button", ("left", "bottom"), (1, -1), + self._roach_pad.bottomleft, self._roach_left) + + self._add_button( + "roach_management/right_button", ("right", "bottom"), (-1, -1), + self._roach_pad.bottomright, self._roach_right) + + self._add_button( + "roach_management/left_button", ("left", "bottom"), (1, -1), + self._inventory_pad.bottomleft, self._inventory_left) + + self._add_button( + "roach_management/right_button", ("right", "bottom"), (-1, -1), + self._inventory_pad.bottomright, self._inventory_right) + + self._add_button( + "roach_management/eject_button", ("right", "top"), (-1, 1), + (TOOLBAR_LEFT_X, TOOLBAR_TOP_Y), self._eject_roach) + + def _roach_left(self): + print("Cycle outside roaches left.") + + def _roach_right(self): + print("Cycle outside roaches right.") + + def _inventory_left(self): + print("Cycle inventory left.") + + def _inventory_right(self): + print("Cycle inventory right.") + + def _eject_roach(self): + print("Eject roach.") def update(self, world, dt): for i, roach in enumerate(world.roaches): @@ -88,3 +104,10 @@ class RoachesScene(Scene): if key == keys.ESCAPE: from .menu import MenuScene return [ChangeSceneEvent(MenuScene())] + + def on_mouse_down(self, pos, button): + if button == mouse.LEFT: + for actor in self.actors.buttons: + if actor.collidepoint(pos): + actor.action() + break -- 2.34.1