| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- import json
- import pygame
- import game.const.game as G
- OBSTACLE_FRAME_ROOT = "game/frame/obstacle/"
- OBSTACLE_IMG_ROOT = "res/img/mapElem/"
- obstacles = {}
- def get_obstacle(type, name):
- obstacles_json = {}
- if type not in obstacles.keys():
- obstacles[type] = dict()
- if name not in obstacles[type].keys():
- obstacles[type][name], obstacles_json = load_obstacle(type, name)
- if obstacles_json == {}:
- _, obstacles_json = load_obstacle(type, name)
- return obstacles[type][name], obstacles_json
- def load_obstacle(type, name):
- an_obstacle = {}
- # print(OBSTACLE_FRAME_ROOT + type + '/' + name + ".json")
- with open(G.GAME_PATH + "\\" + OBSTACLE_FRAME_ROOT + type + '/' + name + ".json") as f:
- obstacle_json = json.load(f)
- an_obstacle["WIDTH"] = obstacle_json["WIDTH"]
- an_obstacle["HEIGHT"] = obstacle_json["HEIGHT"]
- an_obstacle["BLOCK"] = an_obstacle["BLOCK_FLAME"] = obstacle_json["BLOCK"]
- an_obstacle["BREAKABLE"] = obstacle_json["BREAKABLE"]
- an_obstacle["CAN_HIDE"] = obstacle_json["CAN_HIDE"]
- if "BLOCK_FLAME" in obstacle_json.keys():
- an_obstacle["BLOCK_FLAME"] = obstacle_json["BLOCK_FLAME"]
- if "SLIDE" in obstacle_json.keys():
- an_obstacle["SLIDE"] = obstacle_json["SLIDE"]
- if "BACKGROUND" in obstacle_json.keys():
- an_obstacle["BACKGROUND"] = bool(obstacle_json["BACKGROUND"])
- if "CAN_PUSH" in obstacle_json.keys():
- an_obstacle["CAN_PUSH"] = bool(obstacle_json["CAN_PUSH"])
- if "PUSH_TIME" in obstacle_json.keys():
- an_obstacle["PUSH_TIME"] = obstacle_json["PUSH_TIME"]
- if "CONTACT" in obstacle_json.keys():
- an_obstacle["CONTACT"] = obstacle_json["CONTACT"]
- if "SLOW" in obstacle_json.keys():
- an_obstacle["SLOW"] = obstacle_json["SLOW"]
- if "EXPLODE" in obstacle_json.keys():
- an_obstacle["EXPLODE"] = obstacle_json["EXPLODE"]
- if "MULTI_TYPE" in obstacle_json.keys():
- an_obstacle["MULTI_TYPE"] = obstacle_json["MULTI_TYPE"]
- if "TYPE_LEN" in obstacle_json.keys():
- an_obstacle["TYPE_LEN"] = obstacle_json["TYPE_LEN"]
- an_obstacle["INTERVAL"] = obstacle_json["INTERVAL"]
- append_obstacle(obstacle_json, an_obstacle, "STAND")
- append_obstacle(obstacle_json, an_obstacle, "PUSH")
- append_obstacle(obstacle_json, an_obstacle, "DIE")
- append_obstacle(obstacle_json, an_obstacle, "TRIGGER")
- return an_obstacle, obstacle_json
- def append_obstacle(obstacle_json, an_obstacle, state: str):
- if state in obstacle_json:
- an_obstacle[state] = list()
- frames = obstacle_json[state]
- size = len(frames["IMG"])
- for i in range(size):
- # print(OBSTACLE_IMG_ROOT + obstacle_json["TYPE"] + '/' + frames["IMG"][i])
- img = pygame.image.load(G.GAME_PATH + "\\" + OBSTACLE_IMG_ROOT + obstacle_json["TYPE"] + '/' + frames["IMG"][i]).convert_alpha()
- cx = frames["CX"][i]
- cy = frames["CY"][i]
- an_obstacle[state].append(Frame(img, cx, cy))
- def switch_type(obstacle_js, an_obstacle, current_type):
- an_obstacle["STAND"] = list()
- frames = obstacle_js["TYPE_LEN"]
- img = pygame.image.load(G.GAME_PATH + "\\" + OBSTACLE_IMG_ROOT + obstacle_js["TYPE"] + '/' + frames["IMG"][current_type]).convert_alpha()
- cx = frames["CX"][current_type]
- cy = frames["CY"][current_type]
- an_obstacle["STAND"].append(Frame(img, cx, cy))
- class Frame:
- def __init__(self, image: pygame.Surface, cx, cy):
- self.image = image
- self.cx = cx
- self.cy = cy
|