如何在Python2.7中创建基于回合的游戏循环
我正在用Python做一个游戏,现在我需要做一个游戏循环,在这个循环中,随机选择一个人先走,然后轮到他们 到目前为止,我的代码是: 当运行状况>0和运行状况1>0时:如何在Python2.7中创建基于回合的游戏循环,python,Python,我正在用Python做一个游戏,现在我需要做一个游戏循环,在这个循环中,随机选择一个人先走,然后轮到他们 到目前为止,我的代码是: 当运行状况>0和运行状况1>0时: if turn == 1: while loopx == False: try: move = raw_input("Do you want to attack or gegenerate health?
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
打印“游戏结束!”
我使用的是Python2.7。我看到您的代码存在一个明显的问题,那就是您经常执行错误的命令。您使用的是
=
运算符,而不是=
运算符。前者检查是否相等,而不是执行赋值
以下是已修复此问题的代码(有关修复位置,请参阅注释):
你已经尝试过什么?当你输入/编辑你的评论或问题时,如果你选择你的代码并按下按钮,它会为你格式化代码{turn=r.randint(1,2)win=False print'',而win==False:if turn==1:print“你先去!”其他:打印“A.I正在开始。”}代码在评论中发布不好,建议您编辑您的问题并将其放在那里。好的,您的代码有什么问题吗?谢谢!这似乎不相关,但您知道如何编译python脚本吗?
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!"