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=…


This content originally appeared on DEV Community and was authored by Rohith Gilla

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

.ltag__user__id__317018 .follow-action-button { background-color: #129cf3 !important; color: #ffffff !important; border-color: #129cf3 !important; }
gillarohith image

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

Thanks


This content originally appeared on DEV Community and was authored by Rohith Gilla


Print Share Comment Cite Upload Translate Updates
APA

Rohith Gilla | Sciencx (2021-08-09T14:30:38+00:00) Create such amazing art in python with just 11 lines of code. Retrieved from https://www.scien.cx/2021/08/09/create-such-amazing-art-in-python-with-just-11-lines-of-code/

MLA
" » Create such amazing art in python with just 11 lines of code." Rohith Gilla | Sciencx - Monday August 9, 2021, https://www.scien.cx/2021/08/09/create-such-amazing-art-in-python-with-just-11-lines-of-code/
HARVARD
Rohith Gilla | Sciencx Monday August 9, 2021 » Create such amazing art in python with just 11 lines of code., viewed ,<https://www.scien.cx/2021/08/09/create-such-amazing-art-in-python-with-just-11-lines-of-code/>
VANCOUVER
Rohith Gilla | Sciencx - » Create such amazing art in python with just 11 lines of code. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/09/create-such-amazing-art-in-python-with-just-11-lines-of-code/
CHICAGO
" » Create such amazing art in python with just 11 lines of code." Rohith Gilla | Sciencx - Accessed . https://www.scien.cx/2021/08/09/create-such-amazing-art-in-python-with-just-11-lines-of-code/
IEEE
" » Create such amazing art in python with just 11 lines of code." Rohith Gilla | Sciencx [Online]. Available: https://www.scien.cx/2021/08/09/create-such-amazing-art-in-python-with-just-11-lines-of-code/. [Accessed: ]
rf:citation
» Create such amazing art in python with just 11 lines of code | Rohith Gilla | Sciencx | https://www.scien.cx/2021/08/09/create-such-amazing-art-in-python-with-just-11-lines-of-code/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.