Эх сурвалжийг харах

gravity added to videogame and also set_gravitative() added to character.

Zero! Studio 1 жил өмнө
parent
commit
907af64e1d

+ 3 - 0
data/02.yaml

@@ -1,6 +1,9 @@
+attributes:
+  gravity: 5
 scene: test_large.png
 characters:
   protagonist:
+    gravitative: True
     speed: 20
     width: 50
     position: [465, 300]

+ 4 - 2
jogai/_globals.py

@@ -14,8 +14,10 @@ _events = []
 _keys = []
 _window = None
 
+_gravity = 0
+
+marks = None
+mark_font = None
 vg_scene = None
 vg_characters = []
 vg_groups = []
-marks = None
-mark_font = None

+ 16 - 2
jogai/character.py

@@ -42,6 +42,7 @@ class Character(pygame.sprite.Sprite):
 
     _predefined_attributes_allowed = [
         _('collidable'),
+        _('gravitative'),
         _('height'),
         _('keys'),
         _('position'),
@@ -69,6 +70,7 @@ class Character(pygame.sprite.Sprite):
         self.name = name
         self.x = 0
         self.y = 0
+        self.gravitative = False
         self.is_jumping = False
         self.jump_force = settings.DEFAULT_JUMP_FORCE
         self._jump_counter = self.jump_force
@@ -500,9 +502,19 @@ class Character(pygame.sprite.Sprite):
         """
         if not isinstance(collidable, bool):
             raise TypeError(_('The collidable argument must be a boolean.'))
-
         self.collidable = collidable
 
+    def set_gravitative(self, gravitative: bool = True):
+        """
+        Set the character gravitative.
+
+        Parameters:
+            gravitative: True if it is collidable, False otherwise.
+        """
+        if not isinstance(gravitative, bool):
+            raise TypeError(_('The gravitative argument must be a boolean.'))
+        self.gravitative = gravitative
+
     def set_height(self, height: int):
         """
         Set the character height.
@@ -690,6 +702,7 @@ class Character(pygame.sprite.Sprite):
         """
         new_character = Character(self.image_filename)
         new_character.set_collidable(self.collidable)
+        new_character.set_gravitative(self.gravitative)
         new_character.set_height(self.height)
         new_character.set_width(self.width)
         new_character.set_position((self.x, self.y))
@@ -758,5 +771,6 @@ class Character(pygame.sprite.Sprite):
     def update(self):
         for action, key in self.predefined_keys.items():
             eval(f'self.{action}(key="{key}")')
-
+        if self.gravitative:
+            self.down(_globals._gravity)
         self._update_jump()

+ 40 - 6
jogai/videogame.py

@@ -42,6 +42,23 @@ def _load_scene(data: dict) -> 'scene.Scene':
     return scene.Scene()
 
 
+def _load_attributes(data: dict):
+    """
+    Load attributes from data schema.
+
+    Parameters:
+        data: input schema
+    """
+    if _('attributes') in data:
+        attributes = data[_('attributes')]
+        if not isinstance(attributes, dict):
+            raise TypeError(
+                _('The attributes in the input schema must be a dict.')
+            )
+        if _('gravity') in attributes:
+            set_gravity(attributes[_('gravity')])
+
+
 def _load_characters(data: dict) -> list:
     """
     Load characters from data schema.
@@ -235,13 +252,28 @@ def paint_group(group: Type['group.SquareGroup']):
     group.draw(_globals._window)
 
 
+def set_gravity(gravity: int):
+    """
+    Set the gravity in the videogame.
+
+    Parameters:
+        gravity: Gravity force to be setted.
+
+    Examples:
+        >>> set_gravity(5)
+    """
+    if not isinstance(gravity, int):
+        raise TypeError(_('The gravity must be an integer.'))
+    _globals._gravity = gravity
+
+
 def set_marks(
-    x_gap=0,
-    y_gap=0,
-    x_color=(255, 180, 30),
-    y_color=(0, 0, 255),
-    width=1,
-    debug=False,
+    x_gap: int = 0,
+    y_gap: int = 0,
+    x_color: tuple = (255, 180, 30),
+    y_color: tuple = (0, 0, 255),
+    width: int = 1,
+    debug: bool = False,
 ):
     _globals.marks = utils.Marks(x_gap, y_gap, x_color, y_color, width, debug)
 
@@ -264,6 +296,7 @@ def prepare(data_filename: str = ''):
 
     _globals.vg_scene = _load_scene(vg_data)
     _globals.vg_characters = _load_characters(vg_data)
+    _globals.vg_attributes = _load_attributes(vg_data)
 
     if _globals.vg_characters:
         _globals.character_focus = _globals.vg_characters[0]
@@ -303,6 +336,7 @@ def update():
             for e in g.sprites():
                 e._update_rect_position()
                 paint(e)
+                e.update()
     if _globals.marks:
         _globals.marks.draw()
     pygame.display.flip()

+ 0 - 2
scripts/basics.py

@@ -10,5 +10,3 @@ def test_example01():
 
     while is_running():
         update()
-        if _get_keys()[eval_key('o')]:
-            protagonist.move_to(sq.sprites()[0].x, sq.sprites()[0].y)