//
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.
```# "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.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))
```