Create such amazing art in python with just 11 lines of code

This might have been an normal image for most of you, but this image has been generated with python using turtle

Code

import turtle
import random
turtle.bgcolor('black')
turtle.colormode(255)
turtle.speed(0)
for x in range(500): r,b,g=random.randint(0,255),random.randint(0,255),random.randint(0,255)
    turtle.pencolor(r,g,b)
    turtle.fd(x+50)
    turtle.rt(91)
turtle.exitonclick()
import turtle
import random

We need the turtle module to create graphics and random module to generate random colours needed for the lines.

turtle.bgcolor('black')
We now say turtle to use black colour canvas.

We now iterate over 500 times, this can be any arbitrary number on how many lines you want in the diagram.

r,b,g=random.randint(0,255),random.randint(0,255),random.randint(0,255)
This one liner give r,g,b with random values between 0 and 255.

turtle.pencolor(r,g,b)
We now set the turtle pen color, which means the colour of the line.

turtle.fd(x+50)
Here we move forward by x+50 which means initially we move forward by 50 units then by 51 units then 52 so on till 50+499 units.

turtle.rt(91)
After each movement we turn right about 91 degrees.

turtle.exitonclick()
This will ensure the canvas doesn't close automatically until you click it.

If you like to see this in replit check it out below.

Follow me for short and long posts

gillarohith image

My recent posts include deploying server less application in python, deploying full stack scalable application using Blitz JS.

Thanks

17