player.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import pygame
  2. from tiles import *
  3. from variables import *
  4. from world import World
  5. class Player:
  6. def __init__(self, x: int, y: int, world: World, display: pygame.display):
  7. """Un joueur, avec ses coordonnées (x, y)"""
  8. self.display = display
  9. self.x: int = x
  10. self.y: int = y
  11. self.picture = PLAYER_RU
  12. self.world: World = world
  13. self.next_move = "up"
  14. self.moving = True
  15. self.egg = 0
  16. self.actions_till_next_farmer = 10
  17. self.last_tile = self.find_current_tile()
  18. def draw(self):
  19. """Affiche le joueur"""
  20. image = pygame.transform.scale(self.picture, (self.world.tile_width, self.world.tile_height))
  21. 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
  22. # On dessine l'oeuf si besoin
  23. def move(self, direction: str):
  24. """Modifie les coordonnées x et y du joueur en fonction de la direction donnée"""
  25. if direction == "up":
  26. 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
  27. self.y -= self.world.gap + self.world.tile_height
  28. self.next_move = "up"
  29. if self.picture == PLAYER_LD:
  30. self.picture = PLAYER_LU
  31. elif self.picture == PLAYER_RD:
  32. self.picture = PLAYER_RU
  33. elif direction == "down":
  34. if self.y + self.world.gap + self.world.tile_height <= self.world.bottom_border: # Inversement ici
  35. self.y += self.world.gap + self.world.tile_height
  36. self.next_move = "down"
  37. if self.picture == PLAYER_LU:
  38. self.picture = PLAYER_LD
  39. elif self.picture == PLAYER_RU:
  40. self.picture = PLAYER_RD
  41. elif direction == "left":
  42. if self.x - self.world.gap - 3 * self.world.tile_width // 4 >= self.world.left_border:
  43. if self.next_move == "up":
  44. if self.y - self.world.gap // 2 - self.world.tile_height // 2 >= self.world.top_border:
  45. self.x -= self.world.gap + 3 * self.world.tile_width // 4
  46. self.y -= self.world.gap // 2 + self.world.tile_height // 2
  47. self.next_move = "down"
  48. self.picture = PLAYER_LD
  49. else:
  50. self.next_move = "down"
  51. self.move("left")
  52. elif self.next_move == "down":
  53. if self.y + self.world.gap // 2 + self.world.tile_height // 2 <= self.world.bottom_border:
  54. self.x -= self.world.gap + 3 * self.world.tile_width // 4
  55. self.y += self.world.gap // 2 + self.world.tile_height // 2
  56. self.next_move = "up"
  57. self.picture = PLAYER_LU
  58. else:
  59. self.next_move = "up"
  60. self.move("left")
  61. elif direction == "right":
  62. if self.x + self.world.gap + 3 * self.world.tile_width // 4 <= self.world.right_border:
  63. if self.next_move == "up":
  64. if self.y - self.world.gap // 2 - self.world.tile_height // 2 >= self.world.top_border:
  65. self.x += self.world.gap + 3 * self.world.tile_width // 4
  66. self.y -= self.world.gap // 2 + self.world.tile_height // 2
  67. self.next_move = "down"
  68. self.picture = PLAYER_RD
  69. else:
  70. self.next_move = "down"
  71. self.move("right")
  72. elif self.next_move == "down":
  73. if self.y + self.world.gap // 2 + self.world.tile_height // 2 <= self.world.bottom_border:
  74. self.x += self.world.gap + 3 * self.world.tile_width // 4
  75. self.y += self.world.gap // 2 + self.world.tile_height // 2
  76. self.next_move = "up"
  77. self.picture = PLAYER_RU
  78. else:
  79. self.next_move = "up"
  80. self.move("right")
  81. def find_current_tile(self):
  82. """Renvoie la tuile sur laquelle se trouve le joueur"""
  83. found = False
  84. i = 0
  85. while not found and i < len(self.world.pos_list):
  86. if self.world.pos_list[i] == {"x": self.x, "y": self.y}:
  87. found = True
  88. else:
  89. i += 1
  90. if found:
  91. return self.world.tiles_list[i]
  92. else:
  93. print("Aucune tuile trouvée")
  94. def action(self):
  95. """L'action de ce tour"""
  96. tile = self.find_current_tile()
  97. if (tile.x, tile.y) != (self.last_tile.x, self.last_tile.y):
  98. if tile.tiletype == "seed":
  99. self.moving = False
  100. self.seed()
  101. tile.tiletype = "green"
  102. self.world.spawn_new_seed()
  103. self.actions_till_next_farmer -= 1
  104. self.last_tile = tile
  105. self.moving = True
  106. def seed(self):
  107. if self.egg == 0:
  108. self.egg = 1
  109. else:
  110. print("Il y a déjà un oeuf")