obstacle.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import json
  2. import pygame
  3. import game.const.game as G
  4. OBSTACLE_FRAME_ROOT = "game/frame/obstacle/"
  5. OBSTACLE_IMG_ROOT = "res/img/mapElem/"
  6. obstacles = {}
  7. def get_obstacle(type, name):
  8. obstacles_json = {}
  9. if type not in obstacles.keys():
  10. obstacles[type] = dict()
  11. if name not in obstacles[type].keys():
  12. obstacles[type][name], obstacles_json = load_obstacle(type, name)
  13. if obstacles_json == {}:
  14. _, obstacles_json = load_obstacle(type, name)
  15. return obstacles[type][name], obstacles_json
  16. def load_obstacle(type, name):
  17. an_obstacle = {}
  18. # print(OBSTACLE_FRAME_ROOT + type + '/' + name + ".json")
  19. with open(G.GAME_PATH + "\\" + OBSTACLE_FRAME_ROOT + type + '/' + name + ".json") as f:
  20. obstacle_json = json.load(f)
  21. an_obstacle["WIDTH"] = obstacle_json["WIDTH"]
  22. an_obstacle["HEIGHT"] = obstacle_json["HEIGHT"]
  23. an_obstacle["BLOCK"] = an_obstacle["BLOCK_FLAME"] = obstacle_json["BLOCK"]
  24. an_obstacle["BREAKABLE"] = obstacle_json["BREAKABLE"]
  25. an_obstacle["CAN_HIDE"] = obstacle_json["CAN_HIDE"]
  26. if "BLOCK_FLAME" in obstacle_json.keys():
  27. an_obstacle["BLOCK_FLAME"] = obstacle_json["BLOCK_FLAME"]
  28. if "SLIDE" in obstacle_json.keys():
  29. an_obstacle["SLIDE"] = obstacle_json["SLIDE"]
  30. if "BACKGROUND" in obstacle_json.keys():
  31. an_obstacle["BACKGROUND"] = bool(obstacle_json["BACKGROUND"])
  32. if "CAN_PUSH" in obstacle_json.keys():
  33. an_obstacle["CAN_PUSH"] = bool(obstacle_json["CAN_PUSH"])
  34. if "PUSH_TIME" in obstacle_json.keys():
  35. an_obstacle["PUSH_TIME"] = obstacle_json["PUSH_TIME"]
  36. if "CONTACT" in obstacle_json.keys():
  37. an_obstacle["CONTACT"] = obstacle_json["CONTACT"]
  38. if "SLOW" in obstacle_json.keys():
  39. an_obstacle["SLOW"] = obstacle_json["SLOW"]
  40. if "EXPLODE" in obstacle_json.keys():
  41. an_obstacle["EXPLODE"] = obstacle_json["EXPLODE"]
  42. if "MULTI_TYPE" in obstacle_json.keys():
  43. an_obstacle["MULTI_TYPE"] = obstacle_json["MULTI_TYPE"]
  44. if "TYPE_LEN" in obstacle_json.keys():
  45. an_obstacle["TYPE_LEN"] = obstacle_json["TYPE_LEN"]
  46. an_obstacle["INTERVAL"] = obstacle_json["INTERVAL"]
  47. append_obstacle(obstacle_json, an_obstacle, "STAND")
  48. append_obstacle(obstacle_json, an_obstacle, "PUSH")
  49. append_obstacle(obstacle_json, an_obstacle, "DIE")
  50. append_obstacle(obstacle_json, an_obstacle, "TRIGGER")
  51. return an_obstacle, obstacle_json
  52. def append_obstacle(obstacle_json, an_obstacle, state: str):
  53. if state in obstacle_json:
  54. an_obstacle[state] = list()
  55. frames = obstacle_json[state]
  56. size = len(frames["IMG"])
  57. for i in range(size):
  58. # print(OBSTACLE_IMG_ROOT + obstacle_json["TYPE"] + '/' + frames["IMG"][i])
  59. img = pygame.image.load(G.GAME_PATH + "\\" + OBSTACLE_IMG_ROOT + obstacle_json["TYPE"] + '/' + frames["IMG"][i]).convert_alpha()
  60. cx = frames["CX"][i]
  61. cy = frames["CY"][i]
  62. an_obstacle[state].append(Frame(img, cx, cy))
  63. def switch_type(obstacle_js, an_obstacle, current_type):
  64. an_obstacle["STAND"] = list()
  65. frames = obstacle_js["TYPE_LEN"]
  66. img = pygame.image.load(G.GAME_PATH + "\\" + OBSTACLE_IMG_ROOT + obstacle_js["TYPE"] + '/' + frames["IMG"][current_type]).convert_alpha()
  67. cx = frames["CX"][current_type]
  68. cy = frames["CY"][current_type]
  69. an_obstacle["STAND"].append(Frame(img, cx, cy))
  70. class Frame:
  71. def __init__(self, image: pygame.Surface, cx, cy):
  72. self.image = image
  73. self.cx = cx
  74. self.cy = cy