我正在 python 编写游戏代码,但卡住了
I'm coding a game in python but ı stuck
骰子游戏规则:如果骰子为1,则打开另一位玩家“电脑”
如果不是,我现在可以再次投掷以获得积分。骰子会给予多少。它增加了我的分数,但如果有 1 个,我不能接受这些分数。我应该在 1 来之前停下来。
我说是,(这就够了):我赚了分,然后轮到电脑了。 İf 1 来了,我没有积分,转牌也是在电脑上。
对于赢,我们中的一个人必须达到 100 点。(99 不是赢,但最后的骰子是 6 点,那将是 105- 仍然是赢)
有人可以帮我在游戏中添加计算机吗?不知道怎么办。
from random import randint
run = True
point = 0
while run:
inp = input("throw dice: ")
if inp == "y":
dice = randint(1, 7)
if dice != 1:
point = point + dice
print(dice.__str__() + " came")
print("your point is: " + point.__str__())
else:
#1e gelince
run = False
print("1 came, you cant continue. Your point was: " + point.__str__())
else:
#say false to end game
run = False
print("last point: " + point.__str__())
您应该添加与计算机相关的逻辑。您可以像这样更改算法:
from random import randint
run = True
computerTurn = False
point = 0
computerPoint = 0
while run:
if computerTurn:
dice = randint(1, 7)
if dice != 1:
computerPoint += dice
print(dice.__str__() + " came")
print("computer point is: " + computerPoint.__str__())
else:
print("computer point is: " + computerPoint.__str__())
print("1 came, your turn!!!")
computerTurn = False
else:
inp = input("throw dice: ")
if inp == "y":
dice = randint(1, 7)
if dice != 1:
point = point + dice
print(dice.__str__() + " came")
print("your point is: " + point.__str__())
else:
print("your point is: " + point.__str__())
print("1 came, computer turn!!!")
computerTurn = True
else:
#say false to end game
run = False
print("last point: " + point.__str__())
您的算法运行良好,您可以在计算机回合中重复使用玩家回合的大量逻辑。
唯一需要为计算机以不同方式实现的是某种逻辑,可以帮助它决定何时结束自己的回合,从而使游戏变得有趣而不是纯粹与运气有关。
例如,在下面的算法中,计算机将掷骰子直到达到 >= 20 的转折点分数,然后玩家开始转弯。
为计算机掷骰子添加轻微的延迟也是一个好主意,这样您就可以直观地看到它的回合进度,而不是它立即在屏幕上闪烁,这可以通过 time.sleep() 来自时间库的函数。
from random import randint
import time
run = True
playerturn = True
turnpoints = 0
playerpoints = 0
computerpoints = 0
while run:
if playerturn:
inp = input("do you want to throw dice?: (y or n) ")
if inp == "y":
dice = randint(1, 7)
if dice != 1:
turnpoints = turnpoints + dice
print(dice.__str__() + " came")
print("your points for this turn are: " + turnpoints.__str__())
if (playerpoints + turnpoints) >= 100:
playerpoints = playerpoints + turnpoints
print("You won the game with a point score of: " + playerpoints.__str__())
break
else:
print("1 came, you cant continue. Your earned no points this turn. Current Points: " + playerpoints.__str__())
playerturn = False
turnpoints = 0
else:
playerpoints = playerpoints + turnpoints
print("You finished the turn. You turn is now over. Current Points: " + playerpoints.__str__())
playerturn = False
turnpoints = 0
else: #computer's turn
dice = randint(1, 7)
if dice != 1:
turnpoints = turnpoints + dice
print(dice.__str__() + " came")
print("computer points for this turn are: " + turnpoints.__str__())
time.sleep(1)
if (computerpoints + turnpoints) >= 100:
computerpoints = computerpoints + turnpoints
print("Computer won the game with a point score of: " + computerpoints.__str__())
break
if turnpoints > 20:
computerpoints = computerpoints + turnpoints
print("Computers turn is now over. Current Points: " + computerpoints.__str__())
playerturn = True
turnpoints = 0
else:
playerturn = True
print("1 came, Computer's turn is over. Computer earned no points this turn. Current Points: " + computerpoints.__str__())
turnpoints = 0
骰子游戏规则:如果骰子为1,则打开另一位玩家“电脑” 如果不是,我现在可以再次投掷以获得积分。骰子会给予多少。它增加了我的分数,但如果有 1 个,我不能接受这些分数。我应该在 1 来之前停下来。 我说是,(这就够了):我赚了分,然后轮到电脑了。 İf 1 来了,我没有积分,转牌也是在电脑上。 对于赢,我们中的一个人必须达到 100 点。(99 不是赢,但最后的骰子是 6 点,那将是 105- 仍然是赢)
有人可以帮我在游戏中添加计算机吗?不知道怎么办。
from random import randint
run = True
point = 0
while run:
inp = input("throw dice: ")
if inp == "y":
dice = randint(1, 7)
if dice != 1:
point = point + dice
print(dice.__str__() + " came")
print("your point is: " + point.__str__())
else:
#1e gelince
run = False
print("1 came, you cant continue. Your point was: " + point.__str__())
else:
#say false to end game
run = False
print("last point: " + point.__str__())
您应该添加与计算机相关的逻辑。您可以像这样更改算法:
from random import randint
run = True
computerTurn = False
point = 0
computerPoint = 0
while run:
if computerTurn:
dice = randint(1, 7)
if dice != 1:
computerPoint += dice
print(dice.__str__() + " came")
print("computer point is: " + computerPoint.__str__())
else:
print("computer point is: " + computerPoint.__str__())
print("1 came, your turn!!!")
computerTurn = False
else:
inp = input("throw dice: ")
if inp == "y":
dice = randint(1, 7)
if dice != 1:
point = point + dice
print(dice.__str__() + " came")
print("your point is: " + point.__str__())
else:
print("your point is: " + point.__str__())
print("1 came, computer turn!!!")
computerTurn = True
else:
#say false to end game
run = False
print("last point: " + point.__str__())
您的算法运行良好,您可以在计算机回合中重复使用玩家回合的大量逻辑。
唯一需要为计算机以不同方式实现的是某种逻辑,可以帮助它决定何时结束自己的回合,从而使游戏变得有趣而不是纯粹与运气有关。
例如,在下面的算法中,计算机将掷骰子直到达到 >= 20 的转折点分数,然后玩家开始转弯。
为计算机掷骰子添加轻微的延迟也是一个好主意,这样您就可以直观地看到它的回合进度,而不是它立即在屏幕上闪烁,这可以通过 time.sleep() 来自时间库的函数。
from random import randint
import time
run = True
playerturn = True
turnpoints = 0
playerpoints = 0
computerpoints = 0
while run:
if playerturn:
inp = input("do you want to throw dice?: (y or n) ")
if inp == "y":
dice = randint(1, 7)
if dice != 1:
turnpoints = turnpoints + dice
print(dice.__str__() + " came")
print("your points for this turn are: " + turnpoints.__str__())
if (playerpoints + turnpoints) >= 100:
playerpoints = playerpoints + turnpoints
print("You won the game with a point score of: " + playerpoints.__str__())
break
else:
print("1 came, you cant continue. Your earned no points this turn. Current Points: " + playerpoints.__str__())
playerturn = False
turnpoints = 0
else:
playerpoints = playerpoints + turnpoints
print("You finished the turn. You turn is now over. Current Points: " + playerpoints.__str__())
playerturn = False
turnpoints = 0
else: #computer's turn
dice = randint(1, 7)
if dice != 1:
turnpoints = turnpoints + dice
print(dice.__str__() + " came")
print("computer points for this turn are: " + turnpoints.__str__())
time.sleep(1)
if (computerpoints + turnpoints) >= 100:
computerpoints = computerpoints + turnpoints
print("Computer won the game with a point score of: " + computerpoints.__str__())
break
if turnpoints > 20:
computerpoints = computerpoints + turnpoints
print("Computers turn is now over. Current Points: " + computerpoints.__str__())
playerturn = True
turnpoints = 0
else:
playerturn = True
print("1 came, Computer's turn is over. Computer earned no points this turn. Current Points: " + computerpoints.__str__())
turnpoints = 0