如何选择随机的乌龟移动随机数量的步骤?
我正在做一场乌龟比赛,其中有5只不同的海龟互相竞赛,它们各自采取了一定的步骤。但是,我该如何选择一个随机的乌龟来移动随机数量的步骤?我找不到使程序选择乌龟的方法。我已经尝试制作一个颜色库,然后制作颜色海龟并选择随机颜色(Reddit上的某人建议这样做),但这只是在屏幕中间添加了一只乌龟,但并没有停止前进。所以我尝试了:
colours = ("red","blue","green","yellow","purple")
turtles = {colour: turtle.Turtle() for colour in colours}
ranTur = turtles[random.choice(colours)]
这正是Reddit上有人给我的东西,但这无效。我希望该程序选择我正在赛车的5只乌龟之一(我有更多的海龟,但它们是为了画车道和东西),然后使乌龟向前移动X空间。只是做rantur = [“红色”,“蓝色”,“绿色”,“黄色”,“紫色”]也不起作用。有没有办法选择随机海龟?还是不可能?
这是我的代码,而没有该部分:
import turtle
import random
import time
#turtles
red = turtle.Turtle()
blue = turtle.Turtle()
green = turtle.Turtle()
yellow = turtle.Turtle()
purple = turtle.Turtle()
lijn = turtle.Turtle()
winner1 = turtle.Turtle()
winner2 = turtle.Turtle()
arrowR = turtle.Turtle()
arrowB = turtle.Turtle()
arrowG = turtle.Turtle()
arrowY = turtle.Turtle()
arrowP = turtle.Turtle()
#font
fontLines = ("Arial", 16, "normal")
#turtle colors
red.color("red")
blue.color("blue")
green.color("green")
yellow.color("yellow")
purple.color("purple")
lijn.color("black")
winner1.color("black")
arrowR.color("red")
arrowB.color("blue")
arrowG.color("green")
arrowY.color("yellow")
arrowP.color("purple")
#turtle penup
red.penup()
blue.penup()
green.penup()
yellow.penup()
purple.penup()
winner1.penup()
winner2.penup()
arrowR.penup()
arrowB.penup()
arrowG.penup()
arrowY.penup()
arrowP.penup()
lijn.penup()
#turtle shapes
red.shape("turtle")
blue.shape("turtle")
green.shape("turtle")
yellow.shape("turtle")
purple.shape("turtle")
arrowR.shape("arrow")
arrowB.shape("arrow")
arrowG.shape("arrow")
arrowY.shape("arrow")
arrowP.shape("arrow")
#turtle speed
arrowR.speed(0)
arrowB.speed(0)
arrowG.speed(0)
arrowY.speed(0)
arrowP.speed(0)
red.speed(0)
blue.speed(0)
green.speed(0)
yellow.speed(0)
purple.speed(0)
winner1.speed(0)
winner2.speed(0)
lijn.speed(0)
#hide turtles
arrowR.hideturtle()
arrowB.hideturtle()
arrowG.hideturtle()
arrowY.hideturtle()
arrowP.hideturtle()
winner1.hideturtle()
winner2.hideturtle()
lijn.hideturtle()
#arrow positions
arrowR.goto(-190,70)
arrowB.goto(-190,35)
arrowG.goto(-190,0)
arrowY.goto(-190,-35)
arrowP.goto(-190,-70)
#start turtles
xBegin = -180
def raceTur():
red.goto(-180,70)
blue.goto(-180,35)
green.goto(-180,0)
yellow.goto(-180,-35)
purple.goto(-180,-70)
raceTur()
#race lanes
def line(x,y,width,text):
lijn.penup()
lijn.goto(x,y)
for i in range (15):
lijn.write(text, font=fontLines)
lijn.forward(width)
def raceBaan():
line(-150,60,20,"|")
line(-150,25,20,"|")
line(-150,-10,20,"|")
line(-150,-45,20,"|")
line(-150,-80,20,"|")
raceBaan()
# reset
def reset():
raceTur()
raceBaan()
#numbers
lijn.goto(-150,90)
lijn.write("0 1 2 3 4 5 6 7 8 9 10 11 12 13 14")
#winner text
winner1.goto(xBegin,-180)
winner1.write("De winnaar is: ",font=("Arial",30,"normal"))
winner2.goto(20,-180)
#finish line
finishLine = 140
#race
def Race():
while True:
x = random.randint(1,10)
red.forward(x)
if red.xcor() > blue.xcor() and red.xcor() > green.xcor() and red.xcor() > yellow.xcor() and red.xcor() > purple.xcor():
arrowR.showturtle()
else:
arrowR.hideturtle()
if red.pos()[0]>=finishLine:
winner2.color("red")
winner2.write("rood",font=("Arial",30,"bold"))
break
x = random.randint(1,10)
blue.forward(x)
if blue.xcor() > red.xcor() and blue.xcor() > green.xcor() and blue.xcor() > yellow.xcor() and blue.xcor() > purple.xcor():
arrowB.showturtle()
else:
arrowB.hideturtle()
if blue.pos()[0]>=finishLine:
winner2.color("blue")
winner2.write("blauw",font=("Arial",30,"bold"))
break
x = random.randint(1,10)
green.forward(x)
if green.xcor() > red.xcor() and green.xcor() > blue.xcor() and green.xcor() > yellow.xcor() and green.xcor() > purple.xcor():
arrowG.showturtle()
else:
arrowG.hideturtle()
if green.pos()[0]>=finishLine:
winner2.color("green")
winner2.write("groen",font=("Arial",30,"bold"))
break
x = random.randint(1,10)
yellow.forward(x)
if yellow.xcor() > red.xcor() and yellow.xcor() > green.xcor() and yellow.xcor() > blue.xcor() and yellow.xcor() > purple.xcor():
arrowY.showturtle()
else:
arrowY.hideturtle()
if yellow.pos()[0]>=finishLine:
winner2.color("yellow")
winner2.write("geel",font=("Arial",30,"bold"))
break
x = random.randint(1,10)
purple.forward(x)
if purple.xcor() > blue.xcor() and purple.xcor() > green.xcor() and purple.xcor() > yellow.xcor() and purple.xcor() > red.xcor():
arrowP.showturtle()
else:
arrowP.hideturtle()
if purple.pos()[0]>=finishLine:
winner2.color("purple")
winner2.write("paars",font=("Arial",30,"bold"))
break
time.sleep(3)
winner2.clear()
arrowR.hideturtle()
arrowB.hideturtle()
arrowG.hideturtle()
arrowY.hideturtle()
arrowP.hideturtle()
reset()
Race()
Race()
turtle.done()
Im making a turtle race where 5 different turtles are racing each other and they each take a random amount of steps. But how do I make it that it also chooses a random turtle to move a random number of steps? I can't find a way to make the program choose a turtle. I already tried to make a library of colors and then make the colors turtles and choose a random color (someone on reddit suggested this) but that just added a turtle in the middle of the screen that didn't stop going forward. so i tried:
colours = ("red","blue","green","yellow","purple")
turtles = {colour: turtle.Turtle() for colour in colours}
ranTur = turtles[random.choice(colours)]
This is exactly what someone on reddit gave me but this didn't work. I want the program to choose one of my 5 turtles that are racing (i have more turtles but they are for drawing the lanes and stuff) and then make that turtle move forward x spaces. And just doing ranTur = ["red","blue","green","yellow","purple"] didn't work either. Is there a way to choose random turtles? Or is that just not possible?
This is my code without that part:
import turtle
import random
import time
#turtles
red = turtle.Turtle()
blue = turtle.Turtle()
green = turtle.Turtle()
yellow = turtle.Turtle()
purple = turtle.Turtle()
lijn = turtle.Turtle()
winner1 = turtle.Turtle()
winner2 = turtle.Turtle()
arrowR = turtle.Turtle()
arrowB = turtle.Turtle()
arrowG = turtle.Turtle()
arrowY = turtle.Turtle()
arrowP = turtle.Turtle()
#font
fontLines = ("Arial", 16, "normal")
#turtle colors
red.color("red")
blue.color("blue")
green.color("green")
yellow.color("yellow")
purple.color("purple")
lijn.color("black")
winner1.color("black")
arrowR.color("red")
arrowB.color("blue")
arrowG.color("green")
arrowY.color("yellow")
arrowP.color("purple")
#turtle penup
red.penup()
blue.penup()
green.penup()
yellow.penup()
purple.penup()
winner1.penup()
winner2.penup()
arrowR.penup()
arrowB.penup()
arrowG.penup()
arrowY.penup()
arrowP.penup()
lijn.penup()
#turtle shapes
red.shape("turtle")
blue.shape("turtle")
green.shape("turtle")
yellow.shape("turtle")
purple.shape("turtle")
arrowR.shape("arrow")
arrowB.shape("arrow")
arrowG.shape("arrow")
arrowY.shape("arrow")
arrowP.shape("arrow")
#turtle speed
arrowR.speed(0)
arrowB.speed(0)
arrowG.speed(0)
arrowY.speed(0)
arrowP.speed(0)
red.speed(0)
blue.speed(0)
green.speed(0)
yellow.speed(0)
purple.speed(0)
winner1.speed(0)
winner2.speed(0)
lijn.speed(0)
#hide turtles
arrowR.hideturtle()
arrowB.hideturtle()
arrowG.hideturtle()
arrowY.hideturtle()
arrowP.hideturtle()
winner1.hideturtle()
winner2.hideturtle()
lijn.hideturtle()
#arrow positions
arrowR.goto(-190,70)
arrowB.goto(-190,35)
arrowG.goto(-190,0)
arrowY.goto(-190,-35)
arrowP.goto(-190,-70)
#start turtles
xBegin = -180
def raceTur():
red.goto(-180,70)
blue.goto(-180,35)
green.goto(-180,0)
yellow.goto(-180,-35)
purple.goto(-180,-70)
raceTur()
#race lanes
def line(x,y,width,text):
lijn.penup()
lijn.goto(x,y)
for i in range (15):
lijn.write(text, font=fontLines)
lijn.forward(width)
def raceBaan():
line(-150,60,20,"|")
line(-150,25,20,"|")
line(-150,-10,20,"|")
line(-150,-45,20,"|")
line(-150,-80,20,"|")
raceBaan()
# reset
def reset():
raceTur()
raceBaan()
#numbers
lijn.goto(-150,90)
lijn.write("0 1 2 3 4 5 6 7 8 9 10 11 12 13 14")
#winner text
winner1.goto(xBegin,-180)
winner1.write("De winnaar is: ",font=("Arial",30,"normal"))
winner2.goto(20,-180)
#finish line
finishLine = 140
#race
def Race():
while True:
x = random.randint(1,10)
red.forward(x)
if red.xcor() > blue.xcor() and red.xcor() > green.xcor() and red.xcor() > yellow.xcor() and red.xcor() > purple.xcor():
arrowR.showturtle()
else:
arrowR.hideturtle()
if red.pos()[0]>=finishLine:
winner2.color("red")
winner2.write("rood",font=("Arial",30,"bold"))
break
x = random.randint(1,10)
blue.forward(x)
if blue.xcor() > red.xcor() and blue.xcor() > green.xcor() and blue.xcor() > yellow.xcor() and blue.xcor() > purple.xcor():
arrowB.showturtle()
else:
arrowB.hideturtle()
if blue.pos()[0]>=finishLine:
winner2.color("blue")
winner2.write("blauw",font=("Arial",30,"bold"))
break
x = random.randint(1,10)
green.forward(x)
if green.xcor() > red.xcor() and green.xcor() > blue.xcor() and green.xcor() > yellow.xcor() and green.xcor() > purple.xcor():
arrowG.showturtle()
else:
arrowG.hideturtle()
if green.pos()[0]>=finishLine:
winner2.color("green")
winner2.write("groen",font=("Arial",30,"bold"))
break
x = random.randint(1,10)
yellow.forward(x)
if yellow.xcor() > red.xcor() and yellow.xcor() > green.xcor() and yellow.xcor() > blue.xcor() and yellow.xcor() > purple.xcor():
arrowY.showturtle()
else:
arrowY.hideturtle()
if yellow.pos()[0]>=finishLine:
winner2.color("yellow")
winner2.write("geel",font=("Arial",30,"bold"))
break
x = random.randint(1,10)
purple.forward(x)
if purple.xcor() > blue.xcor() and purple.xcor() > green.xcor() and purple.xcor() > yellow.xcor() and purple.xcor() > red.xcor():
arrowP.showturtle()
else:
arrowP.hideturtle()
if purple.pos()[0]>=finishLine:
winner2.color("purple")
winner2.write("paars",font=("Arial",30,"bold"))
break
time.sleep(3)
winner2.clear()
arrowR.hideturtle()
arrowB.hideturtle()
arrowG.hideturtle()
arrowY.hideturtle()
arrowP.hideturtle()
reset()
Race()
Race()
turtle.done()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有没有办法选择随机海龟?还是不可能?
简单的答案是:可以使用
random.choice(list_of_turtles)
随机模块的函数。它从项目列表中选择一个随机项目,并与random.randint(1,10)
一起使用。要移动在您的脚本中更改末尾的代码到:
End享受赛车和较短的脚本,因为无需在每个环内移动所有乌龟。
为了完整性,在重新设计的原始代码的完整版本下,专注于使代码更加紧凑和“ Pythonic”,并提供随机选择乌龟移动的要求的特征。
Is there a way to choose random turtles? Or is that just not possible?
The simple answer is: it is possible using the
random.choice(list_of_turtles)
function of the random module. It chooses a random item from a list of items and is used along withrandom.randint(1,10)
for a random choice of the step and the turtle to moveChange in your script the code at the end to:
end enjoy the racing and the much shorter script as there is no need to move within each loop all of the turtles.
For the sake of completeness below a reworked full version of the original code to run focused on making the code a bit more compact and 'Pythonic' along with providing the requested feature of random choice of turtle to move.