2013-09-22 4 views
-1

이것은 stackoverflow에 대한 나의 첫 번째 게시물이며 나는 정말로 붙어 있기 때문에 묻고있는 중이다. 좋은 친구와 나는 간단한 텍스트 기반 게임을 만들기 위해 노력하고있다. 근본적으로 파이썬과의 첫날이고 문제가 생겼습니다. 플레이어는 단검을 가져올 지 선택하지 않습니다. 나중에 나는이 결정이 작용하기를 바랍니다. 나는 플레이어가 무엇을 입력했는지 확인하고 볼 방법을 모른다. 여기 내 코드가 입니다. 참고 :이 문제가 해결되도록 노력하면서 주위를 둘러 보았기 때문에 현재 상황이 정상적으로 작동하지 않습니다.사용자가 다른 기능에서 무엇을 입력했는지 확인하려면 어떻게해야합니까?

def start(): 
     print("You wake up in a dank tunnel reeking of death and decomposition,") 
     print("you have no weapons to defend yourself with.") 
     time.sleep(1) 
     print("\n") 
     print("You head north and find yourself at a four way tunnel.") 
     print("\n") 
     print("Down the west tunnel, you hear sounds which could have only come") 
     print("from hell itself. With every sound uttered, your fear grows.") 
     time.sleep(1) 
     print("\n") 
     print("Down the north tunnel, there is a room with a glimmering object") 
     print("lying on the ground.") 
     time.sleep(1) 
     print("\n") 
     print("Down the East tunnel, there appears to have been a cave in.") 
     time.sleep(1) 
     print("\n") 
     print("The South tunnel is the way you just came. \n") 
     time.sleep(1) 
     while True: 
      r3 = input("Which way would you like to go?\n") 
      if r3 == "west" or r3 == "West": 
       print("The sounds are becoming Louder, do you continue or head back?") 
       while True: 
        w1 = input("") 
        if w1 == "continue" or w1 == "Continue" or w1 == "west": 
          print("You continue further on...") 
          time.sleep(2.5) 
          westtunnel() 
        elif w1 == "head back" or w1 == "Head Back" or w1 == "back" or w1 == "Back": 
         print("\n") 
         print("The voices congregate behind you. No way your going back that way!\n") 
         time.sleep(1) 
         westtunnel() 
        else: 
         print("\n") 
         print("You couldn't possibly do anything else.\n") 
         time.sleep(1) 
         print("Greater minds prevail and you continue westward...\n") 
         time.sleep(2) 
         westtunnel() 
      elif r3 == "north" or r3 == "North": 
       print("You find a rusty dagger on the floor, stained with blood.") 
       print("Do you want to pick up the dagger?") 
       print("1: Yes, that seems useful!") 
       print("2: No, I'm a bit of a pacifist you see.") 
       while True: 
        pd = input("") 
        if pd == "1": 
         print("You slowly picked up the dagger.") 
         number = int(pd) 
         break 
        else: 
         print("You left the dagger. All alone.") 
         number = int(pd) 
         break 
       print("You can go only go back the way you came.\n") 
       time.sleep(1) 
       print("You head back to the four way tunnel.") 
      elif r3 == "east" or r3 == "East": 
       print("\n") 
       print("You can not go that way, there are rocks there. We told you this.\n") 
       time.sleep(1) 
       print("You go back to the four way tunnel") 
      elif r3 == "south" or r3 == "South": 
       print("\n") 
       print("This is the room you awoke in, there is nothing of interest.\n") 
       time.sleep(1) 
       print("You head back to the four way tunnel") 
      else: 
       print("\n") 
       print("You run into a corner, you hurt yourself in confusion. \n") 
       time.sleep(1) 
       print("You stumble back to the four way.") 

def ladder(): 
    print("Do you have a dagger?!") 
    number = pd 
    if number == "1": 
     print("Good you have it!") 
     start() 
     input("") 
    else: 
     print("awh...no dagger for you...") 
     start() 
     input("") 
if __name__ == '__main__': 
    menu() 
+2

관련성을 유지하기 위해이 코드를 줄일 수 있습니까? – arshajii

+0

내가 알아챈 한 가지는 코드에서 많은 break 문을 사용하지 않는다는 것입니다. 따라서 다른 시나리오를 호출하지 않는 한 재생 된 시나리오는 계속 재생됩니다. 이것은 실제로 재귀 코드와 같으며 사용자가 게임을하는 시간에 따라 스택 오버플로 (의도적 인 말장난)가 발생할 수 있습니다. – smac89

답변

3

파이썬 클래스를 살펴보십시오.

게임 과정에서 결정된 결과를 저장하는 게임 상태 개체를 만들고 싶을 수도 있습니다. 그런 다음 나중에 결정 결과가 중요 할 때 상태를 확인합니다.

기본 게임 루프 중에 해당 게임 상태 개체에 대한 참조를 유지해야합니다. 그러나 한 개체에 보관하면 여러 가지 변수에 대한 참조를 유지하는 대신 모든 상태 정보가 정리 된 상태로 유지됩니다.

+0

저는 게임 상태 객체를 어떻게 만들 수 있는지 조금 혼란 스럽습니다. 웹에서 검색해 보았는데 기껏해야 모호한 기사를 발견했습니다. 그래도 도움을 주셔서 감사합니다! – user2804885

관련 문제