【问题标题】:Making a combat system using classes使用类制作战斗系统
【发布时间】:2016-11-26 21:34:30
【问题描述】:

我一直在尝试在我的文字游戏中实现战斗系统。我想我可以在我的每个场景中创建具有不同int 值的实例,以尽量减少我认为书中的练习试图教给我的硬编码。当我通过 dict 访问 TestRoom 类时,它在 Powershell 中显示:

TypeError: __init__() takes exactly 4 arguments (1 given)

请帮我弄清楚如何做到这一点我是 Python 新手,这本书没有很好地解释类。

class Hero(object):

    def __init__(self, health1, damage1, bullets1):
        self.health1 = health1
        self.damage1 = damage1
        self.bullets1 = bullets1

class Gothon(object):

    def __init__(self, health2, damage2):
        self.health2 = health2
        self.damage2 = damage2

class Combat(object):

    def battle():
        while self.health1 != 0 or self.health2 != 0 or self.bullets1 != 0:
            print "You have %r health left" % self.health1
            print "Gothon has %r health left" % self.health2
            fight = raw_input("Attack? Y/N?\n> ")
            if fight == "Y" or fight == "y":
                self.health2 - self.damage1
                self.health1 - self.damage2
            elif fight == "N" or fight == "n":
                self.health1 - self.damage2
            else:
                print "DOES NOT COMPUTE"
            if self.health1 == 0:
                return 'death'
            else:
                pass
class TestRoom(Combat, Hero, Gothon):

    def enter():
        hero = Hero(10, 2, 10)
        gothon = Gothon(5, 1)

这是在 Python 2.7 中

旁注:在 Python 3 退出后学习 Pyhton 2.7 是不是一件坏事?不需要回答,只是想知道。

【问题讨论】:

    标签: python python-2.7 syntax typeerror


    【解决方案1】:

    我不确定您为什么要让 TestRoom 从其他三个类继承。那没有意义;继承意味着“is-a”关系,但是虽然房间可能包含这些东西,但它实际上并不是这些东西本身。

    这是问题的根源,因为 TestRoom 现在已经从定义它的第一个方法 Hero 继承了 __init__ 方法,因此它需要 Hero 所做的参数。删除该继承。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      • 1970-01-01
      • 2012-12-10
      • 2015-09-27
      • 2011-09-08
      • 1970-01-01
      • 2015-09-16
      相关资源
      最近更新 更多