I'm making a game and one of the methods calculates a character's base hit numbers based on skill values. The method currently calculates each value individually, since each skill can be used at short, medium, and long range.

我正在制作游戏,其中一种方法根据技能值计算角色的基本命中数。该方法当前单独计算每个值,因为每种技能可以在短,中和长范围内使用。

I originally thought I could combine the skills into a tuple and iterate over it, dynamically creating each hit number. But I don't know if it's actually possible, since I currently have each hit number assigned to it's own variable.

我原本以为我可以将技能组合成一个元组并迭代它,动态创建每个命中数。但我不知道它是否真的有可能,因为我目前每个命中编号分配给它自己的变量。

I also thought about creating a method for each range, and passing the tuple as an argument. I could create a new tuple or list with the resulting values and then assign them to the individual variables, but I don't see how it would be any better than do it this way, except that it won't look so copy & pasted.

我还考虑为每个范围创建一个方法,并将元组作为参数传递。我可以使用结果值创建一个新的元组或列表,然后将它们分配给各个变量,但我不知道它会如何以这种方式更好,除了它看起来不那么复制和粘贴。

Here's what I currently have:

这是我现在拥有的:

    def calcBaseHitNumbers(self, dict):
        """Calculate character's base hit numbers depending on skill level."""

        self.skill_dict = dict

        self.rifle = self.skill_dict.get('CRM', 0)
        self.pistol = self.skill_dict.get('PST', 0)
        self.big_gun = self.skill_dict.get('LCG', 0)
        self.heavy_weapon = self.skill_dict.get('HW', 0)
        self.bow = self.skill_dict.get('LB', 0)
        #self.skill_tuple = (self.rifle, self.pistol, self.big_gun, self.heavy_weapon,
        #    self.bow)

#---Short range
##        for skill in self.skill_tuple:
##            self.base_hit_short = skill * 0.6
        self.charAttribs.bhCRM_short = self.rifle * 0.6
        self.charAttribs.bhPST_short = self.pistol * 0.6
        self.charAttribs.bhHW_short = self.heavy_weapon * 0.6
        self.charAttribs.bhLCG_short = self.big_gun * 0.6
        self.charAttribs.bhLB_short = self.bow * 0.6
#---Med range
        self.charAttribs.bhCRM_med = self.rifle * 0.3
        self.charAttribs.bhPST_med = self.pistol * 0.3
        self.charAttribs.bhHW_med = self.heavy_weapon * 0.3
        self.charAttribs.bhLCG_med = self.big_gun * 0.3
        self.charAttribs.bhLB_med = self.bow * 0.3
#---Long range
        self.charAttribs.bhCRM_long = self.rifle * 0.1
        self.charAttribs.bhPST_long = self.pistol * 0.1
        self.charAttribs.bhHW_long = self.heavy_weapon * 0.1
        self.charAttribs.bhLCG_long = self.big_gun * 0.1
        self.charAttribs.bhLB_long = self.bow * 0.1

How would you refactor this so it's more dynamic?

你会如何重构这个以使它更具动态性?


Edit: I guess what I want to do is something like this: Have a tuple (like the one I commented out) and iterate over it 3 times, each time making a new value (for each skill) based on the modifier for each particular range. The resulting value is then automatically assigned to it's respective variable.

编辑:我想我想要做的是这样的:有一个元组(就像我注释掉的那个)并迭代3次,每次根据每个特定的修饰符创建一个新值(对于每个技能)范围。然后,结果值将自动分配给它的相应变量。

In my head, it makes sense. But when I actually try to code it, I get lost. The problem, I think, is that this is the first "real" program I've written; all I've done before are small scripts.

在我的脑海里,这是有道理的。但是当我真正尝试编码时,我迷路了。我认为问题在于,这是我写的第一个“真正的”程序;以前我所做的只是小脚本。

This is only the 0.1 version of my program, so it's not critical to refactor it now. However, it seems very un-Pythonic to do this manually and I also want to "future-proof" this in case things change down the road.

这只是我程序的0.1版本,所以现在重构它并不重要。然而,手动执行此操作似乎非常非Pythonic,我也希望“面向未来”,以防万一事情发生变化。

5 个解决方案

#1


6

It feels like what you really want is a class representing the weapon, with attributes to handle the base values and calculate hit values with various modifiers. Here's a simple example:

感觉就像你真正想要的是一个代表武器的类,具有处理基值的属性并使用各种修饰符计算命中值。这是一个简单的例子:

SHORT_RANGE = 'S'
MEDIUM_RANGE = 'M'
LONG_RANGE = 'L'
SHORT_RANGE_MODIFIER = 0.6
MEDIUM_RANGE_MODIFIER = 0.3
LONG_RANGE_MODIFIER = 0.1

class Weapon(object):
    def __init__(self, code_name, full_name, base_hit_value,
                 short_range_modifier=None, medium_range_modifier=None,
                 long_range_modifier=None):
        self.code_name, self.full_name = code_name, full_name
        self.base_hit_value = base_hit_value
        self.range_modifiers = {
            SHORT_RANGE: short_range_modifier or SHORT_RANGE_MODIFIER,
            MEDIUM_RANGE: medium_range_modifier or MEDIUM_RANGE_MODIFIER,
            LONG_RANGE: long_range_modifier or LONG_RANGE_MODIFIER,
        }

    def hit_value(self, range, modifier=1):
        return self.base_hit_value * self.range_modifiers[range] * modifier

From there, you might create instances of Weapon inside your Character object like so:

从那里,您可以在Character对象中创建Weapon实例,如下所示:

    self.rifle = Weapon('CRM', 'rifle', 5)
    self.pistol = Weapon('PST', 'pistol', 10)

And then if, say, the character fires the pistol at short range:

然后,如果该角色在短距离内射击手枪:

    hit_value = self.pistol.hit_value(SHORT_RANGE)

The extra argument to the hit_value() method can be used to pass in character- or situation-specific modifications.

hit_value()方法的额外参数可用于传递特定于字符或特定情况的修改。

Of course, the next step beyond this would be to directly model the weapons as subclasses of Weapon (perhaps breaking down into specific types of weapons, like guns, bows, grenades, etc., each with their own base values) and add an Inventory class to represent the weapons a character is carrying.

当然,除此之外的下一步是直接将武器建模为武器的子类(可能分解为特定类型的武器,如枪支,弓箭,手榴弹等,每个武器都有自己的基础值)并添加一个库存用来表示角色所携带的武器的类。

All of this is pretty standard, boring object-oriented design procedure, but for plenty of situations this type of thinking will get you off the ground quickly and provide at least a little bit of basic flexibility.

所有这些都是非常标准,无聊的面向对象设计程序,但在很多情况下,这种思维方式可以让您快速起步,并提供至少一点基本的灵活性。

更多相关文章

  1. 【分享】4412开发板-嵌入式Linux开发需要掌握的基础知识和技能

随机推荐

  1. Android(安卓)Message机制的灵活应用
  2. Android 学习笔记(十六):Widget-进度条
  3. android:EditText属性
  4. android中的帧动画
  5. Android 查看源码
  6. [置顶] android app 快速接入支付宝流程(a
  7. How To Debug Android Widgets
  8. android 之 adb shell的使用
  9. H5判断 移动端 是android还是ios
  10. Eclipse 连接 MUMU模拟器