所以我有以下代码:
def monsterturn():
global monster
global playerhp
turn = True
print "Rolling for", monster, "...(14 or better to hit)"
time.sleep(1)
if r > 14:
print r, "- The", monster, "hit you!"
playerhp-=1
time.sleep(0.5)
print "Your HP:", playerhp
if playerhp == 0:
turn = False
time.sleep(0.5)
print "YOU HAVE BEEN SLAIN."
else:
turn = False
time.sleep(1.5)
playerturn()
elif r < 14:
print r, "- The", monster, "missed you."
turn = False
playerturn()
r = randrange(1, 21)
该函数playerturn()
的结构与 完全类似monsterturn()
,除了全局变量 monsterhp 代替 playerhp,r2 代替 r,以及玩家而不是怪物的对话。问题是,它计算一个随机整数一次,然后保留它,这通常会导致永远没有人打任何人。我怎样才能让它每次都计算一个不同的随机整数?
此外,该函数playerturn()
被调用,最后触发monsterturn()
,触发playerturn()
,依此类推,直到有人死。