Răsfoiți Sursa

set_position() added to Scene().

mdo 1 an în urmă
părinte
comite
ae7ed2235c
6 a modificat fișierele cu 29 adăugiri și 7 ștergeri
  1. 1 0
      data/02.yaml
  2. BIN
      images/scenes/test_large.png
  3. 4 4
      jogai/character.py
  4. 23 0
      jogai/scene.py
  5. 1 1
      jogai/videogame.py
  6. 0 2
      scripts/basics.py

+ 1 - 0
data/02.yaml

@@ -1,3 +1,4 @@
+scene: test_large.png
 characters:
   protagonist:
     speed: 5

BIN
images/scenes/test_large.png


+ 4 - 4
jogai/character.py

@@ -24,10 +24,10 @@ class Character(pygame.sprite.Sprite):
 
     _predefined_attributes_allowed = [
         _('height'),
+        _('keys'),
         _('position'),
         _('speed'),
         _('width'),
-        _('keys'),
     ]
 
     def __init__(
@@ -322,7 +322,7 @@ class Character(pygame.sprite.Sprite):
         Only declarated actions in _predefined_key_actions_allowed are allowed.
 
         Parameters:
-            predefined_keys: the predefined keys with the associated action.
+            predefined_keys: The predefined keys with the associated action.
 
         Examples:
             >>> ch = Character('protagonist.png')
@@ -343,7 +343,7 @@ class Character(pygame.sprite.Sprite):
         Set the character position as tuple. To use as separate coordinates call move_to().
 
         Parameters:
-            position: a tuple with abscissa and ordinate coordinates
+            position: A tuple with abscissa and ordinate coordinates
             key: Optional key to perform the action
 
         Examples:
@@ -363,7 +363,7 @@ class Character(pygame.sprite.Sprite):
         Set the character speed.
 
         Parameters:
-            speed: the character speed
+            speed: The character speed
 
         Examples:
             >>> ch = Character('protagonist.png')

+ 23 - 0
jogai/scene.py

@@ -73,3 +73,26 @@ class Scene:
             settings.SCENES_DIR, filename
         )
         self.filename = filename
+
+    def set_position(self, x: int):
+        """
+        Set the scene abscissa position.
+
+        Parameters:
+            x: coordinate in horizontal axis
+
+        Examples:
+            >>> sc = Scene('test_large.png')
+            >>> sc.set_position(300)
+        """
+        if not isinstance(x, int):
+            raise TypeError(_('The x coordinate must be an integer.'))
+
+        if not (0 <= x <= self._rect.width - settings.WINDOW_WIDTH):
+            raise ValueError(
+                _(
+                    'The range of x coordinate is between 0 and the scene width.'
+                )
+            )
+
+        self._rect.x = -x

+ 1 - 1
jogai/videogame.py

@@ -209,7 +209,7 @@ def prepare(data_filename: str = ''):
     vg_scene = _load_scene(vg_data)
     vg_characters = _load_characters(vg_data)
     # TODO: Check and filter variable names added to builtins
-    setattr(builtins, 'vg_scene', vg_scene)
+    setattr(builtins, 'vgscene', vg_scene)
     for vg_character in vg_characters:
         setattr(builtins, vg_character.name, vg_character)
 

+ 0 - 2
scripts/basics.py

@@ -7,7 +7,5 @@ def test_example01():
 
     prepare('02.yaml')
 
-    args = utils._convert_function_arguments_to_string({'x': 200, 'y': 400})
-
     while is_running():
         update()