You are on page 1of 3

# "A black and red triangle"

import turtle
turtle.pen(fillcolor="black", pencolor="red", pensize=10)
turtle.fill(True)
for _ in range(3):
  turtle.forward(100)
  turtle.left(120)
turtle.fill(False)
 
# "A spiral out of squares"
import turtle
size=1
while (True):
  turtle.forward(size)
  turtle.right(91)
  size = size + 1
 
#"A nautilus shape"
import turtle
size=1
while (True):
    for _ in range(4):
          turtle.forward(size)
          turtle.right(90)
          size=size + 1
    turtle.right(10)
 
#"A yellow star pattern"
import turtle
turtle.pen(pencolor='red', fillcolor='yellow')
turtle.fill(True)
while True:
    turtle.forward(200)
    turtle.left(170)
    if abs(turtle.pos()) < 1:
        break
turtle.fill(False)
turtle.done()
 
#"Recursive Star"
import turtle
def star(turtle, n,r):
 """ draw a star of n rays of length d"""
 for k in range(0,n):
    turtle.pendown()
    turtle.forward(r)
    turtle.penup()
    turtle.backward(r)
    turtle.left(360/n)
 
def recursive_star(turtle, n, r, depth, f):
 """At each point of the star, draw another smaller star,
 and so on, up to given depth. Rescale the stars by a factor f
 at each generation."""
 
 if depth == 0:
    star(turtle, n, f*4)
 else:
    for k in range(0,n):
        turtle.pendown()
        turtle.forward(r)
        recursive_star(turtle, n, f*r, depth - 1,f)
        turtle.penup()
        turtle.backward(r)
        turtle.left(360/n)
 
fred = turtle.Turtle()
fred.speed("fastest")
recursive_star(fred, 5 , 150, 4, 0.4)
 
import turtle
import random
fred = turtle.Turtle()
 
fred.speed(10)
 
colors=['lavender', 'pink', 'blue', 'midnight blue', 'lime']
 
def flower(x,y,r,g,b):
fred.setposition(x,y)
fred.pendown()
fred.color('green')
fred.setheading(270)
fred.fd(200)
 
fred.setposition(x,y)
 
fred.color(random.choice(colors))
for i in range(12):
fred.circle(20)
fred.left(30)
 
 
for i in range(100):
fred.penup()
flower(random.randint(-200,200), random.randint(-200,0),random.randint(0,255),
random.randint(0,255),random.randint(0,255))

 Turtle Geometry the Python Way

1. 1. Turtle Geometry the Python Way @SteveBattle


2. 2. Turtle Geometry “The tradition of calling our display creatures ‘turtles’started with Grey Walter, a
neurophysiologist who experimented in Britain… with tiny robot creatures he called ‘tortoises’. These inspired
the first turtles designed at MIT.” (1980)
3. 3. Turtles and Tortoises “Not even Dr. Grey Walter, their creator, can say what these mechanical tortoises are
going to do next.” Bristol based Inventor of the first autonomous robots in 1948.
4. 4. BBC Buggy (1983) The Buggy was tethered to the BBC Micro and used the ‘Logo’ language.
5. 5. The Python Way Idle, use tabs not spaces Start IDLE and start typing
6. 6. angles The turtle starts off facing this way A right angle turning left A 360˚ turn returns the turtle to 0˚
7. 7. sequence Create a new file (Python module) Run Save the file (but not as ‘turtle’)
8. 8. turtle functions forward(length) backward(length) left(angle) right(angle) penup() pendown() done() speed(s)
e.g. ‘slow’, ‘fast’, ‘fastest’ shape(name) e.g. ‘turtle’, ‘classic’ goto(x,y) x,y coordinates
9. 9. for loops Everything inside the loop is indented. Use TAB not SPACE The loop repeats a fixed number of times
(4). slow down the output
10. 10. defining functions Function definition Call the function Note the double indentation ‘turtle’ shape
11. 11. function parameters Add parameter ‘length’ Call ‘square’ with argument 100
12. 12. polygons Comma separated parameters calculate angle and assign to a variable The output from ‘print’
appears in the IDLE console The angles of a polygon sum to 360˚
13. 13. Interior & Exterior angles The (interior) angles of a triangle sum to only 180˚ The turtle turns through the
exterior angles, which sum to 360˚
14. 14. stars What happens if we turn turtle through a multiple of 360˚? Try also heptagons: star(100,7,2)
star(100,7,4) Double the angle of a normal pentagon
15. 15. while loops Try a non-integer multiple: spiral(400, 3, 1.01, 10) Exit the ‘while’ loop when the condition is
false. ‘goto’ these coordinates.
16. 16. recursion Recursive functions call themselves. The angle is close to 0 on the ‘straights’ We can also get spiral
motion by increasing the angle
17. 17. The Snowflake Curve Every side has smaller sides and so on, ad infinitum.
18. 18. conditionals test if we’ve bottomed out.

You might also like