-1

我一直无法在我的 python 冒险游戏中做出动作。运行时,玩家可以移动到不同的房间,但当试图回到房间时,代码会停止。

这是代码:

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"
4

2 回答 2

0

您应该附上一段代码,您可以在其中获取和评估命令到另一个大的 while 循环。就像是:

while IsCharacterAlive:
    Command = input()
    ...
于 2016-07-16T16:40:40.210 回答
0

您可能应该将整个 shebang 包装在一个 while 循环中。现在您的代码执行以下操作...

  1. 介绍
  2. 运行房间 A1 逻辑
  3. 运行房间 B1 逻辑
  4. 出口

在最后一个 while 循环不再为真之后,它就没有更多的代码可以去(也没有办法让它回到 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.
于 2016-07-16T16:34:51.893 回答