Pygame.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from intelligine.core.exceptions import NoMolecule
  2. from intelligine.synergy.object.ant.Ant import Ant
  3. from synergine_xyz.display.Pygame import Pygame as XyzPygame
  4. import pygame
  5. from intelligine.cst import PHEROMON_DIR_HOME, PHEROMON_DIR_EXPLO, MOLECULES, \
  6. SMELL_EGG, SMELL_FOOD, MOLECULES_DIRECTION
  7. from intelligine.display.pygame.visualisation import SURFACE_PHEROMONE_EXPLORATION, SURFACE_PHEROMONE_HOME, \
  8. SURFACE_SMELL_EGG, SURFACE_SMELL_FOOD
  9. class Pygame(XyzPygame):
  10. def __init__(self, config, context, synergy_manager):
  11. super().__init__(config, context, synergy_manager)
  12. self._is_display_molecules = False
  13. self._is_display_smells = False
  14. self._draw_callbacks = []
  15. def receive(self, actions_done):
  16. super().receive(actions_done)
  17. if self._is_display_molecules:
  18. molecules_positions = self._context.metas.list.get(MOLECULES,
  19. MOLECULES,
  20. allow_empty=True)
  21. self._display_molecules(molecules_positions, self._context)
  22. def _display_molecules(self, molecules_positions, context):
  23. molecule_exploration_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_EXPLORATION)
  24. molecule_home_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_HOME)
  25. smell_egg_surface = self._object_visualizer.get_surface(SURFACE_SMELL_EGG)
  26. smell_food_surface = self._object_visualizer.get_surface(SURFACE_SMELL_FOOD)
  27. for point in molecules_positions:
  28. point_flavour = context.molecules().get_flavour(point)
  29. try:
  30. point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=PHEROMON_DIR_HOME)
  31. self.draw_surface(point, molecule_home_surface)
  32. except NoMolecule:
  33. pass # No molecule here
  34. try:
  35. molecule = point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=PHEROMON_DIR_EXPLO)
  36. self.draw_surface(point, molecule_exploration_surface)
  37. adapted_point = self._get_real_pixel_position_of_position(point)
  38. myfont = pygame.font.SysFont("monospace", 15)
  39. label = myfont.render(str(molecule.get_distance()), 1, (128,255,128))
  40. self._screen.blit(label, adapted_point)
  41. except NoMolecule:
  42. pass # No molecule here
  43. try:
  44. molecule = point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=SMELL_FOOD)
  45. self.draw_surface(point, smell_food_surface)
  46. adapted_point = self._get_real_pixel_position_of_position(point)
  47. adapted_point = (adapted_point[0]+10, adapted_point[1]+10)
  48. myfont = pygame.font.SysFont("monospace", 15)
  49. label = myfont.render(str(molecule.get_distance()), 1, (255,255,0))
  50. self._screen.blit(label, adapted_point)
  51. except NoMolecule:
  52. pass # No molecule here
  53. try:
  54. point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=SMELL_EGG)
  55. self.draw_surface(point, smell_egg_surface)
  56. except NoMolecule:
  57. pass # No molecule here
  58. def _key_pressed(self, key):
  59. if key == pygame.K_m:
  60. if self._is_display_molecules:
  61. self._is_display_molecules = False
  62. else:
  63. self._is_display_molecules = True
  64. def draw_object(self, obj, point):
  65. super().draw_object(obj, point)
  66. # TODO: DEBUG
  67. if isinstance(obj, Ant):
  68. myfont = pygame.font.SysFont("monospace", 15)
  69. label = myfont.render(str(obj.get_id()), 1, (255,255,0))
  70. self._draw_callbacks.append(lambda: self._screen.blit(label, point))
  71. def start_of_cycle(self):
  72. super().start_of_cycle()
  73. self._draw_callbacks = []
  74. def end_of_cycle(self):
  75. for draw_callback in self._draw_callbacks:
  76. draw_callback()
  77. super().end_of_cycle()