2012-10-18 14 views
7

मैं पाइथन 3 का उपयोग कर एक साधारण पाठ आधारित डंगऑन गेम विकसित कर रहा हूं। सबसे पहले उपयोगकर्ता को DVD.py फ़ाइल से नायक चुनने के लिए कहा जाता है। init() लेता है कम से कम 2 तर्कों (1 दिए गए) दुष्ट() या अन्य नायक वर्गों के साथ क्या करना है जो: लेखन त्रुटि:टाइपरर: __init __() में कम से कम 2 तर्क (1 दिया गया) त्रुटि

from game import * 


class GameScreen: 
    '''Display the current state of a game in a text-based format. 
    This class is fully implemented and needs no 
    additional work from students.''' 

    def initialize_game(self): 
     '''(GameScreen) -> NoneType 
     Initialize new game with new user-selected hero class 
     and starting room files.''' 

     hero = None 
     while hero is None: 
      c = input("Select hero type:\n(R)ogue (M)age (B)arbarian\n") 
      c = c.lower() 
      if c == 'r': 
       hero = Rogue() 
      elif c == 'm': 
       hero = Mage() 
      elif c == 'b': 
       hero = Barbarian() 

     self.game = Game("rooms/startroom", hero) 

    def play(self): 
     '''(Game) -> NoneType 
     The main game loop.''' 

     exit = False 
     while not exit: 
      print(self) 
      if self.game.game_over(): 
       break 
      c = input("Next: ") 
      if c in ['q', 'x']: 
       print("Thanks for playing!") 
       exit = True 
      elif c == 'w': # UP 
       self.game.move_hero(-1, 0) 
      elif c == 's': # DOWN 
       self.game.move_hero(1, 0) 
      elif c == 'a': # LEFT 
       self.game.move_hero(0, -1) 
      elif c == 'd': # RIGHT 
       self.game.move_hero(0, 1) 
      elif c == 'r': 
       ## RESTART GAME 
       self.initialize_game() 
      else: 
       pass 

    def __str__(self): 
     '''(GameScreen) -> NoneType 
     Return a string representing the current room. 
     Include the game's Hero string represetation and a 
     status message from the last action taken.''' 

     room = self.game.current_room 
     s = "" 

     if self.game.game_over(): 
      #render a GAME OVER screen with text mostly centered 
      #in the space of the room in which the character died. 

      #top row 
      s += "X" * (2 + room.cols) + "\n" 
      #empty rows above GAME OVER 
      for i in list(range(floor((room.rows - 2)/2))): 
       s += "X" + " " * room.cols + "X\n" 
      # GAME OVER rows 
      s += ("X" + " " * floor((room.cols - 4)/2) + 
       "GAME" + " " * ceil((room.cols - 4)/2) + "X\n") 
      s += ("X" + " " * floor((room.cols - 4)/2) + 
       "OVER" + " " * ceil((room.cols - 4)/2) + "X\n") 
      #empty rows below GAME OVER 
      for i in list(range(ceil((room.rows - 2)/2))): 
       s += "X" + " " * room.cols + "X\n" 
      #bottom row 
      s += "X" * (2 + room.cols) + "\n" 
     else: 
      for i in range(room.rows): 
       for j in room.grid[i]: 
        if j is not None: 
         if j.visible: 
          s += j.symbol() 
         else: 
          #This is the symbol for 'not yet explored' : ? 
          s += "?" 
       s += "\n" 
     #hero representation 
     s += str(self.game.hero) 
     #last status message 
     s += room.status 
     return s 

if __name__ == '__main__': 
    gs = GameScreen() 
    gs.initialize_game() 
    gs.play() 

जब भी मैं इस कोड को चलाने के लिए, मैं इस त्रुटि मिलती है। हेरो.py यहाँ है

class Rogue(Tile): 
    '''A class representing the hero venturing into the dungeon. 
    Heroes have the following attributes: a name, a list of items, 
    hit points, strength, gold, and a viewing radius. Heroes 
    inherit the visible boolean from Tile.''' 

    def __init__(self, rogue, bonuses=(0, 0, 0)): 
     '''(Rogue, str, list) -> NoneType 
     Create a new hero with name Rogue, 
     an empty list of items and bonuses to 
     hp, strength, gold and radius as specified 
     in bonuses''' 

     self.rogue = rogue 
     self.items = [] 
     self.hp = 10 + bonuses[0] 
     self.strength = 2 + bonuses[1] 
     self.radius = 2 + bonuses[2] 
     Tile.__init__(self, True) 

    def symbol(self): 
     '''(Rogue) -> str 
     Return the map representation symbol of Hero: O.''' 

     #return "\u263b" 
     return "O" 

    def __str__(self): 
     '''(Item) -> str 
     Return the Hero's name.''' 

     return "{}\nHP:{:2d} STR:{:2d} RAD:{:2d}\n".format(
        self.rogue, self.hp, self.strength, self.radius) 

    def take(self, item): 
     '''ADD SIGNATURE HERE 
     Add item to hero's items 
     and update their stats as a result.''' 

     # IMPLEMENT TAKE METHOD HERE 
     pass 

    def fight(self, baddie): 
     '''ADD SIGNATURE HERE -> str 
     Fight baddie and return the outcome of the 
     battle in string format.''' 

     # Baddie strikes first 
     # Until one opponent is dead 
      # attacker deals damage equal to their strength 
      # attacker and defender alternate 
     if self.hp < 0: 
      return "Killed by" 
     return "Defeated" 

