2017-11-07 1 views
-1

저는 코딩의 초보자이고 현재는 RPG로 작업 중입니다. 방에 잡을 수있는 항목이있을 때 목록으로 "잡아주는 항목"옵션을 구현하려고합니다. 하지만 아이템을 가져온 후에도 아이템은 여전히 ​​잡을 수 있습니다. 누군가 초보자가 이해할 수있는 간단한 해결책을 줄 수 있습니까? (내일이를 설정해야합니다.)이 grabbable 항목 객실은 다음과 같습니다. '올라가는 계단 과 pokeball'목록을 사용하여 "잡아"옵션을 제거하는 방법은 무엇입니까?

  • 플로어 2 방 1 (currentFloor == 1 및 currentRoom == 0)
  • 'pokeball!' floor 3 room 2 (currentFloor == 2 and currentRoom == 1)
  • '계단이 올라가고 2 개의 pokeballs!' 플로어 3 방 4 (currentFloor == 2 및 currentRoom == 3)
  • '포션!' 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 항목의 위치를 ​​하드 코딩하지 않는 것이 좋습니다. 대신 각 방의 잡을 수있는 항목을 방의 다른 "기능"과는 별도의 데이터 구조에 넣으십시오. 방에 입장 할 때, 피처 데이터 구조와 grabbalbes 데이터 구조를 모두 점검하십시오. 후자가 비어 있으면 항목을 언급하지 않습니다.

이렇게하면 항목을 가져온 후에 grabbables 데이터 구조에서 항목을 제거 할 수 있기 때문에 작업이 더 쉬워집니다. 일단 사라지면, 처음에는 그곳에 어떤 아이템도 없었던 것처럼 행동 할 것입니다.

+0

grabbable 데이터 구조의 예를 들어 주시겠습니까? 나는 그것을하는 방법을 확신하지 못한다. – Adilene

+0

구현 방법은 여러 가지가 있지만, 가장 간단한 방법은 'floorFeature' 목록과 똑같이 보이게하는 것입니다. 각 방의 grabbables 구조에'None'을 넣으면 아무 것도 잡을 수 없습니다. – Blckknght

관련 문제