3.1Turtle
3.1Turtle
AGENDA
• Simple Graphics
• Overview of Turtle Graphics
• The Turtle methods/Operations
• Object Instantiation and the turtle Module
• Examples
• Drawing Two-Dimensional Shapes
• A radial pattern with 10 hexagons
2
SIMPLE GRAPHICS
• Graphics is the discipline that underlies the representation and display of
geometric shapes in two- and three-dimensional space, as well as image
processing.
• Python comes with a large array of resources that support graphics operations.
• However, these operations are complex and not for the faint of heart.
• To help you ease into the world of graphics, python provides a gentler set of
graphics operations known as Turtle graphics.
• A Turtle graphics toolkit provides a simple and enjoyable way to draw pictures
in a window and allows you to run several methods with an object.
3
OVERVI EW OF T URT LE
GRAPHICS
• Turtle graphics were originally developed as part of the children’s
programming language Logo, created by Seymour Papert and his colleagues
at MIT in the late 1960s.
• The name is intended to suggest a way to think about the drawing process.
• Imagine a turtle crawling on a piece of paper with a pen tied to its tail.
• Commands direct the turtle as it moves across the paper and tell it to lift
or lower its tail, turn some number of degrees left or right, and move a
specified distance. Whenever the tail is down, the pen drags along the
paper, leaving a trail. In this manner, it is possible to program the turtle to
draw pictures ranging from the simple to the complex.
4
O V E R V I E W C O N T.
• In the context of a computer, of course, the sheet of paper is a window on a display
screen, and the turtle is an icon, such as an arrowhead.
• At any given moment in time, the turtle is located at a specific position in the window.
• This position is specified with (x, y) coordinates.
• The coordinate system for Turtle graphics is the standard Cartesian system, with the
origin (0, 0) at the center of a window.
• The turtle’s initial position is the origin, which is also called the home.
• An equally important attribute of a turtle is its heading or the direction in which it
currently faces. The turtle’s initial heading is 0 degrees, or due east on its map.
• The degrees of the heading increase as it turns to the left, so 90 degrees is due north.
5
O V E R V I E W C O N T.
• Turtle’s other attributes
1. Heading Specified in degrees, the heading or direction increases in value as the turtle
turns to the left, or counterclockwise. Conversely, a negative quantity of degrees
indicates a right, or clockwise, turn. The turtle is initially facing east, or 0 degrees. North
is 90 degrees.
2. Color Initially black, the color can be changed to any of more than 16 million other
colors.
3. Width This is the width of the line drawn when the turtle moves. The initial width is 1
pixel. (You’ll learn more about pixels shortly.)
4. Down This attribute, which can be either true or false, controls whether the turtle’s pen
is up or down. When true (that is, when the pen is down), the turtle draws a line when it
moves. When false (that is, when the pen is up), the turtle can move without drawing a
line.
6
A T U R T L E ’ S S TAT E
• Together, the attributes make up a turtle’s state.
• The turtle’s state determines how the turtle will behave when any operations
are applied to it.
• For example, a turtle will draw when it is moved if its pen is currently
down, but it will simply move without drawing when its pen is currently
up.
• Operations also change a turtle’s state.
• For instance, moving a turtle changes its position, but not its direction,
pen width, or pen color.
7
T HE TU RT LE
M E T H O D S / O P E R AT I O N S
• Because a turtle is an object, its operations are also defined as methods.
• Table lists some of the methods belonging to the Turtle class.
• In this table, the variable t refers to a particular Turtle object.
8
T HE TU RT LE
M E T H O D S / O P E R AT I O N S
9
T HE TU RT LE
M E T H O D S / O P E R AT I O N S
10
EXAMPLE
• For example, define a function named
from turtle import Turtle
drawSquare.
t=Turtle()
• This function expects a Turtle object, a pair of def drawSquare(t, x, y, length):
integers that indicate the coordinates of the t.up()
square’s upper-left corner, and an integer that t.goto(x, y)
t.setheading(270)
designates the length of a side.
t.down()
• The function begins by lifting the turtle up and for count in range(4):
moving it to the square’s corner point. t.forward(length)
t.left(90)
• It then points the turtle due south—270 degrees
—and places the turtle’s pen down on the x_value=int(input("Enther the x-value :
drawing surface. Finally, it moves the turtle the "))
given length and turns it left by 90 degrees, four y_value=int(input("Enther the y-value :
times. Here is the code for the drawSquare "))
function:
11
O T H E R I M P O R TA N T C L A S S E S
• Two other important classes used in Python’s Turtle graphics system are
• Screen, which represents a turtle’s associated window, and
• Canvas, which represents the area in which a turtle can move and draw
lines. A canvas can be larger than its window, which displays just the area
of the canvas visible to the human user.
12
O B J E C T I N S TA N T I AT I O N A N D
T HE TU RT LE M ODU LE
• Before you use some objects, like a Turtle object, you must create them. That is, we must create an
instance of the object’s class. The process of creating an object is called instantiation.
• We have seen so far, that Python automatically created objects such as numbers, strings, and lists
when it encountered them as literals.
• The programmer must explicitly instantiate other classes of objects, including those that have no
literals.
• The syntax for instantiating a class and assigning the resulting object to a variable is the following:
• <variable name> = <class name>(<any arguments>)
• The expression on the right side of the assignment, also called a constructor, resembles a function
call. The constructor can receive as arguments any initial values for the new object’s attributes, or
other information needed to create the object.
• As you might expect, if the arguments are optional, reasonable defaults are provided automatically.
The constructor then manufactures and returns a new instance of the class.
13
O B J E C T I N S TA N T I AT I O N A N D
T H E T U R T L E M O D U L E C O N T.
• The Turtle class is defined in the turtle module (note carefully the spelling of both
names).
• The following code imports the Turtle class for use in a session:
• from turtle import Turtle
• The next code segment creates and returns a Turtle object and opens a drawing
window.
• t = Turtle()
• The turtle’s icon is located at the home position (0, 0) in the center of the window,
facing east and ready to draw.
• The user can resize the window in the usual manner.
14
EXAMPLE 1
>>> from turtle import Turtle
>>> t=Turtle()
15
EXAMPLE 2
• With the help of the turtle named t, draw the from turtle import Turtle
letter T, in black and red. t=Turtle()
• It begins at the home position, accepts a new t.setheading(90)
pen width of 2, turns 90 degrees left, and t.pencolor("red")
moves north 30 pixels to draw a black vertical t.width(4)
line. t.forward(30)
t.left(90)
• Then it turns 90 degrees left again to face
t.up()
west, picks its pen up, and moves 10 pixels.
t.forward(10)
• The turtle next turns to face due east,
t.pencolor("green")
changes its color from black to red, puts its
t.down()
pen down, and moves 20 pixels to draw a
t.setheading(0)
horizontal line.
t.forward(20)
• Finally, we hide the turtle. t.hideturtle()
16
EXAMPLE 2 O/P
from turtle import Turtle
t=Turtle()
t.setheading(90)
t.pencolor("red")
t.width(4)
t.forward(30)
t.left(90)
t.up()
t.forward(10)
t.pencolor("green")
t.down()
t.setheading(0)
t.forward(20)
t.hideturtle()
17
DRAWING TWO -DIMENSIONAL
SHAPES
18
SQUARE
19
P E N TA G O N
20
HEXAGON
21
A R A D I A L PAT T E R N W I T H 1 0
HEXAGONS
from turtle import Turtle
t=Turtle()
def hexagon(t,length):
for count in range(6):
t.forward(length)
t.left(60)
def radialHexagons(t,nuhex,length):
for count in range(nuhex):
hexagon(t,length)
t.left(360//nuhex)
len=int(input("Enther the length(side) : "))
num_hex=int(input("Enther the no.hexagons : "))
radialHexagons(t,num_hex,len)
22
A R A D I A L PAT T E R N -
COLORING
from turtle import Turtle
import random
t=Turtle()
def hexagon(t,length):
for count in range(6):
t.forward(length)
t.left(60)
def radialHexagons(t,nuhex,length):
colors = ["red", "purple", "brown",
"pink","green","white","yellow","lime","orange","blue"]
for count in range(nuhex):
t.fillcolor(colors[count%10])
t.begin_fill()
hexagon(t,length)
t.end_fill()
t.left(360//nuhex)
len=int(input("Enther the length(side) : "))
num_hex=int(input("Enther the no.hexagons : "))
radialHexagons(t,num_hex,len)
23
THANKS
24