मैं क्या गलत कर रहा हूं?

उत्तर

9

समस्या

GameScreen.initialize_game() में, आप hero=Rogue() सेट है, लेकिन Rogue निर्माता एक तर्क के रूप rogue लेता है। (एक और तरीका कहा, __init__Rogue की आवश्यकता है कि rogue पारित किया जाए।) hero=Mage और hero=Barbarian सेट करते समय आपको यह वही समस्या हो सकती है।

समाधान

सौभाग्य से ठीक सरल है; आप बस hero=Rogue() से hero=Rogue("MyRogueName") बदल सकते हैं। हो सकता है कि आप उपयोगकर्ता को initialize_game में नाम के लिए संकेत दे सकें, और उसके बाद उस नाम का उपयोग करें।

"कम से कम 2 तर्कों (1 दिए गए)" पर नोट्स

जब आप इस तरह त्रुटियों को देखने के लिए, यह मतलब है कि आप एक समारोह या इसे करने के लिए पर्याप्त तर्क गुजर बिना एक विधि कहा जाता है। (__init__ केवल एक विशेष विधि है जिसे ऑब्जेक्ट प्रारंभ होने पर कहा जाता है।) तो जब भविष्य में इस तरह की चीजें डिबगिंग करते हैं, तो देखें कि आप कहां फ़ंक्शन/विधि कहां कॉल करते हैं, और जहां आप इसे परिभाषित करते हैं, और सुनिश्चित करें कि दोनों के पास है पैरामीटर की एक ही संख्या।

ऐसी चीज जो इस प्रकार की त्रुटियों के बारे में मुश्किल है, self है जो पास हो जाती है।

>>> class MyClass: 
...  def __init__(self): 
...    self.foo = 'foo' 
... 
>>> myObj = MyClass() 

कि उदाहरण में, एक सोच सकते हैं, "अजीब है, मैं myObj प्रारंभ, तो MyClass.__init__ लगाया गया; कारण है कि मैं self के लिए कुछ में पारित करने के लिए नहीं था?" जवाब यह है कि जब भी "object.method()" नोटेशन का उपयोग किया जाता है तो self प्रभावी ढंग से पारित किया जाता है। उम्मीद है कि त्रुटि को साफ़ करने में मदद करता है और भविष्य में इसे डीबग करने का तरीका बताता है।

+0

अब इस त्रुटि हो रही है इसे करने के लिए कुछ उचित पैरामीटर पारित करने के लिए की जरूरत है! TypeError: __init __() वास्तव में 2 स्थितित्मक तर्क लेता है (3 दिया गया) जब मैं नायक = दुष्ट ('हैलो') – sachitad

+0

पास करता हूं हां, मुझे भी मैगे और बारबियन में भी यही त्रुटि मिल रही है। अब, जब भी मैं दुष्ट ('हैलो') कॉल करते समय तर्क पारित करता हूं तो मुझे यह त्रुटि मिलती है "TypeError: __init __() वास्तव में 2 स्थितित्मक तर्क लेता है (3 दिया गया)" – sachitad

+0

ओह क्षमा करें मैंने गलती की।यह अभी ठीक काम करता है। अच्छी व्याख्या के लिए बहुत बहुत धन्यवाद। मैं ऑब्जेक्ट उन्मुख अवधारणा और पहले पैरामीटर "स्वयं" के उपयोग को स्पष्ट रूप से समझ गया। – sachitad

1
Class Rogue: 
    ... 
    def __init__(self, rogue, bonuses=(0, 0, 0)): 
     ... 

अपने Rogue वर्ग के __init__ पैरामीटर rogue जरूरत है, लेकिन आप initialize_game में hero = Rogue() के रूप में यह instantiating कर रहे हैं।

आप की तरह hero = Rogue('somename')

+0

अब यह त्रुटि प्राप्त करें! TypeError: __init __() जब मैं नायक = दुष्ट ('हैलो') पास करता हूं तो वास्तव में 2 स्थितित्मक तर्क (3 दिया जाता है) लेता है – sachitad

संबंधित मुद्दे