问题
(For reference, I asked a separate question--which was answered--regarding the same script here)
I'm learning Python, and this is my first program. I'm trying to create a very basic RPG-like game where you select a character, give that character a weapon, and then tell it to attack another character with damage based on formulas that use the stats of the character and the weapon.
I'm sure I'm doing something obviously wrong, but I can't get my [variable] = ______ formulas to return anything other than 1, even though the variables used on the right side of the formula return the correct values independently.
This is what my "Char" (character) class looks like (sorry if I'm providing too much--trying not to leave anything out that might help):
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
My weapon class belongs to the Equip top-level class:
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
and then I'm just running a simple: dean.attack(hamilton) # hamilton is simply another Char
The thing that's confusing me is that when I have "print ........" to test the numbers, the numbers printed are correct. For instance, self.weapon.wgt is shown to be 3 as expected, self.strn prints as 3 expected, and self.weapon.impact prints as 6 as expected. However, in self.break_bone_chance, I've determined that self.weapon.impact is returning as 1 (by temporarily making self.break_bone_chance = only to self.weapon.impact), and self.strike_dmg also comes back as 1.
Again, I can provide the code for the "weapon" and "Char" in question if it will hlep, but I'm trying to leave off anything unnecessary. Does anything look wrong based on what I've provided here?
Thank you for your help.
回答1:
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.
来源:https://stackoverflow.com/questions/19469963/python-variables-with-formulas-all-return-value-1