@newstart skills[literate]就是嵌套的字典变量呗?
terry
-
新版功能测试中 -
初步完成状态窗口后边慢慢优化

# 清华西游记状态栏和状态窗口 import webbrowser, asyncio, ast from pymud import Session, IConfig, alias, trigger, timer, gmcp, exception, Trigger, SimpleTrigger, SimpleAlias, GMCPTrigger, Command from wcwidth import wcswidth from utils.utils import experience_in_M class MyConfig(IConfig): def __init__(self, session: Session, *args, **kwargs) -> None: super().__init__(session, *args, **kwargs) # 将自定义的状态窗口函数赋值给会话的status_maker属性,这样会话就会使用该函数来显示状态信息。 self.session.application.set_status('当前任务:未知') # 未来可以作为当前任务的状态栏 self.session.status_maker = self.status_window def __unload__(self): super().__unload__() # 创建自定义的健康条用作分隔符 def create_status_bar(self, current, maximum, actual_max, bar_length = 20, bar_char = "—"): """ 创建一个健康条,用于显示当前健康值、有效健康值和剩余健康值。 :param current: 当前健康值 :param maximum: 当前最大健康值 :param actual_max: 真实最大值 :param bar_length: 健康条的长度 :param bar_char: 健康条的填充字符 :return: 一个包含健康条的列表 """ # 气血: 1153/ 1702 ( 64%) 内力: 10943 / 5659 (100%) (+360) bar_line = list() char_width = wcswidth(bar_char) # 计算有效健康值 filled_length = int(round(bar_length * current / actual_max / char_width)) # 计算有效健康值部分的长度 current_max_length = int(round(bar_length * maximum / actual_max / char_width)) # 计算剩余部分长度 remaining_length = bar_length - current_max_length # 构造健康条 bar_line.append(("fg:lightcyan", bar_char * filled_length)) bar_line.append(("fg:yellow", bar_char * (current_max_length - filled_length))) bar_line.append(("fg:red", bar_char * remaining_length)) return bar_line # 自定义状态栏窗口。该函数会被渲染框架高频调用,请注意不要在该函数中 info 或者执行其他输出信息!!! def status_window(self): styles = { "title" : "bold", "value" : "lightgreen", "value.better" : "lightcyan", "value.worse" : "yellow", "value.worst" : "red" } try: formatted_list = list() # line 0. hp bar kee = int(self.session.getVariable("kee", 0)) max_kee = int(self.session.getVariable("max_kee", 1)) kee_percent = int(self.session.getVariable("kee_percent", 1)) actual_max_kee = int(round(max_kee / (kee_percent / 100))) sen = int(self.session.getVariable("sen", 0)) max_sen = int(self.session.getVariable("max_sen", 1)) sen_percent = int(self.session.getVariable("sen_percent", 1)) actual_max_sen = int(round(max_sen / (sen_percent / 100))) bar_char = "━" screenwidth = self.session.application.get_width() bar_length = screenwidth // 2 - 1 span = screenwidth - 2 * bar_length kee_bar = self.create_status_bar(kee, max_kee, actual_max_kee, bar_length, bar_char) sen_bar = self.create_status_bar(sen, max_sen, actual_max_sen, bar_length, bar_char) formatted_list.extend(kee_bar) formatted_list.append(("", " " * span)) formatted_list.extend(sen_bar) formatted_list.append(("", "\n")) # line 1. char, family, master, deposit, food, water, exp, pot formatted_list.append((styles["title"], "【角色】")) formatted_list.append((styles["value"], "{0}({1})".format(self.session.getVariable('name'), self.session.getVariable('id')))) formatted_list.append(("", " ")) formatted_list.append((styles["title"], "【门派】")) formatted_list.append((styles["value"], "{}".format(self.session.getVariable('family')))) formatted_list.append(("", " ")) formatted_list.append((styles["title"], "【师父】")) formatted_list.append((styles["value"], "{}".format(self.session.getVariable('master')))) formatted_list.append(("", " ")) formatted_list.append((styles["title"], "【武学】")) formatted_list.append((styles["value"], "{}".format(self.session.getVariable('combat_exp')))) formatted_list.append((styles["value"], "({})".format(experience_in_M(self.session.getVariable('combat_exp'))))) formatted_list.append(("", " ")) formatted_list.append((styles["title"], "【道行】")) formatted_list.append((styles["value"], "{}".format(self.session.getVariable('daoxing_exp')))) formatted_list.append((styles["value"], "({})".format(experience_in_M(self.session.getVariable('daoxing_exp'))))) formatted_list.append(("", " ")) formatted_list.append((styles["title"], "【潜能】")) formatted_list.append((styles["value"], "{}".format(self.session.getVariable('potential')))) # line 2. hp # a new-line formatted_list.append(("", "\n")) formatted_list.append((styles["title"], "【气血】")) if max_kee < actual_max_sen: style = styles["value.worst"] elif kee < 0.8 * max_kee: style = styles["value.worse"] else: style = styles["value"] if max_kee == 0: pct1 = pct2 = 0 else: pct1 = 100.0 * kee / actual_max_kee pct2 = 100.0 * max_kee / actual_max_kee formatted_list.append((style, "{0}[{1:3.0f}%] / {2}[{3:3.0f}%]".format(kee, pct1, max_kee, pct2))) formatted_list.append(("", " ")) formatted_list.append((styles["title"], "【精神】")) if max_sen < actual_max_sen: style = styles["value.worst"] elif sen < 0.8 * max_sen: style = styles["value.worse"] else: style = styles["value"] if max_sen == 0: pct1 = pct2 = 0 else: pct1 = 100.0 * sen / actual_max_sen pct2 = 100.0 * max_sen / actual_max_sen formatted_list.append((style, "{0}[{1:3.0f}%] / {2}[{3:3.0f}%]".format(sen, pct1, max_sen, pct2))) formatted_list.append(("", " ")) formatted_list.append((styles["title"], "【食物】")) food = int(self.session.getVariable('food', '0')) max_food = int(self.session.getVariable('max_food', 350)) if food < 100: style = styles["value.worst"] elif food < 200: style = styles["value.worse"] elif food < max_food: style = styles["value"] else: style = styles["value.better"] formatted_list.append((style, "{0}/{1}".format(food, max_food))) formatted_list.append(("", " ")) formatted_list.append((styles["title"], "【饮水】")) water = int(self.session.getVariable('water', '0')) max_water = int(self.session.getVariable('max_water', 350)) if water < 100: style = styles["value.worst"] elif water < 200: style = styles["value.worse"] elif water < max_water: style = styles["value"] else: style = styles["value.better"] formatted_list.append((style, "{0}/{1}".format(water, max_water))) formatted_list.append(("", " ")) formatted_list.append((styles["title"], "【存款】")) formatted_list.append((styles["value"], "{}金".format(self.session.getVariable('deposit')))) formatted_list.append(("", " ")) # line 3. hp # a new-line formatted_list.append(("", "\n")) force = int(self.session.getVariable("force", 0)) max_force = int(self.session.getVariable("max_force", 1)) mana = int(self.session.getVariable("mana", 0)) max_mana = int(self.session.getVariable("max_mana", 1)) # 内力 formatted_list.append((styles["title"], "【内力】")) if force < 0.6 * max_force: style = styles["value.worst"] elif force < 0.8 * max_force: style = styles["value.worse"] elif force < 1.2 * max_force: style = styles["value"] else: style = styles["value.better"] if max_force == 0: pct = 0 else: pct = 100.0 * force / max_force formatted_list.append((style, "{0} / {1}[{2:3.0f}%]".format(force, max_force, pct))) formatted_list.append(("", " ")) # 精力 formatted_list.append((styles["title"], "【法力】")) if mana < 0.6 * max_mana: style = styles["value.worst"] elif mana < 0.8 * max_mana: style = styles["value.worse"] elif mana < 1.2 * max_mana: style = styles["value"] else: style = styles["value.better"] if max_mana == 0: pct = 0 else: pct = 100.0 * mana / max_mana formatted_list.append((style, "{0} / {1}[{2:3.0f}%]".format(mana, max_mana, pct))) formatted_list.append(("", " ")) return formatted_list except Exception as e: return f"{e}" -
如何加载不同模块(.py)?哦,我不上xkx的论坛的~~~
不过这个帖子好像之前搜索pyMud的时候看过,
但现在又忘记了
这记性真不行了 -
如何加载不同模块(.py)?@newstart 还得群主指导啊,我照猫画虎,优化了点儿
-
如何加载不同模块(.py)?最终的xkx_entry.py文件
import sys import os from pymud import IConfig, Session class EntryConfig(IConfig): def __init__(self, session: Session, *args, **kwargs): reload = kwargs.get("reload", False) # 处理参数里的reload, 用于支持 #reload self.session = session modules = list() root_dir = os.path.dirname(__file__) # 获取当前文件所在的目录 root_folder_name = os.path.basename(root_dir) # 获取当前文件所在的目录名 if os.path.exists(root_dir): for item in os.listdir(root_dir): #优先加载根目录.py文件,并排除 __init__.py if (not os.path.isdir(os.path.join(root_dir,item))) and item.endswith(".py") and (not item.startswith("__")) and (not item.endswith("entry.py")): modules.append(f"{root_folder_name}.{item[:-3]}") for item in os.listdir(root_dir): #二次循环,加载子文件夹下的.py文件,并排除 __init__.py if os.path.isdir(os.path.join(root_dir,item)) and (not item.startswith("__")): for file in os.listdir(os.path.join(root_dir,item)): if (not os.path.isdir(os.path.join(root_dir,item,file))) and file.endswith(".py") and (not file.startswith("__")): modules.append(f"{root_folder_name}.{item}.{file[:-3]}") session.load_module(modules) if reload: # 处理 reload session.reload_module(modules) self.modules = modules # 保存mods供unload使用 def __unload__(self): # 卸载时按照加载的反向顺序逐个卸载 for mod in reversed(self.modules): self.session.unload_module(mod) -
如何加载不同模块(.py)?初步试验了下,是可以的
-
如何加载不同模块(.py)?可否这样优化结构?
根目录下只放pymud.cfg。子目录放不同的mud游戏的文件夹,比如xkx和thu_xyj,然后其对应目录中放一个入口的py文件,比如xkx_entry.py。
但我不明白main.py还可以放什么代码|-----pymud.cfg # 配置文件,不同的游戏设置对应的default_script
|-----xkx # 存放所有xkx所有相关的脚本
|----- __init__.py # 指示 xkx 这个目录是一个 package。
|----- xkx_entry.py # xkx脚本入口
|----- main.py # 不知道需要放哪些通用的配置?
|----- triggers/
|----- __init__.py # 空文件,指示为package
|----- common_triggers.py # 通用trigger
|----- commands/
|----- __init__.py # 空文件,指示为package
|----- fight.py # fight 模块
|-----thu_xyj # 存放所有xkx所有相关的脚本, 与xkx类似
|----- __init__.py # 指示这个目录是一个 package。
|----- thu_entry.py # thu_xyj脚本入口, 其他文件略
|-----utils # 存放通用的方法,比如中文数字转换为阿拉伯数字的方法
|----- __init__.py # 指示 这个目录是一个 package。
|----- utils.py # thu_xyj脚本入口, 其他文件略 -
如何加载不同模块(.py)?太好了,今天就实践这些代码
-
如何加载不同模块(.py)?请教个问题,如何通过一个主.py文件来加载不同的模块,文件结构大概如下:
├── mud_main.py # 主入口
├── pymud.cfg # 配置文件,设置default_script": [" mud_main"]
├── triggers/
│ └── common_triggers.py # 通用trigger
├──commands/
│ └── fight.py如何写mud_main.py代码来加载其他文件夹下的模块?
-
关于多行Trigger的讨论@newstart 在 关于多行Trigger的讨论 中说:
或者可以自己定义多个触发器来配合实现总行数不确定的情况,比如第一个触发器tri1仅触发首行,第二个tri2仅触发尾行,第三个触发器tri3用 .* 作为全部匹配。当第一个触发器触发时,打开第三个触发器,然后对每一个进行判断;当第二个触发器触发时,关闭第三个触发器,这样就能全部手动处理了。
嗯,这个方案可行
-
关于多行Trigger的讨论class Trigger(MatchObject):
def init(self, session, patterns, *args, **kwargs):
pass其中参数
patterns:
匹配模式,应传递字符串(正则表达式或原始数据)。多行触发时,传递一个匹配模式的列表。之前记得在群里讨论,这样的多行触发无法预估结束边界,所以会出错,现在有好的解决办法么?
-
PyMUD运行的好伙伴 - tmux终端复用器唉,只会玩Windows怎么办?
-
PyMUD 帮助文档 -
PyMUD论坛上线啦我来也~~~~~
-
后天就是2026年了明天就是2026了