本文介绍了基于Python文本的RPG问题,请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 以下是我写的游戏代码: 来自 random import randint class 骰子: def die(num): die = randint( 1 ,num) return die class 字符: def __init __(自我,名字,hp,伤害): self.name = name self.hp = hp self.damage = damage class 战士(角色): def __init __(self): super().__ init __(name = input( 你的角色的名字是什么?), hp = 20,伤害= 12) prof = 战斗机 class Assasin(Character): def __init __(self): super().__ init __(name) = input( 你的角色名称是什么?), hp = 15, damage = 14) prof = assasin # #NOW FOR ENEMIES 类精灵(角色): def __init __(self): super().__ init __(name = goblin, hp = 15, damage = 3) class Ogre(字符): def __init __(self): super()。 __init __(name = ogre, hp = 25, damage = 6) # #ENEMIES def 专业(): print ( 你的班级是什么?,' \\ \\ n', 按f进行战斗机,' \ n', 按a for assasin) pclass = input( 输入您的选择>>>) if (pclass == f): prof = Fighter() elif (pclass == a): prof = Assasin() else : prof = Fighter() return prof def ranmob(): mob = Ogre() if Dice.die( 2 )< 2 else Goblin() return mob def playerAttack(): roll = Dice.die ( 10 ) print ( 你点击) if (hero.prof == 战斗机): 如果(roll< 8): print ( 它们造成12点伤害) mob.hp- = 12 print ( ,mob.name, 有,mob.hp , hp left) else : print ( 你错过了你的攻击) elif (hero.prof == assasin): if (roll< 8): 打印( 它们造成14点伤害) mob.hp- = 14 print ( ,mob.name, 具有,mob.hp, hp left) else : print ( 您错过了攻击 ) def monsterAttack(): roll = Dice.die( 10 ) print ( 怪物攻击) if (mob.name == Ogre): if (roll< 8): print ( 6点伤害)英雄。 hp- = 6 else : print ( 攻击未命中) elif (mob.name == Goblin): if (roll< 8): print ( 3受到的伤害) hero.hp- = 3 else : print ( 攻击未命中 ) def commands(): if hero.prof == 战斗机: print ( 按f打击\ n, 按e传递) command = input( >>>>>) if ( command == f): playerAttack() elif command == e: 传递 elif hero.prof == assasin: print ( 按f打击\ n, 按e传递) command = input( >> ;>>>) if (command == f): playerAttack() elif command == e: pass mob = ranmob() hero = profession() print ( name hp ' \ n',hero.name,hero.hp) while True : 如果 mob.hp< = 0: print (' ',mob.name,' 已死') mob = ranmob() 如果 hero.hp< = 0: print (hero.name,' 死亡!') hero =专业() print ( name hp,' \ n',hero.name,hero.hp) print ( 你看,暴徒。 name, ,,mob.name, 有,mob.hp, hp ) 如果 hero.hp> 0:命令() if mob.hp> 0: monsterAttack() 输出引发错误 - Traceback(最近一次调用最后一次):文件C:\ Users \Hi \Desktop \ python programs \\ \\ nvm \ MYFIRSTGAME.py,第129行,< module> 命令()文件C:\ Users \Hi \Desktop\python programs\\\vm \ MYFIRSTGAME.py,第98行,命令如果是英雄。 prof ==fighter: AttributeError:'Fighter'对象没有属性'prof' 任何形式的帮助或建议将不胜感激:-) 我尝试过: 尝试修补战斗机和assasin clas的教授,但没有解决解决方案 Quote: prof =fighter你可能意味着 self.prof =fighter Following is the code for the game which I have written:from random import randintclass Dice: def die(num): die=randint(1,num) return dieclass Character: def __init__(self,name,hp,damage): self.name=name self.hp=hp self.damage=damageclass Fighter(Character): def __init__(self): super().__init__(name=input("what is your character's name?"), hp=20, damage=12) prof= "fighter"class Assasin(Character): def __init__(self): super().__init__(name=input("what is your character's name?"), hp=15, damage=14) prof= "assasin"##NOW FOR ENEMIESclass 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)##ENEMIESdef 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 profdef ranmob(): mob=Ogre() if Dice.die(2)<2 else Goblin() return mobdef 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) print("The monster attacks") if(mob.name=="Ogre"): if(roll<8): print("6 damage taken") hero.hp-=6 else: print("The attack misses") elif(mob.name=="Goblin"): if(roll<8): print("3 damage taken") hero.hp-=3 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": passmob=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()Output throws the error-Traceback (most recent call last): File "C:\Users\Hi\Desktop\python programs\nvm\MYFIRSTGAME.py", line 129, in <module> commands() File "C:\Users\Hi\Desktop\python programs\nvm\MYFIRSTGAME.py", line 98, in commands if hero.prof=="fighter":AttributeError: 'Fighter' object has no attribute 'prof'Any form of help or suggestion would be appreciated :-)What I have tried:Tried tinkering with the prof in the Fighter and assasin clas,but didnt work out 解决方案 Quote:prof= "fighter"You probably meantself.prof = "fighter" 这篇关于基于Python文本的RPG问题,请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-23 00:42