Pygame.py 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from intelligine.core.exceptions import NoPheromone
  2. from xyzworld.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):
  8. super().__init__(config, context)
  9. self._is_display_pheromones = False
  10. def receive(self, synergy_object_manager, context):
  11. super().receive(synergy_object_manager, context)
  12. if self._is_display_pheromones:
  13. self._display_pheromones(context.metas.list.get(PHEROMON_POSITIONS, PHEROMON_POSITIONS, allow_empty=True), context)
  14. def _display_pheromones(self, pheromones_positions, context):
  15. pheromone_exploration_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_EXPLORATION)
  16. pheromone_home_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_HOME)
  17. for point in pheromones_positions:
  18. point_flavour = context.pheromones().get_flavour(point)
  19. try:
  20. point_flavour.get_pheromone(category=PHEROMON_DIRECTION, type=PHEROMON_DIR_HOME)
  21. self.draw_surface(point, pheromone_home_surface)
  22. except NoPheromone:
  23. pass # No pheromone here
  24. try:
  25. point_flavour.get_pheromone(category=PHEROMON_DIRECTION, type=PHEROMON_DIR_EXPLO)
  26. self.draw_surface(point, pheromone_exploration_surface)
  27. except NoPheromone:
  28. pass # No pheromone here
  29. def _key_pressed(self, key):
  30. if key == pygame.K_p:
  31. if self._is_display_pheromones:
  32. self._is_display_pheromones = False
  33. else:
  34. self._is_display_pheromones = True