Legend of the Gold Box... A game written for the LOWREZJAM 2018 game jam
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.4KB

  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):
  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):
  24. if not os.path.isfile(filename):
  25. raise LoadError("Failed to load '{}'. Path missing or invalid.".format(filename))
  26. try:
  27. return pygame.mixer.Sound(filename)
  28. except pygame.error as e:
  29. raise LoadError("Pygame Exception: {}".format(e.message))
  30. def load_JSON(filename):
  31. if not os.path.isfile(filename):
  32. raise LoaderError("File '{}' is missing or not a file.".format(filename))
  33. data = None
  34. try:
  35. with open(filename) as f:
  36. data = json.load(f)
  37. return data
  38. except Exception as e:
  39. raise e