Вы находитесь на странице: 1из 4

150410107100

OEP: EXPLAIN PYTHON LIBRARY TURTLE WITH ALL FUNCTIONS


The turtle module provides turtle graphics primitives, in both object-oriented and procedure-
oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python
installed with Tk support.

The object-oriented interface uses essentially two+two classes:

1. The TurtleScreen class defines graphics windows as a playground for the drawing
turtles. Its constructor needs a Tkinter.Canvas or aScrolledCanvas as argument. It
should be used when turtle is used as part of some application.

The function Screen() returns a singleton object of a TurtleScreen subclass. This function
should be used when turtle is used as a standalone tool for doing graphics. As a
singleton object, inheriting from its class is not possible.

All methods of TurtleScreen/Screen also exist as functions, i.e. as part of the procedure-
oriented interface.

2. RawTurtle (alias: RawPen) defines Turtle objects which draw on a TurtleScreen. Its
constructor needs a Canvas, ScrolledCanvas or TurtleScreen as argument, so the
RawTurtle objects know where to draw.

Derived from RawTurtle is the subclass Turtle (alias: Pen), which draws on
“the” Screen - instance which is automatically created, if not already present.

All methods of RawTurtle/Turtle also exist as functions, i.e. part of the procedure-
oriented interface.

The procedural interface provides functions which are derived from the methods of the
classes Screen and Turtle. They have the same names as the corresponding methods. A screen
object is automatically created whenever a function derived from a Screen method is called. An
(unnamed) turtle object is automatically created whenever any of the functions derived from a
Turtle method is called.
150410107100

TURTLE METHODS

Method Parameter Description

Turtle() None Creates and returns a new tutrle object

forward() amount Moves the turtle forward by the specified amount

backward() amount Moves the turtle backward by the specified amount

right() angle Turns the turtle clockwise

left() angle Turns the turtle counter clockwise

penup() None Picks up the turtle’s Pen

pendown() None Puts down the turtle’s Pen

up() None Picks up the turtle’s Pen

down() None Puts down the turtle’s Pen

color() Color name Changes the color of the turtle’s pen

fillcolor() Color name Changes the color of the turtle will use to fill a polygon

heading() None Returns the current heading

position() None Returns the current position

goto() x, y Move the turtle to position x,y


150410107100

begin_fill() None Remember the starting point for a filled polygon

end_fill() None Close the polygon and fill with the current fill color

dot() None Leave the dot at the current position

Leaves an impression of a turtle shape at the current

stamp() None location

shape() shapename Should be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’

Example 1:

Step 1: Import Turtle module

Step 2: Use the turtle pen and give the pen a nickname “t”
t = turtle.Pen()

Step 3: Give a function and define the function


def draw_line():
draw_line() is a function.
t.up() make the pen up.
t.down() make the pen down.
t.forward() make the pen move forward.
t.right() make the pen turn right.
At first, make the pen up, then move 20 steps. Make the pen down, then draw a line (50 steps
long). Make the pen up again, then move 20 steps. Finally, make the pen turn at a angle of 90
degrees.

Step 4: Make the function do something

for x in range(1, 5):


The function, draw_line(), will work for 4 times.
150410107100

Step 5: Make the pen having a color.


t.color(‘dark orange’)
t.color(‘dark orange’) can be in the first line under def draw_line().

EXAMPLE 2:

import turtle
spiral =turtle.Turtle()

for i inrange(20):
spiral.forward(i*10)
spiral.right(144)

turtle.done()

OUTPUT:

Вам также может понравиться