Procházet zdrojové kódy

ajout du fermier qui apparait et qui s'approche du bord

louciane před 2 týdny
rodič
revize
f18d8eecbb
11 změnil soubory, kde provedl 178 přidání a 52 odebrání
  1. 2 0
      .gitignore
  2. 35 20
      farmer.py
  3. binární
      images/player-ld.png
  4. binární
      images/player-lu.png
  5. binární
      images/player-rd.png
  6. binární
      images/player-ru.png
  7. 17 11
      main.py
  8. 102 3
      player.py
  9. 2 1
      tiles.py
  10. 7 3
      variables.py
  11. 13 14
      world.py

+ 2 - 0
.gitignore

@@ -3,3 +3,5 @@
 __pycache__/
 
 .vscode/
+
+images/*.kra

+ 35 - 20
farmer.py

@@ -42,26 +42,41 @@ class Farmer:
         
         return res
 
-    def next_tuile(self, world: World) -> tuple:
-        """Prend les coordonnées du nid 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 = world.pos_list[0]["x"]
-        y_nest = world.pos_list[0]["y"]
-
-        min = WINDOW_WIDTH ** 2 * 2
-        for tuile in self.neighbors_list:
-            x, y = tuile   
-
-            x = abs(x - x_nest)
-            y = abs(y - y_nest)
-
-            hypo = x ** 2 + y ** 2
-
-            if min > hypo :
-                min = hypo
-                res = tuile
-
-        return(res)
+    def next_tuile(self, tuile_dest: Tile, world: World) -> 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 = tuile_dest["x"]
+            y_nest = tuile_dest["y"]
+    
+            min = WINDOW_WIDTH ** 2 * 2
+            for tuile in self.neighbors_list:
+                x, y = tuile   
+    
+                x = abs(x - x_nest)
+                y = abs(y - y_nest)
+    
+                hypo = x ** 2 + y ** 2
+    
+                if min > hypo :
+                    min = hypo
+                    res = tuile
+    
+            return(res)
+
+    def find_current_tile(self):
+            """Renvoie la tuile sur laquelle se trouve le fermier"""
+            found = False
+            i = 0
+            while not found and i < len(self.world.pos_list):
+                if self.world.pos_list[i] == {"x": self.x, "y": self.y}:
+                    found = True
+                else:
+                    i += 1
+    
+            if found:
+                return self.world.tiles_list[i]
+            else:
+                print("Aucune tuile trouvée")
 
 
     def way_to_follow(self, world: World):

binární
images/player-ld.png


binární
images/player-lu.png


binární
images/player-rd.png


binární
images/player-ru.png


+ 17 - 11
main.py

@@ -6,7 +6,6 @@ from player import *
 from nest import *
 from farmer import *
 
-
 def main():
     """La fonction pricipale"""
 
@@ -41,18 +40,26 @@ def main():
             # On gère les touches
             if event.type == pygame.KEYDOWN:
 
-                farmer.x, farmer.y = farmer.next_tuile(world)
+                farmer.x, farmer.y = farmer.next_tuile(world.pos_list[0], world)
                 farmer.neighbors_list = farmer.neighbors(farmer.x, farmer.y, farmer.world)
-                print("FARMER FARMER FARMER FARMER FARMER FARMER FARMER FARMER FARMER FARMER FARMER ")
-                print(farmer.x, farmer.y)
-                print("LISTE   LISTE   LISTE   LISTE   LISTE   LISTE   LISTE   LISTE   LISTE   LISTE   LISTE   ")
                 farmer.way_to_follow(world)
+                farmer.find_current_tile()
 
                 if event.key == pygame.K_z:
-                    player.move("up")
+                    if player.moving:
+                        player.move("up")
 
                 elif event.key == pygame.K_s:
-                    player.move("down")
+                    if player.moving:
+                        player.move("down")
+
+                elif event.key == pygame.K_q:
+                    if player.moving:
+                        player.move("left")
+
+                elif event.key == pygame.K_d:
+                    if player.moving:
+                        player.move("right")
             
             # Fermeture du jeu
             if event.type == QUIT:
@@ -62,11 +69,10 @@ def main():
         # À chaque tick on fait ce qu'il y a là
         display.fill(BACKGROUND_COLOR)
         world.draw()
-        player.draw()
         nest.draw(world.pos_list[0], 0)
+        player.draw()
+        player.action()
         farmer.draw()
-
-            
         pygame.display.update()
         clock.tick(FPS)
 
@@ -76,4 +82,4 @@ def main():
 
 
 # On lance la fonction principale
-main()
+main()

+ 102 - 3
player.py

@@ -1,5 +1,6 @@
 import pygame
 from tiles import *
+from variables import *
 from world import World
 
 
@@ -9,21 +10,119 @@ class Player:
         self.display = display
         self.x: int = x
         self.y: int = y
-        self.picture: str = "player"
+        self.picture =  PLAYER_RU
         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()
 
     def draw(self):
         """Affiche le joueur"""
-        image = pygame.transform.scale(PLAYER, (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
+        # On dessine l'oeuf si besoin
+        
     
     def move(self, direction: str):
         """Modifie les coordonnées x et y du joueur en fonction de la direction donnée"""
         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
+                self.next_move = "up"
+                if self.picture == PLAYER_LD:
+                    self.picture = PLAYER_LU
+                elif self.picture == PLAYER_RD:
+                    self.picture = PLAYER_RU
+
         elif direction == "down":
             if self.y + self.world.gap + self.world.tile_height <= self.world.bottom_border:    # Inversement ici
                 self.y += self.world.gap + self.world.tile_height
+                self.next_move = "down"
+                if self.picture == PLAYER_LU:
+                    self.picture = PLAYER_LD
+                elif self.picture == PLAYER_RU:
+                    self.picture = PLAYER_RD
+
+        elif direction == "left":
+            if self.x - self.world.gap - 3 * self.world.tile_width // 4 >= self.world.left_border:
+                if self.next_move == "up":
+                    if self.y - self.world.gap // 2 - self.world.tile_height // 2 >= self.world.top_border:
+                        self.x -= self.world.gap + 3 * self.world.tile_width // 4
+                        self.y -= self.world.gap // 2 + self.world.tile_height // 2
+                        self.next_move = "down"
+                        self.picture = PLAYER_LD
+                    else:
+                        self.next_move = "down"
+                        self.move("left")
+
+                elif self.next_move == "down":
+                    if self.y + self.world.gap // 2 + self.world.tile_height // 2 <= self.world.bottom_border:
+                        self.x -= self.world.gap + 3 * self.world.tile_width // 4
+                        self.y += self.world.gap // 2 + self.world.tile_height // 2
+                        self.next_move = "up"
+                        self.picture = PLAYER_LU
+                    else:
+                        self.next_move = "up"
+                        self.move("left")
+
+        elif direction == "right":
+            if self.x + self.world.gap + 3 * self.world.tile_width // 4 <= self.world.right_border:
+                if self.next_move == "up":
+                    if self.y - self.world.gap // 2 - self.world.tile_height // 2 >= self.world.top_border:
+                        self.x += self.world.gap + 3 * self.world.tile_width // 4
+                        self.y -= self.world.gap // 2 + self.world.tile_height // 2
+                        self.next_move = "down"
+                        self.picture = PLAYER_RD
+                    else:
+                        self.next_move = "down"
+                        self.move("right")
+
+                elif self.next_move == "down":
+                    if self.y + self.world.gap // 2 + self.world.tile_height // 2 <= self.world.bottom_border:
+                        self.x += self.world.gap + 3 * self.world.tile_width // 4
+                        self.y += self.world.gap // 2 + self.world.tile_height // 2
+                        self.next_move = "up"
+                        self.picture = PLAYER_RU
+                    else:
+                        self.next_move = "up"
+                        self.move("right")
+
 
-    
+    def find_current_tile(self):
+        """Renvoie la tuile sur laquelle se trouve le joueur"""
+        found = False
+        i = 0
+        while not found and i < len(self.world.pos_list):
+            if self.world.pos_list[i] == {"x": self.x, "y": self.y}:
+                found = True
+            else:
+                i += 1
+
+        if found:
+            return self.world.tiles_list[i]
+        else:
+            print("Aucune tuile trouvée")
+
+    
+    def action(self):
+        """L'action de ce tour"""
+        tile = self.find_current_tile()
+        if (tile.x, tile.y) != (self.last_tile.x, self.last_tile.y):
+            if tile.tiletype == "seed":
+                self.moving = False
+                self.seed()
+                tile.tiletype = "green"
+                self.world.spawn_new_seed()
+
+            self.actions_till_next_farmer -= 1
+            self.last_tile = tile
+            self.moving = True
+
+    
+    def seed(self):
+        if self.egg == 0:
+            self.egg = 1
+        else:
+            print("Il y a déjà un oeuf")

+ 2 - 1
tiles.py

@@ -20,4 +20,5 @@ class Tile:
             image = pygame.transform.scale(HEXA, (self.width, self.height))
         self.display.blit(image, (self.x - self.width // 2, self.y - self.height // 2))
 
-
+        
+        

+ 7 - 3
variables.py

@@ -2,8 +2,8 @@ from pygame import image
 from os import path
 
 # taille de la fenêtre
-WINDOW_WIDTH = 1000
-WINDOW_HEIGHT = 700
+WINDOW_WIDTH = 1920
+WINDOW_HEIGHT = 1080
 
 # Nombre d'images par secondes
 FPS = 60
@@ -20,7 +20,11 @@ GAP = 8
 HEXA = image.load(path.join("images/hexa.png"))
 SEED = image.load(path.join("images/seed.png"))
 
-PLAYER = image.load(path.join("images/player.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"))
 
 NEST = image.load(path.join("images/nest.png"))

+ 13 - 14
world.py

@@ -18,7 +18,6 @@ class World:
         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.border_tiles_list = self.border_tiles(self.tiles_list)
-
     
     def draw(self):
         """Dessine les tuiles du monde"""
@@ -27,8 +26,8 @@ class World:
 
 
     def random_tile_type(self) -> str:
-        """Renvoie une chaine de caractère contenant 'green' ou 'seed' avec une probabilité respective de 80% et 20%"""
-        t_type = ["green"] * 4 + ["seed"]
+        """Renvoie une chaine de caractère contenant 'green' ou 'seed' avec une probabilité respective de 90% et 10%"""
+        t_type = ["green"] * 9 + ["seed"]
         return choice(t_type)
 
     
@@ -62,6 +61,7 @@ class World:
         # Le nombre de colonnes doit être impair
         if self.nb_col % 2 != 0:
             self.nb_col += 1
+            print("Il y a déjà un oeuf")
 
         res = []
         x = center[0]
@@ -170,6 +170,15 @@ class World:
 
         return res
 
+
+    def spawn_new_seed(self):
+        """Place aléatoirement une nouvelle graine dans le monde"""
+        tile = choice(self.tiles_list[1:])
+        while tile.tiletype == "seed":
+            tile = choice(self.tiles_list[1:])
+        tile.tiletype = "seed"
+
+
     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"""
         res = []
@@ -183,14 +192,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
-
-
-
-
-
-
-
-
-
-#           res.append(Tile(self.pos_list[0]["x"], self.pos_list[0]["y"], self.tile_width, self.tile_height, "green", self.display))
+        return res