1
0

farmer.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import pygame
  2. from tiles import *
  3. from variables import *
  4. from world import World
  5. class Farmer:
  6. def __init__(self, x: int, y: int, world: World, display: pygame.display):
  7. """Un fermier, avec ses coordonnées (x, y)"""
  8. self.display = display
  9. self.x: int = x
  10. self.y: int = y
  11. self.world: World = world
  12. self.neighbors_list: list = self.neighbors(self.x, self.y, self.world)
  13. def draw(self):
  14. """Affiche le fermier"""
  15. image = pygame.transform.scale(FARMER, (self.world.tile_width, self.world.tile_height))
  16. self.display.blit(image, (self.x - self.world.tile_width // 2, self.y - self.world.tile_height // 2))
  17. def neighbors(self, x: int, y: int, world: World) -> list:
  18. """Prend la position d'une tuile ((x, y), qui n'est pas son centre), et renvoie la position des tuiles adjacentess dans une liste"""
  19. x_centre = x
  20. y_centre = y
  21. res = []
  22. top_tile = (x_centre, y_centre - world.gap - world.tile_height)
  23. bottom_tile = (x_centre, y_centre + world.gap + world.tile_height)
  24. left_high_tile = (x_centre - world.gap - 3 * world.tile_width // 4, y_centre - world.gap // 2 - world.tile_height // 2)
  25. left_bottom_tile = (x_centre - world.gap - 3 * world.tile_width // 4, y_centre + world.gap // 2 + world.tile_height // 2)
  26. right_high_tile = (x_centre + world.gap + 3 * world.tile_width // 4, y_centre - world.gap // 2 - world.tile_height // 2)
  27. right_bottom_tile = (x_centre + world.gap + 3 * world.tile_width // 4, y_centre + world.gap // 2 + world.tile_height // 2)
  28. res.append(top_tile)
  29. res.append(bottom_tile)
  30. res.append(left_high_tile)
  31. res.append(left_bottom_tile)
  32. res.append(right_high_tile)
  33. res.append(right_bottom_tile)
  34. return res
  35. def next_tuile(self, tuile_dest: Tile, world: World) -> tuple:
  36. """Prend la tuile à atteindre et renvoie les coordonnées de la tuile sur laquelle le fermier doit aller pour s'en rapprocher le plus"""
  37. res = (0, 0)
  38. x_nest = tuile_dest["x"]
  39. y_nest = tuile_dest["y"]
  40. min = WINDOW_WIDTH ** 2 * 2
  41. for tuile in self.neighbors_list:
  42. x, y = tuile
  43. x = abs(x - x_nest)
  44. y = abs(y - y_nest)
  45. hypo = x ** 2 + y ** 2
  46. if min > hypo :
  47. min = hypo
  48. res = tuile
  49. return(res)
  50. def find_current_tile(self):
  51. """Renvoie la tuile sur laquelle se trouve le fermier"""
  52. found = False
  53. i = 0
  54. while not found and i < len(self.world.pos_list):
  55. if self.world.pos_list[i] == {"x": self.x, "y": self.y}:
  56. found = True
  57. else:
  58. i += 1
  59. if found:
  60. return self.world.tiles_list[i]
  61. else:
  62. print("Aucune tuile trouvée")
  63. def way_to_follow(self, world: World):
  64. """Renvoie les coordonées par lesquelles va passer le fermier sous forme d'une liste"""
  65. return None