world.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from tiles import *
  2. from variables import *
  3. def create_world(display: pygame.display, nb_col: int, nb_row: int, gap: int) -> list:
  4. """Crée un monde avec le nombre de colonnes et de lignes données, et l'espacement donné entre les tuiles"""
  5. tile_width, tile_height = tile_size(nb_col, nb_row, gap)
  6. pos_list = position_of_tiles(nb_col, nb_row, gap, tile_height, tile_width, (WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2))
  7. for pos in pos_list:
  8. tile = Tile(pos["x"] - tile_width // 2, pos["y"] - tile_height // 2, tile_width, tile_height, "green", display)
  9. tile.draw()
  10. def tile_size(nb_col: int, nb_row: int, gap: int) -> tuple:
  11. """Renvoie un tuple contenant la largeur et la hauteur d'une tuile en fonction du nombre de lignes, de colonnes, et de l'espacement entre le tuiles"""
  12. width = (WINDOW_WIDTH - nb_col * gap) // nb_col
  13. height = (WINDOW_HEIGHT - nb_row * gap) // nb_row
  14. # On cale le plus grand sur le plus petit pour conserver la forme des tuiles et ne pas dépasser d'un côté
  15. if height < 0.86 * width:
  16. width = height // 0.86
  17. else:
  18. height = round(width * 0.86)
  19. return (width, height)
  20. def position_of_tiles(nb_col: int, nb_row: int, gap: int, tile_height: int, tile_width: int, center: tuple) -> list:
  21. """Renvoie la position du centre de toutes les tuiles du monde en fonction des paramètres donnés"""
  22. # Le nombre de colonnes doit être impair
  23. if nb_col % 2 != 0:
  24. nb_col += 1
  25. res = []
  26. x = center[0]
  27. # Si le nombre de lignes est pair
  28. if nb_row % 2 == 0:
  29. # Colonne centrale
  30. res += position_of_odd_columns(nb_row - 1, gap, tile_height, x)
  31. # Colonnes paires de droite
  32. x += (tile_width * 3) // 4 + gap
  33. res += position_of_even_columns(nb_row, gap, tile_height, x)
  34. for i in range(nb_col // 4 - 1):
  35. x += (tile_width * 3) // 2 + 2 * gap
  36. res += position_of_even_columns(nb_row, gap, tile_height, x)
  37. # Colonnes paires de gauche
  38. x = center[0] - (tile_width * 3) // 4 - gap
  39. res += position_of_even_columns(nb_row, gap, tile_height, x)
  40. for i in range(nb_col // 4 - 1):
  41. x -= (tile_width * 3) // 2 + 2 * gap
  42. res += position_of_even_columns(nb_row, gap, tile_height, x)
  43. # Colonnes impaires de droite
  44. x = center[0]
  45. for i in range(nb_col // 4):
  46. x += (tile_width * 3) // 2 + 2 * gap
  47. res += position_of_odd_columns(nb_row - 1, gap, tile_height, x)
  48. # Colonnes impaires de gauche
  49. x = center[0]
  50. for i in range(nb_col // 4):
  51. x -= (tile_width * 3) // 2 + 2 * gap
  52. res += position_of_odd_columns(nb_row - 1, gap, tile_height, x)
  53. # Si le nombre de lignes est impair
  54. else:
  55. # Colonne centrale
  56. res += position_of_odd_columns(nb_row, gap, tile_height, x)
  57. # Colonnes paires de droite
  58. x += (tile_width * 3) // 4 + gap
  59. res += position_of_even_columns(nb_row - 1, gap, tile_height, x)
  60. for i in range(nb_col // 4 - 1):
  61. x += (tile_width * 3) // 2 + 2 * gap
  62. res += position_of_even_columns(nb_row - 1, gap, tile_height, x)
  63. # Colonnes paires de gauche
  64. x = center[0] - (tile_width * 3) // 4 - gap
  65. res += position_of_even_columns(nb_row - 1, gap, tile_height, x)
  66. for i in range(nb_col // 4 - 1):
  67. x -= (tile_width * 3) // 2 + 2 * gap
  68. res += position_of_even_columns(nb_row - 1, gap, tile_height, x)
  69. # Colonnes impaires de droite
  70. x = center[0]
  71. for i in range(nb_col // 4 - 1):
  72. x += (tile_width * 3) // 2 + 2 * gap
  73. res += position_of_odd_columns(nb_row, gap, tile_height, x)
  74. # Colonnes impaires de gauche
  75. x = center[0]
  76. for i in range(nb_col // 4 - 1):
  77. x -= (tile_width * 3) // 2 + 2 * gap
  78. res += position_of_odd_columns(nb_row, gap, tile_height, x)
  79. return res
  80. def position_of_odd_columns(nb_row: int, gap: int, tile_height: int, x: int) -> list:
  81. """Renvoie la position des tuiles dans une colonne au nombre de tuiles impair"""
  82. y = WINDOW_HEIGHT // 2
  83. # Tuile du milieu
  84. res = [{"x": x, "y": y}]
  85. # Tuiles en dessous du milieu
  86. for i in range(nb_row // 2):
  87. y += tile_height + gap
  88. res.append({"x": x, "y": y})
  89. y = WINDOW_HEIGHT // 2
  90. # Tuiles en dessous du milieu
  91. for i in range(nb_row // 2):
  92. y -= tile_height + gap
  93. res.append({"x": x, "y": y})
  94. return res
  95. def position_of_even_columns(nb_row: int, gap: int, tile_height: int, x: int) -> list:
  96. res = []
  97. # Tuiles du bas
  98. y = WINDOW_HEIGHT // 2 + gap // 2 + tile_height // 2
  99. res.append({"x": x, "y": y})
  100. for i in range(nb_row // 2 - 1):
  101. y += tile_height + gap
  102. res.append({"x": x, "y": y})
  103. # Tuiles du haut
  104. y = WINDOW_HEIGHT // 2 - gap // 2 - tile_height // 2
  105. res.append({"x": x, "y": y})
  106. for i in range(nb_row // 2 - 1):
  107. y -= tile_height + gap
  108. res.append({"x": x, "y": y})
  109. return res