Introduction: Easy Designs - Turtle Graphics Python

Look at the beauty of the pictures. It is things like these that got me into turtle. To be honest, I was an MS Logo fan at the age of 7, so when I read up on a similar thing called 'Turtle', I was naturally skeptical. Not any more.

Generally, the elite programmers brush off turtle as 'primitive', but I beg to differ.

In this Instructable, I am going to write a very simple guide for turtle, in Python.

Another great use for turtle is teaching kids basic programming. The young ones are fascinated by visually appealing designs rather than text.

Let's begin.

EDIT: For some reason, the code has html in it, like <p>, <br>, when I upload it. If that is the case, there is a notepad file with both codes. Download it. I hope the issue is fixed :(

Step 1: Python

For this tutorial I will be using Python 2.7. 1.) Python can be downloaded for free here. ( I used this particular version, but it is entire up to you what YOU would like.) Even if you do not know Python, you can hang around, but do try to learn the language. It is, in my opinion a very simple yet powerful language.

Turtle is a built in module, and can be imported, so we do not need to import it.

Simple put, a module is an easily callable collection of programming function, i.e. commands for the computer to follow a certain set of steps to get results.

Step 2: The First Design : Code & Explanation

# What comes after the # is not seen by python. These are comments. :)

from turtle import * # imports the module turtle,
#* stands for all, which makes things easier

speed(0) # sets the speed of drawing to 0, which is the fastest
pencolor('white') # sets the color of the pen/lines to white
bgcolor('black') # sets the color of the background/canvas to black

x = 0 # creates a variable x with value 0
up() # lifts up the pen, so no lines are drawn

#note fd() means move forward, bk() means move back
# rt() or lt() means tilt right by a certain angle

rt(45)
fd(90)
rt(135)

down() # sets down the pen, so that turtle can draw
while x < 120: # while the value of x is lesser than 120,
#continuously do this:
fd(200)
rt(61)
fd(200)
rt(61)
fd(200)
rt(61)
fd(200)
rt(61)
fd(200)
rt(61)
fd(200)
rt(61)

rt(11.1111)
x = x+1 # adds 1 to the value of x,
# so that it is closer to 120 after every loop

exitonclick() # When you click, turtle exits.

#That's all! Try customizing the script! 8)

The code is self-explanatory speed(), pencolor(), fd(), rt(), lt(), etc. are all functions. Basically they are instructions for Python to follow. The result will be beauty.

Rather than keep it like this, try to use simple logic to create more beautiful designs.

After saving by pressing Ctrl + S, execute by pressing F5. The result in the form of a video.

Step 3: Second Design : Cool Spiral

What do I need to say? The Beauty of the code is that it generates a unique combination of colors each time it runs.

The code is again well commented and self-explanatory. There is also a video.

from turtle import *

from random import randint # from the random module import the function randint
#like turtle it is a module, read ahead for use

speed(0)

bgcolor('black')

x = 1

while x < 400:

r = randint(0,255) #makes variables r,g,b whose value is an integer,
g = randint(0,255) # which is between 0 and 255. It is random, and
b = randint(0,255) # changes every time the loop runs

colormode(255) # this is something that is irrelevant at this point
# check the pythondocs link at the end for more info


pencolor(r,g,b) # changes the color of the pen to the rgb coordinates
# obtained by the variables r, g, b changing each time

fd(50 + x)
rt(90.911)


x = x+1

exitonclick()

#again, try to customize :)

Step 4: Conclusion

As proud as you may be at this point, you have barely scratched the surface. People make things as complex as fractal trees with python.

If turtle is your thing, check out turtle and learn more at PythonDocs, where it is well documented. There are many more aspects such as fills, and circles. Experiment and have fun.

I hope I have inspired someone, and they continue to learn python. It is, in my opinion the simplest, yet most powerful programming language.

To further impress you, I leave the code for a rainbow triangle. This one is a customized version of a StackOverflow question:

import turtle
turtle.setup(width=600, height=500)
turtle.reset()
turtle.hideturtle()
turtle.speed(0)

turtle.bgcolor('black')

c = 0
x = 0

