2012-03-29 7 views
1

私は、エンティティ(壁、プレーヤー、本など)ごとにクラスを含む小さなテキストファンタジータイプのゲームを作ろうとする問題を抱えています。私はこのようになります部屋と呼ばれるクラスていますPythonオブジェクトはまだリストにありますか?

class Room: 

    def __init__(self, desc, items, wallF, wallB, wallL, wallR, isdark): 
     self.desc = desc 
     self.items = items 
     self.wallF = wallF 
     self.wallB = wallB 
     self.wallL = wallL 
     self.wallR = wallR 
     self.isdark = False 

今、私はこのように定義されている2つの部屋(その右のことを言っていない)があります。

roomstart = Room('There is a hole in the ceiling where you seemed to have fallen through, there is no way back up...', [candle], True, False, False, False, False) 
room2 = Room('You enter a small cobblestone cavort. It is dark, and the smell of rot pervades you', [spellbook], False, True, False, True, True) 

は今、問題はこれです:

(<type 'exceptions.ValueError'>, ValueError("'candle' is not in list",), <traceb 
ack object at 0x00B8D648>) 

(はい、私はを使用しました:私は、プログラムを実行すると、それは私がroomstartからろうそくを取るしようとするまで、それはその後、ろうそくがリストにないというエラーを出してくれる罰金作品)

各オブジェクト(ろうそく、ダガー、ローブ等)は同様にクラスを有する:ここで

class Object: 

    def __init__(self, desc, worth, emitslight, readable, wearable, name): 
     self.desc = desc 
     self.worth = worth 
     self.emitslight = emitslight 
     self.readable = readable 
     self.wearable = wearable 
     self.name = name 

は、ユーザ入力のためのコードである:

def handleinput(): 
global moves 
room = Player.location 
if room.isdark == True: 
    print 'It is rather dark in here...' 
else: 
    print room.desc, 'You see here:', 
    for i in room.items: 
     print i.name 
input = str(raw_input('What now? ')).lower() 
if 'look' in input: 
    if room.isdark==True: 
     print "You can't see anything! Its too dark." 
    else: 
     print 'You see:',room.desc, room.items.name 
     if room.wallF == True: 
      print 'There is an exit to the front.' 
     elif room.wallB == True: 
      print 'There is an exit behind you.' 
     elif room.wallL == True: 
      print 'There is an exit to your left.' 
     elif room.wallR == True: 
      print 'There is an exit to your right.' 

elif 'grab' in input: 
    if room.isdark==True: 
     print 'You flail about blindly in the dark room!' 
    else: 
     input2 = str(raw_input('Take what? ')) 
     try: 
      popp = room.items.index(input2) 
      print popp 
     except: 
      print sys.exc_info() 
      print input2.title(),"doesn't exist!" 
     else: 
      print "You take the",input2,"and store it in your knapsack." 
      room.items.pop(popp) 
      Player.inventory.append(input2) 

elif 'wipe face' in input: 
    os.system('cls') 
moves += 1 
+4

aaaahhh壁を経由しての項目のインデックスを見つけることができます! – Puppy

+3

少なくとも、彼はこれまでに試したことを彼が示していないと不平を言うことはできません。 –

+1

あなたは「ろうそく」と「ろうそく」が異なるオブジェクトであることを知っていますよね? –

答えて

5

オブジェクトcandleがリストにありますが、文字列'candle'はありません。あなたは、オブジェクトの辞書でこれを解決することを望むかもしれない:

objects = {} 
objects['candle'] = candle 
objects['robe'] = robe 
... 

その後、あなたはコードの

popp = room.items.index(objects[input2]) 
+0

うわー!それは完璧に働いた!オブジェクトをリストに追加することを忘れないでください;)もう一度ありがとう!成功の甘いメッセージ:「あなたが倒れたように見える天井に穴があります。 帰り道はありません...ここに表示されます:ろうそく 今は何ですか? take 何を取る?キャンドル あなたはキャンドルを取り出し、あなたのナップザックに保管します。 天井に穴があり、そこに落ちたように見えますが、 はありません...ここに表示されます:今何? – CR0SS0V3R

関連する問題