Pygame.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from intelligine.core.exceptions import NoMolecule
  2. from synergine_xyz.display.Pygame import Pygame as XyzPygame
  3. import pygame
  4. from intelligine.cst import PHEROMON_DIR_HOME, PHEROMON_DIR_EXPLO, MOLECULES, \
  5. 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. def receive(self, actions_done):
  14. super().receive(actions_done)
  15. if self._is_display_molecules:
  16. molecules_positions = self._context.metas.list.get(MOLECULES,
  17. MOLECULES,
  18. allow_empty=True)
  19. self._display_molecules(molecules_positions, self._context)
  20. def _display_molecules(self, molecules_positions, context):
  21. molecule_exploration_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_EXPLORATION)
  22. molecule_home_surface = self._object_visualizer.get_surface(SURFACE_PHEROMONE_HOME)
  23. smell_egg_surface = self._object_visualizer.get_surface(SURFACE_SMELL_EGG)
  24. smell_food_surface = self._object_visualizer.get_surface(SURFACE_SMELL_FOOD)
  25. for point in molecules_positions:
  26. point_flavour = context.molecules().get_flavour(point)
  27. try:
  28. point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=PHEROMON_DIR_HOME)
  29. self.draw_surface(point, molecule_home_surface)
  30. except NoMolecule:
  31. pass # No molecule here
  32. try:
  33. molecule = point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=PHEROMON_DIR_EXPLO)
  34. self.draw_surface(point, molecule_exploration_surface)
  35. adapted_point = self._get_real_pixel_position_of_position(point)
  36. myfont = pygame.font.SysFont("monospace", 15)
  37. label = myfont.render(str(molecule.get_distance()), 1, (128,255,128))
  38. self._screen.blit(label, adapted_point)
  39. except NoMolecule:
  40. pass # No molecule here
  41. try:
  42. molecule = point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=SMELL_FOOD)
  43. self.draw_surface(point, smell_food_surface)
  44. adapted_point = self._get_real_pixel_position_of_position(point)
  45. adapted_point = (adapted_point[0]+10, adapted_point[1]+10)
  46. myfont = pygame.font.SysFont("monospace", 15)
  47. label = myfont.render(str(molecule.get_distance()), 1, (255,255,0))
  48. self._screen.blit(label, adapted_point)
  49. except NoMolecule:
  50. pass # No molecule here
  51. try:
  52. point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=SMELL_EGG)
  53. self.draw_surface(point, smell_egg_surface)
  54. except NoMolecule:
  55. pass # No molecule here
  56. def _key_pressed(self, key):
  57. if key == pygame.K_m:
  58. if self._is_display_molecules:
  59. self._is_display_molecules = False
  60. else:
  61. self._is_display_molecules = True