Forráskód Böngészése

key_pressed() added to videogame.

mdo 1 éve
szülő
commit
2a954fc9e0
2 módosított fájl, 20 hozzáadás és 2 törlés
  1. 19 1
      jogai/videogame.py
  2. 1 1
      scripts/basics.py

+ 19 - 1
jogai/videogame.py

@@ -70,7 +70,7 @@ def _load_characters(data: dict) -> list:
     return characters
 
 
-def simulate_key_press(key: [int, str], down: bool = True):
+def simulate_key_press(key: int | str, down: bool = True):
     """
     Post a key press simulation to pygame event module.
 
@@ -93,6 +93,24 @@ def simulate_key_press(key: [int, str], down: bool = True):
     pygame.event.post(key_event)
 
 
+def key_pressed(key: int | str) -> bool:
+    """
+    Check if a key has been pressed.
+
+    Parameters:
+        key: The key to be checked
+
+    Returns:
+        True if the key is pressed, False otherwise.
+
+    Examples:
+        >>> key_pressed('a')
+        False
+    """
+    key = utils.eval_key(key)
+    return _get_keys()[key]
+
+
 def add_character(new_character: 'character.Character'):
     """
     Add a new character to the videogame.

+ 1 - 1
scripts/basics.py

@@ -6,7 +6,7 @@ def test_example01():
         ...
 
     prepare('02.yaml')
-    sq = SquareGroup(brick, 3)
+    sq = StairsGroup(brick, 5)
     add_group(sq)
 
     while is_running():