简体   繁体   English

在python中使用turtle函数时,如何生成随机点颜色?

[英]When using the turtle function in python, how can I generate a random dot color?

For a my class, I am tasked with programming a turtle function that is user controlled via the arrow keys. 对于我的班级,我的任务是编写一个乌龟函数,该函数由用户通过箭头键控制。 The function displays itself and then a mirror of itself with dots. 该函数先显示自身,然后显示其自身的点镜。 Each dot should be a completely randomized color. 每个点应为完全随机的颜色。 So far, I have this: 到目前为止,我有这个:

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()

the issue for me is, where I have "red" and "blue" are supposed to be random colors (that still act as a mirror (ie; if the first dot typed is blue, the mirror should be blue as well). 对我来说,问题是,我的“红色”和“蓝色”应该是随机的颜色(仍然充当镜子(即,如果键入的第一个点是蓝色,则镜子也应该是蓝色))。

If you search for a function that gives you a random color string, see this: 如果搜索提供随机颜色字符串的函数,请参见以下内容:

def randcolor():
    return "#"+"".join([random.choice("0123456789ABCDEF") for i in range(6)]

You are very close to a solution. 您非常接近解决方案。 You need small changes to randomColor and to move . 您需要对randomColormove进行randomColor小的更改。

In randomColor , you have successfully created three random numbers. randomColor ,您已经成功创建了三个随机数。 Now you just need to create a tuple and return it, like so: 现在,您只需要创建一个tuple并返回它,就像这样:

def randomColor():
    r = randrange(256)    # red component of color
    g = randrange(256)    # green component
    b = randrange(256)    # blue component
    return r,g,b

Then, you need to use that random color in your call to dot() : 然后,您需要在对dot()调用中使用该随机颜色:

        mirrorTurtle.dot(10, randomColor())

Additionally, to get your program to work in my environment, I need to add a call to update() at the end of move() , and a call to mainloop() at the end of main() . 另外,为了使您的程序在我的环境中工作,我需要在move()末尾添加对update()的调用,并在main()末尾添加对mainloop()的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM