import json import os import sys import gc import pygame import time import concurrent.futures from functools import partial from datetime import datetime from json import JSONDecodeError from game.algo import rougeGen as RG from game.const import window as W, color as C, game as G from game.const import rouge as R from game.algo import utils as util from game.effect import effect from game.frame import character, flame from game.level.level import Level from game.sprite.hero import Hero from game.file_loader.fileLoader import load_json def garbage_collect(last_time): if time.time() - last_time[0] >= 10: collected = gc.collect() # print(f"Garbage collector: collected {collected} objects.") last_time[0] = time.time() def rouge_reset(): R.has_ocean_shop = False R.show_cursor = False R.coin = 8 R.current_standard_defense = 0 R.monster_defense_delta = 0 R.monster_HP_rate = 1 # 100% R.player_bomb_self_damage_reduce_rate = 1 # 100% R.slow_immune = 0 R.reverse_immune = 0 R.root_immune = 0 R.stun_immune = 0 R.random_dmg = False R.rouge_destiny["RougeTreasureDestiny"] = R.rouge_destiny["RougeTreasureDestinyWizard"] = R.rouge_destiny["RougeTreasureDestinyMachine"] = 0 def _preload_flame(orientation): try: flame.get_flame(orientation) except Exception as e: if R.print_mode: print(f"Error processing flame {orientation}: {e}") def _preload_effect(e): try: name = e.split('.')[0] effect.get_effect(name) except Exception as e: if R.print_mode: print(f"Error processing effect {e}: {e}") def _preload_character(f): try: jf = load_json(G.GAME_PATH + "\\" + "game/frame/character/" + f) character.get_character(jf, C.CHARACTER_WHITE, dict()) except Exception as e: if R.print_mode: print(f"Error processing character {f}: {e}") class Control: def __init__(self, screen, is_continue=False): self.map_json = None self.map_file = None self.map_name_json = None self.is_continue = is_continue self.cfg_json = None self.your_name = None # 玩家名称 self.map_set_json = None self.map_set_at = None # 当前在map_set的第几关 从0开始 self.mapmax_x = None # 当前关宽度 self.mapmax_y = None # 当前关高度 self.music_volume = None # 游戏音乐音量大小 self.sound_volume = None # 游戏音效音量大小 self.frame_rate = None # 游戏帧速率 self.display_frame_rate = None # 显示帧率 self.grid_damage_duration = None # 网格伤害持续时间 self.display_flags = 0 # 画面显示特征 self.screen = screen # 游戏值 self.me = None # 玩家Hero对象 self.current_level = None # 当前关卡Level对象 self.orientations = dict() # 方向键与字符串的映射 self.walking_stack = [] # 方向键栈 self.bomb_old = 0 # 连按空格键标志 self.f6_old = False # 连按F6键标志(名字牌显示开关) self.f7_old = False # 连按F7键标志(名字血量切换) self.pause_old = False self.reset_old = False # 连按reset键标志 self.skills_old = [False, False, False, False, False, False, False] # 连按技能键标志 self.message_old = [False, False, False, False] self.key2idx = dict() # 数字键与技能下标的映射 self.key2message = dict() self.cfg_space = None # 用户自定义空格 self.cfg_f6 = None # 用户自定义f6 self.cfg_f7 = None # 用户自定义f7 self.cfg_pause = None self.cfg_reset = None # 用户自定义重启0 self.text_font = None self.description_shown = False self.wall_walking = True # 初始化pygame W.PAGE = W.Page.GAME G.CONTROL = self self.init_game() # self.preload() self.proceed_game() # self.update() def init_game(self): # 游戏初始化 读取配置文件 try: with open("config.json", encoding="utf-8") as f: self.cfg_json = json.load(f) except JSONDecodeError: with open("config.json", encoding="utf-8_sig") as f: self.cfg_json = json.load(f) self.map_set_at = -1 G.DISPLAY_NPC_NAME_CARD = True G.DISPLAY_NPC_BLOOD = self.cfg_json["display_npc_blood"] self.music_volume = self.cfg_json["music_volume"] self.frame_rate = int(self.cfg_json["frame_rate"]) G.SOUND_VOLUME = self.cfg_json["sound_volume"] G.DISPLAY_FRAME_RATE = self.cfg_json["display_frame_rate"] G.FIRST_FRAME_SHORTEN_RATE = float(self.cfg_json["first_frame_shorten_rate"]) G.LOW_CONFIG_MODE = bool(self.cfg_json["low_config_mode"]) self.grid_damage_duration = G.GRID_DMG_DURATION self.your_name = self.cfg_json['your_name'] if G.IS_ONLINE: self.your_name = G.USERNAME self.init_keys(self.cfg_json["keys"]) self.text_font = pygame.font.Font("res/font/simsun.ttc", 16) G.DROP_ITEM = False G.LAST_MAP_TIME = G.LAST_LEVEL_TIME = pygame.time.get_ticks() // 1000 if not self.is_continue: self.map_set_json = { "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" ] } else: self.map_set_at = G.CURRENT_LEVEL - 1 self.map_set_json = R.central_mapset G.TOTAL_LEVEL = len(self.map_set_json["maps"]) G.MAP_SET_NAME = self.cfg_json["map_set"] G.SHOW_STAT = True G.MONSTER_RECORD = False try: G.SHOW_TIME = bool(self.cfg_json["show_time"]) except Exception as e: print(e) # G.LAST_MAP_TIME = G.TIME+3 # G.LAST_LEVEL_TIME = G.TIME+3 # logging.info(control准备判断是否有特殊地图参数, t={datetime.now().strftime("%Y-%m-%d %H:%M:%S")}') if not self.is_continue: self.map_file = { "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" ] } self.map_json = R.map_json_dic["rogueMap1"] else: self.map_file = R.central_mapset self.map_json = R.map_json_dic[R.central_mapset["maps"][G.CURRENT_LEVEL-1]] if "special" in self.map_json["basic"].keys(): if "defense" in self.map_json["basic"]["special"].keys(): G.SPECIAL_DEFENSE = self.map_json["basic"]["special"]["defense"] else: G.SPECIAL_DEFENSE = 0 if "damage" in self.map_json["basic"]["special"].keys(): G.SPECIAL_DAMAGE = self.map_json["basic"]["special"]["damage"] else: G.SPECIAL_DAMAGE = 0 if "speed" in self.map_json["basic"]["special"].keys(): G.SPECIAL_SPEED = self.map_json["basic"]["special"]["speed"] else: G.SPECIAL_SPEED = 0 if "power" in self.map_json["basic"]["special"].keys(): G.SPECIAL_POWER = self.map_json["basic"]["special"]["power"] else: G.SPECIAL_POWER = 0 if "bomb" in self.map_json["basic"]["special"].keys(): G.SPECIAL_BOMB = self.map_json["basic"]["special"]["bomb"] else: G.SPECIAL_BOMB = 0 else: G.SPECIAL_DAMAGE = 0 G.SPECIAL_DEFENSE = 0 G.SPECIAL_SPEED = 1 G.SPECIAL_POWER = 0 G.SPECIAL_BOMB = 0 def init_keys(self, keys_root): # 加载自定义按键 self.orientations[keys_root["RIGHT"]] = "R" self.orientations[keys_root["UP"]] = "U" self.orientations[keys_root["LEFT"]] = "L" self.orientations[keys_root["DOWN"]] = "D" self.key2idx[keys_root["1"]] = 0 self.key2idx[keys_root["2"]] = 1 self.key2idx[keys_root["3"]] = 2 self.key2idx[keys_root["4"]] = 3 self.key2idx[keys_root["5"]] = 4 self.key2idx[keys_root["6"]] = 5 self.key2idx[keys_root["7"]] = 6 self.key2message[keys_root["R"]] = 0 self.key2message[keys_root["T"]] = 1 self.key2message[keys_root["Y"]] = 2 self.key2message[keys_root["U"]] = 3 self.cfg_space = keys_root["SPACE"] self.cfg_f6 = keys_root["F6"] self.cfg_f7 = keys_root["F7"] self.cfg_reset = keys_root["RESET"] try: self.cfg_pause = keys_root["P"] except Exception as e: print(e) # self.cfg_pause = 112 @classmethod def preload(cls): try: with open("config.json", encoding="utf-8") as f: cfg_json = json.load(f) except JSONDecodeError: with open("config.json", encoding="utf-8_sig") as f: cfg_json = json.load(f) if not cfg_json["allow_preload"]: return # 使用 ThreadPoolExecutor 来并行化 I/O 操作 with concurrent.futures.ThreadPoolExecutor() as executor: futures = [] ch_root = "game/frame/character/" # 对所有文件进行异步处理 for f in os.listdir(ch_root): futures.append(executor.submit(_preload_character, f)) eff_root = "game/effect/effect" for e in os.listdir(eff_root): futures.append(executor.submit(_preload_effect, e)) for orientation in flame.ORIENTATIONS: futures.append(executor.submit(_preload_flame, orientation)) # 等待所有任务完成 for future in concurrent.futures.as_completed(futures): try: future.result() except Exception as e: print(f"Error during preload: {e}") def proceed_game(self, is_reset=False): # 进入下一个关卡 if self.map_set_at + 1 < len(self.map_set_json["maps"]): self.map_set_at += 1 map_name = self.map_set_json["maps"][self.map_set_at] hero_name = self.cfg_json["your_hero"] character_color = self.cfg_json["your_character_color"] print(map_name, self.map_set_at) self.set_level(self.your_name, map_name, hero_name, character_color, is_reset) else: # 正常刷新 if not self.description_shown: self.current_level.update(self.screen) def proceed_custom_game(self, custom_map): # 进入指定关卡 self.map_set_at = 0 map_name = custom_map hero_name = self.cfg_json["your_hero"] character_color = self.cfg_json["your_character_color"] G.TOTAL_LEVEL = G.CURRENT_LEVEL + 1 self.set_level(self.your_name, map_name, hero_name, character_color) G.HAS_RECORD = False def set_level(self, your_name, map_name, hero_name, character_color, is_reset=False): # 改变当前关卡 character_colors = { "Red": C.CHARACTER_RED, "Blue": C.CHARACTER_BLUE, "SkyBlue": C.CHARACTER_SKYBLUE, "Yellow": C.CHARACTER_YELLOW, "Green": C.CHARACTER_GREEN, "Orange": C.CHARACTER_ORANGE, "Pink": C.CHARACTER_PINK, "SakuraPink": C.CHARACTER_SAKURAPINK, "Purple": C.CHARACTER_PURPLE, "Black": C.CHARACTER_BLACK, "White": C.CHARACTER_WHITE, "ForestGreen": C.CHARACTER_FORESTGREEN, "GrassGreen": C.CHARACTER_GRASSGREEN, "Withered": C.CHARACTER_WITHERED } character_color = character_colors[character_color] # 保留我的技能剩余数 print(f">>>>>>>>>>>>>>> {hero_name}") me = Hero(hero_name, (0, 0), character_color) if self.me is not None and not is_reset: me.skill_remains = self.me.skill_remains self.me = me # 设置新关卡 self.current_level = Level(your_name, map_name, self.me, self.music_volume, self.grid_damage_duration, self) def update(self): last_time = [time.time()] start = time.time() while W.PAGE == W.Page.GAME: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.display.quit() pygame.quit() sys.exit() if self.current_level is not None: if self.current_level.finish_flag: # 如果过关 self.proceed_game() elif self.current_level.custom_finish_flag: # 如果过关 self.proceed_custom_game(self.current_level.custom_next_map) else: # 正常刷新 if not self.description_shown: self.current_level.update(self.screen) self.key_pressed() pygame.display.flip() pygame.time.Clock().tick(min(self.frame_rate, 2400)) garbage_collect(last_time) def key_pressed(self): # 键盘事件检测 keys = pygame.key.get_pressed() self.player_bomb(keys) self.player_move(keys) self.player_f6(keys) self.player_f7(keys) # self.player_pause(keys) self.player_reset(keys) self.player_skill(keys) self.player_message(keys) def player_move(self, keys): # 玩家移动 for enum in self.orientations.keys(): if keys[enum]: if enum not in self.walking_stack: self.walking_stack.insert(0, enum) else: if enum in self.walking_stack: self.walking_stack.remove(enum) # 取栈顶元素first first = 0 if len(self.walking_stack) > 0: first = self.walking_stack[0] # 判断first是哪个方向 if first == 0: self.me.set_motion() else: self.me.set_motion(self.orientations[first]) if G.IN_BUBBLE: if self.orientations[first] != G.LAST_KEY: G.LAST_KEY = self.orientations[first] G.KEY_QUEUE.append(self.orientations[first]) def player_bomb(self, keys): if keys[self.cfg_space]: if self.bomb_old == 0: self.me.set_bomb() self.bomb_old += 1 else: self.bomb_old = 0 def player_f6(self, keys): if keys[self.cfg_f6]: if self.f6_old is True: return # 按下F6键 G.DISPLAY_NPC_NAME_CARD = not G.DISPLAY_NPC_NAME_CARD self.f6_old = True else: self.f6_old = False def player_f7(self, keys): if keys[self.cfg_f7]: if self.f7_old is True: return # 按下F7键 G.DISPLAY_NPC_NAME_CARD = True G.DISPLAY_NPC_BLOOD = not G.DISPLAY_NPC_BLOOD self.f7_old = True else: self.f7_old = False def player_pause(self, keys): if keys[self.cfg_pause]: if self.pause_old is True: return # 按下键 G.PAUSE = not G.PAUSE self.pause_old = True else: self.pause_old = False def player_reset(self, keys): if keys[self.cfg_reset]: if self.reset_old is True: return self.do_reset(generate_new=True) else: self.reset_old = False def do_reset(self, generate_new=True): R.battle_district = [] R.se_district = [] R.story_eden, R.story_destiny, R.story_ocean, R.story_skyland = False, False, False, False R.continuing_game = False R.last_item = None user_documents_folder = os.path.expanduser("~\\Documents") # 获取用户的"文档"文件夹路径 qqtpve_folder = os.path.join(user_documents_folder, "QQTPVE") # 构建QQTPVE文件夹路径 os.makedirs(qqtpve_folder, exist_ok=True) # 确保QQTPVE文件夹存在,如果不存在则创建 last_game_file_path = os.path.join(qqtpve_folder, "rougeOBLastGame.rougedata") # 构建文件路径 if generate_new: if os.path.exists(last_game_file_path): os.remove(last_game_file_path) print("清除旧数据") rouge_reset() G.CURRENT_LEVEL = 0 if generate_new: RG.main_func() self.init_game() self.proceed_game(is_reset=True) self.reset_old = True G.CHEAT = False G.HAS_RECORD = False G.LAST_MAP_TIME = G.TIME G.LAST_LEVEL_TIME = G.TIME G.ITEM = 0 G.MONSTER_RECORD_LIST = [] G.HAS_RECORD_FULL = False G.HAS_GOLDEN = False self.cfg_json = load_json("config.json") self.map_set_json = { "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" ] } G.TOTAL_LEVEL = len(self.map_set_json["maps"]) self.map_name_json = R.map_json_dic["rogueMap1"] if "environment_effect" in self.map_name_json["basic"].keys(): G.ENVIRONMENT_EFFECT = self.map_name_json["basic"]["environment_effect"] else: G.ENVIRONMENT_EFFECT = [] if "player_light" in self.map_name_json["basic"].keys(): self.me.light = self.map_name_json["basic"]["player_light"] def player_skill(self, keys): for k in self.key2idx.keys(): idx = self.key2idx[k] # 常量转索引 if keys[k]: if self.skills_old[idx]: return self.me.use_skill(idx) self.skills_old[idx] = True else: self.skills_old[idx] = False def player_message(self, keys): for k in self.key2message.keys(): idx = self.key2message[k] # 常量转索引 if keys[k]: if self.message_old[idx]: return if R.debug_mode: G.LAST_LEVEL_TIME = self.current_level.times_num # print(R.current_standard_damage, G.SPECIAL_DAMAGE) R.current_standard_damage -= G.SPECIAL_DAMAGE R.current_standard_defense -= G.SPECIAL_DEFENSE self.proceed_game() self.me.use_message(idx) self.message_old[idx] = True else: self.message_old[idx] = False def reset(self, map_set): """ with open("config.json", "r", encoding="utf-8") as json_file: mapsetdata = json.load(json_file) mapsetdata["map_set"] = map_set with open("config.json", "w", encoding="utf-8") as json_file: json.dump(mapsetdata, json_file, indent=4, ensure_ascii=False) self.init_game() """ self.map_set_json = { "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" ] } self.map_set_at = 0 map_name = self.map_set_json["maps"][self.map_set_at] hero_name = self.cfg_json["your_hero"] character_color = self.cfg_json["your_character_color"] self.set_level(self.your_name, map_name, hero_name, character_color, is_reset=True) R.game_exp = 0 # R.coin = 8 self.reset_old = True G.CHEAT = False G.HAS_RECORD = False G.CURRENT_LEVEL = 1 G.LAST_MAP_TIME = G.TIME G.LAST_LEVEL_TIME = G.TIME G.ITEM = 0 # G.MONSTER_RECORD_LIST = [] G.HAS_RECORD_FULL = False G.HAS_GOLDEN = False self.map_set_json = { "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" ] } G.TOTAL_LEVEL = len(self.map_set_json["maps"]) self.map_name_json = R.map_json_dic["rogueMap1"] if "environment_effect" in self.map_name_json["basic"].keys(): G.ENVIRONMENT_EFFECT = self.map_name_json["basic"]["environment_effect"] else: G.ENVIRONMENT_EFFECT = [] if "player_light" in self.map_name_json["basic"].keys(): self.me.light = self.map_name_json["basic"]["player_light"] now = datetime.now() # 格式化年月日 year = now.strftime("%Y") month = now.strftime("%m") day = now.strftime("%d") # 移除月份和日期的前导零(如果有) month = month.lstrip("0") day = day.lstrip("0") R.today_datetime["year"] = int(year) R.today_datetime["month"] = int(month) R.today_datetime["day"] = int(day)