Browse Source

traitement du nid comme une tuile, ajout des poussins qui suivent le joueur, on peut sauter au dessus

Hvrnbi 3 days ago
parent
commit
0bbc076027
13 changed files with 113 additions and 56 deletions
  1. BIN
      images/egg1.png
  2. BIN
      images/egg2.png
  3. BIN
      images/egg3.png
  4. BIN
      images/egg4.png
  5. BIN
      images/egg5.png
  6. BIN
      images/nest.png
  7. BIN
      images/piou-piou.png
  8. 0 4
      main.py
  9. 0 43
      nest.py
  10. 84 6
      player.py
  11. 12 0
      tiles.py
  12. 9 2
      variables.py
  13. 8 1
      world.py

BIN
images/egg1.png


BIN
images/egg2.png


BIN
images/egg3.png


BIN
images/egg4.png


BIN
images/egg5.png


BIN
images/nest.png


BIN
images/piou-piou.png


+ 0 - 4
main.py

@@ -3,7 +3,6 @@ from pygame.locals import * # Import des constantes de pygame
 from world import World
 from world import World
 from variables import *
 from variables import *
 from player import *
 from player import *
-from nest import *
 from farmer import *
 from farmer import *
 
 
 def main():
 def main():
@@ -24,8 +23,6 @@ def main():
 
 
     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
     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))
     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)
     farmer = Farmer(random_border_tile_example.x, random_border_tile_example.y, world, display)
 
 
@@ -68,7 +65,6 @@ def main():
         # À chaque tick on fait ce qu'il y a là
         # À chaque tick on fait ce qu'il y a là
         display.fill(BACKGROUND_COLOR)
         display.fill(BACKGROUND_COLOR)
         world.draw()
         world.draw()
-        nest.draw(world.pos_list[0], 0)
         player.draw()
         player.draw()
         player.action()
         player.action()
         farmer.draw()
         farmer.draw()

+ 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)

+ 84 - 6
player.py

@@ -14,19 +14,25 @@ class Player:
         self.world: World = world
         self.world: World = world
         self.next_move = "up"
         self.next_move = "up"
         self.moving = True
         self.moving = True
-        self.egg = 0
         self.actions_till_next_farmer = 10
         self.actions_till_next_farmer = 10
         self.last_tile = self.find_current_tile()
         self.last_tile = self.find_current_tile()
+        self.pos_history = [(self.x, self.y)]
+        self.piou_piou_number = 0
 
 
     def draw(self):
     def draw(self):
         """Affiche le joueur"""
         """Affiche le joueur"""
         image = pygame.transform.scale(self.picture, (self.world.tile_width, self.world.tile_height))
         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
         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):
     def move(self, direction: str):
         """Modifie les coordonnées x et y du joueur en fonction de la direction donnée"""
         """Modifie les coordonnées x et y du joueur en fonction de la direction donnée"""
+        really_move = True
+
         if direction == "up":
         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 
             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
                 self.y -= self.world.gap + self.world.tile_height
@@ -35,6 +41,13 @@ class Player:
                     self.picture = PLAYER_LU
                     self.picture = PLAYER_LU
                 elif self.picture == PLAYER_RD:
                 elif self.picture == PLAYER_RD:
                     self.picture = PLAYER_RU
                     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":
         elif direction == "down":
             if self.y + self.world.gap + self.world.tile_height <= self.world.bottom_border:    # Inversement ici
             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
                     self.picture = PLAYER_LD
                 elif self.picture == PLAYER_RU:
                 elif self.picture == PLAYER_RU:
                     self.picture = PLAYER_RD
                     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":
         elif direction == "left":
             if self.x - self.world.gap - 3 * self.world.tile_width // 4 >= self.world.left_border:
             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.y -= self.world.gap // 2 + self.world.tile_height // 2
                         self.next_move = "down"
                         self.next_move = "down"
                         self.picture = PLAYER_LD
                         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:
                     else:
                         self.next_move = "down"
                         self.next_move = "down"
                         self.move("left")
                         self.move("left")
+                        really_move = False
 
 
                 elif self.next_move == "down":
                 elif self.next_move == "down":
                     if self.y + self.world.gap // 2 + self.world.tile_height // 2 <= self.world.bottom_border:
                     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.y += self.world.gap // 2 + self.world.tile_height // 2
                         self.next_move = "up"
                         self.next_move = "up"
                         self.picture = PLAYER_LU
                         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:
                     else:
                         self.next_move = "up"
                         self.next_move = "up"
                         self.move("left")
                         self.move("left")
