Turtle Graphics – more features

Ok

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 ")
fname="dial.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):
	turtle.fill(True)
	turtle.left(5)
	for n in range(0,sides):
		turtle.forward(x)
		turtle.left(shapeangle) 
		turtle.fill(1)
#hide turtle once drawing complete
turtle.hideturtle()
#pause
time.sleep(1)
 
#grab screen
ts = turtle.getscreen()
#save canvas to filename specified earlier
ts.getcanvas().postscript(file=fname)
 
#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."
 
turtle.exitonclick()

The output for a 4 sided shape looks like this.

loop42

 

 

 

 

 

 

 

 

#fname= raw_input("Filename (must include .eps ")
fname="dial.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 “)
    fname=”dial.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 *