0

(作为参考,我问了一个单独的问题 - 已回答 - 关于此处的相同脚本)

我正在学习 Python,这是我的第一个程序。我正在尝试创建一个非常基本的类似 RPG 的游戏,您可以在其中选择一个角色,给该角色一把武器,然后根据使用角色和武器的统计数据的公式告诉它以伤害攻击另一个角色。

我确定我在做一些明显错误的事情,但我无法让我的 [variable] = ______ 公式返回 1 以外的任何值,即使公式右侧使用的变量独立地返回正确的值。

这就是我的“Char”(字符)类的样子(对不起,如果我提供的太多了——尽量不要遗漏任何可能有帮助的东西):

def __init__(self, name):
    self.name = name
    self.hp = 300
    self.mp = 10
    self.strn = 1
    self.dex = 1
    self.armor = 0
    self.xp = 0
    self.items = []
    self.spells = []
    self.weapon = weapon


    self.attack_speed = self.dex

    self.intact_bones = ["right arm", "left arm", "right leg", "leg leg", 
                    "skull", "sternum", "nose"] # JUST ASSUME RIGHT SIDE 
                                                # IS PRIMARY SIDE FOR NOW

    self.broken_bones = [] ### define what to do per bone if bone is in this list

    self.base_dmg = self.strn * self.weapon.wgt
    self.break_bone_chance = (self.weapon.impact / 50) * 100
    self.bleed_chance = (self.weapon.sharp / 50) * 100

    self.strike_dmg = self.base_dmg # NEED TO ADD A RANDOM ELEMENT TO DAMAGE

def break_crit(self, target):
    print "checking break_crit" # ALL THESE PRINTS ARE TEMPORARY TO TEST THE VARIABLES
    print "self.break_bone_chance = %r" % self.break_bone_chance
    print "self.base_dmg = %r" % self.base_dmg
    print "self.strike_dmg = %r" % self.strike_dmg
    print "len(target.intact_bones) = %r" % len(target.intact_bones)
    print "self.weapon = %r" % self.weapon.name
    print "self.weapon.impact = %r" % self.weapon.impact
    print "self.weapon.wgt = %r" % self.weapon.wgt

    if (randint(1, 100) <= self.break_bone_chance) and (self.strike_dmg > 0) and (len(target.intact_bones) != 0):
        random.shuffle(target.intact_bones)
        target.new_broken_bone = target.intact_bones.pop()
        target.broken_bones.append(target.new_broken_bone)
        print "%s hit %s square in the %s with his %s, cracking his %s!" % (self, target, target.new_broken_bone, self.weapon, target.new_broken_bone)
        self.bleed_crit(target)
    else:
        self.bleed_crit(target)


def bleed_crit(self, target):
    if randint(1, 100) <= self.bleed_chance and self.strike_dmg > 0:
        pass
    else:
        pass # GO TO NEXT ACTION


def attack(self, target):
    ###self.strike_dmg = self.dmg - target.armor # NEED TO ADD A RANDOM ELEMENT TO DAMAGE###
    if self.strike_dmg > 0:
        target.hp -= self.strike_dmg
        print "%s hit %s for %s damage!" % (self.name, target.name, self.strike_dmg)
        self.break_crit(target)
    else: 
        print "%s's strike did no damage!" % self.name          

我的武器类属于 Equip 顶级类:

class Equip(object):
    wgt = 1

class weapon(Equip):
    impact = 1
    sharp = 1

    def __init__(self, name):
        self.name = name
    desc = ""

chuck = weapon("Nun-chuck")
chuck.wgt = 3
chuck.impact = 6
chuck.sharp = 0

dean = Char("Dean")
dean.strn = 3
dean.dex = 8
dean.weapon = chuck

然后我只是运行一个简单的: dean.attack(hamilton) # hamilton 只是另一个 Char

让我感到困惑的是,当我有“打印…………”来测试数字时,打印的数字是正确的。例如,self.weapon.wgt 按预期显示为 3,self.strn 显示为 3,self.weapon.impact 按预期显示为 6。但是,在 self.break_bone_chance 中,我确定 self.weapon.impact 返回为 1(通过暂时将 self.break_bone_chance = 设置为 self.weapon.impact),并且 self.strike_dmg 也返回为 1。

同样,我可以提供有问题的“武器”和“字符”的代码,如果它有用的话,但我试图省略任何不必要的东西。根据我在这里提供的内容,有什么看起来不对吗?

谢谢您的帮助。

4

1 回答 1

0

I only looked at this quickly, but kroolik's comment is probably the problem you're running into - integer division truncates.

You can change 50 to 50.0, which will fix this, or you could use the from future import division future import to cause all of your divisions to be floating point division by default. Then you would need to use // if you ever actually wanted integer division.

于 2013-10-19T19:50:24.030 回答