2017-03-17 7 views
1

私はPythonで単純なテキストベースのRPGを作っています。現在、私はほとんどの部屋に2つの方法があります.1つは最初に入力するときと1つ戻すときのどちらかです。他の方法を使わずに前にその部屋にいなかったことを確認できる方法はありますか?ユーザーがメソッドを完了しているかどうかを確認する方法

たとえば、tomb()という名前のメソッドがある場合は、tombAlready()という別のメソッドを作成します。このメソッドには、部屋の紹介テキスト以外は同じコードが含まれています。

私は

slow_type("\n\nTomb\n\n") 
    slow_type("There is an altar in the middle of the room, with passages leading down and west.") 
    choice = None 
    while choice == None: 
    userInput = input("\n>") 
    if checkDirection(userInput) == False: 
     while checkDirection == False: 
     userInput = input("\n>") 
     checkDirection(userInput) 
    userInput = userInput.lower().strip() 
    if userInput == "d": 
     catacombs() 
    elif userInput == "n": 
     altar() 
    elif userInput == "w": 
     throneroom() 
    else: 
     slow_type("You cannot perform this action.") 

を持っていたのであればその後tombAlready()が何をしたいslow_type("There is an altar in the middle of the room, with passages leading down and west.")

+0

グローバル変数? – rassar

+0

私はグローバル変数をどこに定義することを提案していますか? – nichilus

+0

tom文の後にif文とFalseからTrueに変更する変数を追加します。 – abccd

答えて

1

を除いて同じコードを持つことになり、機能に関連付けられている状態です。メソッドを持つオブジェクトを使用します。そして、あなたが各部屋にRoom対象持つことができます

class Room: 
    def __init__(self, description): 
     self._description = description 
     self._visited = False 

    def visit(self): 
     if not self._visited: 
      print(self._description) 
      self._visited = True 

catacombs = Room('There is a low, arched passageway. You have to stoop.') 
tomb = Room('There is an altar in the middle of the room, with passages leading down and west.') 
throneroom = Room('There is a large chair. It looks inviting.') 

をあなたは二回の部屋を訪問することができますが、それは一度だけその説明を出力します。

>>> catacombs.visit() 
There is a low, arched passageway. You have to stoop. 

>>> catacombs.visit() 
+1

ありがとうございます。魅力のように動作します:) – nichilus

+0

簡単な質問、BTW ...私は 'catacombs()'のようなメソッドで自分の部屋を持っていれば、私は自分の変数に同じ名前を使用できますか? – nichilus

+0

以上、具体的には、プログラムに干渉しないように変数を配置する場所はどこですか? – nichilus

関連する問題