Jelajahi Sumber

ajout de la disparition des fermiers ainsi que des vagues

louciane 3 hari lalu
induk
melakukan
49e81e0c3c
14 mengubah file dengan 190 tambahan dan 92 penghapusan
  1. 55 9
      farmer.py
  2. TEMPAT SAMPAH
      images/egg1.png
  3. TEMPAT SAMPAH
      images/egg2.png
  4. TEMPAT SAMPAH
      images/egg3.png
  5. TEMPAT SAMPAH
      images/egg4.png
  6. TEMPAT SAMPAH
      images/egg5.png
  7. TEMPAT SAMPAH
      images/nest.png
  8. TEMPAT SAMPAH
      images/piou-piou.png
  9. 15 15
      main.py
  10. 0 43
      nest.py
  11. 86 6
      player.py
  12. 13 11
      tiles.py
  13. 9 4
      variables.py
  14. 12 4
      world.py

+ 55 - 9
farmer.py

@@ -2,24 +2,52 @@ import pygame
 from tiles import *
 from variables import *
 from world import World
-from player import *
+
+def create_farmer(world, display):
+    """Crée un fermier sur une tuile au bord de la map"""
+    random_border_tile_example = world.random_border_tile(world.border_tiles(world.tiles_list))
+    farmer = Farmer(random_border_tile_example.x, random_border_tile_example.y, world, display)
+    return farmer
+
+def farmers_control(world, player, display):
+    """Permet de controler l'apparition des fermiers, et de moduler leur nombre, ainsi que le moment ou ils apparraissent.
+       On peut maintenant créer des vagues de fermiers en fonction du nombre de poussins"""
+    if player.piou_piou_number in [0, 1, 2]:
+        if world.egg == 4:
+            world.farmers.append(create_farmer(world, display))
+    elif player.piou_piou_number in [3, 4, 5]:
+        if world.egg == 1:
+            world.farmers.append(create_farmer(world, display))
+    elif player.piou_piou_number > 5:
+        if world.egg == 1:
+            world.farmers.append(create_farmer(world, display))
+        if world.egg == 4:
+            world.farmers.append(create_farmer(world, display))
+        if world.egg == 9:
+            world.farmers.append(create_farmer(world, display))
+
+
 
 class Farmer:
-    def __init__(self, x: int, y: int, world: World, player: Player, display: pygame.display):
+    def __init__(self, x: int, y: int, world: World, display: pygame.display):
         """Un fermier, avec ses coordonnées (x, y)"""
         self.display = display
         self.x: int = x
         self.y: int = y
         self.world: World = world
-        self.player = player
         self.neighbors_list: list = self.neighbors(self.x, self.y, self.world)
         self.origin: Tile = self.find_current_tile()
         self.destination: Tile = world.pos_list[0]
+        self.goal_done: bool = self.is_at_nest()
+        self.alive: bool = True
+        self.tuile_traversees = 0
 
     def draw(self):
         """Affiche le fermier"""
