//
you're reading...
Uncategorized

Python Turtle Cheat Sheet and Geometric Shapes

Python Turtle Cheat Sheet

  • turtle.pen(pencolor=”red”, pensize=10, fillcolor=”black”): Pens have attributes such as color, size, and fillcolor.
  • turtle.up(): Sets the pen state to be up (not drawing).  turtle.down(): Sets the pen state to be down (drawing).
  • turtle.right(degrees): Turns the direction that the turtle is facing right (clockwise) by the amount
  • indicated (in degrees). turtle.left(degrees): Turns the direction that the turtle is facing left (counter clockwise) by the amount indicated (in degrees).
  • turtle.forward(distance): Moves the turtle forward (in the direction the turtle is facing) the
    distance indicated (in pixels). Draws a line if the pen is down, not if the pen is up. turtle.backward(distance): Moves the turtle backward (in the direction opposite to how the turtle is facing) the distance indicated (in pixels).
  • turtle.setheading(angle): Sets the orientation of the turtle to angle. Here are some common
    directions in degrees: 0 (east), 90 (north), 180 (west), 270 (south).
  • turtle.goto(x,y): Moves the turtle to the specified coordinates, drawing a straight line to the
    destination (x,y) if the pen is down, and not drawing if the pen is up.
  • turtle.circle(radius): Draws a circle of the indicated radius. The turtle draws the circle tangent to the direction the turtle is facing.
  • turtle.fill(True): To fill a figure, use turtle.fill(True) before you start drawing the figure.  The figure drawn will be filled with the present color setting.
  • turtle.hideturtle(), turtle.showturtle(): Sets the state to hide / show the turtle. When shown, you see it as a small arrowhead pointed in the direction of the heading.
  • turtle.xcor(), turtle.ycor(): Returns the x-coordinate / y-coordinate of the current pen position.
  • turtle.bye(): Close the turtle drawing window.

Screen Shot 2015-05-05 at 10.05.06 PMScreen Shot 2015-05-05 at 9.24.20 AMScreen Shot 2015-05-04 at 2.32.35 PMScreen Shot 2015-05-05 at 9.16.45 AMScreen Shot 2015-05-05 at 10.24.07 PM

# "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)
Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: