Pygame.py 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from intelligine.core.exceptions import NoPheromone
  2. from synergine_xyz.display.Pygame import Pygame as XyzPygame
  3. import pygame
  4. from intelligine.cst import PHEROMON_DIRECTION, PHEROMON_DIR_HOME, PHEROMON_DIR_EXPLO, PHEROMON_POSITIONS
  5. from intelligine.display.pygame.visualisation import SURFACE_PHEROMONE_EXPLORATION, SURFACE_PHEROMONE_HOME
  6. class Pygame(XyzPygame):
  7. def __init__(self, config, context, synergy_manager):
  8. super().__init__(config, context, synergy_manager)
  9. self._is_display_pheromones = False
  10. def receive(self, actions_done):
  11. super().receive(actions_done)
  12. if self._is_display_pheromones:
  13. pheromones_positions = self._context.metas.list.get(PHEROMON_POSITIONS,
  14. PHEROMON_POSITIONS,
  15. allow_empty=True)
  16. self._display_pheromones(pheromones_positions, self._context)
  17. def _display_pheromones(self, pheromones_positions, context):
  18. pheromone_exploration_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_EXPLORATION)
  19. pheromone_home_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_HOME)
  20. for point in pheromones_positions:
  21. point_flavour = context.pheromones().get_flavour(point)
  22. try:
  23. point_flavour.get_pheromone(category=PHEROMON_DIRECTION, type=PHEROMON_DIR_HOME)
  24. self.draw_surface(point, pheromone_home_surface)
  25. except NoPheromone:
  26. pass # No pheromone here
  27. try:
  28. point_flavour.get_pheromone(category=PHEROMON_DIRECTION, type=PHEROMON_DIR_EXPLO)
  29. self.draw_surface(point, pheromone_exploration_surface)
  30. except NoPheromone:
  31. pass # No pheromone here
  32. def _key_pressed(self, key):
  33. if key == pygame.K_p:
  34. if self._is_display_pheromones:
  35. self._is_display_pheromones = False
  36. else:
  37. self._is_display_pheromones = True