How to make random colors in Python – Turtle?
The turtle is a built-in module from the Python library. The turtle module is used to draw interesting shapes or drawings. We can use the turtle module by calling import turtle. The random module is used to generate random numbers.
Methods Used
- randint(0,255): It is used to generate numbers between 0 and 255.
- speed(0): It is used to set speed to display the drawing on board.
- colormode(255): It should be set to 255 to generate a color number till 255.
- begin_fill(): It begins to fill the circle with color.
- end_fill(): It ends to fill the circle with color.
- penup(): It will stop drawing on the board.
- pendown(): Turtle operates with pendown() state by default. To go back to the past drawing state on board.
- circle(radius): It is used to generate a circle of a particular radius.
All the above methods will be called inside an infinite loop in order to illustrate a randomly generated coloured circles of the same radius.
Below is the implementation.
Python3
from turtle import *
from random import randint
speed( 0 )
pensize( 10 )
colormode( 255 )
while True :
color(randint( 0 , 255 ),
randint( 0 , 255 ),
randint( 0 , 255 ))
begin_fill()
circle( 20 )
end_fill()
penup()
goto(randint( - 500 , 500 ), randint( - 300 , 270 ))
pendown()
|
Output
Random color
Last Updated :
30 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...