【发布时间】:2021-06-28 20:38:51
【问题描述】:
我目前正在开发一款棋盘游戏。在对骰子进行编码时,我遇到了一些麻烦。出于某种原因,while 循环在每次运行后都会返回相同的随机整数,而不是一个新整数。
这是我已经走了多远:
def dice():
turtle.addshape("bull.gif")
player1 = turtle.Turtle()
player1.shape("bull.gif")
player1.penup()
player1.goto(-240, -260)
turtle.addshape("cow.gif")
player2 = turtle.Turtle()
player2.shape("cow.gif")
player2.penup()
player2.goto(-240, -220)
dice1 = random.randint(1, 6)
p1_list = ([-240, -260], [-120, -260], [0, -260], [120, -260], [240, -260], [240, -140], [120, -140], [0, -140], [-120, -140], [-240, -140], [-240, -20], [-120, -20], [0, -20], [120, -20], [240, -20], [240, 100], [120, 100], [0, 100], [-120, 100], [-240, 100], [-240, 220], [-120, 220], [0, 220], [120, 220], [240, 220])
p2_list = ([-240, -220], [-120, -220], [0, -220], [120, -220], [240, -220], [240, -100], [120, -100], [0, -100], [-120, -100], [-240, -100], [-240, 20], [-120, 20], [0, 20], [120, 20], [240, 20],[ 240, 140], [120, 140], [0, 140], [-120, 140], [-240, 140], [-240, 260], [-120, 260], [0, 260], [120, 260], [240, 260])
dieValue = dice1
P1_pos = player1.goto(p1_list[0])
pos = 0
w = True
while w == True:
player1_dice = input("\nBULL (P1): Please press 'ENTER' to roll the dice ")
if player1_dice == "":
print("BULL (P1) rolled a", dice1)
elif player1_dice != "":
print("BULL (P1): Please make sure to press 'ENTER' ")
for pos in range(dieValue) :
pos += 1
P1_pos = player1.goto(p1_list[pos])
pos = pos
dice()
【问题讨论】:
-
dice1 = random.randint(1, 6)这只会在循环之前被调用一次。将其移至循环内部。
标签: python for-loop if-statement random while-loop