Turtle programming

Class objectives

Syntax table

Keyword
description
import
import statement is the most common way of invocking the required modules
star mark(*)
stands for everything
forward(distance)
Parameter: distance- is a number (integer or float). Move the turtle forward by the specified distance, in the direction the turtle is headed.
backward(distance)
Parameter: distance- is a number (integer or float). Move the turtle backward by the specified distance, in the direction the turtle is headed.
right(angle)
Parameter: angle- a number (integer or float). Turn turtle right by angle units.(Units are by default degrees)
left(angle)
Parameter: angle- a number (integer or float). Turn turtle left by angle units.(Units are by default degrees)
goto(x, y=None)
Parameters: x and y are numbers. y can be None
pendown()
Pull the pen down – drawing when moving.
penup()
Pull the pen up – no drawing when moving.
color(color1, color2)
color1 is fill color. color2 is pen color.
shape(name=None)
Parameter: name - a string which is a valid shapename. Set turtle shape to shape with given name or, if name is not given, return name of current shape.
done()
Must be the last statement that finalize the program

Draw the first turtle graphic

Square programs in comparison

  1. Square program in Scratch

  1. Square program in Python
shape("turtle")
forward(100)
right(90)
forward(100)
right(90)
forward(100)
right(90)
forward(100)
right(90)
  • forward():
  • right():

Square program in comparison (with loop)

  1. Square program in Scratch

  1. Square program in Python
for each_time in range(4):
	shape("turtle")
	forward(100)
	right(90)

Write a square program in Python

Step 1: Create Python file

Open Sublime Text editor, create a file by clickng File -> New File at the left top, name it as turtle_program.py.

Step 2: Import turtle module

Import Turtle module to the turtle_program.py file.

from turtle import *

Look at syntax table for more introduction about import

Step 3: Draw a square

Draw a square of which 100 pixels each side.

shape("turtle")
forward(100)
right(90)
forward(100)
right(90)
forward(100)
right(90)
forward(100)
right(90)

Look at syntax table for more introduction about forward() and right()

Step 4: Finalize the program

Finalize the program with done() statement at the last line

done()

Look at syntax table for more introduction about done() statement

Step 5: Complete code for square

from turtle import *

# Square program without loop
forward(100)
right(90)
forward(100)
right(90)
forward(100)
right(90)
forward(100)
right(90)

done()

Step 6: Run the square program

Run the program by pressing Ctrl + B if it presents the result as below:

Draw shapes in practice

Draw a triangle

from turtle import *

# Triangle program with a loop
for each_time in range(3):
	forward(100)
	right(120)

done()

Draw a Polygon

from turtle import *

# Polygram program with a loop
for each_time in range(6):
	forward(100)
	right(60)

done()

Draw polygons

for each_square in range(18):
	color("red", "green")
	speed(100)
	for each_line in range(6):
		forward(100)
		right(60)
	left(20)

done()

Draw more polygons

for each_square in range(36):
	color("red", "green")
	speed(100)
	for each_line in range(6):
		forward(100)
		right(10)
	left(20)

done()

Draw a symbol

from turtle import *

reset()
Screen()
up()
goto(-320,-195)
width(70)

for i in range(7):
	color("green")
	down()
	forward(640)
	up()
	backward(640)
	left(90)
	forward(66)
	right(90)

width(25)
color("white")
goto(0,-170)
down()

circle(170)
left(90)
forward(340)
up()
left(180)
forward(170)
right(45)
down()
forward(170)
up()
backward(170)
left(90)
down()
forward(170)
up()
goto(0,300)

done()

Homework: Draw different shapes at different position

Given following part of code with three coordinates (-400, 100),(0, 100) and (400, 100).Please complete this code to draw shapes below:

Example code:

from turtle import *

width(10)
speed(100)
penup()
goto(-400, 100)
pendown()
forward(100)

done()

Output: