Pygame.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=SMELL_EGG)
  50. self.draw_surface(point, smell_egg_surface)
  51. except NoMolecule:
  52. pass # No molecule here
  53. def _key_pressed(self, key):
  54. if key == pygame.K_m:
  55. if self._is_display_molecules:
  56. self._is_display_molecules = False
  57. else:
  58. self._is_display_molecules = True
  59. def draw_object(self, obj, point):
  60. super().draw_object(obj, point)
  61. # TODO: DEBUG
  62. if isinstance(obj, Ant):
  63. myfont = pygame.font.SysFont("monospace", 15)
  64. label = myfont.render(str(obj.get_id()), 1, (255,255,0))
  65. self._draw_callbacks.append(lambda: self._screen.blit(label, point))
  66. label2 = myfont.render(',', 1, (0,0,0))
  67. ant_mem = self._context.metas.value.get(MOVE_BYBASS_MEMORY, obj.get_id(), allow_empty=True,
  68. empty_value=[])
  69. def print_mem(points):
  70. for m in points:
  71. real_point = self._get_real_pixel_position_of_position((0, m[0], m[1]))
  72. self._screen.blit(label2, real_point)
  73. self._draw_callbacks.append(lambda: print_mem(ant_mem) )
  74. def start_of_cycle(self):
  75. super().start_of_cycle()
  76. self._draw_callbacks = []
  77. def end_of_cycle(self):
  78. for draw_callback in self._draw_callbacks:
  79. draw_callback()
  80. super().end_of_cycle()