소스 검색

speed changed to velocity (with axis x and y)

mdo 1 년 전
부모
커밋
d621a5c15c
2개의 변경된 파일54개의 추가작업 그리고 13개의 파일을 삭제
  1. 54 11
      jogai/character.py
  2. 0 2
      scripts/basics.py

+ 54 - 11
jogai/character.py

@@ -11,6 +11,14 @@ from jogai._globals import _window
 from jogai.translations import gettext as _
 
 
+class Velocity:
+    x = 0
+    y = 0
+
+    def __repr__(self):
+        return f'{self.x}, {self.y}'
+
+
 class Character(pygame.sprite.Sprite):
     """
     A videogame character with a bunch of actions.
@@ -43,7 +51,7 @@ class Character(pygame.sprite.Sprite):
             raise TypeError(_('The character name must be a string.'))
 
         self.scale_factor = 1.0
-        self.speed = 0
+        self.velocity = Velocity()
         self.predefined_keys = {}
         self.image_filename = image_filename
         self.image = None
@@ -85,13 +93,17 @@ class Character(pygame.sprite.Sprite):
                 eval(f'self.set_{attribute}(data["{attribute}"])')
 
     @property
-    def height(self):
+    def height(self) -> int:
         return self.rect.height
 
     @property
-    def width(self):
+    def width(self) -> int:
         return self.rect.width
 
+    @property
+    def speed(self) -> float:
+        return (abs(self.velocity.x) + abs(self.velocity.y)) / 2
+
     def _eval_predefined_function(
         self, function: str, arguments: str | list | dict
     ):
@@ -126,9 +138,9 @@ class Character(pygame.sprite.Sprite):
             >>> protagonist.move()
         """
         if x is None:
-            x = self.speed
+            x = self.velocity.x
         if y is None:
-            y = self.speed
+            y = self.velocity.y
 
         if not isinstance(x, int) or not isinstance(y, int):
             raise TypeError(_('Amount to move ​​must be an integer.'))
@@ -140,6 +152,12 @@ class Character(pygame.sprite.Sprite):
             if relative:
                 self.x += x
                 self.y += y
+                self.velocity.x = abs(self.velocity.x)
+                if x < 0:
+                    self.velocity.x *= -1
+                self.velocity.y = abs(self.velocity.y)
+                if y < 0:
+                    self.velocity.y *= -1
             else:
                 self.x = x
                 self.y = y
@@ -173,7 +191,11 @@ class Character(pygame.sprite.Sprite):
             >>> protagonist.backward()
         """
         if amount is None:
-            amount = self.speed
+            amount = abs(self.velocity.x)
+
+        if not isinstance(amount, int):
+            raise TypeError(_('The amount to go up must be an integer.'))
+
         self.move(-amount, 0, key)
 
     def down(self, amount: int | None = None, key: str = ''):
@@ -190,7 +212,10 @@ class Character(pygame.sprite.Sprite):
             >>> protagonist.down()
         """
         if amount is None:
-            amount = self.speed
+            amount = abs(self.velocity.y)
+
+        if not isinstance(amount, int):
+            raise TypeError(_('The amount to go down must be an integer.'))
 
         if not self.is_jumping:
             self.move(0, amount, key)
@@ -209,7 +234,11 @@ class Character(pygame.sprite.Sprite):
             >>> protagonist.forward()
         """
         if amount is None:
-            amount = self.speed
+            amount = abs(self.velocity.x)
+
+        if not isinstance(amount, int):
+            raise TypeError(_('The amount to go forward must be an integer.'))
+
         self.move(amount, 0, key)
 
     def up(self, amount: int | None = None, key: str = ''):
@@ -226,7 +255,10 @@ class Character(pygame.sprite.Sprite):
             >>> protagonist.up()
         """
         if amount is None:
-            amount = self.speed
+            amount = abs(self.velocity.y)
+
+        if not isinstance(amount, int):
+            raise TypeError(_('The amount to go up must be an integer.'))
 
         if not self.is_jumping:
             self.move(0, -amount, key)
@@ -265,6 +297,7 @@ class Character(pygame.sprite.Sprite):
             not key or _get_keys()[utils.eval_key(key)]
         ):
             self.is_jumping = True
+            self.velocity.y = -abs(self.velocity.y)
 
     def rotate(self, increment: int = 1):
         """
@@ -485,8 +518,10 @@ class Character(pygame.sprite.Sprite):
             >>> protagonist = Character('protagonist.png')
             >>> protagonist.set_speed(10)
         """
-        if isinstance(speed, int):
-            self.speed = speed
+        if not isinstance(speed, int):
+            raise TypeError(_('The speed must be an integer.'))
+        self.velocity.x = speed
+        self.velocity.y = speed
 
     def set_width(self, width: int):
         """
@@ -560,6 +595,12 @@ class Character(pygame.sprite.Sprite):
                     return True
         return False
 
+    def collide(
+        self,
+        other_characters: Type['Character'] | Type['jogai.group.Group'] | list,
+    ):
+        ...
+
     def load(self, filename: str):
         """
         >>> protagonist = Character('protagonist.png')
@@ -605,6 +646,8 @@ class Character(pygame.sprite.Sprite):
         Calculate and update the character position during the jump.
         """
         if self.is_jumping:
+            if self._jump_counter == 0:
+                self.velocity.y = abs(self.velocity.y)
             if self._jump_counter >= -self.jump_force:
                 self.move(
                     0,

+ 0 - 2
scripts/basics.py

@@ -11,5 +11,3 @@ def test_example01():
 
     while is_running():
         update()
-        if protagonist.touch(sq):
-            secondary.flip(True, False)