Home > Software engineering >  How can I get a random color mode to fill in my turtle?
How can I get a random color mode to fill in my turtle?

Time:10-04

EDIT: I have changed the code, but I don't know what I am still doing wrong. I need it to run for a color defined as Rgb.

def draw_border(turtle):

#top border
turtle.up()
turtle.goto(-330,380)
turtle.down()

r = random.randint(0,255)
g = random.randint(0,255)
b = random.randint(0,255)

rgb =(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
turtle.color("black","black")
turtle.colormode(255)
turtle.begin_fill()

for i in range(14):
    turtle.forward(50.7)
    turtle.right(135)
    turtle.forward(math.sqrt(5140.98))
    turtle.right(135)
    turtle.forward(101.4)
    turtle.right(90)
    
turtle.end_fill()

CodePudding user response:

import turtle import random

colors=("red","blue","green")

turtle.color(random.choice(colors))

turtle.begin_fill()

turtle.forward(90)

turtle.left(120)

turtle.forward(90)

turtle.left(120)

turtle.forward(90)

turtle.left(120)

turtle.end_fill()

CodePudding user response:

You can create a random rgb by using rgb = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)). To have turtle use an rgb color, you have to set turtle.colormode(255), and pass rgb to turtle.color(). Here is the code:

def draw_border(turtle):
    
    #top border
    turtle.up()
    turtle.goto(-330,380)
    turtle.down()

    r = random.randint(0,255)
    g = random.randint(0,255)
    b = random.randint(0,255)

    rgb = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    turtle.colormode(255)
    turtle.color(rgb)
    turtle.begin_fill()

    for i in range(14):
        turtle.forward(50.7)
        turtle.right(135)
        turtle.forward(math.sqrt(5140.98))
        turtle.right(135)
        turtle.forward(101.4)
        turtle.right(90)
        
    turtle.end_fill()
  • Related