DEV Community

Rohith Gilla
Rohith Gilla

Posted on

26 1

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

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay