import random import pygame from game.skill.skill_map import skill_map from game.skill.skill import Protect005s, Protect3s from game.skill.npc_skill import Skill21018 from game.algo import aStar from game.const import color as C from game.const import game as G from game.const import rouge as R from game.const import rouge_theme as T from game.frame import bomb, item from game.level import level from game.ui.game.blood_bar import BossBloodBar, BossHead from game.sound import sound_player from game.sprite.bomb_instance import BombInstance from game.sprite.ghost import Ghost from game.sprite.hero import Hero from game.sprite.item_instance import ItemInstance from game.sprite.player import Player, PlayerState from game.sprite.throwable import Throwable from game.file_loader.fileLoader import load_json, load_png # 配置logging基础配置并设置日志级别为INFO # logging.basicConfig(filename='npc.log', level=logging.INFO) origin_attr = {"blood": 1, "defense": 0, "contact": 1, "speed": 1} variant_attr = {"blood": 1.6, "defense": 2000, "contact": 1.2, "speed": 1.1} def get_variant_attr(is_var, a_name): if is_var: return variant_attr.get(a_name) return origin_attr.get(a_name) def render_digit(screen, number, x_pos, y_pos, alpha, offset=0): num_img = G.NUMBER_IMG[int(max(min(number, 9), 0))] # 获取对应的数字图片 num_img.set_alpha(alpha) # 设置透明度 screen.blit(num_img, (x_pos + offset, y_pos - 110)) # 渲染到屏幕 class Npc(Player, Throwable): def __init__(self, npc_name, xy, color=C.CHARACTER_RED, variant=False): super(Npc, self).__init__(npc_name, xy, color) self.npc_json = None self.chs_name = None # Npc中文名 self.stats_name = npc_name self.idx_motion = {0: "R", 1: "U", 2: "L", 3: "D"} self.contact = 0 # Npc接触伤害 self.original_contact = 0 self.maria_contact = 0 self.resent_dist = None # Npc仇恨范围(几格以内) self.resentful = False # Npc玩家仇恨 self.mocking = False # Npc受到嘲笑药水的作用暂时仇恨 self.friendly = False # Npc受到玫瑰药水的作用暂时解除仇恨 self.boss_mode = False # Boss模式 self.refresh = 0 self.original_speed = 0 self.variant = variant self.isElite = False self.isBoss = False self.isSpawned = False self.isTreasure = False self.is_ghost = False # 幽灵模式 self.fake_blood = 0 self.energy_amount = 0 # 充能格数 self.max_energy_amount = 0 self.has_energy = False self.wall_walking = False # 穿墙 self.flying = False # 飞行(免疫墙体减速) self.sliding = False # 踩技能滑行中 self.sliding_direction = None # 踩技能后滑的方向 self.witch_protected_remove = False # 巫师无防护状态 self.fire_load_time = 0 self.slime = False self.last_in_water = 0 self.slime_time = 0 self.free_chasing = False self.chasing_point = (1, 1) self.imitating = False self.combo_num = 1 self.is_combo_target = False self.bomb_shield_num = 0 # 防糖泡电磁护盾 self.bomb_shield_protect = False self.is_target = True self.light = 0 # 鬼屋模式发光大小 0不发光,1发光20像素,2发光40像素 最大9 self.sharing_damage = False self.obstacle_can_push = False self.npc_time_init = None # Npc初始化时刻 self.wander_random = 10 # Npc在无仇恨时的闲逛时间 self.district_locked = True self.chase_path = None # Npc追踪路径 self.npc_skill_time = pygame.time.get_ticks() # npc施放技能时的时刻 设为成员变量是为了统一多个技能的时间 self.is_skill_init = False self.chs_name_bg = load_png(G.GAME_PATH + "\\" + "res/img/ui/game/misc171.png") # Npc姓名牌背景 # self.chs_name_bg = pygame.image.load("res/img/ui/game/misc171.png").convert_alpha() # Npc姓名牌背景 self.blood_bar = None self.blood_bar_pos = 0 self.profile = None # self.head_cx = -100000 # self.head_cy = -100000 self.chs_name_font = pygame.font.Font(G.GAME_PATH + "\\" + "res/font/simsun.ttc", 13) # Npc姓名牌字体 self.chs_name_text = None # Npc姓名牌前景文字 self.chs_name_text_shadow = None # Npc姓名牌背景文字 self.chs_name_text_half_width = None # Npc姓名牌文字的半宽 用于居中显示文字 self.chs_name_show = True # 是否显示名牌 self.bomb_skin = bomb.get_bomb("bomb1") self.gifts = None # 爆装备 self.death = None # 死亡状态 self.last_motion = 0 self.last_random = 10 self.energy_num = None self.load_npc(npc_name, color) def load_npc(self, npc_name, color): # 加载npc的json文件 # print(self.stats_name) if not R.test_mode: # self.npc_json = load_json(G.GAME_PATH + "\\" + "game/npc/" + npc_name + ".json") self.npc_json = load_json("game/npc/" + npc_name + ".json") else: try: self.npc_json = load_json(G.GAME_PATH + "\\" + "gtest/test_npc/" + npc_name + ".json") except Exception as e: print(e) self.npc_json = load_json(G.GAME_PATH + "\\" + "game/npc/" + npc_name + ".json") self.chs_name = self.npc_json["chs_name"] if self.variant: self.chs_name = "(异变)" + self.chs_name self.blood = int( self.npc_json["blood"] * R.monster_HP_rate * R.diff_HP[R.difficulty] * get_variant_attr(self.variant, "blood")) self.light = self.npc_json.get("light", self.light) if "self_damage_blood" in self.npc_json.keys(): self.self_damage_blood = self.npc_json["self_damage_blood"] * get_variant_attr(self.variant, "blood") self.speed = (self.npc_json["speed"] * G.GAME_SQUARE * R.diff_speed[R.difficulty] * get_variant_attr( self.variant, "speed")) / 1000 self.original_speed = self.speed if self.npc_json["contact"] > 0: self.contact = int(self.npc_json["contact"] * R.diff_contact[R.difficulty] * get_variant_attr(self.variant, "contact")) else: self.contact = int(self.npc_json["contact"] / (R.diff_contact[R.difficulty] * get_variant_attr(self.variant, "contact"))) self.original_contact = self.contact self.defense = self.npc_json["defense"] + R.monster_defense_delta + R.diff_Delta_Defense[R.difficulty] + get_variant_attr(self.variant, "defense") if "boss_mode" in self.npc_json.keys(): self.boss_mode = bool(self.npc_json["boss_mode"]) if "elite" in self.npc_json.keys(): if self.npc_json["elite"]: try: self.chs_name_bg = load_png(G.GAME_PATH + "\\" + "res/img/ui/game/misc170.png") # self.chs_name_bg = pygame.image.load("res/img/ui/game/misc170.png").convert_alpha() # Npc姓名牌背景 except: pass if "boss" in self.npc_json.keys(): if self.npc_json["boss"]: self.boss_mode = True try: self.chs_name_bg = load_png(G.GAME_PATH + "\\" + "res/img/ui/game/misc169.png") # self.chs_name_bg = pygame.image.load("res/img/ui/game/misc169.png").convert_alpha() # Npc姓名牌背景 except: pass if "ally" in self.npc_json.keys(): if self.npc_json["ally"]: try: self.chs_name_bg = load_png(G.GAME_PATH + "\\" + "res/img/ui/game/misc172.png") # self.chs_name_bg = pygame.image.load("res/img/ui/game/misc172.png").convert_alpha() # Npc姓名牌背景 except: pass if "spawned" in self.npc_json.keys(): if self.npc_json["spawned"]: self.isSpawned = True if "treasure" in self.npc_json.keys(): if self.npc_json["treasure"]: self.isTreasure = True if "max_energy_amount" in self.npc_json.keys(): if self.npc_json["max_energy_amount"] > 0: self.has_energy = True self.max_energy_amount = self.npc_json["max_energy_amount"] + R.diff_power[R.difficulty] self.energy_amount = self.npc_json["max_energy_amount"] + R.diff_power[R.difficulty] else: pass if "flying" in self.npc_json.keys(): self.flying = self.npc_json["flying"] if "slime" in self.npc_json.keys(): self.slime = self.npc_json["slime"] if "gifts" in self.npc_json.keys(): self.gifts = self.npc_json["gifts"] if "death" in self.npc_json.keys(): self.death = self.npc_json["death"] if "show_name" in self.npc_json.keys(): self.chs_name_show = self.npc_json["show_name"] if "is_target" in self.npc_json.keys(): self.is_target = self.npc_json["is_target"] if "light" in self.npc_json.keys(): self.light = self.npc_json["light"] self.resent_dist = self.npc_json["resent_dist"] * G.GAME_SQUARE # 仇恨距离(像素) for n, s in enumerate(self.npc_json["skills"]): self.skill_names.append(s["name"]) self.skill_init_times.append(s["init"]) self.skill_intervals.append(s["interval"]) self.skill_remains.append(s["max"]) self.skill_params.append(s["params"] if "params" in s else []) self.skill_params_order.append(n) self.skill_spawns.append(s["spawns"] if "spawns" in s else []) self.skill_spawn_order.append(s["spawn_order"] if "spawn_order" in s else n) self.remain_blood = self.blood self.chase_path = dict() self.chs_name_text = self.chs_name_font.render(self.chs_name, True, (255, 255, 0)) self.chs_name_text_shadow = self.chs_name_font.render(self.chs_name, True, (0, 0, 0)) self.chs_name_text_half_width = self.chs_name_text.get_width() // 2 self.character = self.load_character(self.npc_json["character"], color, dict()) self.npc_time_init = pygame.time.get_ticks() if "head_pos" in self.npc_json.keys(): self.head_cx, self.head_cy = self.npc_json["head_pos"][0], self.npc_json["head_pos"][1] if "blood_bar" in self.npc_json.keys(): if self.npc_json["blood_bar"]: for bar_pos in range(1, 7): if level.current_level.boss_blood_bar_dic[bar_pos] == 0: level.current_level.boss_blood_bar_dic[bar_pos] = 1 self.blood_bar_pos = bar_pos self.blood_bar = BossBloodBar(self, bar_pos) level.current_level.ui_instances_top.append(self.blood_bar) self.profile = BossHead(self, bar_pos) level.current_level.ui_instances_top.append(self.profile) break else: if self.boss_mode: for bar_pos in range(1, 7): if level.current_level.boss_blood_bar_dic[bar_pos] == 0: level.current_level.boss_blood_bar_dic[bar_pos] = 1 self.blood_bar_pos = bar_pos self.blood_bar = BossBloodBar(self, bar_pos) level.current_level.ui_instances_top.append(self.blood_bar) self.profile = BossHead(self, bar_pos) level.current_level.ui_instances_top.append(self.profile) break def update(self): super().update() current_time = pygame.time.get_ticks() self.update_npc_blood_display(current_time) self.wander_and_detect(current_time) try: self.chase_hero() except Exception as e: pass self.contact_damage() self.try_using_skills() self.throw() def update_npc_blood_display(self, current_time): if G.DISPLAY_NPC_NAME_CARD and G.DISPLAY_NPC_BLOOD: self.chs_name_text = self.chs_name_font.render(str(self.remain_blood - self.fake_blood), True, (255, 255, 0)) self.chs_name_text_shadow = self.chs_name_font.render(str(self.remain_blood - self.fake_blood), True, (0, 0, 0)) self.chs_name_text_half_width = self.chs_name_text.get_width() // 2 else: self.chs_name_text = self.chs_name_font.render(self.chs_name, True, (255, 255, 0)) self.chs_name_text_shadow = self.chs_name_font.render(self.chs_name, True, (0, 0, 0)) self.chs_name_text_half_width = self.chs_name_text.get_width() // 2 def wander_and_detect(self, current_time): # 游走并检测玩家距离 if (self.resentful or self.mocking) and not self.friendly: return # 初始化时刻小于0.3s if current_time - self.npc_time_init < 300: return # 随机运动时间 if level.current_level.in_district: if (current_time - self.last_motion) >= 50 * self.last_random: if self.speed >= (5.8 * G.GAME_SQUARE) / 1000: self.last_random = random.randint(5, 15) if 0 <= (current_time % 1200) <= 300 or (random.randint(1, 100) >= 85): self.last_motion = current_time if random.randint(1, 100) >= 25: motion = self.idx_motion[random.randint(0, 3)] self.set_motion(motion) else: self.set_motion(self.orientation) else: self.set_motion(self.orientation) else: self.set_motion(self.orientation) # 技能初始时间更新 for i in range(len(self.skill_names)): # 游荡状态下,技能初始时间永远等于当前时刻加init值 self.skill_init_times[i] = self.npc_json["skills"][i]["init"] + current_time self.is_skill_init = True # 检测与玩家的距离 me: Hero = level.current_level.me if me.district_locked and me.state == PlayerState.NORMAL: if abs(me.x_pos - self.x_pos) < self.resent_dist and abs(me.y_pos - self.y_pos) < self.resent_dist: # 如果玩家活着、已进入区域锁,并且距离小于临界值 则产生玩家仇恨 self.resentful = True level.current_level.recal_npc_paths = True def chase_hero(self): if not self.resentful and not self.mocking or self.friendly or self.imitating or ( self.speed == 0 and self.original_speed == 0): return me = (level.current_level.me.x, level.current_level.me.y) if self.free_chasing: me = self.chasing_point # 玩家在糖泡中 定身Npc if len(level.current_level.get_bomb_instance(*me)) > 0 and not self.is_ghost and not self.free_chasing: self.set_motion() return now = (self.x, self.y) path_map = level.current_level.path_map # 如果重新计算路径标志为True则重新追踪玩家 if level.current_level.recal_npc_paths: if now not in path_map.keys(): self.chase_path = path_map[now] = aStar.cal_path( now, me, (level.current_level.district_square_grid["x1"], level.current_level.district_square_grid["y1"]), (level.current_level.district_square_grid["x2"], level.current_level.district_square_grid["y2"]), is_ghost=self.is_ghost, path_cache=path_map ) else: self.chase_path = path_map[now] if now in self.chase_path.keys(): _next = self.chase_path[now] if _next is None: self.set_motion() elif _next[0] > self.x: self.set_motion("R") elif _next[1] < self.y: self.set_motion("U") elif _next[0] < self.x: self.set_motion("L") elif _next[1] > self.y: self.set_motion("D") else: self.set_motion() def half_body_damage(self, point, cl, current_time): if self.bomb_shield_num > 0: if self.protected > 0: return if self.bomb_shield_protect: self.try_damage(cl.grid_damage_blood[point], bomb_damage=True) Protect3s(self, self.skill_instances) self.energy_amount = int(max(0, self.bomb_shield_num - 1)) return self.try_damage(cl.grid_damage_blood[point], bomb_damage=True) self.energy_amount = int(max(0, self.bomb_shield_num - cl.grid_damage_multiples[point])) Protect005s(self, self.skill_instances) return self.try_damage(cl.grid_damage_blood[point], bomb_damage=True) def contact_damage(self): # 检查npc与人物的接触伤害 if level.current_level.me.x == self.x and level.current_level.me.y == self.y: level.current_level.me.try_damage(self.contact, "C") def check_task(self): if self.stats_name == "RougeTreasureDestinyElite": R.task_RougeTreasureDestinyElite += 1 elif self.stats_name == "MineCFireDragonBoss": R.task_MineCFireDragonBoss += 1 elif self.stats_name == "RougeTreasureDestinyWizard": R.task_RougeTreasureDestinyWizard += 1 def die(self): super().die() sound_player.play("npc_dead") self.check_task() if R.pass_status[R.difficulty] == 2 and not R.story_destiny and R.rouge_story != "重塑光影" and R.rouge_story != "渊海之息": if self.stats_name in ["RougeTreasureDestiny", "RougeTreasureDestinyWizard", "RougeTreasureDestinyMachine"]: R.rouge_destiny[self.stats_name] = 1 if R.rouge_destiny["RougeTreasureDestiny"] + R.rouge_destiny["RougeTreasureDestinyWizard"] + \ R.rouge_destiny["RougeTreasureDestinyMachine"] == 3: level.current_level.message_entry.new_message(sender='命运', message='时空法则开始崩溃,因果维度相互纠缠') level.current_level.message_entry.new_message(sender='*获得永久效果(坍缩)', message='玩家每次被攻击时随机受到1~800点伤害,生命值+1000') R.random_dmg = True R.current_standard_HP += 1000 del R.se_district[15:20] level.current_level.me.blood += 1000 level.current_level.me.remain_blood += 1000 level.current_level.game.map_set_json = level.current_level.game.map_file = R.central_mapset = { "maps": ["rogueMap1", "rogueAttMap1", "rogueMap2", "rogueAttMap2", "rogueMap3", "rogueAttMap3", "rogueMap4", "rogueAttMap4", "rogueMap5", "rogueAttMap5", "rogueMap6", "rogueAttMap6", "rogueMap7", "rogueAttMap7", "rogueMap8", "rogueAttMap8", "rogueMap9", "rogueAttMap9", "rogueMap10", "rogueAttMap10", "rogueMap11", "rogueAttMap11", "rogueMap12", "rogueAttMap12", "rogueMap13", "rogueAttMap13", "rogueMap14", "rogueAttMap14", "rogueMap15", "rogueAttMap15_continue", "rogueMap16_destiny", "rogueAttMap16_destiny", "rogueMap17_destiny", "rogueAttMap17_destiny", "rogueMap18_destiny", "rogueAttMap18_destiny", "rogueMap19_destiny", "rogueAttMap19_destiny", "rogueMap20_destiny", "rogueAttMap20_destiny"]} R.rouge_story = "命运乐章" R.rouge_theme_4 = "重构秩序" R.story_destiny = True G.TOTAL_LEVEL = 40 for key, value in T.theme_destiny_mon_dic.items(): if value['type'] == 'normal': R.general_mon_dic["4"]["normal"].append(key) if value['type'] == 'elite': R.general_mon_dic["4"]["elite"].append(key) R.general_mon_dic["4"]["elite"].append("RogueDestinyASanGrail") if not self.isSpawned: variant_bonus = 1 if self.variant: variant_bonus = 3 if G.CURRENT_LEVEL <= 10: if self.stats_name in T.boss_list: R.coin += random.randint(int(4 * R.coin_lv(R.rouge_lv)), int(8 * R.coin_lv(R.rouge_lv))) * variant_bonus R.rouge_exp += random.randint(30, 50) * variant_bonus elif self.stats_name in T.elite_list: # R.coin += random.randint(0, int(2*R.coin_lv(R.rouge_lv))) R.coin += random.choices([0, 1, 2], weights=[1, R.coin_lv(R.rouge_lv) - 0.6, R.coin_lv(R.rouge_lv) - 0.8], k=1)[0] * variant_bonus R.rouge_exp += random.randint(3, 5) * variant_bonus else: # R.coin += random.randint(0, int(1*R.coin_lv(R.rouge_lv))) # random.choices([0, 1], weights=[70, 30], k=1)[0] R.coin += random.choices([0, 1], weights=[1.6, R.coin_lv(R.rouge_lv) - 0.6], k=1)[0] * variant_bonus R.rouge_exp += random.randint(1, 3) * variant_bonus elif 11 <= G.CURRENT_LEVEL <= 20: if self.stats_name in T.boss_list: R.coin += random.randint(int(6 * R.coin_lv(R.rouge_lv)), int(10 * R.coin_lv(R.rouge_lv))) * variant_bonus R.rouge_exp += random.randint(50, 80) * variant_bonus elif self.stats_name in T.elite_list: # R.coin += random.randint(0, int(3*R.coin_lv(R.rouge_lv))) R.coin += random.choices([0, 1, 2, 3], weights=[1, R.coin_lv(R.rouge_lv) - 0.4, R.coin_lv(R.rouge_lv) - 0.6, R.coin_lv(R.rouge_lv) - 0.8], k=1)[0] * variant_bonus R.rouge_exp += random.randint(4, 6) * variant_bonus else: # R.coin += random.randint(0, int(2*R.coin_lv(R.rouge_lv))) R.coin += random.choices([0, 1, 2], weights=[1.6, R.coin_lv(R.rouge_lv) - 0.4, R.coin_lv(R.rouge_lv) - 0.6], k=1)[0] * variant_bonus R.rouge_exp += random.randint(2, 4) * variant_bonus elif 21 <= G.CURRENT_LEVEL <= 30: if self.stats_name in T.boss_list: R.coin += random.randint(int(8 * R.coin_lv(R.rouge_lv)), int(12 * R.coin_lv(R.rouge_lv))) * variant_bonus R.rouge_exp += random.randint(100, 150) * variant_bonus elif self.stats_name in T.elite_list: # R.coin += random.randint(0, int(3*R.coin_lv(R.rouge_lv))) R.coin += random.choices([0, 1, 2, 3, 4], weights=[1, R.coin_lv(R.rouge_lv) - 0.2, R.coin_lv(R.rouge_lv) - 0.4, R.coin_lv(R.rouge_lv) - 0.6, R.coin_lv(R.rouge_lv) - 0.8], k=1)[0] * variant_bonus R.rouge_exp += random.randint(5, 8) * variant_bonus else: # R.coin += random.randint(0, int(2*R.coin_lv(R.rouge_lv))) R.coin += random.choices([0, 1, 2, 3], weights=[1.6, R.coin_lv(R.rouge_lv) - 0.2, R.coin_lv(R.rouge_lv) - 0.4, R.coin_lv(R.rouge_lv) - 0.6], k=1)[0] * variant_bonus R.rouge_exp += random.randint(3, 5) * variant_bonus else: if self.stats_name in T.boss_list: R.coin += random.randint(int(30 * R.coin_lv(R.rouge_lv)), int(50 * R.coin_lv(R.rouge_lv))) * variant_bonus R.rouge_exp += random.randint(150, 250) * variant_bonus elif self.stats_name in T.elite_list: R.coin += random.randint(1, 2) * random.choices( [2, 3, 4], weights=[ R.coin_lv(R.rouge_lv), R.coin_lv(R.rouge_lv) - 0.2, R.coin_lv(R.rouge_lv) - 0.4 ], k=1)[0] * variant_bonus R.rouge_exp += random.randint(8, 12) * variant_bonus else: R.coin += random.randint(1, 2) * random.choices([2, 3], weights=[R.coin_lv(R.rouge_lv), R.coin_lv(R.rouge_lv) - 0.2], k=1)[0] * variant_bonus R.rouge_exp += random.randint(5, 7) * variant_bonus if self.blood_bar_pos != 0: try: level.current_level.ui_instances_top.remove(self.blood_bar) level.current_level.ui_instances_top.remove(self.profile) level.current_level.boss_blood_bar_dic[self.blood_bar_pos] = 0 except Exception as e: print(e) try: print("血条错误", self.chs_name, self.blood_bar_pos) except Exception as e: print(f"npc.py: die(): {e}") self.hell_plus_death_soldier_spawn() self.gene_gifts() self.gene_ghost() def hell_plus_death_soldier_spawn(self): if self.stats_name in {"RogueEdenAGhostWizard", "RogueEdenAGhost"}: return if not self.isSpawned and not self.isTreasure and R.difficulty == "Hell+" and random.randint(0, 100) >= 97: n_name = random.choice(["RogueEdenAGhostWizard", "RogueEdenAGhost"]) an_npc = Npc(n_name, (self.x, self.y)) Protect3s(an_npc, an_npc.skill_instances) level.current_level.npcs.append(an_npc) Skill21018(an_npc, level.current_level.me, level.current_level.skill_instances) def gene_gifts(self): if not self.boss_mode: return if self.stats_name not in ["RougeTreasureDestinyWizard", "RogueForestAWizardKing"]: return cl = level.current_level gx = int((cl.map_x_pos + 1) / 40 - 1) gy = int((cl.map_y_pos + 1) / 40 - 1) x_range = sorted(range(max(0, self.x - 2), min(self.x + 3, gx)), key=lambda k: abs(k)) y_range = sorted(range(max(0, self.y - 2), min(self.y + 3, gy)), key=lambda k: abs(k)) gift = "" prob = 0 diff_add = {"Easy": 0, "Normal": 0.03, "Hell": 0.06, "Hell+": 0.08} if self.stats_name == "RougeTreasureDestinyWizard": print("RougeTreasureDestinyWizard") gift = "item_bomb26001" prob = 0.03 + diff_add[R.difficulty] if self.stats_name == "RogueForestAWizardKing": print("RogueForestAWizardKing") gift = "item_bomb26002" prob = 0.03 + diff_add[R.difficulty] if prob - random.uniform(0, 1) > 0: grids = [] for x in x_range: for y in y_range: if (x, y) not in cl.obstacle_instances.keys() and (x, y) not in cl.item_instances.keys() and len( cl.get_bomb_instance(x, y)) == 0: grids.append((x, y)) if len(grids) >= 3: break if len(grids) >= 3: break if len(grids) == 0: print("哦豁没位置") return random.shuffle(grids) point = grids[0] try: print(gift) an_item = ItemInstance(self.x, self.y, level.current_level.item_instances, item.get_item(gift)) an_item.throw_to(point[0], point[1], an_item.get_direction(point[0], point[1])) except Exception as e: print(e) def gene_ghost(self): if self.death is None: return for gene in self.death: if "ghost" in gene.keys(): # 生成幽灵Ghost,指定生存时间,速度,接触伤害(自身无敌 + 穿墙穿泡、无视区域) level.current_level.ghosts.append( Ghost(gene["name"], (self.x, self.y), gene["ghost"]["time"], gene["ghost"]["speed"], gene["ghost"]["contact"])) elif "prob" in gene.keys(): if float(gene["prob"]) - random.uniform(0, 1) > 0: # 生成普通Npc,并给予无敌防护 an_npc = Npc(gene["name"], (self.x, self.y)) Protect3s(an_npc, an_npc.skill_instances) level.current_level.npcs.append(an_npc) if "inherit" in gene.keys(): an_npc.remain_blood = self.remain_blood break else: pass else: # 生成普通Npc,并给予无敌防护 an_npc = Npc(gene["name"], (self.x, self.y)) Protect3s(an_npc, an_npc.skill_instances) level.current_level.npcs.append(an_npc) if "inherit" in gene.keys(): an_npc.remain_blood = self.remain_blood def collide_wall(self): # npc 游走时撞墙 则改变方向 if self.speed >= (5.8 * G.GAME_SQUARE) / 1000: if self.orientation == "U": if random.randint(1, 100) >= 20: self.set_motion("D") else: if random.randint(1, 100) >= 40: self.set_motion("L") else: self.set_motion("R") elif self.orientation == "D": if random.randint(1, 100) >= 20: self.set_motion("U") else: if random.randint(1, 100) >= 40: self.set_motion("R") else: self.set_motion("L") elif self.orientation == "L": if random.randint(1, 100) >= 20: self.set_motion("R") else: if random.randint(1, 100) >= 40: self.set_motion("D") else: self.set_motion("U") elif self.orientation == "R": if random.randint(1, 100) >= 20: self.set_motion("L") else: if random.randint(1, 100) >= 40: self.set_motion("U") else: self.set_motion("D") else: if self.orientation == "U": self.set_motion("D") if self.orientation == "D": self.set_motion("U") if self.orientation == "L": self.set_motion("R") if self.orientation == "R": self.set_motion("L") def try_push(self, direction, offset=(0, 0)): pass def try_using_skills(self): # npc 循环使用技能 if not self.resentful and not self.mocking or self.friendly: return # npc 技能时间更新 self.npc_skill_time = pygame.time.get_ticks() if not self.is_skill_init: self.is_skill_init = True for i in range(len(self.skill_names)): self.skill_init_times[i] = self.npc_json["skills"][i]["init"] + pygame.time.get_ticks() # 每一帧都尝试使用全部技能 for i in range(len(self.skill_names)): self.use_skill(i) def use_skill(self, idx: int): if self.npc_skill_time < self.skill_init_times[idx] or self.skill_remains[idx] == 0: # 未到初始时间 或技能冷却中 或次数已用完(-1则可以无限使用) return self.skill_init_times[idx] += self.skill_intervals[idx] self.skill_remains[idx] -= 1 name = self.skill_names[idx] param = self.skill_params[idx] spawn_order = self.skill_spawn_order[idx] try: self.using_skill_map(name, param, spawn_order) except Exception as e: print(f"{name}: {e}") def set_bomb(self): # npc尝试放置一个糖泡 if self.state != PlayerState.NORMAL: return p = (self.x, self.y) bis = level.current_level.bomb_instances bs = level.current_level.get_bomb_instance(*p) if len(bs) > 0: # 当前位置有糖泡 return BombInstance(*p, bis, self.bomb_skin, 3, 1000) def set_general_bomb(self, power, damage, skin=False): # npc尝试放置一个糖泡 if self.state != PlayerState.NORMAL: return p = (self.x, self.y) bis = level.current_level.bomb_instances bs = level.current_level.get_bomb_instance(*p) if len(bs) > 0: # 当前位置有糖泡 return sound_player.play("bomb") if not skin: BombInstance(*p, bis, self.bomb_skin, power, damage) else: BombInstance(*p, bis, bomb.get_bomb(skin), power, damage) def set_custom_bomb(self, x, y, power, damage): # npc尝试放置一个糖泡 if self.state != PlayerState.NORMAL: return p = (x, y) bis = level.current_level.bomb_instances bs = level.current_level.get_bomb_instance(*p) if len(bs) > 0: # 当前位置有糖泡 return BombInstance(*p, bis, self.bomb_skin, power, damage) def set_restoration(self, to_x, to_y): self.x = to_x self.y = to_y def draw(self, screen: pygame.Surface): super().draw(screen) if G.DISPLAY_NPC_NAME_CARD and self.chs_name_show: self.chs_name_bg.set_alpha(self.temporary_alpha) screen.blit(self.chs_name_bg, (self.x_pos - 40, self.y_pos - 100)) self.chs_name_text_shadow.set_alpha(self.temporary_alpha) screen.blit(self.chs_name_text_shadow, (self.x_pos + 3 - self.chs_name_text_half_width, self.y_pos - 95)) self.chs_name_text.set_alpha(self.temporary_alpha) screen.blit(self.chs_name_text, (self.x_pos + 2 - self.chs_name_text_half_width, self.y_pos - 95)) self.render_energy(screen) def render_energy(self, screen): if not self.has_energy: return energy_str = str(self.energy_amount) max_len = len(energy_str) if max_len > 5: energy_str = '99999' x_pos_offset = -5 * max(0, max_len - 1) # 初始偏移量 for i, digit in enumerate(energy_str): render_digit(screen, int(digit), self.x_pos, self.y_pos, self.temporary_alpha, x_pos_offset + 10 * i) def using_skill_map(self, skill_name, param, spawn_order): cl = level.current_level if skill_name not in skill_map.keys(): return try: skill_attributes = skill_map[skill_name] skill_class = skill_attributes["skill"] skill_target = skill_attributes.get("target") skill_target_map = {"me": cl.me, "npcs": cl.npcs, "listed_me": [cl.me]} final_target = None if skill_target_map is not None: final_target = skill_target_map.get(skill_target) effect_target_map = {"self": self.skill_instances, "cl": cl.skill_instances} effect_target = effect_target_map.get(skill_attributes.get("effect")) if skill_attributes.get("old_spawn"): skill_class(self, final_target, self.skill_spawns, spawn_order, effect_target) # 旧版召唤 elif skill_target is not None: if not skill_attributes.get("param"): skill_class(self, final_target, effect_target) # 有目标无参数 else: skill_class(self, final_target, param, effect_target) # 有目标有参数 else: if not skill_attributes.get("param"): skill_class(self, effect_target) # 无目标无参数 else: skill_class(self, param, effect_target) # 无目标有参数 except Exception as e: print(f"{skill_name}: {e}")