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.

83 lines
2.4KB

  1. import os
  2. import json
  3. import pygame
  4. class LoadError(Exception):
  5. pass
  6. def calculate_real_path(path):
  7. path = os.path.expandvars(os.path.expanduser(path))
  8. path = os.path.realpath(path)
  9. path = os.path.abspath(path)
  10. return os.path.normcase(os.path.normpath(path))
  11. def join_path(lpath, rpath):
  12. return os.path.normcase(os.path.normpath(os.path.join(lpath, rpath)))
  13. def file_exists(path):
  14. return os.path.isfile(path)
  15. def load_image(filename, params={}):
  16. if not os.path.isfile(filename):
  17. raise LoadError("Failed to load '{}'. Path missing or invalid.".format(filename))
  18. try:
  19. i = pygame.image.load(filename)
  20. return i.convert_alpha()
  21. except pygame.error as e:
  22. raise LoadError("Pygame/SDL Exception: {}".format(e))
  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))
  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))
  44. raise LoadError("Font subsystem not initialized before attempting to obtain resource.")
  45. class DataContainer:
  46. def __init__(self, data):
  47. self._data = data
  48. @property
  49. def data(self):
  50. return self._data
  51. def load_JSON(filename, params={}):
  52. if not os.path.isfile(filename):
  53. raise LoaderError("File '{}' is missing or not a file.".format(filename))
  54. data = None
  55. try:
  56. with open(filename) as f:
  57. data = json.load(f)
  58. return DataContainer(data)
  59. except Exception as e:
  60. raise e
  61. def save_JSON(filename, data):
  62. try:
  63. with open(filename, "w") as f:
  64. json.dump(data, f, indent=4)
  65. except Exception as e:
  66. raise e