我是一般的编码新手,我选择python作为我的第一语言学习,因为有些朋友说这很简单有趣。我决定制作一个基于文本的小游戏,除了一个问题,我的演示进展顺利。我无法对健康进行调整,经过大量的测试和搜索,我仍然无法弄清楚如何使这项工作。
def combat():
global playerhealthpoints
playerhealthpoints = 100
def cavecombat():
combatroundinput = raw_input("The round has begun. Do you Block, Dodge, or Run?").lower()
if combatroundinput == "block":
enemyattackroll = random.randrange(1,10)
if enemyattackroll < 5:
print "Enemy has missed their attack this round."
cavecombat()
elif enemyattackroll >= 5:
enemydamageroll = random.randrange(1,10) / 2
playerhealthpoints = playerhealthpoints - enemydamageroll
print "You have taken %s damage." %enemydamageroll
print "You have %s health remaining." %playerhealthpoints
cavecombat()
elif combatroundinput == "dodge":
enemyattackroll = random.randrange(1,10) / 2
if enemyattackroll < 5:
print "Enemy has missed their attack this round."
cavecombat()
elif enemyattackroll >= 5:
enemydamageroll = random.randrange(1,10)
playerhealthpoints = playerhealthpoints - enemydamageroll
print "You have taken %s damage." %enemydamageroll
print "You have %s health remaining." %playerhealthpoints
cavecombat()
elif combatroundinput == "run":
playerrunroll = random.randrange(1,10)
enemyattackroll = random.randrange(1,10) * 2
if enemyattackroll < 5 and playerrunroll <5:
print "Enemy has missed their attack this round."
print "You have failed to run from this encounter."
cavecombat()
elif enemyattackroll >= 5 and playerrunroll >= 5:
enemydamageroll = random.randrange(1,10)
playerhealthpoints = playerhealthpoints - enemydamageroll
print "You have taken %s damage." %enemydamageroll
print "You have %s health remaining." %playerhealthpoints
print "You have succeeded in escaping the enemy."
elif enemyattackroll < 5 and playerrunroll >= 5:
print "Enemy has missed their attack this round."
print "You have succeeded in escaping the enemy."
elif enemyattackroll >= 5 and playerrunroll < 5:
enemydamageroll = random.randrange(1,10)
playerhealthpoints = playerhealthpoints - enemydamageroll
print "You have taken %s damage." %enemydamageroll
print "You have %s health remaining." %playerhealthpoints
print "You have failed to run from this encounter."
cavecombat()
else:
print "Learn to type, you suck."
cavecombat()
cavecombat()
combat()
这是我最近一次尝试让健康得到坚持,任何建议都值得赞赏。
答案 0 :(得分:0)
您需要return
健康。我是python的新手,所以最好检查一下。函数内部的IMO函数很奇怪。尝试将该函数分开,并使用return
保留该值。
只是为了说明我的意思。
以下return
pl
值:
def cavecombat():
combatroundinput = input("The round has begun. Do you Block, Dodge, or Run?").lower()
if combatroundinput == "block":
enemyattackroll = random.randint(1, 10)
if enemyattackroll < 5:
print("Enemy has missed their attack this round.")
pl = 0
return pl
elif enemyattackroll >= 5:
enemydamageroll = random.randint(1, 10) / 2
pl = enemydamageroll
print("You have taken %s damage." % enemydamageroll)
return pl
现在可以完成以下工作:
damage = cavecombat()
它将执行该函数,然后将pl
的值置于损坏状态。
所以如果从上面打印说“你已经受到3次伤害。 损害的价值是3。