player.py 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. def draw(self):
  15. """Affiche le joueur"""
  16. image = pygame.transform.scale(self.picture, (self.world.tile_width, self.world.tile_height))
  17. 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
  18. def move(self, direction: str):
  19. """Modifie les coordonnées x et y du joueur en fonction de la direction donnée"""
  20. if direction == "up":
  21. 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
  22. self.y -= self.world.gap + self.world.tile_height
  23. self.next_move = "up"
  24. if self.picture == PLAYER_LD:
  25. self.picture = PLAYER_LU
  26. elif self.picture == PLAYER_RD:
  27. self.picture = PLAYER_RU
  28. elif direction == "down":
  29. if self.y + self.world.gap + self.world.tile_height <= self.world.bottom_border: # Inversement ici
  30. self.y += self.world.gap + self.world.tile_height
  31. self.next_move = "down"
  32. if self.picture == PLAYER_LU:
  33. self.picture = PLAYER_LD
  34. elif self.picture == PLAYER_RU:
  35. self.picture = PLAYER_RD
  36. elif direction == "left":
  37. if self.x - self.world.gap - 3 * self.world.tile_width // 4 >= self.world.left_border:
  38. if self.next_move == "up":
  39. if self.y - self.world.gap // 2 - self.world.tile_height // 2 >= self.world.top_border:
  40. self.x -= self.world.gap + 3 * self.world.tile_width // 4
  41. self.y -= self.world.gap // 2 + self.world.tile_height // 2
  42. self.next_move = "down"
  43. self.picture = PLAYER_LD
  44. else:
  45. self.next_move = "down"
  46. self.move("left")
  47. elif self.next_move == "down":
  48. if self.y + self.world.gap // 2 + self.world.tile_height // 2 <= self.world.bottom_border:
  49. self.x -= self.world.gap + 3 * self.world.tile_width // 4
  50. self.y += self.world.gap // 2 + self.world.tile_height // 2
  51. self.next_move = "up"
  52. self.picture = PLAYER_LU
  53. else:
  54. self.next_move = "up"
  55. self.move("left")
  56. elif direction == "right":
  57. if self.x + self.world.gap + 3 * self.world.tile_width // 4 <= self.world.right_border:
  58. if self.next_move == "up":
  59. if self.y - self.world.gap // 2 - self.world.tile_height // 2 >= self.world.top_border:
  60. self.x += self.world.gap + 3 * self.world.tile_width // 4
  61. self.y -= self.world.gap // 2 + self.world.tile_height // 2
  62. self.next_move = "down"
  63. self.picture = PLAYER_RD
  64. else:
  65. self.next_move = "down"
  66. self.move("right")
  67. elif self.next_move == "down":
  68. if self.y + self.world.gap // 2 + self.world.tile_height // 2 <= self.world.bottom_border:
  69. self.x += self.world.gap + 3 * self.world.tile_width // 4
  70. self.y += self.world.gap // 2 + self.world.tile_height // 2
  71. self.next_move = "up"
  72. self.picture = PLAYER_RU
  73. else:
  74. self.next_move = "up"
  75. self.move("right")