Python-在基于文本的格斗游戏中出现错误

问题描述 投票:0回答:2

我对Python和一般编程非常陌生。我正在尝试为孩子们制作一个简单的基于文本的格斗游戏。这是我自己编写的第一个程序。我不断收到错误消息,我不确定自己做错了什么。

这里是代码:

import random
print("Welcome to Sword Fight!")

#Character Stat Section
#Strength
x_strength = 8
y_strength = 7
z_strength = 4
a_strength = 2
#Speed
x_spd = 4
y_spd = 6
z_spd = 4
a_spd = 3
#Agility
x_agl = 3
y_agl = 5
z_agl = 6
a_agl = 2

#Weapon Stat Section
#Strength
katana_strength = 5
broadsword_strength = 6
spear_strength = 7
dagger_strength = 3
#Speed
katana_spd = 5
broadsword_spd = 4
spear_spd = 3
dagger_spd = 7
#Reach
katana_rch = 5
broadsword_rch = 6
spear_rch = 7
dagger_rch = 3


#Enemy Character Section
enemy = ['a goblin', 'a ninja', 'a swordfighter', 'an orc']
#Strength
goblin_strength = 2
ninja_strength = 4
swordfighter_strength = 5
orc_strength = 7
#Speed
goblin_spd = 4
ninja_spd = 7
swordfighter_spd = 5
orc_spd = 3
#Agility
goblin_agl = 3
ninja_agl = 6
swordfighter_agl = 5
orc_agl = 2

#Enemy Weapon Stat Section
#Strength
shiv_strength = 2
ninjato_strength = 4
longsword_strength = 5
axe_strength = 7
#Speed
shiv_spd = 6
ninjato_spd = 6
longsword_spd = 5
axe_spd = 3
#Reach
shiv_rch = 2
ninjato_rch = 4
longsword_rch = 5
axe_rch = 4

player = str(input("Select your character - X, Y, Z or A: "))
if player == "X" or "x":
    print("You've selected X.")
    p_strength = x_strength
    p_spd = x_spd
    p_agl = x_agl
elif player == "Y" or "y":
    print("You've selected Y.")
    p_strength = y_strength
    p_spd = y_spd
    p_agl = y_agl
elif player == "Z" or "z":
    print("You've selected Z.")
    p_strength = z_strength
    p_spd = z_spd
    p_agl = z_agl
elif player == "A" or "a":
    print("You've selected A.")
    p_strength = a_strength
    p_spd = a_spd
    p_agl = a_agl

weapon = str(input("Select your weapon - katana, broadsword, spear, or dagger: "))
if weapon == "Katana" or "katana":
    p_weap = 'katana'
    print("You've selected a katana.")
    w_strength = katana_strength
    w_spd = katana_spd
    w_rch = katana_rch
elif weapon == "Broadsword" or "broadsword":
    p_weap = 'broadsword'
    print("You've selected a broadsword.")
    w_strength = broadsword_strength
    w_spd = broadsword_spd
    w_rch = broadsword_rch
elif weapon == "Spear" or "spear":
    p_weap = 'spear'
    print("You've selected a spear.")
    w_strength = spear_strength
    w_spd = spear_spd
    w_rch = spear_rch
elif weapon == "Dagger" or "dagger":
    p_weap = 'dagger'
    print("You've selected a dagger.")
    w_strength = dagger_strength
    w_spd = dagger_spd
    w_rch = dagger_rch

print("An enemy approaches! It's", random.choice(enemy), "!")

if random.choice(enemy) == 'a goblin':
    print("It's armed with a shiv.")
    e_strength = goblin_strength
    e_spd = goblin_spd
    e_agl = goblin_agl
    ew_strength = shiv_strength
    ew_spd = shiv_spd
    ew_rch = shiv_rch
elif random.choice(enemy) == 'a ninja':
    print("He's armed with a ninjato.")
    e_strength = ninja_strength
    e_spd = ninja_spd
    e_agl = ninja_agl
    ew_strength = ninjato_strength
    ew_spd = ninjato_spd
    ew_rch = ninjato_rch
elif random.choice(enemy) == 'a swordfighter':
    print("She's armed with a longsword.")
    e_strength = swordfighter_strength
    e_spd = swordfighter_spd
    e_agl = swordfighter_agl
    ew_strength = longsword_strength
    ew_spd = longsword_spd
    ew_rch = longsword_rch
elif random.choice(enemy) == 'an orc':
    print("It's armed with a battleaxe.")
    e_strength = orc_strength
    e_spd = orc_spd
    e_agl = orc_agl
    ew_strength = axe_strength
    ew_spd = axe_spd
    ew_rch = axe_rch

print("The battle begins!")

p_hp = 10
e_hp = 10

