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

changed touch() in characters to detect collision sides.

mdo 1 éve
szülő
commit
d1be21bac3
4 módosított fájl, 27 hozzáadás és 25 törlés
  1. 9 9
      data/02.yaml
  2. 17 14
      jogai/character.py
  3. 0 1
      scripts/basics.py
  4. 1 1
      tests/test_character.py

+ 9 - 9
data/02.yaml

@@ -1,14 +1,14 @@
 scene: test_large.png
 characters:
   protagonist:
-    speed: 5
+    speed: 20
     width: 50
     position: [50, 300]
     keys:
-      forward: d
-      backward: a
-      up: w
-      down: s
+      forward: RIGHT
+      backward: LEFT
+      up: UP
+      down: DOWN
       increase: e
       decrease: q
       jump: SPACE
@@ -17,10 +17,10 @@ characters:
     width: 150
     position: [600, 200]
     keys:
-      forward: RIGHT
-      backward: LEFT
-      up: UP
-      down: DOWN
+      forward: d
+      backward: a
+      up: w
+      down: s
   brick:
     height: 40
     position: [600, 230]

+ 17 - 14
jogai/character.py

@@ -24,10 +24,11 @@ class Character(pygame.sprite.Sprite):
     A videogame character with a bunch of actions.
     """
 
-    TOP = _('top')
-    BOTTOM = _('bottom')
-    LEFT = _('left')
-    RIGHT = _('right')
+    TOP_COLLISION = _('top')
+    BOTTOM_COLLISION = _('bottom')
+    LEFT_COLLISION = _('left')
+    RIGHT_COLLISION = _('right')
+    STATIC_COLLISION = _('static')
 
     _predefined_key_actions_allowed = [
         _('backward'),
@@ -535,7 +536,7 @@ class Character(pygame.sprite.Sprite):
         self,
         other_characters: Type['Character'] | Type['jogai.group.Group'] | list,
         key: str = '',
-    ) -> bool:
+    ) -> str:
         """
         Detect if one character is touching another(s).
 
@@ -549,7 +550,7 @@ class Character(pygame.sprite.Sprite):
             >>> protagonist = Character('protagonist.png')
             >>> secondary = Character('secondary.png')
             >>> protagonist.touch(secondary)
-            True
+            'static'
         """
 
         if isinstance(other_characters, Character):
@@ -577,14 +578,16 @@ class Character(pygame.sprite.Sprite):
         for other_character in other_characters:
             if not key or _get_keys()[utils.eval_key(key)]:
                 if self.rect.colliderect(other_character.rect):
-                    return True
-        return False
-
-    def collide(
-        self,
-        other_characters: Type['Character'] | Type['jogai.group.Group'] | list,
-    ):
-        ...
+                    if self.velocity.y > 0:
+                        return self.TOP_COLLISION
+                    elif self.velocity.y < 0:
+                        return self.BOTTOM_COLLISION
+                    if self.velocity.x > 0:
+                        return self.LEFT_COLLISION
+                    elif self.velocity.x < 0:
+                        return self.RIGHT_COLLISION
+                    return self.STATIC_COLLISION
+        return ''
 
     def load(self, filename: str):
         """

+ 0 - 1
scripts/basics.py

@@ -11,4 +11,3 @@ def test_example01():
 
     while is_running():
         update()
-        print(protagonist.velocity)

+ 1 - 1
tests/test_character.py

@@ -154,7 +154,7 @@ def test_touch_without_sprites_colliding(prepare_videogame):
     protagonist.move_to(0, 0)
     secondary.move_to(5000, 500)
     vg.update()
-    assert protagonist.touch(secondary) == False
+    assert protagonist.touch(secondary) == ''
 
 
 def test_jump_with_force_as_integer(prepare_videogame):