Legend of the Gold Box... A game written for the LOWREZJAM 2018 game jam
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

69 lines
2.2KB

  1. import os
  2. import pygame
  3. class LoadError(Exception):
  4. pass
  5. def calculate_real_path(path):
  6. path = os.path.expandvars(os.path.expanduser(path))
  7. path = os.path.realpath(path)
  8. path = os.path.abspath(path)
  9. return os.path.normcase(os.path.normpath(path))
  10. def join_path(lpath, rpath):
  11. return os.path.normcase(os.path.normpath(os.path.join(lpath, rpath)))
  12. def file_exists(path):
  13. return os.path.isfile(path)
  14. def load_image(filename, params={}):
  15. if not os.path.isfile(filename):
  16. raise LoadError("Failed to load '{}'. Path missing or invalid.".format(filename))
  17. with open(filename) as f:
  18. try:
  19. i = pygame.image.load(f, filename)
  20. return i.convert_alpha()
  21. except pygame.error as e:
  22. raise LoadError("Pygame/SDL Exception: {}".format(e.message))
  23. def load_audio(filename, params={}):
  24. if not os.path.isfile(filename):
  25. raise LoadError("Failed to load '{}'. Path missing or invalid.".format(filename))
  26. try:
  27. if pygame.mixer.get_init() is not None:
  28. return pygame.mixer.Sound(filename)
  29. except pygame.error as e:
  30. raise LoadError("Pygame Exception: {}".format(e.message))
  31. raise LoadError("Audio subsystem not initialized before attempting to obtain resource.")
  32. def load_font(filename, params={}):
  33. if not os.path.isfile(filename):
  34. raise LoadError("Failed to load '{}'. Path missing or invalid.".format(filename))
  35. try:
  36. if pygame.font.get_init():
  37. size = 26
  38. if "size" in params:
  39. if isinstance(params["size"], int) and params["size"] > 0:
  40. size = params["size"]
  41. return pygame.font.Font(filename, size)
  42. except pygame.error as e:
  43. raise LoadError("Pygame Exception: {}".format(e.message))
  44. raise LoadError("Font subsystem not initialized before attempting to obtain resource.")
  45. def load_JSON(filename, params={}):
  46. if not os.path.isfile(filename):
  47. raise LoaderError("File '{}' is missing or not a file.".format(filename))
  48. data = None
  49. try:
  50. with open(filename) as f:
  51. data = json.load(f)
  52. return data
  53. except Exception as e:
  54. raise e