while p_hp > 0 or e_hp > 0:
    print = ("Which attack will you use?")
    if p_weap == 'katana':
        attack = str(input("Slice, Stab, or Spinning Slash?: "))
    if attack == 'Slice' or 'slice':
        p_atk = (p_strength + w_strength)
        e_atk = (e_strength + ew_strength)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Stab' or 'stab':
        p_atk = (p_spd + w_spd)
        e_atk = (e_spd + ew_spd)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You stab the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Spinning slash' or 'spinning slash' or 'Spinning Slash':
        p_atk = (p_agl + w_rch)
        e_atk = (e_agl + ew_rch)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    else:
        print("Sorry, your input is invalid. Try again.")    
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if p_weap == 'broadsword':
        attack = str(input("Overhead chop, Stab, or Slash?: "))
    if attack == 'Overhead chop' or 'overhead chop' or 'Overhead Chop':
        p_atk = (p_strength + w_strength)
        e_atk = (e_strength + ew_strength)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Stab' or 'stab':
        p_atk = (p_spd + w_spd)
        e_atk = (e_spd + ew_spd)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You stab the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Slash' or 'slash':
        p_atk = (p_agl + w_rch)
        e_atk = (e_agl + ew_rch)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    else:
        print("Sorry, your input is invalid. Try again.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if p_weap == 'spear':
        attack = str(input("Slam, Swipe, or Thrust?: "))
    if attack == 'Slam' or 'slam':
        p_atk = (p_strength + w_strength)
        e_atk = (e_strength + ew_strength)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Swipe' or 'swipe':
        p_atk = (p_spd + w_spd)
        e_atk = (e_spd + ew_spd)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You stab the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Thrust' or 'thrust':
        p_atk = (p_agl + w_rch)
        e_atk = (e_agl + ew_rch)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    else:
        print("Sorry, your input is invalid. Try again.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if p_weap == 'dagger':
        attack = str(input("Slash, Stab, or Thrust?: "))
    if attack == 'Slash' or 'slash':
        p_atk = (p_strength + w_strength)
        e_atk = (e_strength + ew_strength)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Stab' or 'stab':
        p_atk = (p_spd + w_spd)
        e_atk = (e_spd + ew_spd)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You stab the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

    if attack == 'Thrust' or 'thrust':
        p_atk = (p_agl + w_rch)
        e_atk = (e_agl + ew_rch)
    if p_atk > e_atk:
        e_hp = e_hp - (p_atk - e_atk)
        print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
    if p_atk < e_atk:
        p_hp = p_hp - (e_atk - p_atk)
        print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
    if p_atk == e_atk:
        print("You both attack at the same time. Your weapons clash, and you separate. No damage was done.")
    else:
        print("Sorry, your input is invalid. Try again.")
    print("Your remaining hitpoints:", p_hp)
    print("The enemy's remaining hitpoints:", e_hp)

if p_hp == 0:
    print("You have been defeated...")
if e_hp == 0:
    print("You are victorious!")

我确信可以通过所有更简单的方法来完成所有这些工作,但是我还没有学到更高级的命令或技术。

我遇到的第一个错误是,无论用户输入什么进行字符选择,程序都只会选择字符X。

第二个错误-无论用户选择哪种武器,程序都会选择武士刀。

第三错误-程序选择敌人时,有时会选择正确的敌人武器,有时会选择错误的敌人武器,有时根本不选择敌人武器。

第四错误-如果程序选择了一个敌人并使用了正确的敌人武器,那么当用户进行攻击时,我会得到一个追溯错误:

line 171, in <module>
    print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")
TypeError: 'str' object is not callable
1|:/ $

第五错误-当程序无法选择敌方武器,而用户进入攻击时,我会收到另一个回溯错误:

line 168, in <module>
    e_atk = (e_strength + ew_strength)
NameError: name 'e_strength' is not defined
1|:/ $

第六个错误-当程序选择一个敌人和不正确的敌人武器,并且用户进行攻击时,另一个回溯错误:

line 174, in <module>
    print("The enemy attacks and does", (e_atk - p_atk), "points of damage.")
TypeError: 'str' object is not callable
1|:/ $

任何帮助将不胜感激。谢谢!

python python-3.x
2个回答
0
投票

第三个问题是,每次您运行random.choice(enemy)时,它每次都会随机选择一个新敌人。您应该将random.choice(enemy)放入变量中,然后执行if / else语句。

关于第四个问题,我建议改用.format。

之前

print("You slice the enemy and do", (p_atk - e_atk), "points of damage.")

之后

print("You slice the enemy and do {} points of damage.".format(p_atk - e_atk))

将random.choice(enemy)放入变量时,应该固定第五和第六个错误。


0
投票
  1. 第一个问题是您的或陈述错误。 if player == "X"是正确的,但是or player "x":始终返回true,因为您本质上是在问“是否存在以下字符串'x'?”如果要继续这样做,则需要将所有if或语句更改为if player == "X" or player == "x":

    • 现在更简单的方法是更改​​所有输入(我将使用select character作为示例)

    来自:player = str(input("Select your character - X, Y, Z or A: "))

    至:player = str(input("Select your character - X, Y, Z or A:")).lower()

    整洁的东西现在就是玩家输入的内容都是小写的。您需要做的就是检查它是否等于小写字符串。如果玩家键入“ HellOw WoRlD”,则lower()会将其转换为“ hello world”。

  2. 与问题一相同,您需要将所有类似if weapon == "Katana" or "katana":的if语句更改为if weapon == "Katana" or weapon == "katana":

    • 同样,如果使输入小写,则只需要检查武器==“ katana”。
  3. e_strength在多个if语句中定义,但绝不在任何范围之外,您需要将其放在外部范围内。有关更多信息,请参见python中作用域的this说明。基本上,答案是您需要首先在if语句之外(可能在第123行上)定义e_strength并为其提供所需的任何值,因为想法是应将其覆盖。在修复了该问题之后,这似乎是一个经常发生的问题,其他一些与范围相关的错误也变得显而易见。

  4. 哇,这很时髦。第163行,您编写了print = ("Which attack will you use?"),应该是print("Which attack will you use?"),这完全破坏了它后面的所有代码。

  5. 与4.有相同的问题。

  6. 与4.有相同的问题。

© www.soinside.com 2019 - 2024. All rights reserved.