Pygame.py 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from intelligine.core.exceptions import NoMolecule
  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_molecules = False
  12. self._is_display_smells = False
  13. def receive(self, actions_done):
  14. super().receive(actions_done)
  15. if self._is_display_molecules:
  16. molecules_positions = self._context.metas.list.get(PHEROMON_POSITIONS,
  17. PHEROMON_POSITIONS,
  18. allow_empty=True)
  19. self._display_molecules(molecules_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_molecules(self, molecules_positions, context):
  26. molecule_exploration_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_EXPLORATION)
  27. molecule_home_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_HOME)
  28. for point in molecules_positions:
  29. point_flavour = context.molecules().get_flavour(point)
  30. try:
  31. point_flavour.get_molecule(category=PHEROMON_DIRECTION, type=PHEROMON_DIR_HOME)
  32. self.draw_surface(point, molecule_home_surface)
  33. except NoMolecule:
  34. pass # No molecule here
  35. try:
  36. point_flavour.get_molecule(category=PHEROMON_DIRECTION, type=PHEROMON_DIR_EXPLO)
  37. self.draw_surface(point, molecule_exploration_surface)
  38. except NoMolecule:
  39. pass # No molecule 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. if key == pygame.K_p:
  51. if self._is_display_molecules:
  52. self._is_display_molecules = False
  53. else:
  54. self._is_display_molecules = True
  55. if key == pygame.K_s:
  56. if self._is_display_smells:
  57. self._is_display_smells = False
  58. else:
  59. self._is_display_smells = True