基于Python文本的RPG(代码工作在3.6但不在2.7.8) [英] Python text based rpg (code working in 3.6 but not in 2.7.8)

查看:106
本文介绍了基于Python文本的RPG(代码工作在3.6但不在2.7.8)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在python 3.6中成功编译了这段代码,但是当我尝试做同样的事情时它会抛出一个错误 -



Code-



I have successfully compiled this code in python 3.6,but when I try to do the same it throws an error-

Code-

from random import randint
class Dice:
    def die(num):
        die=randint(1,num)
        return die

class Character:
    def __init__(self,name,hp,damage):
        self.name=name
        self.hp=hp
        self.damage=damage

class Fighter(Character):
    def __init__(self):
        super().__init__(name=input("what is your character's name?"),
                       hp=20,
                       damage=12)
        self.prof= "fighter"
        
class Assasin(Character):
    def __init__(self):
        super().__init__(name=input("what is your character's name?"),
                       hp=15,
                       damage=14)
        self.prof= "assasin"

##NOW FOR ENEMIES

class Goblin(Character):
    def __init__(self):
        super().__init__(name="goblin",
                     hp=15,
                     damage=3)
        
        

class Ogre(Character):
    def __init__(self):
        super().__init__(name="ogre",
                     hp=25,
                     damage=6)

##ENEMIES

def profession():
    print("What is your class?",'\n',
          "Press f for fighter" , '\n',
          "Press a for assasin")
    pclass=input("Enter your choice>>>")
    if(pclass=="f"):
        prof=Fighter()
    elif(pclass=="a"):
        prof=Assasin()
    else:
        prof=Fighter()
    return prof

def ranmob():
    roll=Dice.die(10)
    if roll<8:
        mob=Ogre()
    else:
        mob=Goblin()
    return mob

def playerAttack():
    roll=Dice.die(10)
    print("You hit")
    if (hero.prof=="fighter"):
        if(roll<8):
            print("them for 12 damage")
            mob.hp-=12
            print("The",mob.name,"has",mob.hp,"hp left")
        else:
            print("You missed your attack")
    elif(hero.prof=="assasin"):
        if(roll<8):
            print("them for 14 damage")
            mob.hp-=14
            print("The",mob.name,"has",mob.hp,"hp left")
        else:
            print("You missed your attack")

def monsterAttack():
    roll=Dice.die(10)
    if (mob.name=="ogre"):
        print("THE OGRE SHRIEKS and attacks")
        if(roll<8):
            print("6 damage taken")
            hero.hp-=6
            print("You now have",hero.hp,"hp left")
        else:
            print("The attack misses")

    elif (mob.name=="goblin"):
        print("The goblin prepares his knife and jumps")
        if(roll<8):
            print("3 damage taken")
            hero.hp-=3
            print("You now have",hero.hp,"hp left")
        else:
            print("The attack misses")

def commands():
    if hero.prof=="fighter":
        print("press f to fight\n","press e to pass")
        command=input(">>>>>")
        if(command=="f"):
            playerAttack()
        elif command=="e":
            pass
    elif hero.prof=="assasin":
        print("press f to fight\n","press e to pass")
        command=input(">>>>>")
        if(command=="f"):
            playerAttack()
        elif command=="e":
            pass

mob=ranmob()
hero=profession()

print("name hp"'\n',hero.name,hero.hp)

while True:
    if mob.hp<=0:
        print('The',mob.name,'is dead')
        mob=ranmob()
    if hero.hp<=0:
        print(hero.name,'died!')
        hero=profession()
        print("name hp",'\n',hero.name,hero.hp)

    print("You see",mob.name,",",mob.name,"has",mob.hp,"hp")
    if hero.hp>0:
        commands()
    if mob.hp>0:
        monsterAttack()



错误 -




Error-

Traceback (most recent call last):
  File "C:\Users\Hi\Desktop\python programs\2.7trail.py", line 120, in <module>
    mob=ranmob()
  File "C:\Users\Hi\Desktop\python programs\2.7trail.py", line 59, in ranmob
    roll=Dice.die(10)
TypeError: unbound method die() must be called with Dice instance as first argument (got int instance instead)





我的尝试:



不确定错误。(........... ............................



What I have tried:

Not sure about the error.(......................................)

推荐答案

添加 staticmethod decorator:

Add a staticmethod decorator:
class Dice:
    @staticmethod
    def die(num):
        die=randint(1,num)
        return die

错误原因:Python 2要求第一个参数是类本身的实例;在Python 3中它可以是任何东西。







改变它本身不会使但是,你的代码在Python 2中完美运行。 Python 3的输入接受输入并返回带有该输入的字符串,但Python 2相当于 raw_input 。 Python 2也有一个输入函数,但这不只是输入,它也会对它进行评估(所以Python 2的输入在Python 3中将是 eval(input())。有关Python中向后兼容的输入调用,请参阅此Stack Overflow问题 [ ^ ]。

The reason for the error: Python 2 requires the first argument to be an instance of the class itself; in Python 3 it can be anything.



Changing that alone won't make your code run perfectly in Python 2, though. Python 3's input takes input and returns a string with that input, but the Python 2 equivalent of that is raw_input. Python 2 has an input function too, but that doesn't just take input, it also evaluates it (so Python 2's input would be eval(input()) in Python 3). See this Stack Overflow question for backwards-compatible input calls in Python[^].


这篇关于基于Python文本的RPG(代码工作在3.6但不在2.7.8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