KEMBAR78
Graphic programming using Turtle class in Python | PPTX
Turtle Graphics
The turtle MODULE
Python allows you to import extra modules,
specifically written for Python users.
One of them is called turtle. You must import it
to use it. You can then open a graphics window.
import turtle # so we can use
the turtles library of commands
win = turtle.Screen()
# creates a graphics window
The Turtle CLASS
A Turtle is an example of class. ( turtle.Turtle())
When you create a new turtle it is called an
INSTANCE. You can also call it an OBJECT.
import turtle
win = turtle.Screen()
#make an instance of a turtle and we
will call it delia
delia = turtle.Turtle()
Turtle attributes
Now you have an object called delia, you can
give the turtle unique characteristics or
ATTRIBUTES like shape and speed.
The list of attributes you give to the
Turtle will make delia a unique
turtle.
Example
Predict the shape that the Turtle delia will draw
following the algorithm below.
Examples of some more attributes and instructions
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
import turtle
skk = turtle.Turtle()
for i in range(4):
skk.forward(50)
skk.right(90)
turtle.done()
star = turtle.Turtle()
star.right(75)
star.forward(100)
for i in range(4):
star.right(144)
star.forward(100)
turtle.done()
wn = turtle.Screen()
wn.bgcolor("light green")
skk = turtle.Turtle()
skk.color("blue")
def sqrfunc(size):
for i in range(4):
skk.fd(size)
skk.left(90)
size = size + 5
sqrfunc(6)
sqrfunc(26)
sqrfunc(46)
sqrfunc(66)
sqrfunc(86)
sqrfunc(106)
sqrfunc(126)
sqrfunc(146)
Class Task
• Create two instances of a turtle.
• Call them delia and arthur.
• delia is to go to position (50,50) and draw a square with
that as centre. She then has to go to these 3 points,
without leaving a trail, and stamp the 3 points …(-30,30),
(100,-50), (0,0)
• arthur is to draw two circles. One of radius 50 centre(0,0) and
another of radius 40 centre (100,-50). Also the circles are to be
filled with colours.
Challenge
• Ask the user for the coordinates of the centre of a
circle, a radius and a fill colour.
• Get these properties for both delia and arthur
and instruct them to draw the circles.
• Finish with a stamp on the centre.
• Write on each circle the properties given by the user.
• For example “This is a red circle, centre (30,-30) and radius 45.”

Graphic programming using Turtle class in Python

  • 1.
  • 2.
    The turtle MODULE Pythonallows you to import extra modules, specifically written for Python users. One of them is called turtle. You must import it to use it. You can then open a graphics window. import turtle # so we can use the turtles library of commands win = turtle.Screen() # creates a graphics window
  • 3.
    The Turtle CLASS ATurtle is an example of class. ( turtle.Turtle()) When you create a new turtle it is called an INSTANCE. You can also call it an OBJECT. import turtle win = turtle.Screen() #make an instance of a turtle and we will call it delia delia = turtle.Turtle()
  • 4.
    Turtle attributes Now youhave an object called delia, you can give the turtle unique characteristics or ATTRIBUTES like shape and speed. The list of attributes you give to the Turtle will make delia a unique turtle.
  • 5.
    Example Predict the shapethat the Turtle delia will draw following the algorithm below.
  • 6.
    Examples of somemore attributes and instructions
  • 7.
    from turtle import* color('red', 'yellow') begin_fill() while True: forward(200) left(170) if abs(pos()) < 1: break end_fill() done() import turtle skk = turtle.Turtle() for i in range(4): skk.forward(50) skk.right(90) turtle.done()
  • 8.
    star = turtle.Turtle() star.right(75) star.forward(100) fori in range(4): star.right(144) star.forward(100) turtle.done() wn = turtle.Screen() wn.bgcolor("light green") skk = turtle.Turtle() skk.color("blue") def sqrfunc(size): for i in range(4): skk.fd(size) skk.left(90) size = size + 5 sqrfunc(6) sqrfunc(26) sqrfunc(46) sqrfunc(66) sqrfunc(86) sqrfunc(106) sqrfunc(126) sqrfunc(146)
  • 9.
    Class Task • Createtwo instances of a turtle. • Call them delia and arthur. • delia is to go to position (50,50) and draw a square with that as centre. She then has to go to these 3 points, without leaving a trail, and stamp the 3 points …(-30,30), (100,-50), (0,0) • arthur is to draw two circles. One of radius 50 centre(0,0) and another of radius 40 centre (100,-50). Also the circles are to be filled with colours.
  • 10.
    Challenge • Ask theuser for the coordinates of the centre of a circle, a radius and a fill colour. • Get these properties for both delia and arthur and instruct them to draw the circles. • Finish with a stamp on the centre. • Write on each circle the properties given by the user. • For example “This is a red circle, centre (30,-30) and radius 45.”