Python Turtle : Create cool shapes graphics with Turtle on python
Introduction :
Hello today we are going to learn how to create cool shapes with Turtle on python let's get started ..
What's Turtle on python ?
"Turtle" is a gui python module and a feature like a drawing board, which lets you execute commands to control turtle to draw all over it.
Installing Python Turtle :
$ pip3 install turtle
or
$ pip install turtle
$ pip3 install tkinteror
$ pip install turtle
and if you are linux user maybe you should install tkinter by:
$ sudo apt-get install python3-tk
Start coding
Initialize our file
As usual you have to create new python file etc.
Now we import needed modules :
import turtle
Creating new pen :
i will name it 't'
t=turtle.Pen()
Preparing colors :
now we are going to create a list of colors to use. and we are going change the color of background. you can use any color but it should be on Turtle colors :
colors = ['red', 'purple', 'blue', 'green']### you can change the colors or add more colors
turtle.bgcolor('black')### black is now the backround Color
Loop to generate the shape :
All i am going to do is do a for loop to draw 500 line. and i am going to control these lines.
this is the step to draw each line :
- Choose a color from the list
- Extend width of line
- Forward
- Turn to left by an angle of 59 degree /you can change the angle
Code :
for x in range(500):
t.pencolor(colors[x%numberOfcolors]) #choose a color from the list
t.width(x/100 + 1) #change width of line
t.forward(x) #draw line
t.left(59) #turn to left by an angle of 49 degree /you can change the angle
and that's it
Full code :
import turtle
t=turtle.Pen()
colors = ['red', 'purple', 'blue', 'green']### you can change the colors or add more colors
turtle.bgcolor('black')### black is now the backround Color
numberOfcolors=len(colors) # number of colors
for x in range(500):
t.pencolor(colors[x%numberOfcolors]) #choose a color from the list
t.width(x/100 + 1) #change width of line
t.forward(x) #draw line
t.left(59) #turn to left by an angle of 49 degree /you can change the angle
turtle.done()
OUTPUT :
TAGS :
python turtle example,what's python turtle,cool graphics with python turtle,python gui turtle