image_manager.py 1021 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import pygame
  2. if not pygame.font.get_init():
  3. pygame.font.init()
  4. __images = {}
  5. def add_image(name: str, imagefile: str):
  6. surf = pygame.image.load(imagefile)
  7. __images[name] = surf
  8. def get_image(name: str):
  9. try:
  10. return __images[name].convert_alpha()
  11. except KeyError:
  12. print("WARN: ImageManager': Image '{name}' is not defined".format(name=name))
  13. return None
  14. def get_sprite(name: str, width: int, height: int, pos: int):
  15. try:
  16. img = __images[name].convert_alpha()
  17. except KeyError:
  18. print("WARN: ImageManager': Image '{name}' is not defined".format(name=name))
  19. return None
  20. x_count = img.get_width() // width
  21. pos_x = pos % x_count
  22. pos_y = (pos // x_count) - 1
  23. return img.subsurface((pos_x * width, pos_y * height, width, height))
  24. def del_image(name: str):
  25. try:
  26. __images[name] = None
  27. except KeyError:
  28. print("WARN: ImageManager': Trying to remove image '{name}' which was not defined".format(name=name))