import statement is the most common way of invocking the required modules | |
stands for everything | |
Parameter: distance- is a number (integer or float). Move the turtle forward by the specified distance, in the direction the turtle is headed. | |
Parameter: distance- is a number (integer or float). Move the turtle backward by the specified distance, in the direction the turtle is headed. | |
Parameter: angle- a number (integer or float). Turn turtle right by angle units.(Units are by default degrees) | |
Parameter: angle- a number (integer or float). Turn turtle left by angle units.(Units are by default degrees) | |
Parameters: x and y are numbers. y can be None | |
Pull the pen down – drawing when moving. | |
Pull the pen up – no drawing when moving. | |
color1 is fill color. color2 is pen color. | |
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. | |
Must be the last statement that finalize the program |
shape("turtle")
forward(100)
right(90)
forward(100)
right(90)
forward(100)
right(90)
forward(100)
right(90)
forward()
:right()
:for each_time in range(4):
shape("turtle")
forward(100)
right(90)
Open Sublime Text
editor, create a file by clickng File -> New File
at the left top, name it as turtle_program.py
.
Import Turtle module to the turtle_program.py
file.
from turtle import *
Look at syntax table for more introduction about
import
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()
andright()
Finalize the program with done()
statement at the last line
done()
Look at syntax table for more introduction about
done()
statement
from turtle import *
# Square program without loop
forward(100)
right(90)
forward(100)
right(90)
forward(100)
right(90)
forward(100)
right(90)
done()
Run the program by pressing Ctrl + B
if it presents the result as below:
from turtle import *
# Triangle program with a loop
for each_time in range(3):
forward(100)
right(120)
done()
from turtle import *
# Polygram program with a loop
for each_time in range(6):
forward(100)
right(60)
done()
for each_square in range(18):
color("red", "green")
speed(100)
for each_line in range(6):
forward(100)
right(60)
left(20)
done()
for each_square in range(36):
color("red", "green")
speed(100)
for each_line in range(6):
forward(100)
right(10)
left(20)
done()
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: