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

View File

@ -54,7 +54,7 @@ class Game:
self.display_refresh()
key = screen.getkey()
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 = '')\
-> None:

View File

@ -40,35 +40,35 @@ class TestGame(unittest.TestCase):
self.game.settings = Settings()
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)
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)
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)
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)
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)
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)
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)
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)
self.assertEqual(KeyValues.translate_key(
self.game.settings.KEY_ENTER, self.game.settings)[0],
self.game.settings.KEY_ENTER, self.game.settings),
KeyValues.ENTER)
self.assertEqual(KeyValues.translate_key(' ', self.game.settings)[0],
self.assertEqual(KeyValues.translate_key(' ', self.game.settings),
KeyValues.SPACE)
self.assertEqual(KeyValues.translate_key('plop', self.game.settings)[0],
self.assertEqual(KeyValues.translate_key('plop', self.game.settings),
None)
def test_key_press(self) -> None: