如何修复此代码以使乌龟成为随机颜色? 我希望能够点击然后乌龟转动然后改变颜色。
import turtle
import random
turtle.colormode(255)
R = 0
G = 0
B = 0
def color(x, y):
turtle.color((R, G, B))
def turn(x, y):
turtle.left (10)
for i in range(10000):
turtle.onscreenclick(turn)
turtle.forward(1)
turtle.onrelease(color)
R = random.randrange(0, 257, 10)
B = random.randrange(0, 257, 10)
G = random.randrange(0, 257, 10)
def color(x, y):
turtle.color((R, G, B))
答案 0 :(得分:2)
我希望能够点击然后乌龟转动然后改变颜色。
我相信这就是你所描述的:
import turtle
import random
def change_color():
R = random.random()
B = random.random()
G = random.random()
turtle.color(R, G, B)
def turn_and_change_color(x, y):
turtle.left(10)
change_color()
turtle.onscreenclick(turn_and_change_color)
def move_forward():
turtle.forward(1)
turtle.ontimer(move_forward, 25)
move_forward()
turtle.mainloop()
而不是range(10000)
循环,它使用计时器来保持乌龟移动,这也允许事件循环正常运行。它应该一直运行,直到你关闭龟窗口:
答案 1 :(得分:0)
import turtle, random
def color(x, y):
R = random.randrange(0,257,10)
G = random.randrange(0,257,10)
B = random.randrange(0,257,10)
turtle.color(R,G,B)
turtle.left(10)
turtle.colormode(255)
for i in range(10000):
turtle.onscreenclick(color)
turtle.forward(1)