Skip to content

Instantly share code, notes, and snippets.

@jscalo
Created February 25, 2015 21:57
Show Gist options
  • Save jscalo/f31f70ce58bedc17c521 to your computer and use it in GitHub Desktop.
Save jscalo/f31f70ce58bedc17c521 to your computer and use it in GitHub Desktop.
Basic drawing with Python's turtle module
import turtle
import random
# input: takes a side and a length as integers
# processing: draws a figure based on the inputs
# output: nothing, dones't return any value
def polygon(sides, length):
for x in range(sides):
turtle.forward(length)
turtle.right(360/sides)
turtle.title("My Turtle")
turtle.setup(1000, 1000, 0, 0)
# start off with a square of 50 pixels
size = 200
# start off at -150, 150
xpos = -150
ypos = 150
# start off with a very red color
green = 1.0
# draw as fast as possible! (the default is 10)
turtle.speed(0)
# for "instant" drawing, uncomment:
#turtle.tracer(0)
for x in range(10):
# move to our desired location
turtle.penup()
turtle.goto(xpos, ypos)
turtle.pendown()
# draw our polygon
turtle.pencolor(0,green,0)
turtle.fillcolor(0, green, 0)
turtle.begin_fill()
polygon(4, size)
turtle.end_fill()
# change pos
xpos += 10
ypos -= 10
# decrease green
green -= 0.1
# decrease the size
size -= 20
# for "instant" drawing, uncomment:
#turtle.update()
turtle.exitonclick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment