KeyValues.translate_key returns a KeyValues object

This commit is contained in:
Yohann D'ANELLO 2020-11-11 22:47:51 +01:00
parent edecb7eb90
commit cde0b19c72
3 changed files with 20 additions and 21 deletions

View File

@ -20,25 +20,24 @@ class KeyValues(Enum):
SPACE = auto() SPACE = auto()
@staticmethod @staticmethod
def translate_key(key: str, settings: Settings) \ def translate_key(key: str, settings: Settings) -> Optional["KeyValues"]:
-> Tuple[Optional["KeyValues"], str]:
""" """
Translate the raw string key into an enum value that we can use. Translate the raw string key into an enum value that we can use.
""" """
if key in (settings.KEY_DOWN_SECONDARY, if key in (settings.KEY_DOWN_SECONDARY,
settings.KEY_DOWN_PRIMARY): settings.KEY_DOWN_PRIMARY):
return KeyValues.DOWN, key return KeyValues.DOWN
elif key in (settings.KEY_LEFT_PRIMARY, elif key in (settings.KEY_LEFT_PRIMARY,
settings.KEY_LEFT_SECONDARY): settings.KEY_LEFT_SECONDARY):
return KeyValues.LEFT, key return KeyValues.LEFT
elif key in (settings.KEY_RIGHT_PRIMARY, elif key in (settings.KEY_RIGHT_PRIMARY,
settings.KEY_RIGHT_SECONDARY): settings.KEY_RIGHT_SECONDARY):
return KeyValues.RIGHT, key return KeyValues.RIGHT
elif key in (settings.KEY_UP_PRIMARY, elif key in (settings.KEY_UP_PRIMARY,
settings.KEY_UP_SECONDARY): settings.KEY_UP_SECONDARY):
return KeyValues.UP, key return KeyValues.UP
elif key == settings.KEY_ENTER: elif key == settings.KEY_ENTER:
return KeyValues.ENTER, key return KeyValues.ENTER
elif key == ' ': elif key == ' ':
return KeyValues.SPACE, key return KeyValues.SPACE
return None, key return None

View File

@ -54,7 +54,7 @@ class Game:
self.display_refresh() self.display_refresh()
key = screen.getkey() key = screen.getkey()
self.handle_key_pressed( self.handle_key_pressed(
*KeyValues.translate_key(key, self.settings)) KeyValues.translate_key(key, self.settings), key)
def handle_key_pressed(self, key: Optional[KeyValues], raw_key: str = '')\ def handle_key_pressed(self, key: Optional[KeyValues], raw_key: str = '')\
-> None: -> None:

View File

@ -40,35 +40,35 @@ class TestGame(unittest.TestCase):
self.game.settings = Settings() self.game.settings = Settings()
self.assertEqual(KeyValues.translate_key( self.assertEqual(KeyValues.translate_key(
self.game.settings.KEY_UP_PRIMARY, self.game.settings)[0], self.game.settings.KEY_UP_PRIMARY, self.game.settings),
KeyValues.UP) KeyValues.UP)
self.assertEqual(KeyValues.translate_key( self.assertEqual(KeyValues.translate_key(
self.game.settings.KEY_UP_SECONDARY, self.game.settings)[0], self.game.settings.KEY_UP_SECONDARY, self.game.settings),
KeyValues.UP) KeyValues.UP)
self.assertEqual(KeyValues.translate_key( self.assertEqual(KeyValues.translate_key(
self.game.settings.KEY_DOWN_PRIMARY, self.game.settings)[0], self.game.settings.KEY_DOWN_PRIMARY, self.game.settings),
KeyValues.DOWN) KeyValues.DOWN)
self.assertEqual(KeyValues.translate_key( self.assertEqual(KeyValues.translate_key(
self.game.settings.KEY_DOWN_SECONDARY, self.game.settings)[0], self.game.settings.KEY_DOWN_SECONDARY, self.game.settings),
KeyValues.DOWN) KeyValues.DOWN)
self.assertEqual(KeyValues.translate_key( self.assertEqual(KeyValues.translate_key(
self.game.settings.KEY_LEFT_PRIMARY, self.game.settings)[0], self.game.settings.KEY_LEFT_PRIMARY, self.game.settings),
KeyValues.LEFT) KeyValues.LEFT)
self.assertEqual(KeyValues.translate_key( self.assertEqual(KeyValues.translate_key(
self.game.settings.KEY_LEFT_SECONDARY, self.game.settings)[0], self.game.settings.KEY_LEFT_SECONDARY, self.game.settings),
KeyValues.LEFT) KeyValues.LEFT)
self.assertEqual(KeyValues.translate_key( self.assertEqual(KeyValues.translate_key(
self.game.settings.KEY_RIGHT_PRIMARY, self.game.settings)[0], self.game.settings.KEY_RIGHT_PRIMARY, self.game.settings),
KeyValues.RIGHT) KeyValues.RIGHT)
self.assertEqual(KeyValues.translate_key( self.assertEqual(KeyValues.translate_key(
self.game.settings.KEY_RIGHT_SECONDARY, self.game.settings)[0], self.game.settings.KEY_RIGHT_SECONDARY, self.game.settings),
KeyValues.RIGHT) KeyValues.RIGHT)
self.assertEqual(KeyValues.translate_key( self.assertEqual(KeyValues.translate_key(
self.game.settings.KEY_ENTER, self.game.settings)[0], self.game.settings.KEY_ENTER, self.game.settings),
KeyValues.ENTER) KeyValues.ENTER)
self.assertEqual(KeyValues.translate_key(' ', self.game.settings)[0], self.assertEqual(KeyValues.translate_key(' ', self.game.settings),
KeyValues.SPACE) KeyValues.SPACE)
self.assertEqual(KeyValues.translate_key('plop', self.game.settings)[0], self.assertEqual(KeyValues.translate_key('plop', self.game.settings),
None) None)
def test_key_press(self) -> None: def test_key_press(self) -> None: