Quellcode durchsuchen

RectangleGroup added to groups and rewrited SquareGroup.

mdo vor 1 Jahr
Ursprung
Commit
6c7b2414c4
3 geänderte Dateien mit 62 neuen und 16 gelöschten Zeilen
  1. 46 15
      jogai/group.py
  2. 1 1
      scripts/basics.py
  3. 15 0
      tests/test_group.py

+ 46 - 15
jogai/group.py

@@ -78,7 +78,51 @@ class RandomGroup(Group):
         super().__init__(characters_list)
 
 
-class SquareGroup(Group):
+class RectangleGroup(Group):
+    """
+    Group of characters forming a rectangle.
+
+    Examples:
+        >>> ch = Character('protagonist.png')
+        >>> rg = RectangleGroup(ch, 5, 3)
+    """
+
+    def __init__(self, model: Type['Character'], base: int, height: int):
+        # TODO: check model type
+        if not isinstance(base, int) or not isinstance(height, int):
+            raise TypeError(_('Both the base and rectangle must be integers.'))
+        if base < 1 or height < 1:
+            raise ValueError(
+                _('Both the base and rectangle must be positive.')
+            )
+        if base == 1 and height == 1:
+            raise ValueError(
+                _(
+                    'At least one of the dimensions of the rectangle must be greater than 1.'
+                )
+            )
+        self.base = base
+        self.height = height
+        self.amount = base * height
+
+        characters_list = []
+        for row in range(self.height):
+            for col in range(self.base):
+                if not characters_list:
+                    new_character = model
+                else:
+                    new_character = model.copy()
+                new_character.set_position(
+                    (
+                        model.x + model.width * col,
+                        model.y + model.height * row,
+                    )
+                )
+                characters_list.append(new_character)
+        super().__init__(characters_list)
+
+
+class SquareGroup(RectangleGroup):
     """
     Group of characters forming a square.
 
@@ -96,20 +140,7 @@ class SquareGroup(Group):
                 _('The side of the square must be greater than 1.')
             )
         self.side = side
-        self.amount = side**2
-
-        characters_list = []
-        for pos in range(self.amount):
-            new_character = model.copy()
-            new_character.set_position(
-                (
-                    model.width * (pos // self.side),
-                    model.height * (pos % self.side),
-                )
-            )
-            characters_list.append(new_character)
-
-        super().__init__(characters_list)
+        super().__init__(model, side, side)
 
 
 class StairsGroup(Group):

+ 1 - 1
scripts/basics.py

@@ -6,7 +6,7 @@ def test_example01():
         ...
 
     prepare('02.yaml')
-    sq = StairsGroup(brick, 3, False, False)
+    sq = SquareGroup(brick, 23)
     add_group(sq)
 
     while is_running():

+ 15 - 0
tests/test_group.py

@@ -32,6 +32,21 @@ def test_random_group_with_lists(prepare_videogame):
     RandomGroup(protagonist, 3, [0, 200], [0, 200])
 
 
+def test_rectangle_group_with_wrong_sides_type(prepare_videogame):
+    with pytest.raises(TypeError):
+        RectangleGroup(protagonist, 'foo', 'bar')
+
+
+def test_rectangle_group_with_sides_out_of_range(prepare_videogame):
+    with pytest.raises(ValueError):
+        RectangleGroup(protagonist, -1, 3)
+
+
+def test_rectangle_group_with_both_sides_equal_to_1(prepare_videogame):
+    with pytest.raises(ValueError):
+        RectangleGroup(protagonist, 1, 1)
+
+
 def test_square_group_with_wrong_side_type(prepare_videogame):
     with pytest.raises(TypeError):
         SquareGroup(protagonist, 'foo')