2

在过去的几天里,我已经弄清楚了如何做大部分这些事情,但这就是我所拥有的全部经验,所以这可能很简单。无论如何,一切都很顺利,直到我试图使一些公式复杂化,或者至少改变他们使用的值。这是我正在使用的。

class EnemyStats():
    def Ename(self):
        return #Not sure What i should put in these spots...
    def EBaseDodge(self):
        return 
    def EnemyEvasion(self):
        return 
    def ENickRange(self):
        return 
    def EBaseAttack(self):
        return 
    def EWeaponAttack(self):
        return 
    def EAttackRating(self):
        return 
    def EnemyDefense(self):
        return 
    def EAttackDamage(self):
        return 
    def Damage(self):
        return ((PAttackDamage-EnemyDefense)if (PAttackDamage-EnemyDefense>0) else 0);
    def EDamage(self):
        return ((EAttackDamage-PDefense) if ((EAttackDamage-PDefense)>0) else 0);
    def LightAttackDamage(self):
        return int(LightAttackDamage == (Damage * 0.72));
    def HeavyAttackDamage(self):
        return (Damage * 1.28);
    def LightNicked(self):
        return (LightAttackDamage/2);
    def HeavyNicked(self):
        return (HeavyAttackDamage/2);
    def Nicked(self):
        return (Damage/2)

    def Estats(self):
        Ename = raw_input('Target Enemy Name: ');
        EBaseDodge = int(raw_input('Enter Enemy Dodge: '));
        EnemyEvasion = int(raw_input('Enter Enemy Evasion: '));
        ENickRange = (EBaseDodge + EnemyEvasion);
        EBaseAttack = int(raw_input('Enter Enemy Base Attack: '));
        EWeaponAttack = int(raw_input('Enter Enemy Weapon Attack(If N/A, 0): '));
        EAttackRating = (EBaseAttack + EWeaponAttack);
        EnemyDefense = int(raw_input('Enter Enemy Defense: '));
        EAttackDamage = int(raw_input('Enter Enemy Attack Damage: '));
        Damage = ((PAttackDamage-EnemyDefense)if (PAttackDamage-EnemyDefense>0) else 0);
        EDamage = ((EAttackDamage-PDefense) if ((EAttackDamage-PDefense)>0) else 0);
        LightAttackDamage = (Damage * 0.72);
        HeavyAttackDamage = (Damage * 1.28);
        LightNicked = (LightAttackDamage/2);
        HeavyNicked = (HeavyAttackDamage/2);
        Nicked = (Damage/2)

然后它被this引用。

def PLightAttackForm():#light attack
    print 'Light attack!';
    d = dice()
    lightbase = d.LightAttack()
    EE = EnemyStats()
    if lightbase <= EE.EBaseDodge:
        print 'You rolled', lightbase, ', Miss!', 0, 'Damage!'
    elif lightbase > EE.ENickRange:
        print 'You rolled', lightbase, ', Hit!', EE.LightAttackDamage, 'Damage!'
    elif lightbase < EE.ENickRange:
        print 'You rolled', lightbase, ', Nicked!', EE.LightNicked, 'Damage!'
    else:
        print lightbase

一切都很棒,没有错误,但我明白了。

轻攻击!你投了700,小姐!0伤害!#应该是命中和伤害

普通攻击!你投了 278 ,小姐!0伤害!#应该受到打击和伤害

重击!你投了 135 ,小姐!0 伤害!#should be a Nick and damage

我敢肯定这只是我不知道的事情,但如果你能帮助我,那就太棒了!谢谢!

另外,这是我编写的所有代码。可能有一些冗余......:D

import random
Name = raw_input("Enter Name: ")
#EName===
#EBaseDodge = int(raw_input("Enter Enemy Dodge: "))
PBaseDodge = int(input('Enter Base Dodge: '))
#EnemyEvasion = int(raw_input("Enter Enemy Evasion: "))
PEvasion = int(input('Enter Evasion: '))
#ENickRange = (EBaseDodge + EnemyEvasion)
PNickRange = (PBaseDodge + PEvasion)
PBaseAttack = int(raw_input("Enter Base Attack: "))
#EBaseAttack===
PWeaponAttack = int(raw_input("Enter Weapon Attack: "))
#EWeaponAttack===
PAttackRating = (PBaseAttack + PWeaponAttack)
#EAttackRating = (EBaseAttack + EWeaponAttack)
#EnemyDefense = int(raw_input("Enter Enemy Defense: "))
PDefense = int(input('Enter Defense: '))
PAttackDamage = int(raw_input("Enter Attack Damage: "))
#EAttackDamage===
#Damage = (PAttackDamage-EnemyDefense)
#EDamage = (EAttackDamage-PDefense)
#LightAttackDamage = (Damage * 0.72)
#HeavyAttackDamage = (Damage * 1.28)
#LightNicked = (LightAttackDamage/2)
#HeavyNicked = (HeavyAttackDamage/2)
#Nicked = Damage/2


