Explorar o código

Commit initial : Base de la structure du code, génération de tuile automatisée et gitignore

Hvrnbi hai 3 semanas
achega
5e69b7dc54
Modificáronse 7 ficheiros con 231 adicións e 0 borrados
  1. 3 0
      .gitignore
  2. 7 0
      REAMDE.md
  3. BIN=BIN
      images/hexa.png
  4. 41 0
      main.py
  5. 20 0
      tiles.py
  6. 20 0
      variables.py
  7. 140 0
      world.py

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+.venv/
+
+__pycache__/

+ 7 - 0
REAMDE.md

@@ -0,0 +1,7 @@
+# Projet VISI301 - Jeu avec pygame
+
+Dans notre super jeu dont le nom est encore à définir, vous incarnez un couple poule / coq qui doit s'échapper de son poulailler.
+
+Nourissez-vous, faites des poussins, protégez les des renards et, le moment venu, encerclez le fermier pour lui voler la clé !
+
+### Ce jeu est en développement actif, certaines fonctionnalités ne sont pas encore prêtes.

BIN=BIN
images/hexa.png


+ 41 - 0
main.py

@@ -0,0 +1,41 @@
+import pygame, sys
+from pygame.locals import * # Import des constantes de pygame
+from world import *
+from variables import *
+
+def main():
+    """La fonction pricipale"""
+
+    ### Initialmisation ###
+    pygame.init()   # La fonction secrète, on sait pas ce qu'elle fait mais elle doit être là
+
+    display = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) # Taille de la fenêtre
+    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)
+
+    ### Boucle principale ###
+    while True:
+        
+        # À chaque tick on fait ce qu'il y a là
+        # create_world(display, NB_COL, NB_ROW, GAP)
+
+        for event in pygame.event.get():
+
+            # On fait des trucs
+            
+            # Fermeture du jeu
+            if event.type == QUIT:
+                pygame.quit()
+                sys.exit()
+            pygame.display.update()
+
+        pygame.display.flip()
+
+    # On quitte pygame
+    pygame.quit()
+
+
+# On lance la fonction principale
+main()

+ 20 - 0
tiles.py

@@ -0,0 +1,20 @@
+import pygame, os
+from variables import *
+
+
+class Tile:
+    def __init__(self, x: int, y: int, width: int, height: int, tiletype: str, display: pygame.display):
+        """A class for hexagonal tiles"""
+        self.x: int = x
+        self.y: int = y
+        self.width = width
+        self.height = height
+        self.tiletype: str = tiletype
+        self.display: pygame.display = display
+
+    def draw(self):
+        """Draw the tile"""
+        image = pygame.transform.scale(HEXA, (self.width, self.height))
+        self.display.blit(image, (self.x, self.y))
+        
+        

+ 20 - 0
variables.py

@@ -0,0 +1,20 @@
+from pygame import image
+from os import path
+
+# taille de la fenêtre
+WINDOW_WIDTH = 1920
+WINDOW_HEIGHT = 1080
+
+# Nombre de colonnes et de lignes sur la map
+NB_COL = 17
+NB_ROW = 12
+
+# Espacement entre les tuiles (en px)
+GAP = 8
+
+
+# images
+HEXA = image.load(path.join("images/hexa.png"))
+
+# couleurs
+BACKGROUND_COLOR = (24, 40, 69)

+ 140 - 0
world.py

@@ -0,0 +1,140 @@
+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)
+
+    return res
+
+
+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
+
+    # Tuile du milieu
+    res = [{"x": x, "y": y}]
+
+    # 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 res
+
+
+def position_of_even_columns(nb_row: int, gap: int, tile_height: int, x: int) -> list:
+    res = []
+
+    # 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
+        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
+        res.append({"x": x, "y": y})
+
+    return res