Pygame.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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, POINTS_SMELL, \
  5. POINT_SMELL, SMELL_EGG, SMELL_FOOD
  6. from intelligine.display.pygame.visualisation import SURFACE_PHEROMONE_EXPLORATION, SURFACE_PHEROMONE_HOME, \
  7. SURFACE_SMELL_EGG, SURFACE_SMELL_FOOD
  8. class Pygame(XyzPygame):
  9. def __init__(self, config, context, synergy_manager):
  10. super().__init__(config, context, synergy_manager)
  11. self._is_display_pheromones = False
  12. self._is_display_smells = False
  13. def receive(self, actions_done):
  14. super().receive(actions_done)
  15. if self._is_display_pheromones:
  16. pheromones_positions = self._context.metas.list.get(PHEROMON_POSITIONS,
  17. PHEROMON_POSITIONS,
  18. allow_empty=True)
  19. self._display_pheromones(pheromones_positions, self._context)
  20. if self._is_display_smells:
  21. smell_positions = self._context.metas.list.get(POINTS_SMELL,
  22. POINTS_SMELL,
  23. allow_empty=True)
  24. self._display_smells(smell_positions, self._context)
  25. def _display_pheromones(self, pheromones_positions, context):
  26. pheromone_exploration_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_EXPLORATION)
  27. pheromone_home_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_HOME)
  28. for point in pheromones_positions:
  29. point_flavour = context.pheromones().get_flavour(point)
  30. try:
  31. point_flavour.get_pheromone(category=PHEROMON_DIRECTION, type=PHEROMON_DIR_HOME)
  32. self.draw_surface(point, pheromone_home_surface)
  33. except NoPheromone:
  34. pass # No pheromone here
  35. try:
  36. point_flavour.get_pheromone(category=PHEROMON_DIRECTION, type=PHEROMON_DIR_EXPLO)
  37. self.draw_surface(point, pheromone_exploration_surface)
  38. except NoPheromone:
  39. pass # No pheromone here
  40. def _display_smells(self, smell_positions, context):
  41. smell_egg_surface = self._object_visualizer.get_surface(SURFACE_SMELL_EGG)
  42. smell_food_surface = self._object_visualizer.get_surface(SURFACE_SMELL_FOOD)
  43. for point in smell_positions:
  44. point_flavour = context.metas.value.get(POINT_SMELL, point, allow_empty=True, empty_value={})
  45. if SMELL_EGG in point_flavour:
  46. self.draw_surface(point, smell_egg_surface)
  47. if SMELL_FOOD in point_flavour:
  48. self.draw_surface(point, smell_food_surface)
  49. def _key_pressed(self, key):
  50. self._is_display_pheromones = key == pygame.K_p
  51. self._is_display_smells = key == pygame.K_s