Pygame.py 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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, 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, PHEROMON_POSITIONS, allow_empty=True)
  14. self._display_pheromones(pheromones_positions, self._context)
  15. def _display_pheromones(self, pheromones_positions, context):
  16. pheromone_exploration_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_EXPLORATION)
  17. pheromone_home_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_HOME)
  18. for point in pheromones_positions:
  19. point_flavour = context.pheromones().get_flavour(point)
  20. try:
  21. point_flavour.get_pheromone(category=PHEROMON_DIRECTION, type=PHEROMON_DIR_HOME)
  22. self.draw_surface(point, pheromone_home_surface)
  23. except NoPheromone:
  24. pass # No pheromone here
  25. try:
  26. point_flavour.get_pheromone(category=PHEROMON_DIRECTION, type=PHEROMON_DIR_EXPLO)
  27. self.draw_surface(point, pheromone_exploration_surface)
  28. except NoPheromone:
  29. pass # No pheromone here
  30. def _key_pressed(self, key):
  31. if key == pygame.K_p:
  32. if self._is_display_pheromones:
  33. self._is_display_pheromones = False
  34. else:
  35. self._is_display_pheromones = True