【发布时间】:2020-10-19 20:34:39
【问题描述】:
我正在制作一个有五轮的两人骰子游戏,但我想保持前几轮的分数相同,我该怎么做?
游戏规则如下:
• 每位玩家掷骰子的点数会加到他们的分数中。 • 如果总分是偶数,则在他们的分数上增加 10 分。 • 如果总分是奇数,则从他们的分数中减去 5 分。 • 如果他们掷出双倍骰子,他们可以多掷一个骰子,并将掷出的点数加到 他们的分数。 • 球员的得分在任何时候都不能低于0。 • 5 轮结束时得分最高的人获胜。 • 如果两个玩家在 5 轮结束时得分相同,则他们每人掷 1 颗骰子,然后 得分最高的人获胜(如此重复,直到有人获胜)。
import random
import time
total_player2score = 0
total_player1score = 0
rounds = 0
player_1 = 0
player_2 = 0
while rounds != 5:
total_player2score = total_player2score + player_2
total_player1score = total_player1score + player_1
rounds = rounds + 1
number = random.randint(1, 6)
number2 = random.randint(1, 6)
total_player1score = number + number2
print("*"*50)
print("Round {}!".format(rounds))
print("*"*50)
print("Player 1's turn. Type 'roll' to roll the dice.")
player1_input = input(">>> ")
if player1_input == "roll":
time.sleep(0.5)
print("Player 1's first roll is", number)
print("Player 1's second roll.Type 'roll' to roll the dice")
player1_input = input(">>> ")
if player1_input == "roll":
time.sleep(0.5)
print("player 1's second roll is", number2)
if total_player1score % 2 == 0:
total_player1score = total_player1score + 10
print("Player 1's total is even so 10 points will be added")
print("*"*50)
print("Player 1 has", total_player1score, "points")
else:
total_player1score = total_player1score - 5
total_player1score = max(0, total_player2score)
print("player 1's total is odd so 5 points will be deducted")
print("*"*50)
print("Player 1 has", total_player1score, "points")
number = random.randint(1, 6)
number2 = random.randint(1, 6)
total_player2score = number + number2
print("*"*50)
print("Player 2's turn. Type 'roll' to roll the dice")
player2_input = input(">>> ")
if player2_input == "roll":
time.sleep(0.5)
print("Player 2's first roll is", number)
print("Player 2's second roll.Type 'roll' to roll the dice")
player2_input = input(">>> ")
if player2_input == "roll":
time.sleep(0.5)
print("player 2's second roll is", number2)
if total_player2score % 2 == 0:
total_player2score = total_player2score + 10
print("Player 2's total is even so 10 points will be added")
print("*"*50)
print("Player 2 has", total_player2score, "points")
else:
total_player2score = total_player2score - 5
total_player2score = max(0, total_player2score)
print("player 2's total is odd so 5 points will be deducted")
print("*"*50)
print("Player 2 has", total_player2score, "points")
【问题讨论】:
-
欢迎来到 Stack Overflow。如果您将代码减少到minimal reproducible example,您的问题可能会得到改善。目前很难确定您的问题适用于代码中的哪个位置。
-
我不知道在哪里实际改进我的代码,这样我就可以存储前几轮的值,所以我把所有东西都放进去,这样查看这个问题的人就可以知道我的错误在哪里,也知道重点的程序。
-
您可以将分数存储在一个 .txt 文件中,如下面的@herbaltea 建议的那样,或者您可以为每个玩家创建一个字典,并将每个键设置为回合,将值设置为分数。
标签: python loops while-loop project