colors = [
#reddish colors
(1.00, 0.00, 0.00),(1.00, 0.03, 0.00),(1.00, 0.05, 0.00),(1.00, 0.07, 0.00),(1.00, 0.10, 0.00),(1.00, 0.12, 0.00),(1.00, 0.15, 0.00),(1.00, 0.17, 0.00),(1.00, 0.20, 0.00),(1.00, 0.23, 0.00),(1.00, 0.25, 0.00),(1.00, 0.28, 0.00),(1.00, 0.30, 0.00),(1.00, 0.33, 0.00),(1.00, 0.35, 0.00),(1.00, 0.38, 0.00),(1.00, 0.40, 0.00),(1.00, 0.42, 0.00),(1.00, 0.45, 0.00),(1.00, 0.47, 0.00),
#orangey colors
(1.00, 0.50, 0.00),(1.00, 0.53, 0.00),(1.00, 0.55, 0.00),(1.00, 0.57, 0.00),(1.00, 0.60, 0.00),(1.00, 0.62, 0.00),(1.00, 0.65, 0.00),(1.00, 0.68, 0.00),(1.00, 0.70, 0.00),(1.00, 0.72, 0.00),(1.00, 0.75, 0.00),(1.00, 0.78, 0.00),(1.00, 0.80, 0.00),(1.00, 0.82, 0.00),(1.00, 0.85, 0.00),(1.00, 0.88, 0.00),(1.00, 0.90, 0.00),(1.00, 0.93, 0.00),(1.00, 0.95, 0.00),(1.00, 0.97, 0.00),
#yellowy colors
(1.00, 1.00, 0.00),(0.95, 1.00, 0.00),(0.90, 1.00, 0.00),(0.85, 1.00, 0.00),(0.80, 1.00, 0.00),(0.75, 1.00, 0.00),(0.70, 1.00, 0.00),(0.65, 1.00, 0.00),(0.60, 1.00, 0.00),(0.55, 1.00, 0.00),(0.50, 1.00, 0.00),(0.45, 1.00, 0.00),(0.40, 1.00, 0.00),(0.35, 1.00, 0.00),(0.30, 1.00, 0.00),(0.25, 1.00, 0.00),(0.20, 1.00, 0.00),(0.15, 1.00, 0.00),(0.10, 1.00, 0.00),(0.05, 1.00, 0.00),
#greenish colors
(0.00, 1.00, 0.00),(0.00, 0.95, 0.05),(0.00, 0.90, 0.10),(0.00, 0.85, 0.15),(0.00, 0.80, 0.20),(0.00, 0.75, 0.25),(0.00, 0.70, 0.30),(0.00, 0.65, 0.35),(0.00, 0.60, 0.40),(0.00, 0.55, 0.45),(0.00, 0.50, 0.50),(0.00, 0.45, 0.55),(0.00, 0.40, 0.60),(0.00, 0.35, 0.65),(0.00, 0.30, 0.70),(0.00, 0.25, 0.75),(0.00, 0.20, 0.80),(0.00, 0.15, 0.85),(0.00, 0.10, 0.90),(0.00, 0.05, 0.95),
#blueish colors
(0.00, 0.00, 1.00),(0.05, 0.00, 1.00),(0.10, 0.00, 1.00),(0.15, 0.00, 1.00),(0.20, 0.00, 1.00),(0.25, 0.00, 1.00),(0.30, 0.00, 1.00),(0.35, 0.00, 1.00),(0.40, 0.00, 1.00),(0.45, 0.00, 1.00),(0.50, 0.00, 1.00),(0.55, 0.00, 1.00),(0.60, 0.00, 1.00),(0.65, 0.00, 1.00),(0.70, 0.00, 1.00),(0.75, 0.00, 1.00),(0.80, 0.00, 1.00),(0.85, 0.00, 1.00),(0.90, 0.00, 1.00),(0.95, 0.00, 1.00)
]

while x < 1000:
idx = int(c)
color = colors[idx]
turtle.color(color)
turtle.forward(x)
turtle.right(98)
x = x + 1
c = c + 0.1

turtle.exitonclick()

As an assignment, try to make a Sierpinski Triangle. It takes a lot of thought.

Also, please vote for this instructable if you liked it!!!

Comments

author
adithyashok (author)2015-05-08

Wow, my first featured 'ible.
Thank you so much guys!!!
^^ 8)

author
Maker Saga (author)2017-03-18

Thanks! How do you embed your code like that inside the tutorial?

author
JustinT26 (author)2016-11-06

This is my code for a Sierpinski triangle. I added a bit of colour.

import turtle

def drawTriangle(points,color,myTurtle):

myTurtle.fillcolor(color)

myTurtle.up()

