我正在尝试用python编写一个蛇和梯子的游戏,而我让玩家沿着棋盘移动的功能似乎不起作用。
我需要让我的函数循环一次又一次,直到游戏赢了,但我不知道如何让它用new_pos
值代替current_pos
值重新启动函数。另外,当我尝试运行它时,它说current_pos
没有定义
理想情况下,我需要在整个游戏中在player_one
和player_two
之间切换的函数。有人建议使用我编写的函数player_turns
,但我不知道如何将其实际合并到代码中
import time
import random
def roll_dice():
dice_value = random.randint(1,6)
print("Its a..." + str(dice_value))
return dice_value
player_one = input("What is your name Player One?" + "\n")
player_two = input("What is your name Player Two?" + "\n")
print("\n" + str(player_one) + " Vs " + str(player_two) + "\n" + "Let's Play!")
def player_turns(total_turns):
if total_turns % 2 == 0:
total_turns += 1
return player_one
else:
return player_two
def move_player(player):
snake_squares = {26: 17, 40: 9, 43: 24, 67: 25, 75: 57, 82: 71, 98: 80}
ladder_squares = {10: 40, 16: 38, 37: 46, 41: 52, 50: 68, 53: 73, 87: 97}
print("Your turn, " + str(player))
input(str(player) + " , type 'roll' to roll the dice" + "\n")
obtained_dice = roll_dice()
new_pos = current_pos + obtained_dice
print(str(player) + ", move to space " + str(new_pos))
if new_pos < 100:
if new_pos in snake_squares:
print("Oh no! you have landed on a snake!" + "\n" + "You fall down the snake and land back on square {0}". format(snake_squares[new_pos]))
new_pos = snake_squares[new_pos]
return new_pos
elif new_pos in ladder_squares:
print("Hurray, you have landed on a ladder!" + "\n" + "Climb the ladder and move to square number {0}". format(ladder_squares[new_pos]))
new_pos = ladder_squares[new_pos]
return new_pos
elif new_pos >= 100:
print("Congratulations, " + str(player) + "!" + "\n" + "You win!")
print("Hope you enjoyed the game!")
您可以稍微重新构造代码:
总而言之,它可能是这样的:
输出:
相关问题 更多 >
编程相关推荐