[英]How to create a while loop for a dice roll in python turtle?
我有兩只烏龜,游戲的目標是看哪只烏龜能先到家。 我必須使用擲骰子機制來確定這一點,但我不知道如何在使用 while 循環時添加一個。 一旦烏龜 1 滾動它應該是烏龜 2 的回合。 他們一直這樣做,直到其中一只海龜到達他們的家。 那是游戲結束的時候。
import turtle
import random
turtle.hideturtle()
turtle.penup()
turtle.setpos(300,80)
turtle.pendown()
turtle.speed(9)
turtle.circle(30)
turtle.hideturtle()
turtle.penup()
turtle.setpos(300,-125)
turtle.pendown()
turtle.speed(9)
turtle.circle(30)
p1 = turtle.Turtle()
p1.shape("turtle")
p2 = turtle.Turtle()
p2.shape("turtle")
def p1_start():
p1.speed(9)
p1.penup()
p1.goto(-200,100)
p1.pendown()
p1_start()
def p2_start():
p2.speed(9)
p2.penup()
p2.goto(-200,-100)
p2.pendown()
p2_start()
player1 = input("Player 1 enter name: ")
turtle.penup()
turtle.setpos(-207, 50)
turtle.write(player1, font=('Arial', 16, 'normal'))
turtle.hideturtle()
player2 = input("\nPlayer 2 enter name: ")
turtle.penup()
turtle.setpos(-207, -150)
turtle.write(player2, font=('Arial', 16, 'normal'))
turtle.hideturtle()
player1_color = raw_input("\nPlayer 1 enter color: ")
p1.color(player1_color)
player2_color = raw_input("\nPlayer 2 enter color: ")
p2.color(player2_color)
player1_dict = {"Name": player1, "Color": player1_color}
print("\nPLAYER 1 INFO: ")
print(player1_dict)
player2_dict = {"Name": player2, "Color": player2_color}
print("\nPLAYER 2 INFO: ")
print(player2_dict)
print('\n')
print('Player 1 is now rolling')
roll = int (random.randint (1,6))
if roll==1:
print('The number on the die is',roll)
elif roll== 2:
print('The number on the die is',roll)
elif roll == 3:
print('The number on the die is',roll)
elif roll == 4:
print('The number on the die is',roll)
elif roll == 5:
print('The number on the die is',roll)
else:
print('The number on the die is',roll)
p1.forward(roll*20)
我看到了很多問題:你在解決大問題之前添加了太多的細節——應該是相反的; 您創建了一個數據結構來代表您的玩家,但最終您並沒有真正使用它; 當你真的應該為未知(但合理)數量的玩家設計它以保持你的代碼誠實時,你為固定數量的玩家設計了這個。
讓我們將您的代碼拆開並重新組合以解決上述問題:
from turtle import Screen, Turtle
from random import randint
NUMBER_PLAYERS = 3
POLE = 300
FONT = ('Arial', 16, 'normal')
players = []
for player in range(1, NUMBER_PLAYERS + 1):
name = input("\nPlayer " + str(player) + " enter name: ")
color = input("Player " + str(player) + " enter color: ")
players.append({'Name': name, 'Color': color})
for number, player in enumerate(players, 1):
print("\nPlayer", number, "information:", player)
screen = Screen()
height = screen.window_height()
lane_height = height / (NUMBER_PLAYERS + 1) # divide the vertical space into lanes
marker = Turtle()
marker.hideturtle()
marker.speed('fastest')
for number, player in enumerate(players, 1):
y_coordinate = height/2 - lane_height * number
marker.penup()
marker.setpos(-POLE, y_coordinate + 30)
marker.write(player['Name'], align='center', font=FONT)
marker.setpos(POLE, y_coordinate - 30)
marker.pendown()
marker.circle(30)
tortoise = Turtle()
tortoise.hideturtle()
tortoise.shape('turtle')
tortoise.speed('slowest')
tortoise.penup()
tortoise.goto(-POLE, y_coordinate)
tortoise.pendown()
tortoise.color(player['Color'])
tortoise.showturtle()
player['Turtle'] = tortoise
while True:
for number, player in enumerate(players, 1):
y_coordinate = height/2 - lane_height * number
print()
print(player['Name'], 'is now rolling')
roll = randint(1, 6)
print('The number on the die is:', roll)
player['Turtle'].forward(roll * 5)
if player['Turtle'].distance(POLE, y_coordinate) < 30:
print()
print(player['Name'], "is the winner!")
break
else: # no break
continue
break # yes, this is a tricky loop -- take your time with it
screen.exitonclick()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.