2017-11-07 18 views
-1

私はコーディングの初心者で、現在はRPGに取り組んでいます。私は、部屋にグラバブルアイテムがあるときにリストを持つ "グラブアイテム"オプションを実装しようとしています。しかし、私はアイテムをつかんだ後、アイテムはまだgrabbableです。誰かが私に初心者が理解できる簡単な解決策を教えてもらえますか? (私は明日でこれを有効にする必要があります。)grabbableアイテム付きの客室です。リストを使用して「アイテムを取得」オプションを削除するにはどうすればよいですか?

  • 「階段 が上がっていくとpokeball」。フロア2ルーム1(currentFloor == 1、currentRoom == 0)
  • 'pokeball!'フロア3部屋2(currentFloor == 2、currentRoom == 1)
  • '階段が上がり、2つのポケボール!'フロア3ルーム4(currentFloor == 2、currentRoom == 3)
  • 'a potion!'フロア4部屋3(currentFloorの== 3とcurrentRoom == 2)

def foo(m): 
""" 
Valid commands the player can input. 
""" 
if m.lower() == 'left' or m.lower() == 'right' or m.lower() == 'help' or m.lower() == 'pokemon' or m.lower() == 'bag' or m.lower() == 'up' or m.lower() == 'down' or m.lower() == 'grab': 
    return True 
else: 
    return False 

