关于python继承和参数列表的问题

时间:2014-02-19 20:54:46

标签: python-2.7 typeerror attributeerror

首先我收到此错误

File "E:\New folder (7)\maingame.py", line 64, in play     print self.introduction AttributeError: 'game' object has no attribute 'introduction'

我不确定这意味着什么,因为我正在从上一节课中引入自我介绍..

我也得到了

File "E:\New folder (7)\maingame.py", line 96, in <module>
game.play()
TypeError: play() takes exactly 2 arguments (1 given)

错误,但不能为我的生活找到它正在寻找的论点,我只是希望它能够发挥作用。

  from random import random

  class place(object):
    def __init__(self, title, description, events):
    self.title = title
    self.description = description
    self.events = events

class event(object):
def __init__(self, probability, message, healthChange):
    self.probability = probability
    self.message = message
    self.healthChange = healthChange

def process(self):
    if random() < self.probability:
        print self.message
        return self.healthChange
    return 0

class textadventure():
def __init__(self):
    super(textadventure, self).__init__()
    self.introduction = """ 
Welcome player, you are a lone traveler in space whom has set out to find glories    beyond measure. 
Unfortunately for you the dread pirate Roberts has attacked. You must defeat him.
    """
    commandDeck = place('Command Deck', "You are now in the command center, here you can drive the ship and fire its weapons.",(
        event(0.7, "The pirate ship fires at you! You take damage to your engines!", -10),
        event(0.2, "One of the pirates manages to beam onto your ship! He shoots you before beaming away!",0),
        ))

    engineRoom = place('Engine Room', "You are now in the main engine room here you can repair damage to the ship",(
        event(0.7, "The pirate ship fires at you! You take damage to your engines!", -10),
        ))

    restQuarters = place('Resting Quarters', "Here you can take a rest and heal your self",(
        event(1.0, 'You are able to patch up your wounds and get back to the battle',0),
        event(0.5, "The pirate ship fires at you! You take damage to your engines!", -10),
        ))

    commandDeck.transitions = (engineRoom, restQuarters),
    engineRoom.transitions = (commandDeck, restQuarters),
    restQuarters.transitions = (commandDeck, engineRoom),

    self.location = commandDeck
pirateHp = 50
class game(object, textadventure):
    def __init__(self):
            super(game, self).__init__()
            self.health = 100
    def location(self):
            if self.location == commandDeck:
                    choice = raw_input('would you like to fire on the enemy ship?')
                    if choice == 'yes':
                            print 'You have hit the pirates!'
                            pirateHp -= 10
                    else: choice == 'no'
            elif self.location == engineRoom:
                    choice = raw_input('Would you like to repair the engines?')
                    if choice == "yes":
                            event(1, "You repair what you can of the engines.", 10)
    def __init__(self):
            self.health = 100
    def play(self, textadventure):
            print textadventure.introduction 

            while True:
                    print (self.location.description)
                    for event in self.location.events:
                            self.health += event.process()
                            if self.health <= 0:
                                    print ("Your ship has been destroyed!")
                                    pause
                                    exit(1)
                    print ('Your ships health is at %d percent' % self.health)
                    self._transition()

    def _transition(self):
            transitions = self.location.transitions
            print ('you can go to: ')
            for (index, transition) in enumerate(transitions):
                    print (index + 1, transition.title)

            choice = int(raw_input('Choose one '))
            if choice == 0:
                    exit(0)
            else:
                    self.location = transitions[choice - 1]

    def pirateShip(Object): 
            if pirateHp == 0:
                    print "You have defeated the pirates! Congradualations!"
                    pause
                    exit(1)

game = game()
game.play(game)

1 个答案:

答案 0 :(得分:0)

  

'游戏'对象没有属性'介绍'

初始化游戏时,您应该调用超类的 init 。在您当前的代码中,textadventure。 init 永远不会被调用,这就是为什么介绍永远不会添加到textadventure的原因。

游戏也不应该从对象继承(它通过textadventure来实现)。

class game(textadventure):
    def __init__(self):
            super(game, self).__init__()
            self.health = 100

    def play(self):
        print self.introduction

应该做的伎俩。


  

TypeError:play()只需要2个参数(给定1个)

你永远不会在游戏中使用你的textadventure参数。删除它应该可以解决问题。