Pygame.py 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. 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. self._draw_callbacks = []
  14. def receive(self, actions_done):
  15. super().receive(actions_done)
  16. if self._is_display_molecules:
  17. molecules_positions = self._context.metas.list.get(MOLECULES,
  18. MOLECULES,
  19. allow_empty=True)
  20. self._display_molecules(molecules_positions, self._context)
  21. def _display_molecules(self, molecules_positions, context):
  22. molecule_exploration_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_EXPLORATION)
  23. molecule_home_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_HOME)
  24. smell_egg_surface = self._object_visualizer.get_surface(SURFACE_SMELL_EGG)
  25. smell_food_surface = self._object_visualizer.get_surface(SURFACE_SMELL_FOOD)
  26. for point in molecules_positions:
  27. point_flavour = context.molecules().get_flavour(point)
  28. try:
  29. molecule = point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=PHEROMON_DIR_EXPLO)
  30. self.draw_surface(point, molecule_exploration_surface)
  31. adapted_point = self._get_real_pixel_position_of_position(point)
  32. myfont = pygame.font.SysFont("monospace", 15)
  33. label = myfont.render(str(molecule.get_distance()), 1, (128,255,128))
  34. self._screen.blit(label, adapted_point)
  35. except NoMolecule:
  36. pass # No molecule here
  37. try:
  38. molecule = point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=SMELL_FOOD)
  39. self.draw_surface(point, smell_food_surface)
  40. adapted_point = self._get_real_pixel_position_of_position(point)
  41. adapted_point = (adapted_point[0]+10, adapted_point[1]+10)
  42. myfont = pygame.font.SysFont("monospace", 15)
  43. label = myfont.render(str(molecule.get_distance()), 1, (255,255,0))
  44. self._screen.blit(label, adapted_point)
  45. except NoMolecule:
  46. pass # No molecule here
  47. try:
  48. point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=SMELL_EGG)
  49. self.draw_surface(point, smell_egg_surface)
  50. except NoMolecule:
  51. pass # No molecule here
  52. def _key_pressed(self, key):
  53. if key == pygame.K_m:
  54. if self._is_display_molecules:
  55. self._is_display_molecules = False
  56. else:
  57. self._is_display_molecules = True
  58. def draw_object(self, obj, point):
  59. super().draw_object(obj, point)
  60. # TODO: DEBUG
  61. if isinstance(obj, Ant):
  62. myfont = pygame.font.SysFont("monospace", 15)
  63. label = myfont.render(str(obj.get_id()), 1, (255,255,0))
  64. self._draw_callbacks.append(lambda: self._screen.blit(label, point))
  65. def start_of_cycle(self):
  66. super().start_of_cycle()
  67. self._draw_callbacks = []
  68. def end_of_cycle(self):
  69. for draw_callback in self._draw_callbacks:
  70. draw_callback()
  71. super().end_of_cycle()