【发布时间】:2017-01-31 20:06:53
【问题描述】:
总的来说,我是编码新手,我选择 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()
这是我最近一次坚持健康的尝试,欢迎提出任何建议。
【问题讨论】:
-
有很多代码需要我们弄清楚!尝试将问题简化为几行并将其发布...以及到底出了什么问题。
-
在函数外声明变量。然后使用 global 从函数内部引用它。也就是说 - 我不建议使用全局变量。
标签: python python-2.7 global-variables