我正在尝试制作一个简单的游戏来练习我在课堂上学到的东西。但是我无法修复正在执行我不希望执行的操作的while
循环。
如果player_one_range_counter
与位置(spot_1
,spot_2
,spot_3
等之一)相同,则循环应为False
并且不可访问。如果player_two_range_counter
与这些斑点之一相同,则应该发生相同的事情。
如果两个点都相同,则表示各自的循环变为False
,游戏结束。问题在于,即使它们都与游戏继续进行的任何一个地点都相同。不应该。
有人能启发我做错了什么吗?
continue_game = True
while continue_game:
player_one_play = True
player_two_play = True
while player_one_play:
print(user_1_name)
x = steps_to_move() # returns int from steps_to_move()
player_one_range_counter += x # updates the range for palyer one
user_1_turtle.fd(x) # turtle move x amount of pixels forward
if player_one_range_counter == spot_1 or player_one_range_counter == spot_2 or player_one_range_counter == spot_3 or player_one_range_counter == spot_4 or player_one_range_counter == spot_5:
print("\n" + user_1_name, "stepped on a mine! \n")
player_one_play = False
else:
break
while player_two_play:
print(user_2_name)
y = steps_to_move()
player_two_range_counter += y
user_2_turtle.fd(y)
if player_two_range_counter == spot_1 or player_two_range_counter == spot_2 or player_two_range_counter == spot_3 or player_two_range_counter == spot_4 or player_two_range_counter == spot_5:
print("\n" + user_2_name, "stepped on a mine! \n")
player_two_play = False
else:
break
if player_one_play == False and player_two_play == False:
continue_game = False
答案 0 :(得分:1)
我相信主要的问题是这是while
循环中的 :
player_one_play = True
player_two_play = True
应该在while
循环之前之前。我如何处理您的代码逻辑:
spots = [spot_1, spot_2, spot_3, spot_4, spot_5]
player_one_playing = True
player_two_playing = True
while True:
if player_one_playing:
print(user_1_name)
steps = steps_to_move() # returns int from steps_to_move()
user_1_turtle.fd(steps) # turtle move x amount of pixels forward
player_one_range_counter += steps # updates the range for player one
if player_one_range_counter in spots:
print("\n" + user_1_name, "stepped on a mine!\n")
player_one_playing = False
if player_two_playing:
print(user_2_name)
steps = steps_to_move()
user_2_turtle.fd(steps)
player_two_range_counter += steps
if player_two_range_counter in spots:
print("\n" + user_2_name, "stepped on a mine! \n")
player_two_playing = False
if not (player_one_playing or player_two_playing):
break
原始代码中的内部while
循环实际上是if
语句,因此我在我的代码中将其做成这样。