[英]How can I choose a random turtle to move a random amount of steps?
我正在做一場烏龜比賽,其中 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 = ["red","blue","green","yellow","purple"] 也沒有用。 有沒有辦法隨機選擇海龜? 或者那是不可能的?
這是我沒有那部分的代碼:
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()
簡單的答案是:可以使用 random 模塊的random.choice(list_of_turtles)
函數。 它從項目列表中選擇一個隨機項目,並與random.randint(1,10)
一起用於隨機選擇要移動的步驟和海龜
在腳本中將最后的代碼更改為:
#finish line
finishLine = 140
dict_of_turtles = {
red : ( arrowR , "red" , "rood" ),
blue : ( arrowB , "blue" , "blauw" ),
green : ( arrowG , "green" , "groen" ),
yellow : ( arrowY , "yellow", "geel" ),
purple : ( arrowP , "purple", "paars" ),
}
list_of_turtles = list( dict_of_turtles.keys() )
def Race():
while True:
random_turtle = random.choice(list_of_turtles)
other_turtles = list_of_turtles[:] ; other_turtles.remove(random_turtle)
arrow_turtle, turtle_color, turtle_color_text = dict_of_turtles[random_turtle]
x = random.randint(1,10)
random_turtle.forward(x)
if all( random_turtle.xcor() > other.xcor() for other in other_turtles):
arrow_turtle.showturtle()
else:
arrow_turtle.hideturtle()
if random_turtle.pos()[0] >= finishLine:
winner2.color(turtle_color)
winner2.write(turtle_color_text,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()
最后享受比賽和更短的腳本,因為不需要在每個循環中移動所有海龜。
為了完整起見,下面要運行的原始代碼的重新設計的完整版本專注於使代碼更緊湊和“Pythonic”,同時提供隨機選擇海龜移動的請求特性。
import turtle
import random
import time
screen = turtle.Screen()
colors_EN = ['red', 'blue', 'green','yellow', 'purple']
colors_NL = ["rood", "blauw", "groen", "geel", "paars" ]
len_list = len(colors_EN)
turtles_racing = [ turtle.Turtle() for t in range(len_list)]
arrow_turtles = [ turtle.Turtle() for t in range(len_list)]
lines_and_texts = [ turtle.Turtle() for t in range(4)]
lines_and_texts[1].color("black")
fontLines = ("Arial", 16, "normal")
#turtle colors
for indx, color in enumerate(colors_EN):
turtles_racing[indx].color(color); arrow_turtles[indx].color(color)
#turtle penup and speed
for turtleObjectItem in turtles_racing+arrow_turtles+lines_and_texts:
turtleObjectItem.penup()
turtleObjectItem.speed(0)
#turtle shapes
for turtleObjectItem in turtles_racing: turtleObjectItem.shape("turtle")
for turtleObjectItem in arrow_turtles: turtleObjectItem.shape("arrow")
#hide turtles
for turtleObjectItem in arrow_turtles+lines_and_texts:
turtleObjectItem.hideturtle()
# arrow positions
arrow_turtles[0].goto(-190, 70) # R
arrow_turtles[1].goto(-190, 35) # B
arrow_turtles[2].goto(-190, 0) # G
arrow_turtles[3].goto(-190,-35) # Y
arrow_turtles[4].goto(-190,-70) # P
#start turtles
xBegin = -180
def prepStart():
turtles_racing[0].goto(-180, 70)
turtles_racing[1].goto(-180, 35)
turtles_racing[2].goto(-180, 0)
turtles_racing[3].goto(-180,-35)
turtles_racing[4].goto(-180,-70)
for turtleObjectItem in arrow_turtles:
turtleObjectItem.hideturtle()
prepStart()
#race lanes
def line(x,y,width,text):
lines_and_texts[0].penup()
lines_and_texts[0].goto(x,y)
for i in range (15):
lines_and_texts[0].write(text, font=fontLines)
lines_and_texts[0].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()
#numbers
lines_and_texts[0].goto(-150,90)
lines_and_texts[0].write("0 1 2 3 4 5 6 7 8 9 10 11 12 13 14")
#winner text
lines_and_texts[1].goto(xBegin,-180)
lines_and_texts[1].write("De winnaar is: " , font=("Arial",30,"normal"))
lines_and_texts[2].goto(100,-230) # -180)
lines_and_texts[3].goto(xBegin,150)
lines_and_texts[3].color("#abcdef")
lines_and_texts[3].write(" Quit racing event with 'q'", font=("Arial",15,"bold"))
finishLine = 140
# QUIT by pressing 'q' on the keyboard
screen.quit = False
def quit():
screen.quit = True if not screen.quit else False
screen.onkey(quit, "q")
def eventloop():
while True:
i = index_random_turtle = random.choice([0, 1, 2, 3, 4]) # or randint(0,4)
random_turtle = turtles_racing[i]
other_turtles = turtles_racing[:] ; other_turtles.pop(i)
arrow_turtle = arrow_turtles[i]
turtle_color = colors_EN[i]
x = random.randint(1,10)
random_turtle.forward(x)
if all( random_turtle.xcor() > other.xcor() for other in other_turtles):
arrow_turtle.showturtle()
else:
arrow_turtle.hideturtle()
if random_turtle.pos()[0] >= finishLine:
lines_and_texts[2].color(turtle_color)
lines_and_texts[2].write(turtle_color,font=("Arial",30,"bold"))
break
time.sleep(3)
lines_and_texts[2].clear()
prepStart()
if not screen.quit:
screen.ontimer(eventloop, 10) # 10ms (0.01s) (1000ms/10ms = 100 FPS)
else:
screen.bye()
#:def eventloop()
eventloop()
screen.listen()
turtle.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.