Shape Module
Deprecated Lab — Trinket is shutting down
This lab was written for Trinket.io, which is shutting down in August 2026. The embedded trinket.io links on this page will stop working after that date.
These pages are kept for reference only. The current version of this course now runs every lab as an inline Skulpt editor right in the page — no account or install needed. Start at Chapter 1: Welcome to Python.
Shape Module
In this lab we will create a set of drawing function and put them together into a new file. We will then import this file into our main.py file.
Example code to import the module in main.py
| import turtle
from shape import *
dan = turtle.Turtle()
dan.shape('turtle')
draw_triangle(dan, 'red', 5, 20, 30)
draw_circle(dan, 'orange', 10, 0, 30)
draw_square(dan, 'orange', 15, -20, 30)
|
Sample Codeimited t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 | # This is a custom module we've made.
# Modules are files full of code that you can import into your programs.
# This one teaches our turtle to draw various shapes.
import turtle
def draw_circle(turtle, color, size, x, y):
turtle.penup()
turtle.color(color)
turtle.fillcolor(color)
turtle.goto(x,y)
turtle.pendown()
turtle.begin_fill()
turtle.circle(size)
turtle.end_fill()
def draw_triangle(turtle, color, size, x, y):
turtle.penup()
turtle.color(color)
turtle.fillcolor(color)
turtle.goto(x,y)
turtle.pendown()
turtle.begin_fill()
for i in range (3):
turtle.forward(size*3)
turtle.left(120)
turtle.end_fill()
turtle.setheading(0)
def draw_square(turtle, color, size, x, y):
turtle.penup()
turtle.color(color)
turtle.fillcolor(color)
turtle.goto(x,y)
turtle.pendown()
turtle.begin_fill()
for i in range (4):
turtle.forward(size*2)
turtle.left(90)
turtle.end_fill()
turtle.setheading(0)
def draw_star(turtle, color, size, x, y):
turtle.penup()
turtle.color(color)
turtle.fillcolor(color)
turtle.goto(x,y)
turtle.pendown()
turtle.begin_fill()
turtle.right(144)
for i in range(5):
turtle.forward(size*2)
turtle.right(144)
turtle.forward(size*2)
turtle.end_fill()
turtle.setheading(0)
|
Sample Program
Sample
Experiments
Can you add a new shape called "flower"?