+                        really_move = False
+            else:
+                really_move = False
 
 
         elif direction == "right":
         elif direction == "right":
             if self.x + self.world.gap + 3 * self.world.tile_width // 4 <= self.world.right_border:
             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.y -= self.world.gap // 2 + self.world.tile_height // 2
                         self.next_move = "down"
                         self.next_move = "down"
                         self.picture = PLAYER_RD
                         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:
                     else:
                         self.next_move = "down"
                         self.next_move = "down"
                         self.move("right")
                         self.move("right")
+                        really_move = False
 
 
                 elif self.next_move == "down":
                 elif self.next_move == "down":
                     if self.y + self.world.gap // 2 + self.world.tile_height // 2 <= self.world.bottom_border:
                     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.y += self.world.gap // 2 + self.world.tile_height // 2
                         self.next_move = "up"
                         self.next_move = "up"
                         self.picture = PLAYER_RU
                         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:
                     else:
                         self.next_move = "up"
                         self.next_move = "up"
                         self.move("right")
                         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):
     def find_current_tile(self):
@@ -122,7 +174,33 @@ class Player:
 
 
     
     
     def seed(self):
     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"
+
+
+    def handle_egg(self):
+        """Gère l'oeuf"""
+        if self.world.egg == 2:
+            self.world.tiles_list[0].type = "egg2"
+            self.world.tiles_list[0].tiletype = "egg2"
+
+        elif self.world.egg == 4:
+            self.world.tiles_list[0].type = "egg3"
+            self.world.tiles_list[0].tiletype = "egg3"
+
+        elif self.world.egg == 6:
+            self.world.tiles_list[0].type = "egg4"
+            self.world.tiles_list[0].tiletype = "egg4"
+
+        elif self.world.egg == 8:
+            self.world.tiles_list[0].type = "egg5"
+            self.world.tiles_list[0].tiletype = "egg5"
+        
+        if self.world.egg == 10:
+            self.world.egg = 0
+            self.world.tiles_list[0].tiletype = "nest"
+            self.piou_piou_number += 1
+        elif self.world.egg != 0:
+            self.world.egg += 1
+

+ 12 - 0
tiles.py

@@ -16,6 +16,18 @@ class Tile:
         """Affiche la tuile"""
         """Affiche la tuile"""
         if self.tiletype == "seed" :
         if self.tiletype == "seed" :
             image = pygame.transform.scale(SEED, (self.width, self.height))
             image = pygame.transform.scale(SEED, (self.width, self.height))
+        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 :
         else :
             image = pygame.transform.scale(HEXA, (self.width, self.height))
             image = pygame.transform.scale(HEXA, (self.width, self.height))
         self.display.blit(image, (self.x - self.width // 2, self.y - self.height // 2))
         self.display.blit(image, (self.x - self.width // 2, self.y - self.height // 2))

+ 9 - 2
variables.py

@@ -19,15 +19,22 @@ GAP = 8
 # images
 # images
 HEXA = image.load(path.join("images/hexa.png"))
 HEXA = image.load(path.join("images/hexa.png"))
 SEED = image.load(path.join("images/seed.png"))
 SEED = image.load(path.join("images/seed.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_RU = image.load(path.join("images/player-ru.png"))
 PLAYER_RD = image.load(path.join("images/player-rd.png"))
 PLAYER_RD = image.load(path.join("images/player-rd.png"))
 PLAYER_LU = image.load(path.join("images/player-lu.png"))
 PLAYER_LU = image.load(path.join("images/player-lu.png"))
 PLAYER_LD = image.load(path.join("images/player-ld.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
 # tuile de base pour le personnage
 ORIGIN = 0
 ORIGIN = 0

+ 8 - 1
world.py

@@ -17,6 +17,7 @@ class World:
         self.right_border = max([tile["x"] for tile in self.pos_list])
         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.top_border = min([tile["y"] for tile in self.pos_list])
         self.bottom_border = max([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.border_tiles_list = self.border_tiles(self.tiles_list)
     
     
     def draw(self):
     def draw(self):
@@ -35,7 +36,7 @@ class World:
         """Renvoie une liste avec toutes les tuiles du monde, générées en partie aléatoirement"""
         """Renvoie une liste avec toutes les tuiles du monde, générées en partie aléatoirement"""
         res = []
         res = []
         # Tuile centrale
         # 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
         # Toutes les autres
         for pos in self.pos_list[1:]:
         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))
             res.append(Tile(pos["x"], pos["y"], self.tile_width, self.tile_height, self.random_tile_type(), self.display))
@@ -178,6 +179,12 @@ class World:
             tile = choice(self.tiles_list[1:])
             tile = choice(self.tiles_list[1:])
         tile.tiletype = "seed"
         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:
     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"""
         """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"""