初学者编码,请帮忙 [英] Beginner to coding, little help please

查看:65
本文介绍了初学者编码,请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一个月前开始编程。我把python作为我的学习语言,并考虑从互联网资源制作一个RPG游戏(基于文本)。以下是随机导入randint的代码 -





So I started programming a month ago. I took up python as my learning language and thought of making a rpg game(text-based) from internet sources. Here is the 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)
        prof="fighter"

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

##NOW FOR ENEMIES

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

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




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():
    if Dice.die(2)<2:
        mob=Ogre()
    else:
        mob=Goblin()
    return mob

def playerAttack():
    print("You hit")
    if (hero.prof=="fighter"):
        if(Dice.die(8)<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(Dice.die(8)<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():
    print("The monster attacks")
    if(mob.name=="Ogre"):
        if(Dice.die(8)<8):
            print("6 damage taken")
            hero.hp-=6
        else:
            print("The attack misses")

    elif(mob.name=="Goblin"):
        if(Dice.die(8)<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

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()



但是当我尝试运行它时,它会显示最初的字符选择,之后会抛出错误 -


But when I try to run it,it shows the initial selection of characters after which it throws an error-

Traceback (most recent call last):
  File "C:/Users/Hi/Desktop/python programs/nvm/MYFIRSTGAME.py", line 117, in <module>
    if mob.hp<=0:
AttributeError: 'Goblin' object has no attribute 'hp'



任何人都可以帮助我吗?在此先感谢:-)



我尝试过:



不确定导致问题的原因


Can anyone help me out here? Thanks in advance :-)

What I have tried:

Not sure what is causing the problem

推荐答案

构造函数是
__init__

(注意双下划线),而不是

(note double underscores), not

_init_


除了Carlo发现的错误之外:

- 你不需要声明<$ c你的子类中有$ c> name , hp damage ,因为已经定义了在角色

- 战斗机刺客类在 prof 属性之前需要 self 关键字。
In addition to Carlo's well spotted mistake:
- You do not need to declare name, hp and damage in your subclasses, as the are already defined in Character.
- The Fighter and Assassin classes need the self keyword before the prof attribute.


这篇关于初学者编码,请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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