COMPUTER SCIENCE
code 30424 a.y. 2018-19
Practice session 6
Introduction to Turtle module
SEDIN
IT Education Services Center
2
Objectives of the practice session
• Apply what we learned during the past lessons regarding functions, decision
making structures and iterative structures
• Draw and move in the space using Turtle Module
• Manage graphic elements
3
Import and run Turtle Module
4
Attributes of the turtle
A turtle object has three attributes:
• Location: specifies its position in the work space
• Heading: specifies the direction in which it is aiming
• Pen: specifies if the object must write on the work space when it moves, and
has also some attributes such as size, color etc.
5
Move the turtle object
Method Description
The turtle object moves forward by a given
.forward(distance)
number of pixels (distance)
The turtle object moves backward a given
.backward(distance)
number of pixels (distance)
The turtle object rotates counterclockwise by a
.left(degrees)
given angle in degrees
The turtle object rotates clockwise by a given
.right(degrees)
angle in degrees
.goto(x,y) The turtle object goes to the position (x,y)
6
Move the turtle object - 2
ninja.forward(100)
ninja.left(90)
ninja.forward(100)
7
Exercise 1
Draw a square (side = 100 pixel)
How we can improve the code using our
current competences?
8
Exercise 2
Write a function named square that draws a square.
Two arguments must be passed to the function:
• the name of the turtle object
• the side of the square
Then modify the code so that the length of the side of the square is asked to
the user each time the function is called
9
Exercise 3
Write a function named spiral that draws a squared spiral:
• two arguments must be passed to the function: the turtle object name and the
initial length of the spiral side
• set to 5 pixels the side length increment at each change of direction
• set a maximum side length of 300 pixels
10
Manage graphic elements
Method Description
.up() The turtle object does not writes when it moves
.down() The turtle object writes when it moves (default)
.pencolor(color) Sets the color * of the pen
.width(size) Sets the width of the pen
.fillcolor(color) Sets the color that the turtle object uses to fill the shapes it draws
.begin_fill() The turtle object begins to fill with color the shapes it draws
.end_fill() The turtle object stops to fill with color the shapes it draws
.bgcolor(color) Sets the background color
.pencolor(color) Sets the pen color
* Colors set with RGB code or with standard names ("red", "orange", "yellow",
"green", "blue", "purple", "black“ ecc.)
11
Exercise 4
Write a function named rectangle that draws a rectangle with a yellow fill.
The following arguments must be passed to the function:
• the name of the turtle object
• the length of the two sides of the rectangle
12
Exercise 5
Write a function named squares that draws two colored squares of the same
size, at a distance from each other:
• the turtle object name and the square side length must be passed to the
function as arguments
• ask the user for the fill color of each square
• set a fixed distance between two squares
13
Exercise 6
Write a function named spiderweb that draws a spider web consisting of 15
hexagonal threads:
• the name of the turtle object must be passed as arguments to the function
• the side of the outermost hexagon is 150 pixels and each hexagon must be
spaced 10 pixels from the other hexagons
• the function must also draw the threads that join the
center of the web and the six vertices of each hexagon
14
Files of the lesson
In the zip file 30424 ENG - 25 - Practice 6.zip you will find these files:
• Slide 30424 Practice 6.pdf: these slides
• .py files: solved files of the exercises
15
Book references
Learning Python:
Chapters 11.2.3
Assignment:
Exercises of lesson 12