【发布时间】:2016-11-30 19:12:39
【问题描述】:
最近,我和我的朋友开始在 Python 上为一款奇幻风格的游戏制作基于文本的战斗序列(在控制台中)。
概述
它的工作方式是向用户呈现:他们的攻击(Att)+ HP(HP)的当前情况,谁拥有更高的智力(Inl)谁先走。如果用户先走,他们将获得三个选项——攻击、吓跑和任何其他动作。在示例中,我使用了“Bide your time”。
对手(不受用户控制)然后在他们的任意 3 个动作之间随机选择,有不同的机会,然后执行。然后它应该切换回用户回合,现在不管一个人是否有智力比其他人高。
这应该继续交替,直到其中一个 HP = 0。如果用户击败对手,那么它应该可以选择吸收对手的统计/属性之一。
在代码中,'battle' 变量取决于战斗是否开始。如果'battle'是1,循环应该继续,但如果它是0,那么它应该停止(在你吓跑它之后添加0,或者
问题:
每当我运行代码时,无论发生什么,它都会继续循环,并且一直轮到我。
- 我是否误解了 while 循环的实际工作原理?
未来计划
-
我计划添加不同的种族(即矮人、精灵、兽人等),以平衡的方式改变用户的生命值、攻击力和智力。
-
我还计划添加职业,最终将用特定职业的招式取代“等待你的时间”招式。
感谢您阅读并提前提出您提出的任何建议/修复!
import random
import time
Att = 10
Inl = 5
HP = 100
OpAtt = 10
OpInl = 4
OpHP = 100
FirstMove = 1
OpFirstMove = 0
battle = 1
while battle == 1:
if Inl > OpInl:
FirstMove == 1
OpFirstMove == 0
else:
FirstMove == 0
OpFirstMove == 1
if FirstMove > OpFirstMove:
print("Your Current stats are:")
print("Attack:", Att)
print("HP:", HP)
time.sleep(2)
print("Your Opponent's current stats are:")
print("Attack:", OpAtt)
print("HP:", OpHP)
time.sleep(2)
print("Your turn!")
time.sleep(1)
else:
print("Your Current stats are:")
print("Attack:", Att)
print("HP:", HP)
time.sleep(2)
print("Your Opponent's current stats are:")
print("Attack:", OpAtt)
print("HP:", OpHP)
time.sleep(2)
print("Opponent's turn!")
time.sleep(1)
if FirstMove > OpFirstMove:
print("Select a move:")
Move1 = str(input(print("[A] Attack opponent\n[B] Bide your time\n[C] Try to scare your opponent off")))
if Move1 == 'A' or Move1 == 'a':
Hit1 = random.randint(1, 4)
if Hit1 == 4 or 3 or 2:
damageRand = random.randint(8, 12)
OpHP = OpHP - damageRand
print("Success!, Opponet's HP is now", OpHP, ".")
FirstMove == 0
OpFirstMove == 1
else:
print("Failure, no damage dealt.")
FirstMove == 0
OpFirstMove == 1
elif Move1 == 'b' or Move1 == "B":
print("You do nothing")
FirstMove == 0
OpFirstMove == 1
elif Move1 == "c" or "C":
Scare1 = random.randint(1, 7)
if OpHP > 71:
if Scare1 == 7 or 6:
print("Congratulations!, you scared the opponent away, however, this means you gained no loot.")
battle == 0
elif OpHP < 70:
if Scare1 == 7 or 6 or 5 or 4:
print("Congratulations!, you scared the opponent away, however, this means you gained no loot.")
battle == 0
else:
print("Failure, the opponent stands his ground")
FirstMove == 0
OpFirstMove == 1
if OpFirstMove > FirstMove:
OpMove1 = random.randint(1, 8)
if OpMove1 == 1 or OpMove1 == 2 or OpMove1 == 3 or OpMove1 == 4 or OpMove1 == 5:
OpHit1 = random.randint(1, 4)
if OpHit1 == 4 or 3 or 2:
damageRand = random.randint(8, 12)
HP = HP - damageRand
print("Your Opponent Attacks!, Your HP is now", HP, ".")
FirstMove = 1
OpFirstMove = 0
else:
print("Your Opponent failed to attack you, no damage received.")
FirstMove = 1
OpFirstMove = 0
elif OpMove1 == 6:
print("Your Opponent does nothing")
FirstMove = 1
OpFirstMove = 0
elif OpMove1 == 7 or OpMove1 == 8:
OpScare1 = random.randint(1, 7)
if HP > 71:
if OpScare1 == 7 or 6:
print("Aargh!, you were scared away, don't worry though, this has no bad affects.")
battle == 0
elif HP < 70:
if OpScare1 == 7 or 6 or 5 or 4:
print("Aargh!, you were scared away, don't worry though, this has no bad affects.")
battle == 0
else:
print("Your Opponent tried to scare you away, but failed")
FirstMove = 1
OpFirstMove = 0
else:
print("Pies are tasty")
if OpHP < 1:
print ("Congratulations! You deafeated your Opponent, which one of their stats would you like to absorb?")
print("Your Opponent's current stats are:")
print("Attack:", OpAtt)
print("HP:", OpHP)
print ("Intellect:", OpInl)
absorb1 = print (str(input("[A] Intellect\n[B] Attack\n[C] HP\n[D] None of the above")))
if absorb1 == "a" or absorb1 == "A":
Inl == OpInl
print ("Opponent's intellect absorbed")
elif absorb1 == "b" or absorb1 == "B":
Att == OpAtt
print ("Opponent's Attack absorbed")
elif absorb1 == "c" or absorb1 == "C":
HP == OpHP
print ("Opponent's HP absorbed")
elif (absorb1 == "d" or absorb1 == "D"):
print ("None of your Opponent's stats absorbed")
print("Your current stats are:")
print("Attack:", Att)
print("HP:", HP)
print ("Intellect:", Inl)
elif HP < 1:
print ("Oh no! You were defeated by your Opponent!")
battle == 0
【问题讨论】:
-
请参阅How to ask a good question 和Creating a Minimal, Complete, and Verifiable Example。一般来说,包含代码的问题应该被缩小到只包含重现问题所需的最少量代码——比如说,工作不正常的特定函数(或该函数的几行),以及最少量的代码。使其运行所需的支持代码。
-
battle == 0检查battle是否为零。battle = 0将battle的值赋值为零 -
您不要在
input()中放置打印语句。不确定这是否是错误的,但它是不对的......
标签: python text-based