Pygame.py 5.2KB

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