Turtle Graphics – more features


Taking a simple loop in python turtle graphics I have come up with something that now draws a similar pattern but each time makes the shape a little bit bigger.

#!/usr/bin/env python
#more advanced nested loops program.
#this draws a shape starts small repeats getting bigger each time, 
#change the range x,72 to a bigger number
#note turtle.forward(x) is linked to the range or how many 
import time
import turtle
import math
#set file name
#fname= raw_input("Filename (must include .eps ")
sides = raw_input("number of sides to shape ")
sides = int(sides)
shapeangle = 360 / sides
print shapeangle
print turtle.pencolor()
print turtle.fillcolor()
print turtle.pensize()
turtle.pen(fillcolor="Red", pencolor="blue", pensize=1)
for x in range(25,150):
	for n in range(0,sides):
#hide turtle once drawing complete
#grab screen
ts = turtle.getscreen()
#save canvas to filename specified earlier
#tell user that file is saved to the specified filename
print "Saved image to: ", fname
#prompt user to click image to exit
print "All done. Click image to exit."

The output for a 4 sided shape looks like this.










#fname= raw_input("Filename (must include .eps ")

to specify a file name you need to uncomment the top line above and comment out the bottom line.  The way it is written above is good for testing, as it doesn’t keep asking you for a file name, just saves it as a default name.


One thought on “Turtle Graphics – more features

  1. Notes on the section
    #set file name
    #fname= raw_input(“Filename (must include .eps “)

    I have commented out a line that asks the user for a file name this saves time during testing, as a result the default name is simply dial.eps (set by the next line down) this can be changed, to allow users to set their own filename by changing which line is commented / uncommented.

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress Anti Spam by WP-SpamShield