对于我的课,我的任务是编程一个海龟函数,这个函数是用户通过箭头键控制的。这个函数显示自己,然后是一个带有点的镜子。每个点应该是完全随机的颜色。到目前为止,我有这个:from turtle import *
from random import randrange
FRAMES_PER_SECOND = 10
mirrorTurtle = Turtle()
def turnRight():
global turtle
global mirrorTurtle
turtle.right(45)
def turnLeft():
global turtle
global mirrorTurtle
turtle.left(45)
def randomColor(turtle):
r = randrange(256) # red component of color
g = randrange(256) # green component
b = randrange(256) # blue component
def move():
colormode(255)
global turtle
global mirrorTurtle
global moving
if moving:
for i in range(1):
turtle.penup()
mirrorTurtle.penup()
turtle.forward(40)
turtle.dot(20, "red")
mirrorTurtle.dot(10, "blue")
turtle.forward(1)
mirrorTurtle.setpos(-turtle.xcor(), -turtle.ycor())
ontimer(move, 1000 // FRAMES_PER_SECOND)
def start():
global moving
moving = True
move()
def stop():
global moving
moving = False
def main():
colormode(255)
global turtle
global mirrorTurtle
turtle = Turtle()
turtle.hideturtle()
mirrorTurtle.hideturtle()
onkey(turnRight, "Right")
onkey(turnLeft, "Left")
onkey(start, "Up")
onkey(stop, "Down")
listen()
if __name__ == "__main__":
main()
对我来说,问题是,我的“红”和“蓝”应该是随机的颜色(仍然起到镜像的作用(即,如果输入的第一个点是蓝色,那么镜子也应该是蓝色的)。在