瀏覽代碼

passage du monde en POO

Hvrnbi 3 周之前
父節點
當前提交
a34e83da53
共有 2 個文件被更改,包括 135 次插入 和 126 次删除
  1. 5 3
      main.py
  2. 130 123
      world.py

+ 5 - 3
main.py

@@ -13,7 +13,10 @@ def main():
     display.fill(BACKGROUND_COLOR)
 
     pygame.display.set_caption("VISI301")   # Nom de la fenêtre, à changer à la fin TODO
-    create_world(display, NB_COL, NB_ROW, GAP)
+
+    # Création du monde
+    world = World(display, NB_COL, NB_ROW, GAP)
+    world.draw()
 
     ### Boucle principale ###
     while True:
@@ -29,9 +32,8 @@ def main():
             if event.type == QUIT:
                 pygame.quit()
                 sys.exit()
-            pygame.display.update()
 
-        pygame.display.flip()
+            pygame.display.update()
 
     # On quitte pygame
     pygame.quit()

+ 130 - 123
world.py

@@ -2,139 +2,146 @@ from tiles import *
 from variables import *
 
 
-def create_world(display: pygame.display, nb_col: int, nb_row: int, gap: int) -> list:
-    """Crée un monde avec le nombre de colonnes et de lignes données, et l'espacement donné entre les tuiles"""
-    tile_width, tile_height = tile_size(nb_col, nb_row, gap)
-
-    pos_list = position_of_tiles(nb_col, nb_row, gap, tile_height, tile_width, (WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2))
-
-    for pos in pos_list:
-        tile = Tile(pos["x"] - tile_width // 2, pos["y"] - tile_height // 2, tile_width, tile_height, "green", display)
-        tile.draw()
-
-
-def tile_size(nb_col: int, nb_row: int, gap: int) -> tuple:
-    """Renvoie un tuple contenant la largeur et la hauteur d'une tuile en fonction du nombre de lignes, de colonnes, et de l'espacement entre le tuiles"""
-    width = (WINDOW_WIDTH - nb_col * gap) // nb_col
-    height = (WINDOW_HEIGHT - nb_row * gap) // nb_row
-
-    # On cale le plus grand sur le plus petit pour conserver la forme des tuiles et ne pas dépasser d'un côté
-    if height < 0.86 * width:
-        width = height // 0.86
-    else:
-        height = round(width * 0.86)
-
-    return (width, height)
-
-
-def position_of_tiles(nb_col: int, nb_row: int, gap: int, tile_height: int, tile_width: int, center: tuple) -> list:
-    """Renvoie la position du centre de toutes les tuiles du monde en fonction des paramètres donnés"""
-    # Le nombre de colonnes doit être impair
-    if nb_col % 2 != 0:
-        nb_col += 1
-
-    res = []
-    x = center[0]
-
-    # Si le nombre de lignes est pair
-    if nb_row % 2 == 0:
-        # Colonne centrale
-        res += position_of_odd_columns(nb_row - 1, gap, tile_height, x)
-        # Colonnes paires de droite
-        x += (tile_width * 3) // 4 + gap
-        res += position_of_even_columns(nb_row, gap, tile_height, x)
-        for i in range(nb_col // 4 - 1):
-            x += (tile_width * 3) // 2 + 2 * gap
-            res += position_of_even_columns(nb_row, gap, tile_height, x)
-
-        # Colonnes paires de gauche
-        x = center[0] - (tile_width * 3) // 4 - gap
-        res += position_of_even_columns(nb_row, gap, tile_height, x)
-        for i in range(nb_col // 4 - 1):
-            x -= (tile_width * 3) // 2 + 2 * gap
-            res += position_of_even_columns(nb_row, gap, tile_height, x)
-
-        # Colonnes impaires de droite
-        x = center[0]
-        for i in range(nb_col // 4):
-            x += (tile_width * 3) // 2 + 2 * gap
-            res += position_of_odd_columns(nb_row - 1, gap, tile_height, x)
-
-        # Colonnes impaires de gauche
-        x = center[0]
-        for i in range(nb_col // 4):
-            x -= (tile_width * 3) // 2 + 2 * gap
-            res += position_of_odd_columns(nb_row - 1, gap, tile_height, x)
-
-    # Si le nombre de lignes est impair
-    else:
-        # Colonne centrale
-        res += position_of_odd_columns(nb_row, gap, tile_height, x)
-        # Colonnes paires de droite
-        x += (tile_width * 3) // 4 + gap
-        res += position_of_even_columns(nb_row - 1, gap, tile_height, x)
-        for i in range(nb_col // 4 - 1):
-            x += (tile_width * 3) // 2 + 2 * gap
-            res += position_of_even_columns(nb_row - 1, gap, tile_height, x)
-
-        # Colonnes paires de gauche
-        x = center[0] - (tile_width * 3) // 4 - gap
-        res += position_of_even_columns(nb_row - 1, gap, tile_height, x)
-        for i in range(nb_col // 4 - 1):
-            x -= (tile_width * 3) // 2 + 2 * gap
-            res += position_of_even_columns(nb_row - 1, gap, tile_height, x)
-
-        # Colonnes impaires de droite
-        x = center[0]
-        for i in range(nb_col // 4 - 1):
-            x += (tile_width * 3) // 2 + 2 * gap
-            res += position_of_odd_columns(nb_row, gap, tile_height, x)
-
-        # Colonnes impaires de gauche
-        x = center[0]
-        for i in range(nb_col // 4 - 1):
-            x -= (tile_width * 3) // 2 + 2 * gap
-            res += position_of_odd_columns(nb_row, gap, tile_height, x)
+class World:
+    def __init__(self, display: pygame.display, nb_col: int, nb_row: int, gap: int):
+        """Un monde avec le nombre de colonnes et de lignes données, et l'espacement donné entre les tuiles"""
+        self.display = display
+        self.nb_col = nb_col
+        self.nb_row = nb_row
+        self.gap = gap
+        self.tile_width, self.tile_height = self.tile_size()
+        self.pos_list = self.position_of_tiles((WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2))
 
-    return res
+    
+    def draw(self):
+        """Dessine les tuiles du monde"""
+        for pos in self.pos_list:
+            tile = Tile(pos["x"] - self.tile_width // 2, pos["y"] - self.tile_height // 2, self.tile_width, self.tile_height, "green", self.display)
+            tile.draw()
 
 
-def position_of_odd_columns(nb_row: int, gap: int, tile_height: int, x: int) -> list:
-    """Renvoie la position des tuiles dans une colonne au nombre de tuiles impair"""
-    y = WINDOW_HEIGHT // 2
+    def tile_size(self) -> tuple:
+        """Renvoie un tuple contenant la largeur et la hauteur d'une tuile en fonction du nombre de lignes, de colonnes, et de l'espacement entre le tuiles"""
+        width = (WINDOW_WIDTH - self.nb_col * self.gap) // self.nb_col
+        height = (WINDOW_HEIGHT - self.nb_row * self.gap) // self.nb_row
 
-    # Tuile du milieu
-    res = [{"x": x, "y": y}]
+        # On cale le plus grand sur le plus petit pour conserver la forme des tuiles et ne pas dépasser d'un côté
+        if height < 0.86 * width:
+            width = height // 0.86
+        else:
+            height = round(width * 0.86)
 
-    # Tuiles en dessous du milieu
-    for i in range(nb_row // 2):
-        y += tile_height + gap
-        res.append({"x": x, "y": y})
-    
-    y = WINDOW_HEIGHT // 2
-    # Tuiles en dessous du milieu
-    for i in range(nb_row // 2):
-        y -= tile_height + gap
-        res.append({"x": x, "y": y})
+        return (width, height)
 
-    return res
 
+    def position_of_tiles(self, center: tuple) -> list:
+        """Renvoie la position du centre de toutes les tuiles du monde en fonction des paramètres donnés"""
+        # Le nombre de colonnes doit être impair
+        if self.nb_col % 2 != 0:
+            self.nb_col += 1
 
-def position_of_even_columns(nb_row: int, gap: int, tile_height: int, x: int) -> list:
-    res = []
+        res = []
+        x = center[0]
 
-    # Tuiles du bas
-    y = WINDOW_HEIGHT // 2 + gap // 2 + tile_height // 2
-    res.append({"x": x, "y": y})
-    for i in range(nb_row // 2 - 1):
-        y += tile_height + gap
+        # Si le nombre de lignes est pair
+        if self.nb_row % 2 == 0:
+            # Colonne centrale
+            res += self.position_of_odd_columns(self.nb_row - 1, x)
+            # Colonnes paires de droite
+            x += (self.tile_width * 3) // 4 + self.gap
+            res += self.position_of_even_columns(self.nb_row, x)
+            for i in range(self.nb_col // 4 - 1):
+                x += (self.tile_width * 3) // 2 + 2 * self.gap
+                res += self.position_of_even_columns(self.nb_row, x)
+
+            # Colonnes paires de gauche
+            x = center[0] - (self.tile_width * 3) // 4 - self.gap
+            res += self.position_of_even_columns(self.nb_row, x)
+            for i in range(self.nb_col // 4 - 1):
+                x -= (self.tile_width * 3) // 2 + 2 * self.gap
+                res += self.position_of_even_columns(self.nb_row, x)
+
+            # Colonnes impaires de droite
+            x = center[0]
+            for i in range(self.nb_col // 4):
+                x += (self.tile_width * 3) // 2 + 2 * self.gap
+                res += self.position_of_odd_columns(self.nb_row - 1, x)
+
+            # Colonnes impaires de gauche
+            x = center[0]
+            for i in range(self.nb_col // 4):
+                x -= (self.tile_width * 3) // 2 + 2 * self.gap
+                res += self.position_of_odd_columns(self.nb_row - 1, x)
+
+        # Si le nombre de lignes est impair
+        else:
+            # Colonne centrale
+            res += self.position_of_odd_columns(self.nb_row, x)
+            # Colonnes paires de droite
+            x += (self.tile_width * 3) // 4 + self.gap
+            res += self.position_of_even_columns(self.nb_row - 1, x)
+            for i in range(self.nb_col // 4 - 1):
+                x += (self.tile_width * 3) // 2 + 2 * self.gap
+                res += self.position_of_even_columns(self.nb_row - 1, x)
+
+            # Colonnes paires de gauche
+            x = center[0] - (self.tile_width * 3) // 4 - self.gap
+            res += self.position_of_even_columns(self.nb_row - 1, x)
+            for i in range(self.nb_col // 4 - 1):
+                x -= (self.tile_width * 3) // 2 + 2 * self.gap
+                res += self.position_of_even_columns(self.nb_row - 1, x)
+
+            # Colonnes impaires de droite
+            x = center[0]
+            for i in range(self.nb_col // 4 - 1):
+                x += (self.tile_width * 3) // 2 + 2 * self.gap
+                res += self.position_of_odd_columns(self.nb_row, x)
+
+            # Colonnes impaires de gauche
+            x = center[0]
+            for i in range(self.nb_col // 4 - 1):
+                x -= (self.tile_width * 3) // 2 + 2 * self.gap
+                res += self.position_of_odd_columns(self.nb_row, x)
+
+        return res
+
+
+    def position_of_odd_columns(self, nb_row: int, x: int) -> list:
+        """Renvoie la position des tuiles dans une colonne au nombre de tuiles impair"""
+        y = WINDOW_HEIGHT // 2
+
+        # Tuile du milieu
+        res = [{"x": x, "y": y}]
+
+        # Tuiles en dessous du milieu
+        for i in range(nb_row // 2):
+            y += self.tile_height + self.gap
+            res.append({"x": x, "y": y})
+        
+        y = WINDOW_HEIGHT // 2
+        # Tuiles en dessous du milieu
+        for i in range(nb_row // 2):
+            y -= self.tile_height + self.gap
+            res.append({"x": x, "y": y})
+
+        return res
+
+
+    def position_of_even_columns(self, nb_row: int, x: int) -> list:
+        res = []
+
+        # Tuiles du bas
+        y = WINDOW_HEIGHT // 2 + self.gap // 2 + self.tile_height // 2
         res.append({"x": x, "y": y})
+        for i in range(nb_row // 2 - 1):
+            y += self.tile_height + self.gap
+            res.append({"x": x, "y": y})
 
-    # Tuiles du haut
-    y = WINDOW_HEIGHT // 2 - gap // 2 - tile_height // 2
-    res.append({"x": x, "y": y})
-    for i in range(nb_row // 2 - 1):
-        y -= tile_height + gap
+        # Tuiles du haut
+        y = WINDOW_HEIGHT // 2 - self.gap // 2 - self.tile_height // 2
         res.append({"x": x, "y": y})
+        for i in range(nb_row // 2 - 1):
+            y -= self.tile_height + self.gap
+            res.append({"x": x, "y": y})
 
-    return res
+        return res