myTurtle.goto(points[0][0],points[0][1])

myTurtle.down()

myTurtle.begin_fill()

myTurtle.goto(points[1][0],points[1][1])

myTurtle.goto(points[2][0],points[2][1])

myTurtle.goto(points[0][0],points[0][1])

myTurtle.end_fill()

def getMid(p1,p2):

return ( (p1[0]+p2[0]) / 2, (p1[1] + p2[1]) / 2)

def sierpinski(points,degree,myTurtle):

colormap = ['blue','red','green','white','yellow',

'violet','orange']

drawTriangle(points,colormap[degree],myTurtle)

if degree > 0:

sierpinski([points[0],

getMid(points[0], points[1]),

getMid(points[0], points[2])],

degree-1, myTurtle)

sierpinski([points[1],

getMid(points[0], points[1]),

getMid(points[1], points[2])],

degree-1, myTurtle)

sierpinski([points[2],

getMid(points[2], points[1]),

getMid(points[0], points[2])],

degree-1, myTurtle)

def main():

myTurtle = turtle.Turtle()

myWin = turtle.Screen()

myPoints = [[-100,-50],[0,100],[100,-50]]

sierpinski(myPoints,3,myTurtle)

myWin.exitonclick()

main()

author
maxnonym (author)2016-09-26

Hi, thank you, just great!!!

author
ShiroD1 (author)2016-05-25

couldn't you just use import colorsys in stead of making variables for r, g, b

ex.

#random shapes

import turtle

import colorsys

import random

turtle.bgcolor("black")

turtle.speed(0)

x=random.randint(1, 999999999)

y=random.randint(1, 999999999)

for i in range(1001):

color = colorsys.hsv_to_rgb(i/1000, 1.0, 1.0)

turtle.color(color)

turtle.forward(i)

turtle.right(x)

turtle.left(y)

author
ShiroD1 (author)ShiroD12016-05-25

i almost forgot this code should make a different shape everytime

author
ShiroD1 (author)ShiroD12016-05-25

for step 3 and 4

author
unkerjay (author)2016-01-25

I was trying to figure out what to do with this:

Blender spin duplicates with the array modifier

I stumbled across the video FIRST, THEN your 'ible.

I like dabbling with Python as well. Not to mention BASH scripting
and the terminal command line (Linux). So, this interested me.

In playing with it, and looking at it. I considered 3d application.

Save it, reduce it to black and white, pull it into Inkscape, trace the
bitmap, upload it to svg2stl.com and see what happens.

Turned out they're just a little TOO detailed for the conversion to
happen. Tried Blender as well. No go.

Tried simplifying the code, but, because of the way they're done (at
least some, as I can tell) it's a continuous line so reducing the iterations
makes for a larger object, fewer lines but an obvious design effect.

Creating polygons: octagons, triangles, squares and playing with them
as demonstrated in the embedded video accomplishes comparable effects, more usable for my purposes.

Still, I enjoy playing with this.

Thanx

I've attached examples of what worked (and what didn't)

(The Hexagons: Changed value from 120 to 121)

(Line Spin: Changed value from 1000 to 300)

hexa_spin_PNG.pngline_spin_val_300_PNG.png
author
adithyashok (author)unkerjay2016-01-29

Great work, mate! Hope your interest keeps going on. :)

author
unkerjay (author)unkerjay2016-01-25

Correction:

Changed value from 120 to "21"

author
pandalover123 (author)2015-06-21

Btw very awesome name Ins Trucktibles XD

hqdefault.jpegb6e126b8b0d5f2568d70a22b85c78fc8.jpeg
author
pandalover123 (author)2015-06-21

Very helpful I like drawing designs please make more im gonna show u two drawings I made ones a dragon ones a wolf

hqdefault.jpegb6e126b8b0d5f2568d70a22b85c78fc8.jpeg
author
timmg08 (author)2015-05-09

this is actually very awesome. I've been playing with python for a while and didn't know it had this capability. I want to combine this with the midi lib I found once. super cool thanks.

author
adithyashok (author)timmg082015-05-09

Thanks! And please post an instructable! Python-lovers are a rare species around here. :D

About This Instructable

78,431views

113favorites

License:

Bio: Simplicity is the ultimate sophistication.
More by adithyashok:Arduino Random Music GeneratorSpinning Earth - Beginners Unity Project Easy Designs - Turtle Graphics Python
Add instructable to: