Pygame.py 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=PHEROMON_DIR_EXPLO)
  34. self.draw_surface(point, molecule_exploration_surface)
  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. myfont = pygame.font.SysFont("monospace", 15)
  42. label = myfont.render(str(molecule.get_distance()), 1, (255,255,0))
  43. self._screen.blit(label, adapted_point)
  44. except NoMolecule:
  45. pass # No molecule here
  46. try:
  47. point_flavour.get_molecule(category=MOLECULES_DIRECTION, type=SMELL_EGG)
  48. self.draw_surface(point, smell_egg_surface)
  49. except NoMolecule:
  50. pass # No molecule here
  51. def _key_pressed(self, key):
  52. if key == pygame.K_m:
  53. if self._is_display_molecules:
  54. self._is_display_molecules = False
  55. else:
  56. self._is_display_molecules = True