class dice():
        def NormalAttack(self):
            return random.randint(1, PAttackRating);
        def LightAttack(self):
            return random.randint(1, (int(PAttackRating*1.25)));
        def HeavyAttack(self):
            return random.randint(1, (int(PAttackRating*0.75)));


#def att():
#   d = dice()
#   base = d.roll()
#   if base <= a:
#       print 'You rolled', base, ', Miss!', 0, 'Damage!'
#   elif base > b:
#       print 'You rolled', base, ', Hit!', Damage, 'Damage!'
#   elif base < b:
#       print 'You rolled', base, ', Nicked!', Nicked, 'Damage!'
#   else:
#       print base

####################NEW CODE#######################################
def Menu():
    print '(1)Attack';
    print '(2)Choose Enemy';
    print '(3)Charge';
    print '(4)Item';


def select():
    choice = input('Enter Choice: ');
    EE = EnemyStats()
    if (choice == 1):
        Attacktype();
    elif (choice == 2):
        EE.Estats();
    elif (choice == 3):
        Charge();
    elif (choice == 4):
        ItemSelection();
    else:
        print 'There are Numbers for a reason Nuub!',;

##############Enemy Stats############

class EnemyStats():
    def Ename(self):
        return 
    def EBaseDodge(self):
        return 
    def EnemyEvasion(self):
        return 
    def ENickRange(self):
        return 
    def EBaseAttack(self):
        return 
    def EWeaponAttack(self):
        return 
    def EAttackRating(self):
        return 
    def EnemyDefense(self):
        return 
    def EAttackDamage(self):
        return 
    def Damage(self):
        return ((PAttackDamage-EnemyDefense)if (PAttackDamage-EnemyDefense>0) else 0);
    def EDamage(self):
        return ((EAttackDamage-PDefense) if ((EAttackDamage-PDefense)>0) else 0);
    def LightAttackDamage(self):
        return int(LightAttackDamage == (Damage * 0.72));
    def HeavyAttackDamage(self):
        return (Damage * 1.28);
    def LightNicked(self):
        return (LightAttackDamage/2);
    def HeavyNicked(self):
        return (HeavyAttackDamage/2);
    def Nicked(self):
        return (Damage/2)

    def Estats(self):
        Ename = raw_input('Target Enemy Name: ');
        EBaseDodge = int(raw_input('Enter Enemy Dodge: '));
        EnemyEvasion = int(raw_input('Enter Enemy Evasion: '));
        ENickRange = (EBaseDodge + EnemyEvasion);
        EBaseAttack = int(raw_input('Enter Enemy Base Attack: '));
        EWeaponAttack = int(raw_input('Enter Enemy Weapon Attack(If N/A, 0): '));
        EAttackRating = (EBaseAttack + EWeaponAttack);
        EnemyDefense = int(raw_input('Enter Enemy Defense: '));
        EAttackDamage = int(raw_input('Enter Enemy Attack Damage: '));
        Damage = ((PAttackDamage-EnemyDefense)if (PAttackDamage-EnemyDefense>0) else 0);
        EDamage = ((EAttackDamage-PDefense) if ((EAttackDamage-PDefense)>0) else 0);
        LightAttackDamage = (Damage * 0.72);
        HeavyAttackDamage = (Damage * 1.28);
        LightNicked = (LightAttackDamage/2);
        HeavyNicked = (HeavyAttackDamage/2);
        Nicked = (Damage/2)



#Attacking
def Attacktype():
    print '(1)LightAttack';
    print '(2)NormalAttack';
    print '(3)HeavyAttack';
    print '(4)Use Dem Magicks';
    print '(5)Menu(<<This is for nuublets)';
    Attchoice = input('Enter Choice: ')
    if (Attchoice == 1):
        PLightAttackForm();
    elif (Attchoice == 2):
        PNormalAttackForm();
    elif (Attchoice == 3):
        PHeavyAttackForm();
    elif (Attchoice == 4):
        MagicMenu();
    elif (Attchoice == 5):
        Menu();
    else:
        print 'You wot M8?';
        Menu();


