Samuel Rodriguez
Samuel Rodriguez

Reputation: 5

Making a combat system using classes

I've been trying to implement a combat system in my text game. I thought I could just create instances in each of my scenes that have different int values to minimize hard-coding that the exercise from the book I have I think is trying to teach me. When I access the TestRoom class by dict it says in Powershell:

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

Please help me figure out how to do this I'm new to Python and the book doesn't explain classes that well.

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)

This is in Python 2.7

Side note: Is learning Pyhton 2.7 a bad thing with Python 3 out? Answer isn't really required, just wondering.

Upvotes: 0

Views: 504

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599866

I'm not sure why you've made TestRoom inherit from those other three classes. That doesn't make sense; inheritance means an "is-a" relationship, but while a room might contain those things, it isn't actually any of those things itself.

This is the source of your problem, as TestRoom has now inherited the __init__ method from the first one of those that defines it, Hero, and so it requires the parameters that Hero does. Remove that inheritance.

Upvotes: 3

Related Questions