Contact Us Today ! | canadasteamsyouth@gmail.com

Canada STEAMS Youth Association

It takes a village to raise a child.

Python Unit 3

Class 3: Drawing with Turtle

A turtle in Python is like a turtle in the real world. In Python, a turtle is a small, black arrow that moves slowly around the screen.  The turtle is a nice way to learn some of the basics of computer graphics.

  • Using Python’s Turtle Module

A module in Python is a way of providing useful code to be used by another program. Python has a special module called turtle that we can use to draw pictures on a screen. Importing a module tells Python that you want to use it.

 

>>> import turtle

  • Creating a Canvas

>>> t = turtle.Pen()

  • Moving a Turtle

>>> t.forward(50)

we’ll tell the turtle to turn left 90 degrees

>>> t.left(90)

The direction you’re facing is 0 degrees.

If you hold out your left arm, that’s 90 degrees left.

If you hold out your right arm, that’s 90 degrees right.

 

To erase the canvas, enter reset.

>>> t.reset()

 

We can also turn our turtle right or move it backward. We can use up to lift the pen off the page (in other words, tell the turtle to stop drawing), and down to start drawing.

>>> t.reset()

>>> t.backward(100)

>>> t.up()

>>> t.right(90)

>>> t.forward(20)

>>> t.left(90)

>>> t.down()

>>> t.forward(100)

 

 

Programming Puzzles

#1: A Rectangle

Create a new canvas using the turtle module’s Pen function and

then draw a rectangle.

#2: A Triangle

Create another canvas, and this time, draw a triangle. Look back

at the diagram of the circle with the degrees (“Moving the Turtle”

on page 46) to remind yourself which direction to turn the turtle

using degrees.

#3: A Box Without Corners

Write a program to draw the four lines shown here (the size isn’t

important, just the shape):

 

#1: A Rectangle

>>> import turtle

>>> t = turtle.Pen()

>>> t.forward(100)

>>> t.left(90)

>>> t.forward(50)

>>> t.left(90)

>>> t.forward(100)

>>> t.left(90)

>>> t.forward(50)

#2: A Triangle

>>> import turtle

>>> t = turtle.Pen()

>>> t.forward(100)

>>> t.left(120)

>>> t.forward(100)

>>> t.left(120)

>>> t.forward(100)

#3: A Box Without Corners

import turtle

t = turtle.Pen()

t.forward(50)

t.left(45)

t.up()

t.forward(50)

t.down()

t.left(45)

t.forward(50)

t.left(45)

t.up()

t.forward(50)

t.down()

t.left(45)

t.forward(50)

t.left(45)

t.up()

t.forward(50)

t.down()

t.left(45)

t.forward(50)

t.left(45)

t.up()

t.forward(50)

t.down()

t.left(45)

SEE ALL Add a note
YOU
Add your Comment
 

Canada STEAMS Youth Association

加拿大青少年英才协会

All rights reserved.

X