如何修复我的Python文字游戏中的问题?

huangapple go评论69阅读模式
英文:

How can I fix this issue with my python text-based game?

问题

以下是您提供的代码的翻译部分:

import os
import random

battle = True

nameA = "John"
nameB = "Marcus"

maxhpA = 50
hpA = 50  # 生命值
atkA = 5  # 攻击伤害
potA = 10  # 药水数量
luckA = 30
goldA = 0  # 玩家金币数量

maxhpB = 50
hpB = 50  # 生命值
atkB = 5  # 攻击伤害
potB = 10  # 药水数量
luckB = 30
goldB = 0  # 玩家金币数量

def draw():
    print("XX-----------------------XX")

def clear():
    os.system("cls")

clear()

def stop():
    input(">")

def healA(amount_healed):
    global hpA, maxhpA, nameA
    if hpA + amount_healed < maxhpA:
        hpA += amount_healed
    else:
        hpA = maxhpA
        print(nameA + "'s HP已恢复至" + str(hpA) + "!")

def healB(amount_healed):
    global hpB, maxhpB, nameB
    if hpB + amount_healed < maxhpB:
        hpB += amount_healed
    else:
        hpB = maxhpB
        print(nameB + "'s HP已恢复至" + str(hpB) + "!")

def turnA():
    global hpA, hpB, atkA, atkB, nameA, nameB, potA, potB

    choice = input(nameA + "'s回合>")

    if choice == "1":
        hpB -= atkA
        print(nameA + "攻击了!")
        print(nameB + "受到" + str(atkA) + "伤害!")
        stop()
    elif choice == "2":
        if random.randint(1, 100) < int(luckA):
            hpB -= 2 * atkA
            print()
            print(nameA + "进行了特殊攻击!")
            print(nameB + "受到" + str(2 * atkA) + "伤害!")
            stop()
        else:
            print(nameA + "未命中特殊攻击!")
            stop()
    elif choice == "3":
        if potA > 0:
            potA -= 1
            print(nameA + "使用了药水。")
            healA(25)
            input(">")
        else:
            print("没有药水!")
            stop()
    else:
        print("无效选项!")
        turnA()
        clear()
        stats()
        stop()

def turnB():
    global hpA, hpB, atkA, atkB, nameA, nameB, potA, potB

    choice = input(nameB + "'s回合>")

    if choice == "1":
        hpA -= atkB
        print(nameB + "攻击了!")
        print(nameA + "受到" + str(atkB) + "伤害!")
        stop()
    elif choice == "2":
        if random.randint(1, 100) < int(luckB):
            hpA -= 2 * atkB
            print()
            print(nameB + "进行了特殊攻击!")
            print(nameA + "受到" + str(2 * atkB) + "伤害!")
            stop()
        else:
            print(nameB + "未命中特殊攻击!")
            stop()
    elif choice == "3":
        if potB > 0:
            potB -= 1
            print(nameB + "使用了药水。")
            healB(25)
            stop()
        else:
            print("没有药水!")
            stop()
    else:
        print("无效选项!")
        turnB()
        clear()
        stats()
        stop()

def stats():
    print("名称:    {0:15}{1}".format(str(nameA), str(nameB)))
    print("生命值:      {0:15}{1}".format(str(hpA), str(hpB)))
    print("攻击伤害:     {0:15}{1}".format(str(atkA), str(atkB)))
    print("药水数量: {0:15}{1}".format(str(potA), str(potB)))
    print("幸运值:    {0:15}{1}".format(str(luckA), str(luckB)))

    draw()

    print("1 - 攻击")
    print("2 - 冒险攻击")
    print("3 - 使用药水 (25HP)")

    draw()

while battle:
    stats()

    turnA()

    clear()

    stats()

    turnB()

    clear()

希望这能帮助您理解代码,并找到问题所在。如果您有任何其他问题,请随时提出。

