函数循环随机停止
Loop in function stops randomly
所以是的,我正在制作一个角色扮演游戏,但战斗不太可靠......如果你能弄清楚原因那就太好了!
#Enemy System
def MMEnemy(PL, HP, STR, DEF, SLTH, Name, CLASSNO):
EnemyLvl = random.randint(PL, (PL * 2))
EnemyHP = random.randint(EnemyLvl * 40, EnemyLvl * 80)
Defend = False
while HP >= 1 & EnemyHP >= 1:
HPPercent = HP / PL
HPPercent = int(HPPercent)
if HPPercent >= 90:
HealthBar = ("█████████▓ | ", HP, " / ",(100 * PL))
elif HPPercent >= 80 and HPPercent <= 89:
HealthBar = ("████████▓░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 70 and HPPercent <= 79:
HealthBar = ("███████▓░░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 60 and HPPercent <= 69:
HealthBar = ("██████▓░░░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 50 and HPPercent <= 59:
HealthBar = ("█████▓░░░░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 40 and HPPercent <= 49:
HealthBar = ("████▓░░░░░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 30 and HPPercent <= 39:
HealthBar = ("███▓░░░░░░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 20 and HPPercent <= 29:
HealthBar = ("██▓░░░░░░░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 10 and HPPercent <= 19:
HealthBar = ("█▓░░░░░░░░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 0 and HPPercent <= 9:
HealthBar = ("▓░░░░░░░░░ | ", HP, " / ",(100 * PL))
else:
HealthBar = "Unknown Health!!"
print("The ", Name," is still standing... (", HealthBar, " HP)")
print("What should you do?")
print("1: Attack\n2: Heal\n3: Defend\n4: Run")
choice = input()
choice = int(choice)
if choice == 1:
roll = random.randint(STR, STR + 2)
print("You attack for ", roll)
EnemyHP -= (roll)
print(Name, " is at ", EnemyHP)
elif choice == 2:
roll = random.randint(PL * -2, PL * 5)
if CLASSNO == 2:
roll += PL * 3
if roll >= 1:
print("You healed for ", roll)
if roll <= 0:
print("You failed your heal [", roll," HP]")
HP += roll
elif choice == 3:
Defend = True
elif choice == 4:
print("Run failed, not implemented yet")
else:
print("None were chosen, you stood still")
DamageTaken = random.randint(5, EnemyLvl * 8)
if Defend == True:
prin("Your Defend was successful")
Defend = False
else:
HP -= DamageTaken
DamageTaken = str(DamageTaken)
print("You got damaged for ", DamageTaken,"!")
if HP <= 0:
print("You have been defeated by the ", Name,"...")
return "Lose"
if EnemyHP <= 0:
print("You defeated ", Name,"!")
return "Win"
CLASSNO = 1
level = 1
Strength = 5
Defence = 5
Stealth = 5
HP = 100
GameEnd = False
battle = MMEnemy(level, HP, Strength, Defence, Stealth, "Irc", CLASSNO)
battle = str(battle)
if battle != "Win" or battle != "Lose":
while battle != "Win" or battle != "Lose":
print("Restarted")
battle = MMEnemy(level, HP, Strength, Defence, Stealth, "Irc", CLASSNO)
battle = str(battle)
if battle == "Win":
print("Battle Won")
GameEnd = True
elif battle == "Lose":
print("Battle lost")
GameEnd = True
else:
print("Nothing Worked")
我试过更改循环的标志并简化它,但似乎没什么用。它应该加载并让你进入战斗,但它在中途停止而不返回任何东西,所以它不会脱离循环
想对代码提出改进建议,但在评论中发布代码时遇到问题,所以这里是:
hp_string = ""
if HPPercent > 0:
full_hp = "█"
current_hp = "▓"
used_hp = "░"
for i in range(int((HPPercent-HPPercent % 10)/10)):
hp_string += full_hp
hp_string += current_hp
if HPPercent <= 90:
for i in range(9 - int((HPPercent-HPPercent % 10)/10)):
hp_string += used_hp
HealthBar = (hp_string + " | ", HP, " / ",(100 * PL))
else:
HealthBar = "Unknown Health!!"
此外,正如评论中有人提到的,&
与 python 中的 and
不同。
所以是的,我正在制作一个角色扮演游戏,但战斗不太可靠......如果你能弄清楚原因那就太好了!
#Enemy System
def MMEnemy(PL, HP, STR, DEF, SLTH, Name, CLASSNO):
EnemyLvl = random.randint(PL, (PL * 2))
EnemyHP = random.randint(EnemyLvl * 40, EnemyLvl * 80)
Defend = False
while HP >= 1 & EnemyHP >= 1:
HPPercent = HP / PL
HPPercent = int(HPPercent)
if HPPercent >= 90:
HealthBar = ("█████████▓ | ", HP, " / ",(100 * PL))
elif HPPercent >= 80 and HPPercent <= 89:
HealthBar = ("████████▓░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 70 and HPPercent <= 79:
HealthBar = ("███████▓░░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 60 and HPPercent <= 69:
HealthBar = ("██████▓░░░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 50 and HPPercent <= 59:
HealthBar = ("█████▓░░░░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 40 and HPPercent <= 49:
HealthBar = ("████▓░░░░░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 30 and HPPercent <= 39:
HealthBar = ("███▓░░░░░░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 20 and HPPercent <= 29:
HealthBar = ("██▓░░░░░░░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 10 and HPPercent <= 19:
HealthBar = ("█▓░░░░░░░░ | ", HP, " / ",(100 * PL))
elif HPPercent >= 0 and HPPercent <= 9:
HealthBar = ("▓░░░░░░░░░ | ", HP, " / ",(100 * PL))
else:
HealthBar = "Unknown Health!!"
print("The ", Name," is still standing... (", HealthBar, " HP)")
print("What should you do?")
print("1: Attack\n2: Heal\n3: Defend\n4: Run")
choice = input()
choice = int(choice)
if choice == 1:
roll = random.randint(STR, STR + 2)
print("You attack for ", roll)
EnemyHP -= (roll)
print(Name, " is at ", EnemyHP)
elif choice == 2:
roll = random.randint(PL * -2, PL * 5)
if CLASSNO == 2:
roll += PL * 3
if roll >= 1:
print("You healed for ", roll)
if roll <= 0:
print("You failed your heal [", roll," HP]")
HP += roll
elif choice == 3:
Defend = True
elif choice == 4:
print("Run failed, not implemented yet")
else:
print("None were chosen, you stood still")
DamageTaken = random.randint(5, EnemyLvl * 8)
if Defend == True:
prin("Your Defend was successful")
Defend = False
else:
HP -= DamageTaken
DamageTaken = str(DamageTaken)
print("You got damaged for ", DamageTaken,"!")
if HP <= 0:
print("You have been defeated by the ", Name,"...")
return "Lose"
if EnemyHP <= 0:
print("You defeated ", Name,"!")
return "Win"
CLASSNO = 1
level = 1
Strength = 5
Defence = 5
Stealth = 5
HP = 100
GameEnd = False
battle = MMEnemy(level, HP, Strength, Defence, Stealth, "Irc", CLASSNO)
battle = str(battle)
if battle != "Win" or battle != "Lose":
while battle != "Win" or battle != "Lose":
print("Restarted")
battle = MMEnemy(level, HP, Strength, Defence, Stealth, "Irc", CLASSNO)
battle = str(battle)
if battle == "Win":
print("Battle Won")
GameEnd = True
elif battle == "Lose":
print("Battle lost")
GameEnd = True
else:
print("Nothing Worked")
我试过更改循环的标志并简化它,但似乎没什么用。它应该加载并让你进入战斗,但它在中途停止而不返回任何东西,所以它不会脱离循环
想对代码提出改进建议,但在评论中发布代码时遇到问题,所以这里是:
hp_string = ""
if HPPercent > 0:
full_hp = "█"
current_hp = "▓"
used_hp = "░"
for i in range(int((HPPercent-HPPercent % 10)/10)):
hp_string += full_hp
hp_string += current_hp
if HPPercent <= 90:
for i in range(9 - int((HPPercent-HPPercent % 10)/10)):
hp_string += used_hp
HealthBar = (hp_string + " | ", HP, " / ",(100 * PL))
else:
HealthBar = "Unknown Health!!"
此外,正如评论中有人提到的,&
与 python 中的 and
不同。