def PLightAttackForm():#light attack
    print 'Light attack!';
    d = dice()
    lightbase = d.LightAttack()
    EE = EnemyStats()
    if lightbase <= EE.EBaseDodge:
        print 'You rolled', lightbase, ', Miss!', 0, 'Damage!'
    elif lightbase > EE.ENickRange:
        print 'You rolled', lightbase, ', Hit!', EE.LightAttackDamage, 'Damage!'
    elif lightbase < EE.ENickRange:
        print 'You rolled', lightbase, ', Nicked!', EE.LightNicked, 'Damage!'
    else:
        print lightbase

def PNormalAttackForm():#Normal attack
    print 'Normal Attack!';
    d = dice()
    base = d.NormalAttack()
    EE = EnemyStats()
    if base <= EE.EBaseDodge:
        print 'You rolled', base, ', Miss!', 0, 'Damage!'
    elif base > EE.ENickRange:
        print 'You rolled', base, ', Hit!', EE.Damage, 'Damage!'
    elif base < EE.ENickRange:
        print 'You rolled', base, ', Nicked!', EE.Nicked, 'Damage!'
    else:
        print base

def PHeavyAttackForm():#Heavy Attack
    print 'Heavy Attack!';
    d = dice()
    heavybase = d.HeavyAttack()
    EE = EnemyStats()
    if heavybase <= EE.EBaseDodge:
        print 'You rolled', heavybase, ', Miss!', 0, 'Damage!'
    elif heavybase > EE.ENickRange:
        print 'You rolled', heavybase, ', Hit!', EE.HeavyAttackDamage, 'Damage!'
    elif heavybase < EE.ENickRange:
        print 'You rolled', heavybase, ', Nicked!', EE.HeavyNicked, 'Damage!'
    else:
        print heavybase

def MagicMenu():#magic menu()   
    print 'Magic menu!';


##############Enemy Stats############



####################NEW CODE#######################################
4

2 回答 2

4

正如 That1Guy 所说,您没有将参数传递给您的函数。在 python 中,函数也是对象,您可以将它们与其他对象进行比较。所以当你这样做时:

if lightbase <= EE.EBaseDodge:
    print 'You rolled', lightbase, ', Miss!', 0, 'Damage!'
elif lightbase > EE.ENickRange:
    print 'You rolled', lightbase, ', Hit!', EE.LightAttackDamage, 'Damage!'
elif lightbase < EE.ENickRange:
    print 'You rolled', lightbase, ', Nicked!', EE.LightNicked, 'Damage!'
else:
    print lightbase

lightbase <= EE.EBaseDodge总是评估为真。

您需要在对EBase...方法的调用中添加括号。您还需要将返回值添加到方法的声明中。所以让你的函数看起来像(假设你有一个名为 base_dodge 的变量):

 def EBaseDodge(self):
    return base_dodge

并将您的 ifs 更改为:

 if lightbase <= EE.EBaseDodge():
    ...

我不确定您为什么首先尝试对所有这些值使用方法。如果方法是属性会更有意义。

尝试删除所有方法Enemystats并使变量成为实例变量:

class EnemyStats:
    def Estats(self):
        #Keep this method to set all of the stats and add self. before them like this
        self.Ename = raw_input('Target Enemy Name: ');
        self.EBaseDodge = int(raw_input('Enter Enemy Dodge: '));
        self.EnemyEvasion = int(raw_input('Enter Enemy Evasion: '));
        self.ENickRange = (self.EBaseDodge + self.EnemyEvasion);
        ....

将所有变量放在self.您的所有变量之前,将它们与您的类的一个实例相关联 ( EnemyStats)。

然后创建一个新的 EnemyStats 对象并输入值:

EE = EnemyStats()
EE.Estats() # will prompt you for the values

之后,您可以像之前想要的那样引用您的值:

if lightbase <= EE.EBaseDodge:
   ...

只是让你知道,这不是最有组织/推荐的做事方式。

于 2013-03-05T21:43:22.500 回答
1

你从来没有真正调用过你的任何类方法。您只需将您的值与函数对象本身进行比较,这不是您通常会做的事情。例如,您应该这样做:

if lightbase <= EE.EBaseDodge():

(注意括号) - 当然,一旦你这样做,你会得到很多很多错误,因为这段代码还有其他几个问题。

于 2013-03-05T21:44:39.593 回答