You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python开发:如何从技能描述字符串中提取攻击倍率等乘数?

类宝可梦回合制游戏:技能数值提取与debuff实现方案

一、技能伤害倍率提取

用正则表达式可以精准匹配技能描述中的Nx格式倍率,支持整数和小数(如2.5x),代码实现如下:

import re

def extract_damage_multiplier(skill_desc):
    # 匹配数字+ x的格式,兼容整数与小数
    match_result = re.search(r'(\d+\.?\d*)x', skill_desc)
    if match_result:
        return float(match_result.group(1))
    return 1.0  # 默认返回1倍倍率,避免无匹配时出错

# 测试示例
skill1 = "Deals 5x ATK DMG to enemy"
print(extract_damage_multiplier(skill1))  # 输出:5.0
skill2 = "Deals 2.5x ATK DMG to all enemies"
print(extract_damage_multiplier(skill2))  # 输出:2.5

正则(\d+\.?\d*)x会捕获所有数字(包含可选小数部分)后跟x的内容,提取后转为浮点数即可直接用于伤害计算。

二、敌方ATK降低debuff实现

通过给怪物类添加临时属性与debuff管理机制,实现减益效果的持续与更新:

1. 核心怪物类设计

class Monster:
    def __init__(self, name, hp, atk, spd):
        self.name = name
        self.max_hp = hp
        self.current_hp = hp
        self.base_atk = atk
        self.current_atk = atk  # 实际生效的攻击力,受debuff影响
        self.spd = spd
        self.debuffs = []  # 存储debuff元组:(debuff类型, 影响数值, 剩余持续回合)

    def apply_debuff(self, debuff_type, value, duration):
        # 检查是否已有同类型debuff,存在则覆盖,不存在则新增
        for idx, debuff in enumerate(self.debuffs):
            if debuff[0] == debuff_type:
                self.debuffs[idx] = (debuff_type, value, duration)
                break
        else:
            self.debuffs.append((debuff_type, value, duration))
        self.update_current_stats()  # 立即更新属性

    def update_current_stats(self):
        # 重置为基础属性后,叠加所有生效debuff
        self.current_atk = self.base_atk
        for debuff in self.debuffs:
            if debuff[0] == "atk_down":
                # 这里value为倍率(如0.8对应降低20%),若要固定值则改为减法
                self.current_atk = int(self.current_atk * debuff[1])

    def end_turn(self):
        # 回合结束时减少debuff持续时间,移除过期效果
        active_debuffs = []
        for debuff in self.debuffs:
            debuff_type, value, duration = debuff
            if duration > 1:
                active_debuffs.append((debuff_type, value, duration - 1))
        self.debuffs = active_debuffs
        self.update_current_stats()

2. 减攻技能触发逻辑

从技能描述中提取减益百分比,调用怪物类的debuff方法:

def trigger_atk_down_skill(target):
    skill_desc = "Reduces enemy ATK by 20% for 3 turns"
    # 提取百分比数值
    percent_match = re.search(r'(\d+)%', skill_desc)
    if percent_match:
        reduce_ratio = int(percent_match.group(1)) / 100
        target.apply_debuff("atk_down", 1 - reduce_ratio, 3)
        print(f"{target.name}'s ATK reduced by {reduce_ratio*100}% for 3 turns!")
    else:
        print("Invalid ATK reduction parameter in skill description.")

# 测试
bulbasaur = Monster("Bulbasaur", 120, 18, 12)
trigger_atk_down_skill(bulbasaur)
print(bulbasaur.current_atk)  # 输出:14(18*0.8)
# 模拟回合结束
bulbasaur.end_turn()
print(bulbasaur.debuffs)  # 输出:[('atk_down', 0.8, 2)]

三、回合内技能执行逻辑

根据技能描述判断类型,分别执行伤害计算或debuff施加:

def execute_skill(attacker, target, skill_desc):
    if "DMG" in skill_desc:
        multiplier = extract_damage_multiplier(skill_desc)
        damage = int(attacker.current_atk * multiplier)
        target.current_hp = max(0, target.current_hp - damage)
        print(f"{attacker.name} attacks! Deals {damage} damage to {target.name}")
        print(f"{target.name} HP: {target.current_hp}/{target.max_hp}")
    elif "Reduces enemy ATK" in skill_desc:
        trigger_atk_down_skill(target)
    # 可扩展其他技能类型(如回血、提速等)

内容的提问来源于stack exchange,提问作者peeledbanana31

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 22:15:41