You are on page 1of 10

Drawing Pictures

with Python
Using “turtle” graphics to introduce object
oriented
programming concepts in Python
What does a turtle have to do
with graphics?
What does a turtle have to do with graphics?

● Logo is an educational programming language developed in the late 1960’s


● Logo name comes from the greek logos which means thought
● The language is designed to control a robot with a pen which drove around drawing
on paper on the ground
● That robot is the turtle Apple II Computer
What is Python Turtle? What are Objects?

● A library of objects and methods that you can ● Object-oriented programming is a type of
use to draw programming language that focuses on
modeling programs around objects.
● Turtle is a class that contains all of the
variables and functions that describe a turtle ○ Characteristics(variables) and actions
● To use those functions and variables, we must (functions) in the same place
create a specific turtle (an instance)
● We can make as many turtles as we need to
● Classes are the special modules that contain
make our program work.
the code to describe these objects.
● We can create instances of these objects that
we can control and investigate
Create the
turtle object
from turtle import Turtle and give it a
tommy=Turtle() name/handle

Creating an suzie = Turtle()


ricky = Turtle()
instance of a Turtle
Each new turtle you make needs a
variable name
Create the
turtle object
from turtle import Turtle and give it a
tommy=Turtle() name/handle

tommy.forward(50)
tommy.right(90)
Controlling a Turtle tommy.forward(50)
tommy.right(90)
tommy.forward(50)
Each command has a name and a tommy.right(90)
lot of them have options or settings. tommy.forward(50)
tommy.right(90)
Iteration

from turtle import Turtle from turtle import Turtle


tommy=Turtle() tommy=Turtle()

}
tommy.forward(50) for i in range(4):
tommy.right(90) tommy.forward(50)
tommy.forward(50) tommy.right(90)
tommy.right(90)
tommy.forward(50) Do it 4 times

tommy.right(90)
tommy.forward(50)
tommy.right(90)
Furthering Iteration- Nested Loop
Outside
for i in range(10): loop
tommy.left(36)
for i in range(10): Inside loop
tommy.forward(50)
tommy.left(36)
Useful Commands
More info here on the official turtle graphics page
Including on how to change the background color
Basic Commands and the shape of the

tommy = Turtle() # create turtle tommy.circle(radius) #radius = pixels

tommy.forward(distance) tommy.goto(x, y) #works like the coordinate plane


tommy.backward(distance) #distance = pixels
tommy.pendown() #put pen on paper
tommy.right(angle) tommy.penup() #pick up pen
tommy.left(angle) #angle = degrees
tommy.pensize(num) #changes thickness of line
tommy.home() #puts tommy in center of window
tommy.color(colorName) #colorName is a string
tommy.clear() #clears the canvas
tommy.fillcolor(colorName)#colorName is a string
tommy.stamp()
tommy.begin_fill() #use before you draw the shape
tommy.write(“string”, font = (“family”, size, “weight)) tommy.end_fill() #use after you draw the shape

** These commands assume -> from Turtle import turtle

You might also like