Browse Source

Fixed bugs. Speed recovered.

mdo 1 năm trước cách đây
mục cha
commit
f46b24544a
2 tập tin đã thay đổi với 17 bổ sung33 xóa
  1. 16 33
      jogai/character.py
  2. 1 0
      scripts/basics.py

+ 16 - 33
jogai/character.py

@@ -24,6 +24,11 @@ class Character(pygame.sprite.Sprite):
     A videogame character with a bunch of actions.
     """
 
+    TOP = _('top')
+    BOTTOM = _('bottom')
+    LEFT = _('left')
+    RIGHT = _('right')
+
     _predefined_key_actions_allowed = [
         _('backward'),
         _('decrease'),
@@ -51,6 +56,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
@@ -100,10 +106,6 @@ class Character(pygame.sprite.Sprite):
     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
     ):
@@ -138,9 +140,9 @@ class Character(pygame.sprite.Sprite):
             >>> protagonist.move()
         """
         if x is None:
-            x = self.velocity.x
+            x = self.speed
         if y is None:
-            y = self.velocity.y
+            y = self.speed
 
         if not isinstance(x, int) or not isinstance(y, int):
             raise TypeError(_('Amount to move ​​must be an integer.'))
@@ -152,12 +154,8 @@ 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
+                self.velocity.x = x
+                self.velocity.y = y
             else:
                 self.x = x
                 self.y = y
@@ -191,10 +189,7 @@ class Character(pygame.sprite.Sprite):
             >>> protagonist.backward()
         """
         if amount is None:
-            amount = abs(self.velocity.x)
-
-        if not isinstance(amount, int):
-            raise TypeError(_('The amount to go up must be an integer.'))
+            amount = self.speed
 
         self.move(-amount, 0, key)
 
@@ -212,10 +207,7 @@ class Character(pygame.sprite.Sprite):
             >>> protagonist.down()
         """
         if amount is None:
-            amount = abs(self.velocity.y)
-
-        if not isinstance(amount, int):
-            raise TypeError(_('The amount to go down must be an integer.'))
+            amount = self.speed
 
         if not self.is_jumping:
             self.move(0, amount, key)
@@ -234,10 +226,7 @@ class Character(pygame.sprite.Sprite):
             >>> protagonist.forward()
         """
         if amount is None:
-            amount = abs(self.velocity.x)
-
-        if not isinstance(amount, int):
-            raise TypeError(_('The amount to go forward must be an integer.'))
+            amount = self.speed
 
         self.move(amount, 0, key)
 
@@ -255,10 +244,7 @@ class Character(pygame.sprite.Sprite):
             >>> protagonist.up()
         """
         if amount is None:
-            amount = abs(self.velocity.y)
-
-        if not isinstance(amount, int):
-            raise TypeError(_('The amount to go up must be an integer.'))
+            amount = self.speed
 
         if not self.is_jumping:
             self.move(0, -amount, key)
@@ -297,7 +283,6 @@ 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):
         """
@@ -520,8 +505,8 @@ class Character(pygame.sprite.Sprite):
         """
         if not isinstance(speed, int):
             raise TypeError(_('The speed must be an integer.'))
-        self.velocity.x = speed
-        self.velocity.y = speed
+        self.speed = speed
+        self.speed = speed
 
     def set_width(self, width: int):
         """
@@ -646,8 +631,6 @@ 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,

+ 1 - 0
scripts/basics.py

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