Menu Zamknij

Moduł Turtle

W zależności jak zostanie zaimportowany moduł różnie może wyglądać wywoływanie zawartych w nim poleceń. W przypadku zaimportowania modułu w następujący sposób:

from turtle import *

podstawowe polecenia podajemy w sposób przedstawiony w tabeli poniżej:

polecenie Szczegóły parametru przykład
forward(distance)

 

fd(distance)

distance – a number (integer or float) forward(25)
back(distance)

 

bk(distance)
backward(distance)

backward(30)
right(angle)

 

rt(angle)

angle – a number (integer or float) right(45
left(angle)

 

lt(angle)

left(45)
goto(x, y)

 

setpos(x, y)
setposition(x, y)

  goto(60,30)
setheading(to_angle)

 

seth(to_angle)

to_angle – a number (integer or float)

 

Set the orientation of the turtle to to_angle. Here are some common directions in degrees:

standard mode logo mode
0 – east 0 – north
90 – north 90 – east
180 – west 180 – south
270 – south 270 – west
turtle.setheading(90)
home() Move turtle to the origin – coordinates (0,0) home()
circle(radius, extent, steps) radius – a number

 

extent – a number (or None)

steps – an integer (or None)

circle(50)

 

circle(120, 180)  # draw a semicircle

dot(size=None, *color) size – an integer >= 1 (if given)

 

color – a colorstring or a numeric color tuple

dot(20, „blue”)
speed(speed) speed – an integer in the range 0..10 or a speedstring (see below)

 

·  “fastest”: 0
·   “fast”: 10
·   “normal”: 6
·   “slow”: 3
·   “slowest”: 1

Attention: speed = 0 means that no animation takes place. forward/back makes turtle jump and likewise left/right make the turtle turn instantly.

speed(’normal’)

 

turtle.speed(9)

pendown()

 

pd()
down()

Pull the pen down – drawing when moving.  
penup()

 

pu()
up()

Pull the pen up – no drawing when moving.  
pensize(width)

 

width(width)

width – a positive number turtle.pensize(10)
pencolor()

 

pencolor(colorstring)
pencolor(r, g, b)

  pencolor(„brown”)

 

pencolor(51.0, 204.0, 140.0)
pencolor(’#32c18f’)

fillcolor()

 

fillcolor(colorstring)
fillcolor(r, g, b)

  fillcolor(„brown”)

 

fillcolor(51.0, 204.0, 140.0)
fillcolor(’#32c18f’)

reset() Delete the turtle’s drawings from the screen, re-center the turtle and set variables to the default values. reset()
clear() Delete the turtle’s drawings from the screen. Do not move turtle. State and position of the turtle as well as drawings of other turtles are not affected. clear()
hideturtle()

 

ht()

Make the turtle invisible. It’s a good idea to do this while you’re in the middle of doing some complex drawing, because hiding the turtle speeds up the drawing observably. hideturtle()

 

ht()

showturtle()

 

st()

Make the turtle visible. showturtle()

 

st()

shape(name) name – a string which is a valid shapename

 

Initially there are the following polygon shapes: “arrow”, “turtle”, “circle”, “square”, “triangle”, “classic”.

shape(„turtle”)

Pełna dokumentacja modułu Turtle

Ćwiczenie 1.
Napisz program który narysuje następującą figurę:

 

Jako urozmaicenie tego ćwiczenia możesz każdą z tych figur możesz narysować różnymi kolorami oraz używając zdefiniowanych przez siebie funkcji

Ćwiczenie 2.
Napisz progrogram, który narysuje dowolny wielokąt foremny. Zasosuj funkcję która bedzie miała argumenty ilości oraz długości boków, a ich wartość będzie podawana za pomoga funkcji input().