2016-07-16 2 views
-1

나는 파이썬 어드벤처 게임에서 움직이는 운동을하는데 어려움을 겪고있다. 실행하면 플레이어가 다른 방으로 이동할 수 있지만 다시 방으로 이동하려고하면 코드가 중지됩니다. 당신은 아마 while 루프에서 전체 오두막을 포장한다Zork과 유사한 파이썬 게임에서 루프가 움직이는 동안

A = False 
B = False 
Location = "A1" 
Attack = 0 
Defence = 0 
Speed = 0 
Health = 20 
Knives = 10 
Arrows = 10 
inv = "a torch" 
print('Hello operator') 
Name = input('We will now begin preliminary setup, tell me, what is your name? ') 
print('Good', Name) 
print('Next select a Class') 
print('Knight - A well rounded class with no certain advantage in any category, fights with a sword and metal armor') 
print('Archer - Good with a bow and has amazing speed but wears light metal armor') 
print('Mage - Can cast spells with a magic staff and can deal high amounts of damage but wears only cloth armor') 
print('Rogue - A master of deception and very quick on thier feet, can deal devistating blows with a dagger and throwing knives') 
while A == False: 
    Class = input('Input class name here: ') 
    if Class.lower() == "knight": 
     Attack = 10 
     Defence = 10 
     Speed = 10 
     print('You have chosen Knight') 
     A = True 
    elif Class.lower() == "archer": 
     Attack = 10 
     Defence = 3 
     Speed = 8 
     print('You have chosen Archer') 
     A = True 
    elif Class.lower() == "mage": 
     Attack = 15 
     Defence = 1 
     Speed = 8 
     print('You have chosen Mage') 
     A = True 
    elif Class.lower() == "rogue": 
     Attack = 10 
     Defence = 2 
     Speed = 15 
     print('You have chosen Rogue') 
     A = True 
    else: 
     print('That is not a class') 
print("For a full list of commands type [help]") 
Command = input() 
if Command.lower() == "help": 
    print('help - Displays all commands') 
    print('n,s,e,w - moves one tile in that direction') 
    print('inv - Opens and checks your current inventory') 
    print('stats - Prints all character stats') 
    print('check - prints the information on the surroundings') 
    print('Other words like [attack][run][drop][drink] and others can all be used throught the game, try words out to see if they work!') 
elif Command.lower() == "check": 
    print("You have awoken in a small room. The walls are made of stone and the only light is being given off by a torch") 
elif Command.lower() == "stats": 
    print("Name:", Name,", Class:", Class,", Attack:", Attack,", Defence:", Defence,", Speed:", Speed) 
elif Command.lower() == "inv" and Class.lower() == "knight": 
    print("You are carrying a sword and", inv) 
elif Command.lower() == "inv" and Class.lower() == "mage": 
    print("You are carrying a magical staff and", inv) 
elif Command.lower() == "inv" and Class.lower() == "archer": 
    print("You are carrying a bow,", Arrows, "arrows and", inv) 
elif Command.lower() == "inv" and Class.lower() == "rogue": 
    print("You are carrying", Knives, " throwing knives and", inv)  
while Location == "A1": 
    if Command.lower() == "n": 
     print("You can't go that way") 
     Location = "A1" 
    elif Command.lower() == "s": 
     print("You move into a small room with a corridor on two sides") 
     Location = "B1" 
    elif Command.lower() == "w": 
     print("You can't go that way") 
     Location = "A1" 
    elif Command.lower() == "e": 
     print("You are in a corridor") 
     Location = "A2" 
    elif Command.lower() == "n": 
     print("Starting room") 
     Location = "A1" 
while Location == "A2": 
    Command = input() 
    if Command.lower() == "n": 
     print("You can't go that way") 
     Location = "A2" 
    elif Command.lower() == "s": 
     print("You are in a corridor, a torch lights the hall") 
     Location = "B2" 
    elif Command.lower() == "e": 
     print("You walk into a room, inside the room is a statue") 
     Location = "A3" 
    elif Command.lower() == "w": 
     print("Starting room") 
     Location = "A1" 
while Location == "B1": 
    Command = input() 
    if Command.lower() == "n": 
     print("Starting room") 
     Location = "A1" 
    elif Command.lower() == "s": 
     print("You can't go that way") 
     Location = "B1" 
    elif Command.lower() == "e": 
     print("You are in a corridor, a torch lights the hall") 
     Location = "B2" 
    elif Command.lower() == "w": 
     print("You can't go that way") 
     Location = "B1" 
+0

한 것이 이해가되지 않는 한, PEP8 (https://www.python.org/dev/peps/pep-0008/#prescriptive-naming-conventions) 명명 규칙을 사용하는 것을 고려 그것을 깨기. 코드를 사용하면 다른 프로그래머가 이름이 변수 또는 클래스를 참조하는지 신속하게 인식하기가 어렵습니다. –

답변

0

: 여기

는 코드입니다. 루프가 어떤을하는 동안 지금 당신의 코드는 다음 ...

  1. 소개
  2. 실행 방 A1 로직
  3. 실행이 지난 후 객실 B1 논리
  4. 종료

을하지 않습니다 더 이상 사실이되면 더 이상 코드를 사용할 수 없으며 A1 루프로 돌아갈 방법이 없습니다.

코드를 보면 이미 한 번 해보았습니다.

while A == False: 
    Class = input('Input class name here: ') 
    if Class.lower() == "knight": 
    ... 

기본 명령 루프와 완전히 동일한 패턴을 사용하십시오.

Command = '' 
while Command.lower() != 'exit': 
    print("For a full list of commands type [help]") 
    Command = input() 
    if Command.lower() == "help": 
     ... 
    # elif Command == '' 
    # put the rest of your logic here. 
+0

해결할 수있는 while 루프의 예를 보여줄 수 있습니까? 나는 그것을 어떻게 되돌려 놓을지를 꽤 잘 모른다. –

0

커맨드를 가져 와서 평가할 때 코드 블록을 동그라미로 묶어야합니다. 뭔가 같이 :

while IsCharacterAlive: 
    Command = input() 
    ... 
관련 문제