英文:
import os
import random
battle = True
nameA = &quot;John&quot;
nameB = &quot;Marcus&quot;
maxhpA = 50
hpA =50 #amount of health
atkA = 5 #attack damage
potA = 10 #potion number
luckA = 30
goldA = 0 #amount of player_gold
maxhpB = 50
hpB =50 #amount of health
atkB = 5 #attack damage
potB = 10 #potion number
luckB = 30
goldB = 0 #amount of player_gold
def draw():
print(&quot;XX-----------------------XX&quot;)
def clear():
os.system(&quot;cls&quot;)
clear()
def stop():
input(&quot;&gt;&quot;)
def healA(amount_healed):
global hpA, maxhpA, nameA
if hpA + amount_healed &lt; maxhpA:
hpA += amount_healed
else:
hpA = maxhpA
print(nameA + &quot;&#39;s HP refilled to &quot; + str(hpA) + &quot;!&quot;)
def healB(amount_healed):
global hpB, maxhpB, nameB
if hpB + amount_healed &lt; maxhpB:
hpB += amount_healed
else:
hpB = maxhpB
print(nameB + &quot;&#39;s HP refilled to &quot; + str(hpB) + &quot;!&quot;)
def turnA():
global hpA, hpB, atkA, atkB, nameA, nameB, potA, potB
choice = input( nameA + &quot;&#39;s turn &gt;&quot;)
if choice == &quot;1&quot;:
hpB -= atkA
print(nameA +&quot; attacked!&quot;)
print(nameB + &quot; took &quot; + str(atkA) + &quot; damage!&quot;)
stop()
if choice == &quot;2&quot;:
if random.randint(1,100) &lt; int(luckA):
hpB -= 2*atkA
print()
print(nameA +&quot; preformed a special attack!!&quot;)
print(nameB + &quot; took &quot; + str(2*atkA) + &quot; damage!&quot;)
stop()
else:
print(nameA + &quot; missed their special attack!&quot;)
stop()
if choice == &quot;3&quot;:
if potA &gt; 0:
potA -= 1
print(nameA +&quot; used a potion.&quot;)
healA(25)
input(&quot;&gt;&quot;)            
else:
print(&quot;No potions!&quot;)
stop()    
else: #issue
print(&quot;Invalid option!&quot;)
turnB()
clear()  
stats()
stop() 
def turnB():
global hpA, hpB, atkA, atkB, nameA, nameB, potA, potB
choice = input( nameB + &quot;&#39;s turn &gt;&quot;)
if choice == &quot;1&quot;:
hpA -= atkB
print(nameB +&quot; attacked!&quot;)
print(nameA + &quot; took &quot; + str(atkB) + &quot; damage!&quot;)
stop()
if choice == &quot;2&quot;:
if random.randint(1,100) &lt; int(luckB):
hpA -= 2*atkB
print()
print(nameB +&quot; preformed a special attack!!&quot;)
print(nameA + &quot; took &quot; + str(2*atkB) + &quot; damage!&quot;)
stop()
else:
print(nameB + &quot; missed their special attack!&quot;)
stop()
if choice == &quot;3&quot;:
if potB &gt; 0:
potB -= 1
print(nameB +&quot; used a potion.&quot;)
healB(25)
stop()            
else:
print(&quot;No potions!&quot;)
stop()
else: # issue is here!
print(&quot;Invalid option!&quot;)
turnB()
clear()  
stats()
stop()  
def stats():
print(&quot;Name:    {0:15}{1}&quot;.format(str(nameA), str(nameB)))
print(&quot;HP:      {0:15}{1}&quot;.format(str(hpA), str(hpB)))
print(&quot;ATK:     {0:15}{1}&quot;.format(str(atkA), str(atkB)))
print(&quot;POTIONS: {0:15}{1}&quot;.format(str(potA), str(potB)))
print(&quot;LUCK:    {0:15}{1}&quot;.format(str(luckA), str(luckB)))
draw()
print(&quot;1 - ATTACK&quot;)
print(&quot;2 - RISKY ATTACK&quot;)
print(&quot;3 - USE POTION (25HP)&quot;)
draw()
while battle:
stats()
turnA()
clear()
stats()
turnB()
clear()

Hello. This is a python text-based game I have been working on. It is very simple but there is an error I just cannot figure out. The game works very simply, player A has their turn, and then player B gets theirs, to be expected.

All the options work fine, but I am getting an error with the "else" parts of the turnA and turnB functions. If you play a move that's invalid, I want the player to have another chance to make their move, but it is causing issues as seen in the image below. every second input is now counted as an invalid input.

如何修复我的Python文字游戏中的问题?

答案1

得分: 1

你当前的代码看起来类似于这样:

if choice == "1":
    # 做某事
if choice == "2":
    # 做某事
if choice == "3":
    # 做某事
else:
    # 做某事

使用这个语法,当choice不是选项3时,else语句会执行。相反,你应该使用:

if choice == "1":
    # 做某事
elif choice == "2":
    # 做某事
elif choice == "3":
    # 做某事
else:
    # 做某事

这将依次检查每个选项,然后在没有匹配任何choice选项时运行else条件。

此外,你可以阅读一下关于Python类的内容。这个项目将是一个很好的方式来介绍你自己一些面向对象编程中的新概念,比如在对象中存储属性,比如HP。

英文:

Your current code looks similar to this:

    if choice == &quot;1&quot;:
        # Do something
    if choice == &quot;2&quot;:
        # Do something
    if choice == &quot;3&quot;:
        # Do something
    else:
        # Do something

With this syntax, your else statement is applying when the choice is not Option 3. Instead, you should use:

    if choice == &quot;1&quot;:
        # Do something
    elif choice == &quot;2&quot;:
        # Do something
    elif choice == &quot;3&quot;:
        # Do something
    else:
        # Do something

This will step through each choice and then run the else condition if none of the choice options match.

Also whilst you're here, have a read around Python Classes. This project will be a good way to introduce yourself to some new concepts within object oriented programming e.g. storing properties such as HP within objects.

huangapple
  • 本文由 发表于 2023年3月7日 01:24:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定