From 2be70af38592f9eab413e3fe5823cc3e6b00e3de Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Tue, 13 May 2014 20:29:33 +0200 Subject: [PATCH] Move keys to constants --- naja/constants.py | 12 ++++++++++++ naja/scenes/credits.py | 3 ++- naja/scenes/game.py | 3 ++- naja/scenes/menu.py | 3 ++- naja/widgets/info_area.py | 8 ++++---- naja/widgets/robot.py | 10 +++++----- naja/widgets/selector.py | 8 ++++---- 7 files changed, 31 insertions(+), 16 deletions(-) diff --git a/naja/constants.py b/naja/constants.py index b6fce4e..3f45496 100644 --- a/naja/constants.py +++ b/naja/constants.py @@ -1,3 +1,5 @@ +import pygame.locals as pgl + from naja.attrdict import AttrDict @@ -56,3 +58,13 @@ PLAYER_SIZE = (64, 96) # Player States MOVE = 1 ACT = 2 + +KEYS = AttrDict({ + 'UP': (pgl.K_UP, pgl.K_w, pgl.K_COMMA), + 'DOWN': (pgl.K_DOWN, pgl.K_s, pgl.K_o), + 'LEFT': (pgl.K_LEFT, pgl.K_a), + 'RIGHT': (pgl.K_RIGHT, pgl.K_d, pgl.K_e), + + 'SELECT': (pgl.K_RETURN, pgl.K_KP_ENTER), + 'QUIT': (pgl.K_ESCAPE, pgl.K_q), +}) diff --git a/naja/scenes/credits.py b/naja/scenes/credits.py index 8636024..67cb4e5 100644 --- a/naja/scenes/credits.py +++ b/naja/scenes/credits.py @@ -4,6 +4,7 @@ Credits scene. import pygame.locals as pgl +from naja.constants import KEYS from naja.scenes.scene import Scene from naja.widgets.text import TextWidget, TextBoxWidget from naja.events import SceneChangeEvent @@ -31,7 +32,7 @@ class CreditsScene(Scene): box_width=100)) def handle_scene_event(self, ev): - if ev.type == pgl.KEYUP and ev.key in (pgl.K_q, pgl.K_ESCAPE): + if ev.type == pgl.KEYUP and ev.key in KEYS.QUIT: from naja.scenes.menu import MenuScene SceneChangeEvent.post(MenuScene) return diff --git a/naja/scenes/game.py b/naja/scenes/game.py index cca363f..5b24347 100644 --- a/naja/scenes/game.py +++ b/naja/scenes/game.py @@ -4,6 +4,7 @@ Gameboard scene. import pygame.locals as pgl +from naja.constants import KEYS from naja.events import SceneChangeEvent from naja.scenes.scene import Scene from naja.widgets.board import BoardWidget @@ -28,6 +29,6 @@ class GameScene(Scene): def handle_scene_event(self, ev): from naja.scenes.menu import MenuScene - if ev.type == pgl.KEYUP and ev.key in (pgl.K_q, pgl.K_ESCAPE): + if ev.type == pgl.KEYUP and ev.key in KEYS.QUIT: SceneChangeEvent.post(MenuScene) return diff --git a/naja/scenes/menu.py b/naja/scenes/menu.py index 6b4152d..91f858b 100644 --- a/naja/scenes/menu.py +++ b/naja/scenes/menu.py @@ -4,6 +4,7 @@ Main menu scene. import pygame.locals as pgl +from naja.constants import KEYS from naja.scenes.scene import Scene from naja.widgets.text import TextWidget from naja.widgets.selector import SelectorWidget @@ -32,6 +33,6 @@ class MenuScene(Scene): def handle_scene_event(self, ev): if ev.type == pgl.KEYDOWN: - if ev.key in (pgl.K_q, pgl.K_ESCAPE): + if ev.key in KEYS.QUIT: QuitGameEvent.post() return diff --git a/naja/widgets/info_area.py b/naja/widgets/info_area.py index 40bc75b..4c79f07 100644 --- a/naja/widgets/info_area.py +++ b/naja/widgets/info_area.py @@ -4,7 +4,7 @@ Widget for the game board information area. import pygame import pygame.locals as pgl -from naja.constants import INFO_SIZE, EIGHT_BIT_SCALE, MOVE, ACT +from naja.constants import INFO_SIZE, EIGHT_BIT_SCALE, MOVE, ACT, KEYS from naja.events import InvalidateTheWorld from naja.resources import resources from naja.resources.mutators import EIGHT_BIT @@ -82,7 +82,7 @@ class InfoAreaWidget(Widget): if self.state.gameboard.player_mode == MOVE: return super(InfoAreaWidget, self).handle_event(ev) if ev.type == pgl.KEYDOWN: - if ev.key in (pgl.K_RETURN, pgl.K_KP_ENTER): + if ev.key in KEYS.SELECT: action = self.card.actions[self.chosen] if not action.check_available(self.state.gameboard.player): print "BEEP!" @@ -91,12 +91,12 @@ class InfoAreaWidget(Widget): self.state.gameboard.change_mode() InvalidateTheWorld.post() return True - if ev.key in (pgl.K_UP, pgl.K_w, pgl.K_COMMA): + if ev.key in KEYS.UP: if self.chosen > 0: self.chosen -= 1 InvalidateTheWorld.post() return True - if ev.key in (pgl.K_DOWN, pgl.K_s, pgl.K_o): + if ev.key in KEYS.DOWN: if self.chosen + 1 < len(self.card.actions): self.chosen += 1 InvalidateTheWorld.post() diff --git a/naja/widgets/robot.py b/naja/widgets/robot.py index 2653f81..cbeba2e 100644 --- a/naja/widgets/robot.py +++ b/naja/widgets/robot.py @@ -2,7 +2,7 @@ import pygame.locals as pgl -from naja.constants import PLAYER_SIZE, BIT_SIZE, TILE_SIZE, BITS, ACT +from naja.constants import PLAYER_SIZE, BIT_SIZE, TILE_SIZE, BITS, ACT, KEYS from naja.events import InvalidateTheWorld from naja.resources import resources from naja.resources.mutators import EIGHT_BIT @@ -42,22 +42,22 @@ class RobotWidget(Widget): if self.state.gameboard.player_mode == ACT: return super(RobotWidget, self).handle_event(ev) if ev.type == pgl.KEYDOWN: - if ev.key in (pgl.K_UP, pgl.K_w, pgl.K_COMMA): + if ev.key in KEYS.UP: if self.state.player.move(BITS.NORTH): self.state.gameboard.change_mode() InvalidateTheWorld.post() return True - if ev.key in (pgl.K_DOWN, pgl.K_s, pgl.K_o): + if ev.key in KEYS.DOWN: if self.state.player.move(BITS.SOUTH): self.state.gameboard.change_mode() InvalidateTheWorld.post() return True - if ev.key in (pgl.K_LEFT, pgl.K_a): + if ev.key in KEYS.LEFT: if self.state.player.move(BITS.WEST): self.state.gameboard.change_mode() InvalidateTheWorld.post() return True - if ev.key in (pgl.K_RIGHT, pgl.K_d, pgl.K_e): + if ev.key in KEYS.RIGHT: if self.state.player.move(BITS.EAST): self.state.gameboard.change_mode() InvalidateTheWorld.post() diff --git a/naja/widgets/selector.py b/naja/widgets/selector.py index 49cf8d8..c5ba07f 100644 --- a/naja/widgets/selector.py +++ b/naja/widgets/selector.py @@ -1,5 +1,6 @@ import pygame.locals as pgl +from naja.constants import KEYS from naja.widgets.base import Container from naja.resources import resources from naja.resources.mutators import EIGHT_BIT, R270 @@ -22,15 +23,14 @@ class SelectorWidget(Container): def handle_event(self, ev): if ev.type == pgl.KEYDOWN: - if ev.key in (pgl.K_DOWN, pgl.K_s, pgl.K_o, - pgl.K_UP, pgl.K_w, pgl.K_COMMA): - if ev.key in (pgl.K_DOWN, pgl.K_s, pgl.K_o): + if ev.key in KEYS.UP + KEYS.DOWN: + if ev.key in KEYS.DOWN: self.position += 1 else: self.position -= 1 self.position %= len(self.widgets) return True - elif ev.key in (pgl.K_RETURN, pgl.K_KP_ENTER): + elif ev.key in KEYS.SELECT: return self.widgets[self.position].callback('click') return super(SelectorWidget, self).handle_event(ev) -- 2.34.1