player.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. elif self.next_move == "down":
  45. if self.y + self.world.gap // 2 + self.world.tile_height // 2 <= self.world.bottom_border:
  46. self.x -= self.world.gap + 3 * self.world.tile_width // 4
  47. self.y += self.world.gap // 2 + self.world.tile_height // 2
  48. self.next_move = "up"
  49. self.picture = PLAYER_LU
  50. elif direction == "right":
  51. if self.x + self.world.gap + 3 * self.world.tile_width // 4 <= self.world.right_border:
  52. if self.next_move == "up":
  53. if self.y - self.world.gap // 2 - self.world.tile_height // 2 >= self.world.top_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 = "down"
  57. self.picture = PLAYER_RD
  58. elif self.next_move == "down":
  59. if self.y + self.world.gap // 2 + self.world.tile_height // 2 <= self.world.bottom_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 = "up"
  63. self.picture = PLAYER_RU