Python 战斗序列

标签 python python-3.x wing-ide

我正在尝试构建一个简单的战斗序列,用户可以在其中选择他们的“职业”(战士、弓箭手、法师)以及他们想要战斗的怪物(哥布林、巨魔、兽人)。

我目前的代码是:

import random 

def choosePlayerClass():

    class Warrior:
        health = 100
        attack = 10
        defense = 10

    class Archer:
        health = 75
        attack = 15
        defense = 7

    class Mage:
        health = 50
        attack = 20
        defense = 5


    playerChoice = input("What class do you want to be? (Warrior, Archer, Mage)? ")
    if playerChoice == "Warrior":
        Player = Warrior()
    elif playerChoice == "Archer":
        Player = Archer()
    elif playerChoice == "Mage":
        Player = Mage()

    return Player

def chooseMonsterClass():

    class Goblin:
        health = 25
        attack = 10
        defense = 5
        description = "Goblin"

    class Troll:
        health = 50
        attack = 13
        defense = 7
        description = "Troll"

    class Orc:
        health = 75
        attack = 15
        defense = 10
        description = "Orc"  

    monsterChoice = input("What kind of monster do you want to fight? (Goblin, Troll, Orc)? ")

    if monsterChoice == "Goblin":
        Monster = Goblin()
    elif monsterChoice == "Troll":
        Monster = Troll()
    elif monsterChoice == "Orc":
        Monster = Orc

    return Monster

def fightSequence():
    Player = choosePlayerClass()
    Monster = chooseMonsterClass()
    encounter = 1
    turn = 'player'
    while encounter == 1:
        if turn == 'player':
            action = input("What would you like to do (Attack)? ")
            if action == 'Attack':
                encounter = humanAttack(Player)
                turn = 'monster'
        elif turn == 'monster':
            encounter = monsterAttack(Monster)
            turn = 'player'


fightSequence()

我得到这个错误:

追溯(最近的调用最后): 文件“C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver_sandbox.py”,第 109 行,在 文件“C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver_sandbox.py”,第 102 行,在 fightSequence 中 humanAttack 中的文件“C:\Program Files (x86)\Wing IDE 101 4.1\src\debug\tserver_sandbox.py”,第 63 行 builtins.NameError: 全局名称 'Monster' 未定义

谢谢!

最佳答案

在这里初始化 Monster 变量:

if monsterChoice == "Goblin":
    Monster = Goblin()
elif monsterChoice == "Troll":
    Monster = Troll()
elif monsterChoice == "Orc":
    Monster = Orc()

但是如果这些都不是真的并且没有输入任何 if 语句怎么办?您应该在 if 语句之前为变量设置默认值,以便处理用户输入一些无意义的情况:

Monster = DefaultRace
if monsterChoice == "Goblin":
    Monster = Goblin()
elif monsterChoice == "Troll":
    Monster = Troll()
elif monsterChoice == "Orc":
    Monster = Orc()

最好将整个事情放在一个循环中,并在给出无意义的比赛时要求用户输入有效的比赛。

while True:
    monsterChoice = input("What kind of monster do you want to fight? (Goblin, Troll, Orc)? ")
    if monsterChoice in ["Goblin","Troll","Orc"]:
        break
    else:
        print "Unrecognized race requested, please select one of Goblin, Troll, Orc."

关于Python 战斗序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14243831/

相关文章:

python - dpkt 在有效的 pcap 上抛出 NeedData

python - 如何打印返回值?

python - reshape 序列 Numpy Pandas python

python - Django - 将异常传递给调试器

python - 翼侧特征

python - Wing IDE 调试服务器在探测本地时遇到错误

python - Setup.py:如何添加外部安装候选项?

python - 值错误 : all the input arrays must have same number of dimensions

python - 通过 python 设置 SQLite 数据库的(默认)编码

python - pandas DataFrame.replace 函数因日期时间而损坏