print("You are finally a Pokemon trainer! Today, you have gotten your very 
first Pokemon, a Bulbasaur!") 
name = input("What will you name your Bulbasaur? ") 
print("Unfortunately, " + name + " doesn't seem to obey or like you...") 
print("You try to be friendly to " + name + ", but it just won't listen...") 
print("As " + name + " was busy ignoring you, something seems to catch its 
attention and it runs off!") 
print("You chase after " + name + ", but it's too fast! You see it running 
into an abandoned Pokeball Factory.") 
print("You must explore the abandoned Pokeball Factory and find " + name + " 
before something happens to it!") 
print() 
print("You may input 'help' to display the commands.") 
print() 

gamePlay = True 
floors = [['floor 1 room 1', 'floor 1 room 2', 'floor 1 room 3', 'floor 1 
room 4'],['floor 2 room 1', 'floor 2 room 2', 'floor 2 room 3', 'floor 2 
room 4', 'floor 2 room 5'],['floor 3 room 1', 'floor 3 room 2', 'floor 3 
room 3', 'floor 3 room 4'],['floor 4 room 1', 'floor 4 room 2', 'floor 4 
room 3', 'floor 4 room 4']] 
floorsFeature = [['a potion! But upon closer inspection you find that is it 
used up...', 'nothing here.', 'stairs going up.', 'a Squirtle.'],['stairs 
going up and a pokeball.', 'a FIRE!!!', 'stairs going down.', 'a 
Charmander.'],['stairs going down.', 'a pokeball!', 'a door covered in 
vines.', 'stairs going up and 2 pokeballs!'],['your Bulbasaur!!!', 'a 
pokeball! But it is broken...', 'a potion!', 'an Eevee with a key tied 
around its neck and stairs going down.']] 
currentFloor = 0 
currentRoom = 1 
pokemonGot = [] 
count = 0 
bagItems = ['pokeball'] 
countItems = 1 

while gamePlay == True: 
    print("You are on " + floors[currentFloor][currentRoom] + ". You find " 
+ floorsFeature[currentFloor][currentRoom]) 
    move = input("What would you like to do? ") 
    while foo(move) == False: 
     move = input("There's a time and place for everything, but not now! 
What would you like to do? ") 
    if move.lower() == 'grab': 
     if currentFloor == 1 and currentRoom == 0 or currentFloor == 2 and 
currentRoom == 1 or currentFloor == 2 and currentRoom == 3 or currentFloor 
== 3 and currentRoom == 2: 
      print("Grabbed it!") 
     if currentFloor == 1 and currentRoom == 0: 
      floorsFeature[1].pop(0) 
      floorsFeature[1].insert(0,'stairs going up.') 
      bagItems.append('pokeball') 
      countItems + 1 
     if currentFloor == 2 and currentRoom == 1: 
      floorsFeature[2].pop(1) 
      floorsFeature[2].insert(1,'nothing here.') 
      bagItems.append('pokeball') 
      countItems + 1 
     if currentFloor == 2 and currentRoom == 3: 
      floorsFeature[2].pop(3) 
      floorsFeature[2].insert(3,'stairs going up.') 
      bagItems.append('pokeball') 
      bagItems.append('pokeball') 
      countItems + 1 
     if currentFloor == 3 and currentRoom == 2: 
      floorsFeature[3].pop(2) 
      floorsFeature[3].insert(2,'nothing here.') 
      bagItems.append('potion') 
      countItems + 1 
     else: 
      print("There is nothing to grab!") 
    if move.lower() == 'left': 
     if currentRoom > 0: 
      currentRoom = currentRoom - 1 
      print("Moved to " + floors[currentFloor][currentRoom] + ".") 
     else: 
      print("*Bumping noise* Looks like you can't go that way...") 
    elif move.lower() == 'right': 
     if currentRoom < len(floors) - 1: 
      currentRoom = currentRoom + 1 
      print("Moved to " + floors[currentFloor][currentRoom] + ".") 
     else: 
      print("*Bumping noise* Looks like you can't go that way...") 
    elif move.lower() == 'up': 
     if currentFloor == 0 and currentRoom == 2 or currentFloor == 1 and 
currentRoom == 0 or currentFloor == 2 and currentRoom == 2: 
      currentFloor += 1 
      print("Moved to " + floors[currentFloor][currentRoom] + ".") 
     else: 
      print("*Bumping noise* Looks like you can't go that way...") 
    elif move.lower() == 'down': 
     if currentFloor == 1 and currentRoom == 2 or currentFloor == 2 and 
currentRoom == 0 or currentFloor == 3 and currentRoom == 2: 
      currentFloor -= 1 
      print("Moved to " + floors[currentFloor][currentRoom] + ".") 
    elif move.lower() == 'help': 
     print("Input 'right' to move right. Input 'left' to move left. Input 
'pokemon' to see what Pokemon are on your team. Input 'bag' to see the items 
you are carrying. Input 'up' or 'down' when there are stairs. Input 'grab' 
to grab a grabbable item. Input 'help' to see the commands again.") 
    elif move.lower() == 'pokemon': 
     if count == 0: 
      print("There are no Pokemon on your team.") 
     else: 
      print("The Pokemon on your team are: " + ", ".join(pokemonGot) + 
".") 
    elif move.lower() == 'bag': 
     if countItems <= 0: 
      print("There are no items in your bag.") 
     else: 
      print("The items in your bag are: " + ", ".join(bagItems) + ".") 
    print() 

答えて

0

私はあなたがgrabbableアイテムのハードコーディングの場所を避けることを示唆しています。代わりに、各部屋のグラバーブルアイテムを、部屋の他の「機能」とは別のデータ構造に配置します。部屋に入るときは、フィーチャのデータ構造とグラバブルズのデータ​​構造の両方をチェックしてください。後者が空の場合、項目は言及していません。

アイテムがグラブされた後、グラバブルデータ構造からアイテムを削除するだけで済むので、作業が楽になります。それがなくなると、部屋は最初の場所に決してアイテムがなかったのと同じように動作します。

+0

グラバブルなデータ構造の例を教えてください。私はそれを行う方法がわかりません。 – Adilene

+0

これを実装する方法はいろいろありますが、最も簡単なのは 'floorsFeature'リストのようにすることです。それぞれの部屋のグラバブル構造に 'None'を置くだけで何も得られません。 – Blckknght

関連する問題