utils.py 981 B

123456789101112131415161718192021222324252627282930
  1. import pygame
  2. def get_empty_cells(grid):
  3. """
  4. Returns a list of (x, y) tuples for cells in grid that are empty (value 0).
  5. """
  6. empty = []
  7. for y, row in enumerate(grid):
  8. for x, cell in enumerate(row):
  9. if cell == 0:
  10. empty.append((x, y))
  11. return empty
  12. def get_cell_center(x, y, cell_size=80):
  13. """
  14. Converts cell coordinates (x, y) to pixel coordinates of the cell center.
  15. """
  16. return pygame.Vector2(x * cell_size + cell_size / 2, y * cell_size + cell_size / 2)
  17. def draw_arrow(surface, color, start, end, width=3):
  18. pygame.draw.line(surface, color, start, end, width)
  19. direction = (end - start).normalize()
  20. arrow_size = 10
  21. perpendicular = pygame.Vector2(-direction.y, direction.x)
  22. point1 = end - direction * arrow_size + perpendicular * arrow_size / 2
  23. point2 = end - direction * arrow_size - perpendicular * arrow_size / 2
  24. pygame.draw.polygon(surface, color, [end, point1, point2])