-        image = pygame.transform.scale(FARMER, (self.world.tile_width, self.world.tile_height))
-        self.display.blit(image, (self.x - self.world.tile_width // 2, self.y - self.world.tile_height // 2))  
+        if self.alive :
+            image = pygame.transform.scale(FARMER, (self.world.tile_width, self.world.tile_height))
+            self.display.blit(image, (self.x - self.world.tile_width // 2, self.y - self.world.tile_height // 2))  
+            self.tuile_traversees += 1
 
 
 
@@ -46,7 +74,7 @@ class Farmer:
         
         return res
 
-    def next_tile(self) -> tuple:
+    def next_tuile(self) -> tuple:
             """Prend la tuile à atteindre et renvoie les coordonnées de la tuile sur laquelle le fermier doit aller pour s'en rapprocher le plus"""
             res = (0, 0)
             x_nest = self.destination["x"]
@@ -81,10 +109,11 @@ class Farmer:
                 print("Aucune tuile trouvée")
 
     def is_at_nest(self) -> bool:
-        """Renvoie True si le fermier est au nidn False sinon"""
+        """Renvoie True si le fermier est au nid False sinon. Met automatiquement a jour "self.goal_done" """
         res = False
         if self.find_current_tile() == self.world.pos_list[0]:
             res = True
+            self.goal_done = True
         return res
 
     def is_at_origin(self) -> bool:
@@ -99,5 +128,22 @@ class Farmer:
         if self.is_at_nest() :
             self.destination = self.origin
         elif self.is_at_origin():
-            self.destination = {"x": self.x * 2, "y": self.y * 2}
-        
+            self.destination = self.world.pos_list[0]
+        
+
+    def way_to_follow(self) -> None:
+        """Renvoie les coordonées par lesquelles va passer le fermier sous forme d'une liste"""
+        return None
+
+    def disappear(self) -> None:
+        """Fais disparaitre le fermier si il a atteint le nid et si il est revenu à son point de départ"""
+        if self.is_at_origin() and self.goal_done :
+            self.alive = False
+        return None
+
+    def action(self) -> None:
+        self.change_destination()
+        self.x, self.y = self.next_tuile()
+        self.neighbors_list = self.neighbors(self.x, self.y, self.world)
+        self.find_current_tile()
+        self.disappear()

TEMPAT SAMPAH
images/egg1.png


TEMPAT SAMPAH
images/egg2.png


TEMPAT SAMPAH
images/egg3.png


TEMPAT SAMPAH
images/egg4.png


TEMPAT SAMPAH
images/egg5.png


TEMPAT SAMPAH
images/nest.png


TEMPAT SAMPAH
images/piou-piou.png


+ 15 - 15
main.py

@@ -3,9 +3,11 @@ from pygame.locals import * # Import des constantes de pygame
 from world import World
 from variables import *
 from player import *
-from nest import *
 from farmer import *
 
+
+# Fonctions
+
 def main():
     """La fonction pricipale"""
 
@@ -23,13 +25,7 @@ def main():
     world = World(display, NB_COL, NB_ROW, GAP)
 
     player = Player(world.pos_list[1]["x"], world.pos_list[1]["y"], world, display) # On initialise le joueur sur la première tuile en dessous du nid
-
-    nest = Nest(0, display)
-
-    random_border_tile_example = world.random_border_tile(world.border_tiles(world.tiles_list))
-    farmer = Farmer(random_border_tile_example.x, random_border_tile_example.y, world, player, display)
-
-    farmer.draw()
+    
 
     ### Boucle principale ###
     while True:
@@ -39,10 +35,13 @@ def main():
 
             # On gère les touches
             if event.type == pygame.KEYDOWN:
-                farmer.change_destination()
-                farmer.x, farmer.y = farmer.next_tile()
-                farmer.neighbors_list = farmer.neighbors(farmer.x, farmer.y, farmer.world)
-                farmer.find_current_tile()
+
+                # On gère la création et les actions des fermiers
+
+                farmers_control(world, player, display)
+
+                for farmer in world.farmers:
+                    farmer.action()
 
                 if event.key == pygame.K_z:
                     if player.moving:
@@ -68,10 +67,11 @@ def main():
         # À chaque tick on fait ce qu'il y a là
         display.fill(BACKGROUND_COLOR)
         world.draw()
-        nest.draw(world.pos_list[0], 0)
         player.draw()
         player.action()
-        farmer.draw()
+        for farmer in world.farmers:
+            farmer.draw()
+
         pygame.display.update()
         clock.tick(FPS)
 
@@ -81,4 +81,4 @@ def main():
 
 
 # On lance la fonction principale
-main()
+main()

+ 0 - 43
nest.py

@@ -1,43 +0,0 @@
-import pygame
-from tiles import *
-from variables import *
-
-
-class Nest:
-    def __init__(self, eggs: int, display: pygame.display):
-        """Un joueur, avec ses coordonnées (x, y)"""
-        self.display = display
-        self.eggs: int = eggs
-
-
-    def draw(self, tile: dict, eggs: int):
-        """Affiche le nid"""
-        image = pygame.transform.scale(NEST, (self.nest_size(), self.nest_size()))
-        tile_size = self.tile_size()
-        self.display.blit(image, (tile['x'] - tile_size // 3.7, tile['y'] - tile_size // 3.7))  #la valeur ici est arbitraire mais marche. elle est la pour centrer l'affichage du joueur sur une tuile, a changer dans le futur
-
-    def nest_size(self):
-        """Renvoie la taille que fera le joueur (sa hauteur et sa largeur sont similaires)"""
-        width = (WINDOW_WIDTH - NB_COL * GAP) // NB_COL
-        height = (WINDOW_HEIGHT - NB_ROW * GAP) // NB_ROW
-
-        #Réutilisation du calcul de la taille des tuilles pour calculer la taille du joueur
-        if height < 0.86 * width:
-            width = height // 0.86
-        else:
-            height = round(width * 0.86)
-        return int(width) // 1.8
-
-
-
-    def tile_size(self):
-        """Renvoie la taille d'une tuille                       CECI EST UNE FONCTION TEST QUI POURRAS SUREMENT ETRE ENLEVEE"""
-        width = (WINDOW_WIDTH - NB_COL * GAP) // NB_COL
-        height = (WINDOW_HEIGHT - NB_ROW * GAP) // NB_ROW
-
-        #Réutilisation du calcul de la taille des tuilles pour calculer la taille du joueur
-        if height < 0.86 * width:
-            width = height // 0.86
-        else:
-            height = round(width * 0.86)
-        return int(width)

+ 86 - 6
player.py

@@ -14,19 +14,25 @@ class Player:
         self.world: World = world
         self.next_move = "up"
         self.moving = True
-        self.egg = 0
         self.actions_till_next_farmer = 10
         self.last_tile = self.find_current_tile()
+        self.pos_history = [(self.x, self.y)]
+        self.piou_piou_number = 0
 
     def draw(self):
         """Affiche le joueur"""
         image = pygame.transform.scale(self.picture, (self.world.tile_width, self.world.tile_height))
         self.display.blit(image, (self.x - self.world.tile_width // 2, self.y - self.world.tile_height // 2))  #la valeur ici est arbitraire mais marche. elle est la pour centrer l'affichage du joueur sur une tuile, a changer dans le futur
-        # On dessine l'oeuf si besoin
+        # On dessine tous les poussins
+        for i in range(2, self.piou_piou_number + 2):
+            pioumage = pygame.transform.scale(PIOU_PIOU, (self.world.tile_width, self.world.tile_height))
+            self.display.blit(pioumage, (self.pos_history[-i][0] - self.world.tile_width // 2, self.pos_history[-i][1] - self.world.tile_height // 2))
         
     
     def move(self, direction: str):
         """Modifie les coordonnées x et y du joueur en fonction de la direction donnée"""
+        really_move = True
+
         if direction == "up":
             if self.y - self.world.gap - self.world.tile_height >= self.world.top_border:   # Il faut que ça soit supérieur parce que les coos descendent 
                 self.y -= self.world.gap + self.world.tile_height
@@ -35,6 +41,13 @@ class Player:
                     self.picture = PLAYER_LU
                 elif self.picture == PLAYER_RD:
                     self.picture = PLAYER_RU
+                # Si un poussin est déjà là
+                if self.piou_piou_number > 1:
+                    if (self.x, self.y) in self.pos_history[-self.piou_piou_number:]:
+                        really_move = False
+                        self.move("up")
+            else:
+                really_move = False
 
         elif direction == "down":
             if self.y + self.world.gap + self.world.tile_height <= self.world.bottom_border:    # Inversement ici
@@ -44,6 +57,13 @@ class Player:
                     self.picture = PLAYER_LD
                 elif self.picture == PLAYER_RU:
                     self.picture = PLAYER_RD
+                # Si un poussin est déjà là
+                if self.piou_piou_number > 1:
+                    if (self.x, self.y) in self.pos_history[-self.piou_piou_number:]:
+                        really_move = False
+                        self.move("down")
+            else:
+                really_move = False
 
         elif direction == "left":
             if self.x - self.world.gap - 3 * self.world.tile_width // 4 >= self.world.left_border:
@@ -53,9 +73,15 @@ class Player:
                         self.y -= self.world.gap // 2 + self.world.tile_height // 2
                         self.next_move = "down"
                         self.picture = PLAYER_LD
+                        # Si un poussin est déjà là
+                        if self.piou_piou_number > 1:
+                            if (self.x, self.y) in self.pos_history[-self.piou_piou_number:]:
+                                really_move = False
+                                self.move("left")
                     else:
                         self.next_move = "down"
                         self.move("left")
+                        really_move = False
 
                 elif self.next_move == "down":
                     if self.y + self.world.gap // 2 + self.world.tile_height // 2 <= self.world.bottom_border:
@@ -63,9 +89,17 @@ class Player:
                         self.y += self.world.gap // 2 + self.world.tile_height // 2
                         self.next_move = "up"
                         self.picture = PLAYER_LU
+                        # Si un poussin est déjà là
+                        if self.piou_piou_number > 1:
+                            if (self.x, self.y) in self.pos_history[-self.piou_piou_number:]:
+                                really_move = False
+                                self.move("left")
                     else:
                         self.next_move = "up"
                         self.move("left")
+                        really_move = False
+            else:
+                really_move = False
 
         elif direction == "right":
             if self.x + self.world.gap + 3 * self.world.tile_width // 4 <= self.world.right_border:
@@ -75,9 +109,15 @@ class Player:
                         self.y -= self.world.gap // 2 + self.world.tile_height // 2
                         self.next_move = "down"
                         self.picture = PLAYER_RD
+                        # Si un poussin est déjà là
+                        if self.piou_piou_number > 1:
+                            if (self.x, self.y) in self.pos_history[-self.piou_piou_number:]:
+                                really_move = False
+                                self.move("right")
                     else:
                         self.next_move = "down"
                         self.move("right")
+                        really_move = False
 
                 elif self.next_move == "down":
                     if self.y + self.world.gap // 2 + self.world.tile_height // 2 <= self.world.bottom_border:
@@ -85,9 +125,21 @@ class Player:
                         self.y += self.world.gap // 2 + self.world.tile_height // 2
                         self.next_move = "up"
                         self.picture = PLAYER_RU
+                        # Si au moins deux poussins sont là
+                        if self.piou_piou_number > 1:
+                            if (self.x, self.y) in self.pos_history[-self.piou_piou_number:]:
+                                really_move = False
+                                self.move("right")
                     else:
                         self.next_move = "up"
                         self.move("right")
+                        really_move = False
+            else:
+                really_move = False
+        
+        if really_move:
+            self.handle_egg()
+            self.pos_history.append((self.x, self.y))
 
 
     def find_current_tile(self):
@@ -122,7 +174,35 @@ class Player:
 
     
     def seed(self):
-        if self.egg == 0:
-            self.egg = 1
-        else:
-            print("Il y a déjà un oeuf")
+        if self.world.egg == 0:
+            self.world.egg = 1
+            self.world.tiles_list[0].tiletype = "egg1"
+            self.world.have_egg = True
+
+
+    def handle_egg(self):
+        """Gère l'oeuf"""
+        if self.world.egg == 4:
+            self.world.tiles_list[0].type = "egg2"
+            self.world.tiles_list[0].tiletype = "egg2"
+
+        elif self.world.egg == 8:
+            self.world.tiles_list[0].type = "egg3"
+            self.world.tiles_list[0].tiletype = "egg3"
+
+        elif self.world.egg == 12:
+            self.world.tiles_list[0].type = "egg4"
+            self.world.tiles_list[0].tiletype = "egg4"
+
+        elif self.world.egg == 16:
+            self.world.tiles_list[0].type = "egg5"
+            self.world.tiles_list[0].tiletype = "egg5"
+        
+        if self.world.egg == 20:
+            self.world.egg = 0
+            self.world.have_egg = False
+            self.world.tiles_list[0].tiletype = "nest"
+            self.piou_piou_number += 1
+        elif self.world.egg != 0:
+            self.world.egg += 1
+

+ 13 - 11
tiles.py

@@ -16,19 +16,21 @@ class Tile:
         """Affiche la tuile"""
         if self.tiletype == "seed" :
             image = pygame.transform.scale(SEED, (self.width, self.height))
-        elif self.tiletype == "green" :
+        elif self.tiletype == "nest":
+            image = pygame.transform.scale(NEST, (self.width, self.height))
+        elif self.tiletype == "egg1":
+            image = pygame.transform.scale(EGG1, (self.width, self.height))
+        elif self.tiletype == "egg2":
+            image = pygame.transform.scale(EGG2, (self.width, self.height))
+        elif self.tiletype == "egg3":
+            image = pygame.transform.scale(EGG3, (self.width, self.height))
+        elif self.tiletype == "egg4":
+            image = pygame.transform.scale(EGG4, (self.width, self.height))
+        elif self.tiletype == "egg5":
+            image = pygame.transform.scale(EGG5, (self.width, self.height))
+        else :
             image = pygame.transform.scale(HEXA, (self.width, self.height))
-        elif self.tiletype == "green_path" :
-            image = pygame.transform.scale(HEXA_PATH, (self.width, self.height))
-        elif self.tiletype == "seed_path" : 
-            image = pygame.transform.scale(SEED_PATH, (self.width, self.height))
         self.display.blit(image, (self.x - self.width // 2, self.y - self.height // 2))
 
-    def to_path(self):
-        """Change le tiletype de la tuile si celle ci est sur le chemin du fermier"""
-        if self.tiletype == "green":
-            self.tiletype = "green_path"
-        elif self.tiletype == "seed":
-            self.tiletype = "seed_path"
         
         

+ 9 - 4
variables.py

@@ -19,17 +19,22 @@ GAP = 8
 # images
 HEXA = image.load(path.join("images/hexa.png"))
 SEED = image.load(path.join("images/seed.png"))
-HEXA_PATH = image.load(path.join("images/hexa_path.png"))
-SEED_PATH = image.load(path.join("images/seed_path.png"))
+NEST = image.load(path.join("images/nest.png"))
+EGG1 = image.load(path.join("images/egg1.png"))
+EGG2 = image.load(path.join("images/egg2.png"))
+EGG3 = image.load(path.join("images/egg3.png"))
+EGG4 = image.load(path.join("images/egg4.png"))
+EGG5 = image.load(path.join("images/egg5.png"))
+
 
 PLAYER_RU = image.load(path.join("images/player-ru.png"))
 PLAYER_RD = image.load(path.join("images/player-rd.png"))
 PLAYER_LU = image.load(path.join("images/player-lu.png"))
 PLAYER_LD = image.load(path.join("images/player-ld.png"))
 
-FARMER = image.load(path.join("images/farmer.png"))
+PIOU_PIOU = image.load(path.join("images/piou-piou.png"))
 
-NEST = image.load(path.join("images/nest.png"))
+FARMER = image.load(path.join("images/farmer.png"))
 
 # tuile de base pour le personnage
 ORIGIN = 0

+ 12 - 4
world.py

@@ -17,8 +17,10 @@ class World:
         self.right_border = max([tile["x"] for tile in self.pos_list])
         self.top_border = min([tile["y"] for tile in self.pos_list])
         self.bottom_border = max([tile["y"] for tile in self.pos_list])
+        self.egg = 0
         self.border_tiles_list = self.border_tiles(self.tiles_list)
-    
+        self.farmers = []
+
     def draw(self):
         """Dessine les tuiles du monde"""
         for tile in self.tiles_list:
@@ -27,7 +29,7 @@ class World:
 
     def random_tile_type(self) -> str:
         """Renvoie une chaine de caractère contenant 'green' ou 'seed' avec une probabilité respective de 90% et 10%"""
-        t_type = ["green"] * 9 + ["seed"]
+        t_type = ["green"] * 15 + ["seed"]
         return choice(t_type)
 
     
@@ -35,7 +37,7 @@ class World:
         """Renvoie une liste avec toutes les tuiles du monde, générées en partie aléatoirement"""
         res = []
         # Tuile centrale
-        res.append(Tile(self.pos_list[0]["x"], self.pos_list[0]["y"], self.tile_width, self.tile_height, "green", self.display))
+        res.append(Tile(self.pos_list[0]["x"], self.pos_list[0]["y"], self.tile_width, self.tile_height, "nest", self.display))
         # Toutes les autres
         for pos in self.pos_list[1:]:
             res.append(Tile(pos["x"], pos["y"], self.tile_width, self.tile_height, self.random_tile_type(), self.display))
@@ -178,6 +180,12 @@ class World:
             tile = choice(self.tiles_list[1:])
         tile.tiletype = "seed"
 
+    
+    def egg_life(self):
+        """Gère l'oeuf"""
+        if self.egg == 0:
+            self.egg = 1
+
 
     def border_tiles(self, tiles_list: list) -> list:
         """Renvoie une liste contenant les tuiles des bordures de la map, les tuiles étant les plus éloignées concernant celles d'en haut et d'en bas"""
@@ -192,4 +200,4 @@ class World:
     def random_border_tile(self, border_tiles_list: list) -> list:
         """Renvoie une tuile aléatoire du bord de la map"""
         res = choice(border_tiles_list)
-        return res
+        return res