我正在开发一款关于Python的游戏,现在我需要制作一个游戏循环,其中选择一个随机的人先行,然后轮到他们执行。
到目前为止我的代码是:
健康> 0和health1> 0:
if turn == 1:
while loopx == False:
try:
move = raw_input("Do you want to attack or gegenerate health? Press 1 to ATTACK and 2 to REGEN. ")
print ""
move = int(move)
if move == 1:
health1 = health1 - damage
print "You attacked!"
loopx == True
elif move == 2:
health = health+regen
print "You regenerated health!"
loopx = True
else:
print "Invalid number, try again"
continue
except:
print "Invalid number, try again"
continue
turn == 2
if turn == 2:
AImove = r.randint(1,2)
if AImove == 1:
print "AI attacked!"
health = health - damage1
else:
print "AI regenerated!"
health1 = health1+regen1
turn == 1
continue
打印"游戏结束!"
我正在使用Python 2.7。
答案 0 :(得分:0)
我在你的代码中看到的一个明显的问题是,你经常做错了。您使用的是==
运算符,而不是=
运算符。前者检查是否相等,而不是进行分配。
以下是修复此问题的代码(请参阅修复位置的注释):
while health > 0 and health1 > 0:
if turn == 1:
while loopx == False:
try:
move = raw_input("Do you want to attack or gegenerate health? Press 1 to ATTACK and 2 to REGEN. ")
print ""
move = int(move)
if move == 1:
health1 = health1 - damage
print "You attacked!"
loopx = True # fix1
elif move == 2:
health = health+regen
print "You regenerated health!"
loopx = True
else:
print "Invalid number, try again"
continue
except:
print "Invalid number, try again"
continue
turn = 2 # fix 2
if turn == 2:
AImove = r.randint(1,2)
if AImove == 1:
print "AI attacked!"
health = health - damage1
else:
print "AI regenerated!"
health1 = health1+regen1
turn = 1 # fix 3
continue
print "game over!"