obstacle.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # decompyle3 version 3.9.3
  2. # Python bytecode version base 3.7.0 (3394)
  3. # Decompiled from: Python 3.13.1 (tags/v3.13.1:0671451, Dec 3 2024, 19:06:28) [MSC v.1942 64 bit (AMD64)]
  4. # Embedded file name: game\frame\obstacle.py
  5. import json, pygame
  6. from pathlib import Path
  7. from game.frame.frame import Frame
  8. OBSTACLE_FRAME_ROOT = "game/frame/obstacle/"
  9. OBSTACLE_IMG_ROOT = "res/img/mapElem/"
  10. obstacles = {}
  11. def get_obstacle(type, name):
  12. if type not in obstacles.keys():
  13. obstacles[type] = dict()
  14. if name not in obstacles[type].keys():
  15. obstacles[type][name] = load_obstacle(type, name)
  16. return obstacles[type][name]
  17. def load_obstacle(type, name):
  18. an_obstacle = {}
  19. path = Path(OBSTACLE_FRAME_ROOT) / type / (name + ".json")
  20. if not path.is_file():
  21. path = Path("game/frame/obstacles") / type / (name + ".json")
  22. with path.open(encoding="utf-8-sig") as f:
  23. obstacle_json = json.load(f)
  24. an_obstacle["TYPE"] = type
  25. an_obstacle["NAME"] = name
  26. an_obstacle["WIDTH"] = obstacle_json["WIDTH"]
  27. an_obstacle["HEIGHT"] = obstacle_json["HEIGHT"]
  28. append_obstacle(obstacle_json, an_obstacle, "STAND")
  29. return an_obstacle
  30. def append_obstacle(obstacle_json, an_obstacle, state: str):
  31. if state in obstacle_json:
  32. an_obstacle[state] = list()
  33. frames = obstacle_json[state]
  34. size = len(frames["IMG"])
  35. for i in range(size):
  36. img = pygame.image.load(OBSTACLE_IMG_ROOT + obstacle_json["TYPE"] + "/" + frames["IMG"][i]).convert_alpha()
  37. cx = frames["CX"][i]
  38. cy = frames["CY"][i]
  39. an_obstacle[state].append(Frame(img, cx, cy))