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

Rohith Gilla @gillarohith

About: I code to keep up my sanity, code in multiple languages and frameworks. These are my favs Python | Flutter | TypeScript | JavaScript | React | Expo

Location:
India
Joined:
Jan 14, 2020

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

Publish Date: Aug 9 '21
36 1

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

Main image

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


Enter fullscreen mode Exit fullscreen mode


import turtle
import random


Enter fullscreen mode Exit fullscreen mode

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

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

Thanks

Comments 1 total

    Add comment