Pygame.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_EXPLO, MOLECULES, SMELL_EGG, SMELL_FOOD, MOLECULES_DIRECTION, \
  6. MOVE_BYBASS_MEMORY
  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. molecule = point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=PHEROMON_DIR_EXPLO)
  31. self.draw_surface(point, molecule_exploration_surface)
  32. adapted_point = self._get_real_pixel_position_of_position(point)
  33. myfont = pygame.font.SysFont("monospace", 15)
  34. label = myfont.render(str(molecule.get_distance()), 1, (128,255,128))
  35. self._screen.blit(label, adapted_point)
  36. except NoMolecule:
  37. pass # No molecule here
  38. try:
  39. molecule = point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=SMELL_FOOD)
  40. self.draw_surface(point, smell_food_surface)
  41. adapted_point = self._get_real_pixel_position_of_position(point)
  42. adapted_point = (adapted_point[0]+10, adapted_point[1]+10)
  43. myfont = pygame.font.SysFont("monospace", 15)
  44. label = myfont.render(str(molecule.get_distance()), 1, (255,255,0))
  45. self._screen.blit(label, adapted_point)
  46. except NoMolecule:
  47. pass # No molecule here
  48. try:
  49. molecule = point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=SMELL_EGG)
  50. self.draw_surface(point, smell_food_surface)
  51. adapted_point = self._get_real_pixel_position_of_position(point)
  52. adapted_point = (adapted_point[0]+10, adapted_point[1]+10)
  53. myfont = pygame.font.SysFont("monospace", 15)
  54. label = myfont.render(str(molecule.get_distance()), 1, (255,255,0))
  55. self._screen.blit(label, adapted_point)
  56. except NoMolecule:
  57. pass # No molecule here
  58. try:
  59. point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=SMELL_EGG)
  60. self.draw_surface(point, smell_egg_surface)
  61. except NoMolecule:
  62. pass # No molecule here
  63. def _key_pressed(self, key):
  64. if key == pygame.K_m:
  65. if self._is_display_molecules:
  66. self._is_display_molecules = False
  67. else:
  68. self._is_display_molecules = True
  69. def draw_object(self, obj, point):
  70. super().draw_object(obj, point)
  71. # TODO: DEBUG
  72. if isinstance(obj, Ant):
  73. myfont = pygame.font.SysFont("monospace", 15)
  74. label = myfont.render(str(obj.get_id()), 1, (255,255,0))
  75. self._draw_callbacks.append(lambda: self._screen.blit(label, point))
  76. label2 = myfont.render(',', 1, (0,0,0))
  77. ant_mem = self._context.metas.value.get(MOVE_BYBASS_MEMORY, obj.get_id(), allow_empty=True,
  78. empty_value=[])
  79. def print_mem(points):
  80. for m in points:
  81. real_point = self._get_real_pixel_position_of_position((0, m[0], m[1]))
  82. self._screen.blit(label2, real_point)
  83. self._draw_callbacks.append(lambda: print_mem(ant_mem) )
  84. def start_of_cycle(self):
  85. super().start_of_cycle()
  86. self._draw_callbacks = []
  87. def end_of_cycle(self):
  88. for draw_callback in self._draw_callbacks:
  89. draw_callback()
  90. super().end_of_cycle()