blob: d96a5430895184cad2c16a405536265e2364d421 [file] [log] [blame]
Alexander Belopolskyf0a0d142010-10-27 03:06:43 +00001=================================
2:mod:`turtle` --- Turtle graphics
3=================================
Georg Brandl116aa622007-08-15 14:28:22 +00004
Georg Brandl23d11d32008-09-21 07:50:52 +00005.. module:: turtle
Alexander Belopolskyf0a0d142010-10-27 03:06:43 +00006 :synopsis: An educational framework for simple graphics applications
Georg Brandl2ee470f2008-07-16 12:55:28 +00007.. sectionauthor:: Gregor Lingl <gregor.lingl@aon.at>
8
R. David Murrayf877feb2009-05-05 02:08:52 +00009.. testsetup:: default
10
11 from turtle import *
12 turtle = Turtle()
13
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000014Introduction
15============
16
17Turtle graphics is a popular way for introducing programming to kids. It was
18part of the original Logo programming language developed by Wally Feurzig and
19Seymour Papert in 1966.
20
21Imagine a robotic turtle starting at (0, 0) in the x-y plane. Give it the
22command ``turtle.forward(15)``, and it moves (on-screen!) 15 pixels in the
23direction it is facing, drawing a line as it moves. Give it the command
24``turtle.left(25)``, and it rotates in-place 25 degrees clockwise.
25
26By combining together these and similar commands, intricate shapes and pictures
27can easily be drawn.
28
29The :mod:`turtle` module is an extended reimplementation of the same-named
30module from the Python standard distribution up to version Python 2.5.
31
32It tries to keep the merits of the old turtle module and to be (nearly) 100%
33compatible with it. This means in the first place to enable the learning
34programmer to use all the commands, classes and methods interactively when using
35the module from within IDLE run with the ``-n`` switch.
36
37The turtle module provides turtle graphics primitives, in both object-oriented
Ezio Melotti1a263ad2010-03-14 09:51:37 +000038and procedure-oriented ways. Because it uses :mod:`tkinter` for the underlying
Ezio Melotti0639d5a2009-12-19 23:26:38 +000039graphics, it needs a version of Python installed with Tk support.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000040
41The object-oriented interface uses essentially two+two classes:
42
431. The :class:`TurtleScreen` class defines graphics windows as a playground for
Ezio Melotti1a263ad2010-03-14 09:51:37 +000044 the drawing turtles. Its constructor needs a :class:`tkinter.Canvas` or a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000045 :class:`ScrolledCanvas` as argument. It should be used when :mod:`turtle` is
46 used as part of some application.
47
Martin v. Löwis601149b2008-09-29 22:19:08 +000048 The function :func:`Screen` returns a singleton object of a
49 :class:`TurtleScreen` subclass. This function should be used when
50 :mod:`turtle` is used as a standalone tool for doing graphics.
51 As a singleton object, inheriting from its class is not possible.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000052
53 All methods of TurtleScreen/Screen also exist as functions, i.e. as part of
54 the procedure-oriented interface.
55
562. :class:`RawTurtle` (alias: :class:`RawPen`) defines Turtle objects which draw
57 on a :class:`TurtleScreen`. Its constructor needs a Canvas, ScrolledCanvas
58 or TurtleScreen as argument, so the RawTurtle objects know where to draw.
59
60 Derived from RawTurtle is the subclass :class:`Turtle` (alias: :class:`Pen`),
Alexander Belopolsky435d3062010-10-19 21:07:52 +000061 which draws on "the" :class:`Screen` instance which is automatically
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000062 created, if not already present.
63
64 All methods of RawTurtle/Turtle also exist as functions, i.e. part of the
65 procedure-oriented interface.
66
67The procedural interface provides functions which are derived from the methods
68of the classes :class:`Screen` and :class:`Turtle`. They have the same names as
Georg Brandlae2dbe22009-03-13 19:04:40 +000069the corresponding methods. A screen object is automatically created whenever a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000070function derived from a Screen method is called. An (unnamed) turtle object is
71automatically created whenever any of the functions derived from a Turtle method
72is called.
73
Alexander Belopolsky435d3062010-10-19 21:07:52 +000074To use multiple turtles on a screen one has to use the object-oriented interface.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000075
76.. note::
77 In the following documentation the argument list for functions is given.
78 Methods, of course, have the additional first argument *self* which is
79 omitted here.
Georg Brandl116aa622007-08-15 14:28:22 +000080
81
Alexander Belopolsky435d3062010-10-19 21:07:52 +000082Overview of available Turtle and Screen methods
Martin v. Löwis97cf99f2008-06-10 04:44:07 +000083=================================================
84
85Turtle methods
86--------------
87
88Turtle motion
89 Move and draw
90 | :func:`forward` | :func:`fd`
91 | :func:`backward` | :func:`bk` | :func:`back`
92 | :func:`right` | :func:`rt`
93 | :func:`left` | :func:`lt`
94 | :func:`goto` | :func:`setpos` | :func:`setposition`
95 | :func:`setx`
96 | :func:`sety`
97 | :func:`setheading` | :func:`seth`
98 | :func:`home`
99 | :func:`circle`
100 | :func:`dot`
101 | :func:`stamp`
102 | :func:`clearstamp`
103 | :func:`clearstamps`
104 | :func:`undo`
105 | :func:`speed`
106
107 Tell Turtle's state
108 | :func:`position` | :func:`pos`
109 | :func:`towards`
110 | :func:`xcor`
111 | :func:`ycor`
112 | :func:`heading`
113 | :func:`distance`
114
115 Setting and measurement
116 | :func:`degrees`
117 | :func:`radians`
118
119Pen control
120 Drawing state
121 | :func:`pendown` | :func:`pd` | :func:`down`
122 | :func:`penup` | :func:`pu` | :func:`up`
123 | :func:`pensize` | :func:`width`
124 | :func:`pen`
125 | :func:`isdown`
126
127 Color control
128 | :func:`color`
129 | :func:`pencolor`
130 | :func:`fillcolor`
131
132 Filling
133 | :func:`filling`
134 | :func:`begin_fill`
135 | :func:`end_fill`
136
137 More drawing control
138 | :func:`reset`
139 | :func:`clear`
140 | :func:`write`
141
142Turtle state
143 Visibility
144 | :func:`showturtle` | :func:`st`
145 | :func:`hideturtle` | :func:`ht`
146 | :func:`isvisible`
147
148 Appearance
149 | :func:`shape`
150 | :func:`resizemode`
151 | :func:`shapesize` | :func:`turtlesize`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000152 | :func:`shearfactor`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000153 | :func:`settiltangle`
154 | :func:`tiltangle`
155 | :func:`tilt`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000156 | :func:`shapetransform`
157 | :func:`get_shapepoly`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000158
159Using events
160 | :func:`onclick`
161 | :func:`onrelease`
162 | :func:`ondrag`
163
164Special Turtle methods
165 | :func:`begin_poly`
166 | :func:`end_poly`
167 | :func:`get_poly`
168 | :func:`clone`
169 | :func:`getturtle` | :func:`getpen`
170 | :func:`getscreen`
171 | :func:`setundobuffer`
172 | :func:`undobufferentries`
Georg Brandl116aa622007-08-15 14:28:22 +0000173
174
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000175Methods of TurtleScreen/Screen
176------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000177
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000178Window control
179 | :func:`bgcolor`
180 | :func:`bgpic`
181 | :func:`clear` | :func:`clearscreen`
182 | :func:`reset` | :func:`resetscreen`
183 | :func:`screensize`
184 | :func:`setworldcoordinates`
Georg Brandl116aa622007-08-15 14:28:22 +0000185
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000186Animation control
187 | :func:`delay`
188 | :func:`tracer`
189 | :func:`update`
190
191Using screen events
192 | :func:`listen`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000193 | :func:`onkey` | :func:`onkeyrelease`
194 | :func:`onkeypress`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000195 | :func:`onclick` | :func:`onscreenclick`
196 | :func:`ontimer`
Georg Brandleaa84ef2009-05-05 08:14:33 +0000197 | :func:`mainloop`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000198
199Settings and special methods
200 | :func:`mode`
201 | :func:`colormode`
202 | :func:`getcanvas`
203 | :func:`getshapes`
204 | :func:`register_shape` | :func:`addshape`
205 | :func:`turtles`
206 | :func:`window_height`
207 | :func:`window_width`
208
Georg Brandleaa84ef2009-05-05 08:14:33 +0000209Input methods
210 | :func:`textinput`
211 | :func:`numinput`
212
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000213Methods specific to Screen
214 | :func:`bye`
215 | :func:`exitonclick`
216 | :func:`setup`
217 | :func:`title`
Georg Brandl116aa622007-08-15 14:28:22 +0000218
219
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000220Methods of RawTurtle/Turtle and corresponding functions
221=======================================================
Georg Brandl116aa622007-08-15 14:28:22 +0000222
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000223Most of the examples in this section refer to a Turtle instance called
224``turtle``.
Georg Brandl116aa622007-08-15 14:28:22 +0000225
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000226Turtle motion
227-------------
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229.. function:: forward(distance)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000230 fd(distance)
Georg Brandl116aa622007-08-15 14:28:22 +0000231
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000232 :param distance: a number (integer or float)
233
234 Move the turtle forward by the specified *distance*, in the direction the
235 turtle is headed.
236
R. David Murrayf877feb2009-05-05 02:08:52 +0000237 .. doctest::
238
239 >>> turtle.position()
240 (0.00,0.00)
241 >>> turtle.forward(25)
242 >>> turtle.position()
243 (25.00,0.00)
244 >>> turtle.forward(-75)
245 >>> turtle.position()
246 (-50.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000247
248
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000249.. function:: back(distance)
250 bk(distance)
251 backward(distance)
Georg Brandl116aa622007-08-15 14:28:22 +0000252
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000253 :param distance: a number
Georg Brandl116aa622007-08-15 14:28:22 +0000254
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000255 Move the turtle backward by *distance*, opposite to the direction the
256 turtle is headed. Do not change the turtle's heading.
Georg Brandl116aa622007-08-15 14:28:22 +0000257
R. David Murrayf877feb2009-05-05 02:08:52 +0000258 .. doctest::
259 :hide:
260
261 >>> turtle.goto(0, 0)
262
263 .. doctest::
264
265 >>> turtle.position()
266 (0.00,0.00)
267 >>> turtle.backward(30)
268 >>> turtle.position()
269 (-30.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000270
271
272.. function:: right(angle)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000273 rt(angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000274
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000275 :param angle: a number (integer or float)
276
277 Turn turtle right by *angle* units. (Units are by default degrees, but
278 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
279 orientation depends on the turtle mode, see :func:`mode`.
280
R. David Murrayf877feb2009-05-05 02:08:52 +0000281 .. doctest::
282 :hide:
283
284 >>> turtle.setheading(22)
285
286 .. doctest::
287
288 >>> turtle.heading()
289 22.0
290 >>> turtle.right(45)
291 >>> turtle.heading()
292 337.0
Georg Brandl116aa622007-08-15 14:28:22 +0000293
294
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000295.. function:: left(angle)
296 lt(angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000297
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000298 :param angle: a number (integer or float)
Georg Brandl116aa622007-08-15 14:28:22 +0000299
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000300 Turn turtle left by *angle* units. (Units are by default degrees, but
301 can be set via the :func:`degrees` and :func:`radians` functions.) Angle
302 orientation depends on the turtle mode, see :func:`mode`.
Georg Brandl116aa622007-08-15 14:28:22 +0000303
R. David Murrayf877feb2009-05-05 02:08:52 +0000304 .. doctest::
305 :hide:
306
307 >>> turtle.setheading(22)
308
309 .. doctest::
310
311 >>> turtle.heading()
312 22.0
313 >>> turtle.left(45)
314 >>> turtle.heading()
315 67.0
316
Georg Brandl116aa622007-08-15 14:28:22 +0000317
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000318.. function:: goto(x, y=None)
319 setpos(x, y=None)
320 setposition(x, y=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000321
R. David Murrayf877feb2009-05-05 02:08:52 +0000322 :param x: a number or a pair/vector of numbers
323 :param y: a number or ``None``
Georg Brandl116aa622007-08-15 14:28:22 +0000324
R. David Murrayf877feb2009-05-05 02:08:52 +0000325 If *y* is ``None``, *x* must be a pair of coordinates or a :class:`Vec2D`
326 (e.g. as returned by :func:`pos`).
Georg Brandl116aa622007-08-15 14:28:22 +0000327
R. David Murrayf877feb2009-05-05 02:08:52 +0000328 Move turtle to an absolute position. If the pen is down, draw line. Do
329 not change the turtle's orientation.
Georg Brandl116aa622007-08-15 14:28:22 +0000330
R. David Murrayf877feb2009-05-05 02:08:52 +0000331 .. doctest::
332 :hide:
333
334 >>> turtle.goto(0, 0)
335
336 .. doctest::
337
338 >>> tp = turtle.pos()
339 >>> tp
340 (0.00,0.00)
341 >>> turtle.setpos(60,30)
342 >>> turtle.pos()
343 (60.00,30.00)
344 >>> turtle.setpos((20,80))
345 >>> turtle.pos()
346 (20.00,80.00)
347 >>> turtle.setpos(tp)
348 >>> turtle.pos()
349 (0.00,0.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000350
Georg Brandl116aa622007-08-15 14:28:22 +0000351
352.. function:: setx(x)
353
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000354 :param x: a number (integer or float)
355
356 Set the turtle's first coordinate to *x*, leave second coordinate
357 unchanged.
358
R. David Murrayf877feb2009-05-05 02:08:52 +0000359 .. doctest::
360 :hide:
361
362 >>> turtle.goto(0, 240)
363
364 .. doctest::
365
366 >>> turtle.position()
367 (0.00,240.00)
368 >>> turtle.setx(10)
369 >>> turtle.position()
370 (10.00,240.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000371
Georg Brandl116aa622007-08-15 14:28:22 +0000372
373.. function:: sety(y)
374
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000375 :param y: a number (integer or float)
376
Benjamin Peterson058e31e2009-01-16 03:54:08 +0000377 Set the turtle's second coordinate to *y*, leave first coordinate unchanged.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000378
R. David Murrayf877feb2009-05-05 02:08:52 +0000379 .. doctest::
380 :hide:
381
382 >>> turtle.goto(0, 40)
383
384 .. doctest::
385
386 >>> turtle.position()
387 (0.00,40.00)
388 >>> turtle.sety(-10)
389 >>> turtle.position()
390 (0.00,-10.00)
Georg Brandl116aa622007-08-15 14:28:22 +0000391
Georg Brandl116aa622007-08-15 14:28:22 +0000392
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000393.. function:: setheading(to_angle)
394 seth(to_angle)
Georg Brandl116aa622007-08-15 14:28:22 +0000395
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000396 :param to_angle: a number (integer or float)
397
398 Set the orientation of the turtle to *to_angle*. Here are some common
399 directions in degrees:
400
401 =================== ====================
402 standard mode logo mode
403 =================== ====================
404 0 - east 0 - north
405 90 - north 90 - east
406 180 - west 180 - south
407 270 - south 270 - west
408 =================== ====================
409
R. David Murrayf877feb2009-05-05 02:08:52 +0000410 .. doctest::
411
412 >>> turtle.setheading(90)
413 >>> turtle.heading()
414 90.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000415
416
417.. function:: home()
418
419 Move turtle to the origin -- coordinates (0,0) -- and set its heading to
420 its start-orientation (which depends on the mode, see :func:`mode`).
421
R. David Murrayf877feb2009-05-05 02:08:52 +0000422 .. doctest::
423 :hide:
424
425 >>> turtle.setheading(90)
426 >>> turtle.goto(0, -10)
427
428 .. doctest::
429
430 >>> turtle.heading()
431 90.0
432 >>> turtle.position()
433 (0.00,-10.00)
434 >>> turtle.home()
435 >>> turtle.position()
436 (0.00,0.00)
437 >>> turtle.heading()
438 0.0
439
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000440
441.. function:: circle(radius, extent=None, steps=None)
442
443 :param radius: a number
444 :param extent: a number (or ``None``)
445 :param steps: an integer (or ``None``)
446
447 Draw a circle with given *radius*. The center is *radius* units left of
448 the turtle; *extent* -- an angle -- determines which part of the circle
449 is drawn. If *extent* is not given, draw the entire circle. If *extent*
450 is not a full circle, one endpoint of the arc is the current pen
451 position. Draw the arc in counterclockwise direction if *radius* is
452 positive, otherwise in clockwise direction. Finally the direction of the
453 turtle is changed by the amount of *extent*.
454
455 As the circle is approximated by an inscribed regular polygon, *steps*
456 determines the number of steps to use. If not given, it will be
457 calculated automatically. May be used to draw regular polygons.
458
R. David Murrayf877feb2009-05-05 02:08:52 +0000459 .. doctest::
460
461 >>> turtle.home()
462 >>> turtle.position()
463 (0.00,0.00)
464 >>> turtle.heading()
465 0.0
466 >>> turtle.circle(50)
467 >>> turtle.position()
468 (-0.00,0.00)
469 >>> turtle.heading()
470 0.0
471 >>> turtle.circle(120, 180) # draw a semicircle
472 >>> turtle.position()
473 (0.00,240.00)
474 >>> turtle.heading()
475 180.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000476
477
478.. function:: dot(size=None, *color)
479
480 :param size: an integer >= 1 (if given)
481 :param color: a colorstring or a numeric color tuple
482
483 Draw a circular dot with diameter *size*, using *color*. If *size* is
484 not given, the maximum of pensize+4 and 2*pensize is used.
485
R. David Murrayf877feb2009-05-05 02:08:52 +0000486
487 .. doctest::
488
489 >>> turtle.home()
490 >>> turtle.dot()
491 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
492 >>> turtle.position()
493 (100.00,-0.00)
494 >>> turtle.heading()
495 0.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000496
497
498.. function:: stamp()
499
500 Stamp a copy of the turtle shape onto the canvas at the current turtle
501 position. Return a stamp_id for that stamp, which can be used to delete
502 it by calling ``clearstamp(stamp_id)``.
503
R. David Murrayf877feb2009-05-05 02:08:52 +0000504 .. doctest::
505
506 >>> turtle.color("blue")
507 >>> turtle.stamp()
508 11
509 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000510
511
512.. function:: clearstamp(stampid)
513
514 :param stampid: an integer, must be return value of previous
515 :func:`stamp` call
516
517 Delete stamp with given *stampid*.
518
R. David Murrayf877feb2009-05-05 02:08:52 +0000519 .. doctest::
520
521 >>> turtle.position()
522 (150.00,-0.00)
523 >>> turtle.color("blue")
524 >>> astamp = turtle.stamp()
525 >>> turtle.fd(50)
526 >>> turtle.position()
527 (200.00,-0.00)
528 >>> turtle.clearstamp(astamp)
529 >>> turtle.position()
530 (200.00,-0.00)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000531
532
533.. function:: clearstamps(n=None)
534
535 :param n: an integer (or ``None``)
536
537 Delete all or first/last *n* of turtle's stamps. If *n* is None, delete
538 all stamps, if *n* > 0 delete first *n* stamps, else if *n* < 0 delete
539 last *n* stamps.
540
R. David Murrayf877feb2009-05-05 02:08:52 +0000541 .. doctest::
542
543 >>> for i in range(8):
544 ... turtle.stamp(); turtle.fd(30)
545 13
546 14
547 15
548 16
549 17
550 18
551 19
552 20
553 >>> turtle.clearstamps(2)
554 >>> turtle.clearstamps(-2)
555 >>> turtle.clearstamps()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000556
557
558.. function:: undo()
559
560 Undo (repeatedly) the last turtle action(s). Number of available
561 undo actions is determined by the size of the undobuffer.
562
R. David Murrayf877feb2009-05-05 02:08:52 +0000563 .. doctest::
564
565 >>> for i in range(4):
566 ... turtle.fd(50); turtle.lt(80)
567 ...
568 >>> for i in range(8):
569 ... turtle.undo()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000570
571
572.. function:: speed(speed=None)
573
574 :param speed: an integer in the range 0..10 or a speedstring (see below)
575
576 Set the turtle's speed to an integer value in the range 0..10. If no
577 argument is given, return current speed.
578
579 If input is a number greater than 10 or smaller than 0.5, speed is set
580 to 0. Speedstrings are mapped to speedvalues as follows:
581
582 * "fastest": 0
583 * "fast": 10
584 * "normal": 6
585 * "slow": 3
586 * "slowest": 1
587
588 Speeds from 1 to 10 enforce increasingly faster animation of line drawing
589 and turtle turning.
590
591 Attention: *speed* = 0 means that *no* animation takes
592 place. forward/back makes turtle jump and likewise left/right make the
593 turtle turn instantly.
594
R. David Murrayf877feb2009-05-05 02:08:52 +0000595 .. doctest::
596
597 >>> turtle.speed()
598 3
599 >>> turtle.speed('normal')
600 >>> turtle.speed()
601 6
602 >>> turtle.speed(9)
603 >>> turtle.speed()
604 9
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000605
606
607Tell Turtle's state
608-------------------
609
610.. function:: position()
611 pos()
612
613 Return the turtle's current location (x,y) (as a :class:`Vec2D` vector).
614
R. David Murrayf877feb2009-05-05 02:08:52 +0000615 .. doctest::
616
617 >>> turtle.pos()
618 (440.00,-0.00)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000619
620
621.. function:: towards(x, y=None)
622
623 :param x: a number or a pair/vector of numbers or a turtle instance
624 :param y: a number if *x* is a number, else ``None``
625
626 Return the angle between the line from turtle position to position specified
627 by (x,y), the vector or the other turtle. This depends on the turtle's start
628 orientation which depends on the mode - "standard"/"world" or "logo").
629
R. David Murrayf877feb2009-05-05 02:08:52 +0000630 .. doctest::
631
632 >>> turtle.goto(10, 10)
633 >>> turtle.towards(0,0)
634 225.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000635
636
637.. function:: xcor()
638
639 Return the turtle's x coordinate.
640
R. David Murrayf877feb2009-05-05 02:08:52 +0000641 .. doctest::
642
643 >>> turtle.home()
644 >>> turtle.left(50)
645 >>> turtle.forward(100)
646 >>> turtle.pos()
647 (64.28,76.60)
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000648 >>> print(round(turtle.xcor(), 5))
649 64.27876
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000650
651
652.. function:: ycor()
653
654 Return the turtle's y coordinate.
655
R. David Murrayf877feb2009-05-05 02:08:52 +0000656 .. doctest::
657
658 >>> turtle.home()
659 >>> turtle.left(60)
660 >>> turtle.forward(100)
Ezio Melotti985e24d2009-09-13 07:54:02 +0000661 >>> print(turtle.pos())
R. David Murrayf877feb2009-05-05 02:08:52 +0000662 (50.00,86.60)
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000663 >>> print(round(turtle.ycor(), 5))
664 86.60254
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000665
666
667.. function:: heading()
668
669 Return the turtle's current heading (value depends on the turtle mode, see
670 :func:`mode`).
671
R. David Murrayf877feb2009-05-05 02:08:52 +0000672 .. doctest::
673
674 >>> turtle.home()
675 >>> turtle.left(67)
676 >>> turtle.heading()
677 67.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000678
679
680.. function:: distance(x, y=None)
681
682 :param x: a number or a pair/vector of numbers or a turtle instance
683 :param y: a number if *x* is a number, else ``None``
684
685 Return the distance from the turtle to (x,y), the given vector, or the given
686 other turtle, in turtle step units.
687
R. David Murrayf877feb2009-05-05 02:08:52 +0000688 .. doctest::
689
690 >>> turtle.home()
691 >>> turtle.distance(30,40)
692 50.0
693 >>> turtle.distance((30,40))
694 50.0
695 >>> joe = Turtle()
696 >>> joe.forward(77)
697 >>> turtle.distance(joe)
698 77.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000699
700
701Settings for measurement
702------------------------
703
704.. function:: degrees(fullcircle=360.0)
705
706 :param fullcircle: a number
707
708 Set angle measurement units, i.e. set number of "degrees" for a full circle.
709 Default value is 360 degrees.
710
R. David Murrayf877feb2009-05-05 02:08:52 +0000711 .. doctest::
712
713 >>> turtle.home()
714 >>> turtle.left(90)
715 >>> turtle.heading()
716 90.0
Alexander Belopolsky3cdfb122010-10-29 17:16:49 +0000717
718 Change angle measurement unit to grad (also known as gon,
719 grade, or gradian and equals 1/100-th of the right angle.)
720 >>> turtle.degrees(400.0)
R. David Murrayf877feb2009-05-05 02:08:52 +0000721 >>> turtle.heading()
722 100.0
723 >>> turtle.degrees(360)
724 >>> turtle.heading()
725 90.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000726
727
728.. function:: radians()
729
730 Set the angle measurement units to radians. Equivalent to
731 ``degrees(2*math.pi)``.
732
R. David Murrayf877feb2009-05-05 02:08:52 +0000733 .. doctest::
734
735 >>> turtle.home()
736 >>> turtle.left(90)
737 >>> turtle.heading()
738 90.0
739 >>> turtle.radians()
740 >>> turtle.heading()
741 1.5707963267948966
742
743 .. doctest::
744 :hide:
745
746 >>> turtle.degrees(360)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000747
748
749Pen control
750-----------
751
752Drawing state
753~~~~~~~~~~~~~
754
755.. function:: pendown()
756 pd()
757 down()
758
759 Pull the pen down -- drawing when moving.
760
761
762.. function:: penup()
763 pu()
764 up()
765
766 Pull the pen up -- no drawing when moving.
767
768
769.. function:: pensize(width=None)
770 width(width=None)
771
772 :param width: a positive number
773
774 Set the line thickness to *width* or return it. If resizemode is set to
775 "auto" and turtleshape is a polygon, that polygon is drawn with the same line
776 thickness. If no argument is given, the current pensize is returned.
777
R. David Murrayf877feb2009-05-05 02:08:52 +0000778 .. doctest::
779
780 >>> turtle.pensize()
781 1
782 >>> turtle.pensize(10) # from here on lines of width 10 are drawn
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000783
784
785.. function:: pen(pen=None, **pendict)
786
787 :param pen: a dictionary with some or all of the below listed keys
788 :param pendict: one or more keyword-arguments with the below listed keys as keywords
789
790 Return or set the pen's attributes in a "pen-dictionary" with the following
791 key/value pairs:
792
793 * "shown": True/False
794 * "pendown": True/False
795 * "pencolor": color-string or color-tuple
796 * "fillcolor": color-string or color-tuple
797 * "pensize": positive number
798 * "speed": number in range 0..10
799 * "resizemode": "auto" or "user" or "noresize"
800 * "stretchfactor": (positive number, positive number)
801 * "outline": positive number
802 * "tilt": number
803
R. David Murrayf877feb2009-05-05 02:08:52 +0000804 This dictionary can be used as argument for a subsequent call to :func:`pen`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000805 to restore the former pen-state. Moreover one or more of these attributes
806 can be provided as keyword-arguments. This can be used to set several pen
807 attributes in one statement.
808
R. David Murrayf877feb2009-05-05 02:08:52 +0000809 .. doctest::
810 :options: +NORMALIZE_WHITESPACE
811
812 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
813 >>> sorted(turtle.pen().items())
814 [('fillcolor', 'black'), ('outline', 1), ('pencolor', 'red'),
815 ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000816 ('shearfactor', 0.0), ('shown', True), ('speed', 9),
817 ('stretchfactor', (1.0, 1.0)), ('tilt', 0.0)]
R. David Murrayf877feb2009-05-05 02:08:52 +0000818 >>> penstate=turtle.pen()
819 >>> turtle.color("yellow", "")
820 >>> turtle.penup()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000821 >>> sorted(turtle.pen().items())[:3]
822 [('fillcolor', ''), ('outline', 1), ('pencolor', 'yellow')]
R. David Murrayf877feb2009-05-05 02:08:52 +0000823 >>> turtle.pen(penstate, fillcolor="green")
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000824 >>> sorted(turtle.pen().items())[:3]
825 [('fillcolor', 'green'), ('outline', 1), ('pencolor', 'red')]
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000826
827.. function:: isdown()
828
829 Return ``True`` if pen is down, ``False`` if it's up.
830
R. David Murrayf877feb2009-05-05 02:08:52 +0000831 .. doctest::
832
833 >>> turtle.penup()
834 >>> turtle.isdown()
835 False
836 >>> turtle.pendown()
837 >>> turtle.isdown()
838 True
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000839
840
841Color control
842~~~~~~~~~~~~~
843
844.. function:: pencolor(*args)
845
846 Return or set the pencolor.
847
848 Four input formats are allowed:
849
850 ``pencolor()``
R. David Murrayf877feb2009-05-05 02:08:52 +0000851 Return the current pencolor as color specification string or
852 as a tuple (see example). May be used as input to another
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000853 color/pencolor/fillcolor call.
854
855 ``pencolor(colorstring)``
856 Set pencolor to *colorstring*, which is a Tk color specification string,
857 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
858
859 ``pencolor((r, g, b))``
860 Set pencolor to the RGB color represented by the tuple of *r*, *g*, and
861 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
862 colormode is either 1.0 or 255 (see :func:`colormode`).
863
864 ``pencolor(r, g, b)``
865 Set pencolor to the RGB color represented by *r*, *g*, and *b*. Each of
866 *r*, *g*, and *b* must be in the range 0..colormode.
867
868 If turtleshape is a polygon, the outline of that polygon is drawn with the
869 newly set pencolor.
870
R. David Murrayf877feb2009-05-05 02:08:52 +0000871 .. doctest::
872
873 >>> colormode()
874 1.0
875 >>> turtle.pencolor()
876 'red'
877 >>> turtle.pencolor("brown")
878 >>> turtle.pencolor()
879 'brown'
880 >>> tup = (0.2, 0.8, 0.55)
881 >>> turtle.pencolor(tup)
882 >>> turtle.pencolor()
Mark Dickinson5a55b612009-06-28 20:59:42 +0000883 (0.2, 0.8, 0.5490196078431373)
R. David Murrayf877feb2009-05-05 02:08:52 +0000884 >>> colormode(255)
885 >>> turtle.pencolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000886 (51.0, 204.0, 140.0)
R. David Murrayf877feb2009-05-05 02:08:52 +0000887 >>> turtle.pencolor('#32c18f')
888 >>> turtle.pencolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000889 (50.0, 193.0, 143.0)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000890
891
892.. function:: fillcolor(*args)
893
894 Return or set the fillcolor.
895
896 Four input formats are allowed:
897
898 ``fillcolor()``
R. David Murrayf877feb2009-05-05 02:08:52 +0000899 Return the current fillcolor as color specification string, possibly
900 in tuple format (see example). May be used as input to another
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000901 color/pencolor/fillcolor call.
902
903 ``fillcolor(colorstring)``
904 Set fillcolor to *colorstring*, which is a Tk color specification string,
905 such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
906
907 ``fillcolor((r, g, b))``
908 Set fillcolor to the RGB color represented by the tuple of *r*, *g*, and
909 *b*. Each of *r*, *g*, and *b* must be in the range 0..colormode, where
910 colormode is either 1.0 or 255 (see :func:`colormode`).
911
912 ``fillcolor(r, g, b)``
913 Set fillcolor to the RGB color represented by *r*, *g*, and *b*. Each of
914 *r*, *g*, and *b* must be in the range 0..colormode.
915
916 If turtleshape is a polygon, the interior of that polygon is drawn
917 with the newly set fillcolor.
918
R. David Murrayf877feb2009-05-05 02:08:52 +0000919 .. doctest::
920
921 >>> turtle.fillcolor("violet")
922 >>> turtle.fillcolor()
923 'violet'
924 >>> col = turtle.pencolor()
925 >>> col
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000926 (50.0, 193.0, 143.0)
R. David Murrayf877feb2009-05-05 02:08:52 +0000927 >>> turtle.fillcolor(col)
928 >>> turtle.fillcolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000929 (50.0, 193.0, 143.0)
R. David Murrayf877feb2009-05-05 02:08:52 +0000930 >>> turtle.fillcolor('#ffffff')
931 >>> turtle.fillcolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000932 (255.0, 255.0, 255.0)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000933
934
935.. function:: color(*args)
936
937 Return or set pencolor and fillcolor.
938
939 Several input formats are allowed. They use 0 to 3 arguments as
940 follows:
941
942 ``color()``
943 Return the current pencolor and the current fillcolor as a pair of color
R. David Murrayf877feb2009-05-05 02:08:52 +0000944 specification strings or tuples as returned by :func:`pencolor` and
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000945 :func:`fillcolor`.
946
947 ``color(colorstring)``, ``color((r,g,b))``, ``color(r,g,b)``
948 Inputs as in :func:`pencolor`, set both, fillcolor and pencolor, to the
949 given value.
950
951 ``color(colorstring1, colorstring2)``, ``color((r1,g1,b1), (r2,g2,b2))``
952 Equivalent to ``pencolor(colorstring1)`` and ``fillcolor(colorstring2)``
953 and analogously if the other input format is used.
954
955 If turtleshape is a polygon, outline and interior of that polygon is drawn
956 with the newly set colors.
957
R. David Murrayf877feb2009-05-05 02:08:52 +0000958 .. doctest::
959
960 >>> turtle.color("red", "green")
961 >>> turtle.color()
962 ('red', 'green')
963 >>> color("#285078", "#a0c8f0")
964 >>> color()
Alexander Belopolskya9615d12010-10-31 00:51:11 +0000965 ((40.0, 80.0, 120.0), (160.0, 200.0, 240.0))
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000966
967
968See also: Screen method :func:`colormode`.
969
970
971Filling
972~~~~~~~
973
R. David Murrayf877feb2009-05-05 02:08:52 +0000974.. doctest::
975 :hide:
976
977 >>> turtle.home()
978
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000979.. function:: filling()
980
981 Return fillstate (``True`` if filling, ``False`` else).
982
R. David Murrayf877feb2009-05-05 02:08:52 +0000983 .. doctest::
984
985 >>> turtle.begin_fill()
986 >>> if turtle.filling():
987 ... turtle.pensize(5)
988 ... else:
989 ... turtle.pensize(3)
990
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000991
992
993.. function:: begin_fill()
994
995 To be called just before drawing a shape to be filled.
996
Martin v. Löwis97cf99f2008-06-10 04:44:07 +0000997
998.. function:: end_fill()
999
1000 Fill the shape drawn after the last call to :func:`begin_fill`.
1001
R. David Murrayf877feb2009-05-05 02:08:52 +00001002 .. doctest::
1003
1004 >>> turtle.color("black", "red")
1005 >>> turtle.begin_fill()
1006 >>> turtle.circle(80)
1007 >>> turtle.end_fill()
1008
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001009
1010More drawing control
1011~~~~~~~~~~~~~~~~~~~~
1012
1013.. function:: reset()
1014
1015 Delete the turtle's drawings from the screen, re-center the turtle and set
1016 variables to the default values.
1017
R. David Murrayf877feb2009-05-05 02:08:52 +00001018 .. doctest::
1019
1020 >>> turtle.goto(0,-22)
1021 >>> turtle.left(100)
1022 >>> turtle.position()
1023 (0.00,-22.00)
1024 >>> turtle.heading()
1025 100.0
1026 >>> turtle.reset()
1027 >>> turtle.position()
1028 (0.00,0.00)
1029 >>> turtle.heading()
1030 0.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001031
1032
1033.. function:: clear()
1034
1035 Delete the turtle's drawings from the screen. Do not move turtle. State and
1036 position of the turtle as well as drawings of other turtles are not affected.
1037
1038
1039.. function:: write(arg, move=False, align="left", font=("Arial", 8, "normal"))
1040
1041 :param arg: object to be written to the TurtleScreen
1042 :param move: True/False
1043 :param align: one of the strings "left", "center" or right"
1044 :param font: a triple (fontname, fontsize, fonttype)
1045
1046 Write text - the string representation of *arg* - at the current turtle
1047 position according to *align* ("left", "center" or right") and with the given
1048 font. If *move* is True, the pen is moved to the bottom-right corner of the
1049 text. By default, *move* is False.
1050
1051 >>> turtle.write("Home = ", True, align="center")
1052 >>> turtle.write((0,0), True)
1053
1054
1055Turtle state
1056------------
1057
1058Visibility
1059~~~~~~~~~~
1060
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001061.. function:: hideturtle()
1062 ht()
1063
1064 Make the turtle invisible. It's a good idea to do this while you're in the
1065 middle of doing some complex drawing, because hiding the turtle speeds up the
1066 drawing observably.
1067
R. David Murrayf877feb2009-05-05 02:08:52 +00001068 .. doctest::
1069
1070 >>> turtle.hideturtle()
1071
1072
1073.. function:: showturtle()
1074 st()
1075
1076 Make the turtle visible.
1077
1078 .. doctest::
1079
1080 >>> turtle.showturtle()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001081
1082
1083.. function:: isvisible()
1084
1085 Return True if the Turtle is shown, False if it's hidden.
1086
1087 >>> turtle.hideturtle()
R. David Murrayf877feb2009-05-05 02:08:52 +00001088 >>> turtle.isvisible()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001089 False
R. David Murrayf877feb2009-05-05 02:08:52 +00001090 >>> turtle.showturtle()
1091 >>> turtle.isvisible()
1092 True
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001093
1094
1095Appearance
1096~~~~~~~~~~
1097
1098.. function:: shape(name=None)
1099
1100 :param name: a string which is a valid shapename
1101
1102 Set turtle shape to shape with given *name* or, if name is not given, return
1103 name of current shape. Shape with *name* must exist in the TurtleScreen's
1104 shape dictionary. Initially there are the following polygon shapes: "arrow",
1105 "turtle", "circle", "square", "triangle", "classic". To learn about how to
1106 deal with shapes see Screen method :func:`register_shape`.
1107
R. David Murrayf877feb2009-05-05 02:08:52 +00001108 .. doctest::
1109
1110 >>> turtle.shape()
1111 'classic'
1112 >>> turtle.shape("turtle")
1113 >>> turtle.shape()
1114 'turtle'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001115
1116
1117.. function:: resizemode(rmode=None)
1118
1119 :param rmode: one of the strings "auto", "user", "noresize"
1120
1121 Set resizemode to one of the values: "auto", "user", "noresize". If *rmode*
1122 is not given, return current resizemode. Different resizemodes have the
1123 following effects:
1124
1125 - "auto": adapts the appearance of the turtle corresponding to the value of pensize.
1126 - "user": adapts the appearance of the turtle according to the values of
1127 stretchfactor and outlinewidth (outline), which are set by
1128 :func:`shapesize`.
1129 - "noresize": no adaption of the turtle's appearance takes place.
1130
1131 resizemode("user") is called by :func:`shapesize` when used with arguments.
1132
R. David Murrayf877feb2009-05-05 02:08:52 +00001133 .. doctest::
1134
1135 >>> turtle.resizemode()
1136 'noresize'
1137 >>> turtle.resizemode("auto")
1138 >>> turtle.resizemode()
1139 'auto'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001140
1141
1142.. function:: shapesize(stretch_wid=None, stretch_len=None, outline=None)
R. David Murray7fcd3de2009-06-25 14:26:19 +00001143 turtlesize(stretch_wid=None, stretch_len=None, outline=None)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001144
1145 :param stretch_wid: positive number
1146 :param stretch_len: positive number
1147 :param outline: positive number
1148
1149 Return or set the pen's attributes x/y-stretchfactors and/or outline. Set
1150 resizemode to "user". If and only if resizemode is set to "user", the turtle
1151 will be displayed stretched according to its stretchfactors: *stretch_wid* is
1152 stretchfactor perpendicular to its orientation, *stretch_len* is
1153 stretchfactor in direction of its orientation, *outline* determines the width
1154 of the shapes's outline.
1155
R. David Murrayf877feb2009-05-05 02:08:52 +00001156 .. doctest::
1157
1158 >>> turtle.shapesize()
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001159 (1.0, 1.0, 1)
R. David Murrayf877feb2009-05-05 02:08:52 +00001160 >>> turtle.resizemode("user")
1161 >>> turtle.shapesize(5, 5, 12)
1162 >>> turtle.shapesize()
1163 (5, 5, 12)
1164 >>> turtle.shapesize(outline=8)
1165 >>> turtle.shapesize()
1166 (5, 5, 8)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001167
1168
R. David Murray7fcd3de2009-06-25 14:26:19 +00001169.. function:: shearfactor(shear=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001170
1171 :param shear: number (optional)
1172
1173 Set or return the current shearfactor. Shear the turtleshape according to
1174 the given shearfactor shear, which is the tangent of the shear angle.
1175 Do *not* change the turtle's heading (direction of movement).
1176 If shear is not given: return the current shearfactor, i. e. the
1177 tangent of the shear angle, by which lines parallel to the
1178 heading of the turtle are sheared.
1179
1180 .. doctest::
1181
1182 >>> turtle.shape("circle")
1183 >>> turtle.shapesize(5,2)
1184 >>> turtle.shearfactor(0.5)
1185 >>> turtle.shearfactor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001186 0.5
Georg Brandleaa84ef2009-05-05 08:14:33 +00001187
1188
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001189.. function:: tilt(angle)
1190
1191 :param angle: a number
1192
1193 Rotate the turtleshape by *angle* from its current tilt-angle, but do *not*
1194 change the turtle's heading (direction of movement).
1195
R. David Murrayf877feb2009-05-05 02:08:52 +00001196 .. doctest::
1197
1198 >>> turtle.reset()
1199 >>> turtle.shape("circle")
1200 >>> turtle.shapesize(5,2)
1201 >>> turtle.tilt(30)
1202 >>> turtle.fd(50)
1203 >>> turtle.tilt(30)
1204 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001205
1206
1207.. function:: settiltangle(angle)
1208
1209 :param angle: a number
1210
1211 Rotate the turtleshape to point in the direction specified by *angle*,
1212 regardless of its current tilt-angle. *Do not* change the turtle's heading
1213 (direction of movement).
1214
R. David Murrayf877feb2009-05-05 02:08:52 +00001215 .. doctest::
1216
1217 >>> turtle.reset()
1218 >>> turtle.shape("circle")
1219 >>> turtle.shapesize(5,2)
1220 >>> turtle.settiltangle(45)
1221 >>> turtle.fd(50)
1222 >>> turtle.settiltangle(-45)
1223 >>> turtle.fd(50)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001224
Ezio Melotti4e511282010-02-14 03:11:06 +00001225 .. deprecated:: 3.1
1226
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001227
Georg Brandleaa84ef2009-05-05 08:14:33 +00001228.. function:: tiltangle(angle=None)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001229
Georg Brandleaa84ef2009-05-05 08:14:33 +00001230 :param angle: a number (optional)
1231
1232 Set or return the current tilt-angle. If angle is given, rotate the
1233 turtleshape to point in the direction specified by angle,
1234 regardless of its current tilt-angle. Do *not* change the turtle's
1235 heading (direction of movement).
1236 If angle is not given: return the current tilt-angle, i. e. the angle
1237 between the orientation of the turtleshape and the heading of the
1238 turtle (its direction of movement).
1239
R. David Murrayf877feb2009-05-05 02:08:52 +00001240 .. doctest::
1241
1242 >>> turtle.reset()
1243 >>> turtle.shape("circle")
1244 >>> turtle.shapesize(5,2)
1245 >>> turtle.tilt(45)
1246 >>> turtle.tiltangle()
1247 45.0
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001248
1249
Georg Brandleaa84ef2009-05-05 08:14:33 +00001250.. function:: shapetransform(t11=None, t12=None, t21=None, t22=None)
1251
1252 :param t11: a number (optional)
1253 :param t12: a number (optional)
1254 :param t21: a number (optional)
1255 :param t12: a number (optional)
1256
1257 Set or return the current transformation matrix of the turtle shape.
1258
1259 If none of the matrix elements are given, return the transformation
1260 matrix as a tuple of 4 elements.
1261 Otherwise set the given elements and transform the turtleshape
1262 according to the matrix consisting of first row t11, t12 and
1263 second row t21, 22. The determinant t11 * t22 - t12 * t21 must not be
1264 zero, otherwise an error is raised.
1265 Modify stretchfactor, shearfactor and tiltangle according to the
1266 given matrix.
1267
1268 .. doctest::
1269
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001270 >>> turtle = Turtle()
Georg Brandleaa84ef2009-05-05 08:14:33 +00001271 >>> turtle.shape("square")
1272 >>> turtle.shapesize(4,2)
1273 >>> turtle.shearfactor(-0.5)
1274 >>> turtle.shapetransform()
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001275 (4.0, -1.0, -0.0, 2.0)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001276
1277
R. David Murray7fcd3de2009-06-25 14:26:19 +00001278.. function:: get_shapepoly()
Georg Brandleaa84ef2009-05-05 08:14:33 +00001279
1280 Return the current shape polygon as tuple of coordinate pairs. This
1281 can be used to define a new shape or components of a compound shape.
1282
1283 .. doctest::
1284
1285 >>> turtle.shape("square")
1286 >>> turtle.shapetransform(4, -1, 0, 2)
1287 >>> turtle.get_shapepoly()
1288 ((50, -20), (30, 20), (-50, 20), (-30, -20))
1289
1290
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001291Using events
1292------------
1293
1294.. function:: onclick(fun, btn=1, add=None)
1295
1296 :param fun: a function with two arguments which will be called with the
1297 coordinates of the clicked point on the canvas
1298 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1299 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1300 added, otherwise it will replace a former binding
1301
1302 Bind *fun* to mouse-click events on this turtle. If *fun* is ``None``,
1303 existing bindings are removed. Example for the anonymous turtle, i.e. the
1304 procedural way:
1305
R. David Murrayf877feb2009-05-05 02:08:52 +00001306 .. doctest::
1307
1308 >>> def turn(x, y):
1309 ... left(180)
1310 ...
1311 >>> onclick(turn) # Now clicking into the turtle will turn it.
1312 >>> onclick(None) # event-binding will be removed
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001313
1314
1315.. function:: onrelease(fun, btn=1, add=None)
1316
1317 :param fun: a function with two arguments which will be called with the
1318 coordinates of the clicked point on the canvas
1319 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1320 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1321 added, otherwise it will replace a former binding
1322
1323 Bind *fun* to mouse-button-release events on this turtle. If *fun* is
1324 ``None``, existing bindings are removed.
1325
R. David Murrayf877feb2009-05-05 02:08:52 +00001326 .. doctest::
1327
1328 >>> class MyTurtle(Turtle):
1329 ... def glow(self,x,y):
1330 ... self.fillcolor("red")
1331 ... def unglow(self,x,y):
1332 ... self.fillcolor("")
1333 ...
1334 >>> turtle = MyTurtle()
1335 >>> turtle.onclick(turtle.glow) # clicking on turtle turns fillcolor red,
1336 >>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent.
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001337
1338
1339.. function:: ondrag(fun, btn=1, add=None)
1340
1341 :param fun: a function with two arguments which will be called with the
1342 coordinates of the clicked point on the canvas
1343 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1344 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1345 added, otherwise it will replace a former binding
1346
1347 Bind *fun* to mouse-move events on this turtle. If *fun* is ``None``,
1348 existing bindings are removed.
1349
1350 Remark: Every sequence of mouse-move-events on a turtle is preceded by a
1351 mouse-click event on that turtle.
1352
R. David Murrayf877feb2009-05-05 02:08:52 +00001353 .. doctest::
1354
1355 >>> turtle.ondrag(turtle.goto)
1356
1357 Subsequently, clicking and dragging the Turtle will move it across
1358 the screen thereby producing handdrawings (if pen is down).
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001359
1360
1361Special Turtle methods
1362----------------------
1363
1364.. function:: begin_poly()
1365
1366 Start recording the vertices of a polygon. Current turtle position is first
1367 vertex of polygon.
1368
1369
1370.. function:: end_poly()
1371
1372 Stop recording the vertices of a polygon. Current turtle position is last
1373 vertex of polygon. This will be connected with the first vertex.
1374
1375
1376.. function:: get_poly()
1377
1378 Return the last recorded polygon.
1379
R. David Murrayf877feb2009-05-05 02:08:52 +00001380 .. doctest::
1381
1382 >>> turtle.home()
1383 >>> turtle.begin_poly()
1384 >>> turtle.fd(100)
1385 >>> turtle.left(20)
1386 >>> turtle.fd(30)
1387 >>> turtle.left(60)
1388 >>> turtle.fd(50)
1389 >>> turtle.end_poly()
1390 >>> p = turtle.get_poly()
1391 >>> register_shape("myFavouriteShape", p)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001392
1393
1394.. function:: clone()
1395
1396 Create and return a clone of the turtle with same position, heading and
1397 turtle properties.
1398
R. David Murrayf877feb2009-05-05 02:08:52 +00001399 .. doctest::
1400
1401 >>> mick = Turtle()
1402 >>> joe = mick.clone()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001403
1404
1405.. function:: getturtle()
R. David Murray7fcd3de2009-06-25 14:26:19 +00001406 getpen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001407
1408 Return the Turtle object itself. Only reasonable use: as a function to
1409 return the "anonymous turtle":
1410
R. David Murrayf877feb2009-05-05 02:08:52 +00001411 .. doctest::
1412
1413 >>> pet = getturtle()
1414 >>> pet.fd(50)
1415 >>> pet
1416 <turtle.Turtle object at 0x...>
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001417
1418
1419.. function:: getscreen()
1420
1421 Return the :class:`TurtleScreen` object the turtle is drawing on.
1422 TurtleScreen methods can then be called for that object.
1423
R. David Murrayf877feb2009-05-05 02:08:52 +00001424 .. doctest::
1425
1426 >>> ts = turtle.getscreen()
1427 >>> ts
1428 <turtle._Screen object at 0x...>
1429 >>> ts.bgcolor("pink")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001430
1431
1432.. function:: setundobuffer(size)
1433
1434 :param size: an integer or ``None``
1435
1436 Set or disable undobuffer. If *size* is an integer an empty undobuffer of
1437 given size is installed. *size* gives the maximum number of turtle actions
1438 that can be undone by the :func:`undo` method/function. If *size* is
1439 ``None``, the undobuffer is disabled.
1440
R. David Murrayf877feb2009-05-05 02:08:52 +00001441 .. doctest::
1442
1443 >>> turtle.setundobuffer(42)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001444
1445
1446.. function:: undobufferentries()
1447
1448 Return number of entries in the undobuffer.
1449
R. David Murrayf877feb2009-05-05 02:08:52 +00001450 .. doctest::
1451
1452 >>> while undobufferentries():
1453 ... undo()
1454
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001455
1456
1457.. _compoundshapes:
1458
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001459Compound shapes
Alexander Belopolsky41f56f02010-10-21 18:15:39 +00001460---------------
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001461
1462To use compound turtle shapes, which consist of several polygons of different
1463color, you must use the helper class :class:`Shape` explicitly as described
1464below:
1465
14661. Create an empty Shape object of type "compound".
14672. Add as many components to this object as desired, using the
1468 :meth:`addcomponent` method.
1469
1470 For example:
1471
R. David Murrayf877feb2009-05-05 02:08:52 +00001472 .. doctest::
1473
1474 >>> s = Shape("compound")
1475 >>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5))
1476 >>> s.addcomponent(poly1, "red", "blue")
1477 >>> poly2 = ((0,0),(10,-5),(-10,-5))
1478 >>> s.addcomponent(poly2, "blue", "red")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001479
14803. Now add the Shape to the Screen's shapelist and use it:
1481
R. David Murrayf877feb2009-05-05 02:08:52 +00001482 .. doctest::
1483
1484 >>> register_shape("myshape", s)
1485 >>> shape("myshape")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001486
1487
1488.. note::
1489
1490 The :class:`Shape` class is used internally by the :func:`register_shape`
1491 method in different ways. The application programmer has to deal with the
1492 Shape class *only* when using compound shapes like shown above!
1493
1494
1495Methods of TurtleScreen/Screen and corresponding functions
1496==========================================================
1497
1498Most of the examples in this section refer to a TurtleScreen instance called
1499``screen``.
1500
R. David Murrayf877feb2009-05-05 02:08:52 +00001501.. doctest::
1502 :hide:
1503
1504 >>> screen = Screen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001505
1506Window control
1507--------------
1508
1509.. function:: bgcolor(*args)
1510
1511 :param args: a color string or three numbers in the range 0..colormode or a
1512 3-tuple of such numbers
1513
Alexander Belopolsky3cdfb122010-10-29 17:16:49 +00001514
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001515 Set or return background color of the TurtleScreen.
1516
R. David Murrayf877feb2009-05-05 02:08:52 +00001517 .. doctest::
1518
1519 >>> screen.bgcolor("orange")
1520 >>> screen.bgcolor()
1521 'orange'
1522 >>> screen.bgcolor("#800080")
1523 >>> screen.bgcolor()
Alexander Belopolskya9615d12010-10-31 00:51:11 +00001524 (128.0, 0.0, 128.0)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001525
1526
1527.. function:: bgpic(picname=None)
1528
1529 :param picname: a string, name of a gif-file or ``"nopic"``, or ``None``
1530
1531 Set background image or return name of current backgroundimage. If *picname*
1532 is a filename, set the corresponding image as background. If *picname* is
1533 ``"nopic"``, delete background image, if present. If *picname* is ``None``,
R. David Murrayf877feb2009-05-05 02:08:52 +00001534 return the filename of the current backgroundimage. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001535
R. David Murrayf877feb2009-05-05 02:08:52 +00001536 >>> screen.bgpic()
1537 'nopic'
1538 >>> screen.bgpic("landscape.gif")
1539 >>> screen.bgpic()
1540 "landscape.gif"
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001541
1542
1543.. function:: clear()
1544 clearscreen()
1545
1546 Delete all drawings and all turtles from the TurtleScreen. Reset the now
1547 empty TurtleScreen to its initial state: white background, no background
1548 image, no event bindings and tracing on.
1549
1550 .. note::
1551 This TurtleScreen method is available as a global function only under the
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001552 name ``clearscreen``. The global function ``clear`` is a different one
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001553 derived from the Turtle method ``clear``.
1554
1555
1556.. function:: reset()
1557 resetscreen()
1558
1559 Reset all Turtles on the Screen to their initial state.
1560
1561 .. note::
1562 This TurtleScreen method is available as a global function only under the
1563 name ``resetscreen``. The global function ``reset`` is another one
1564 derived from the Turtle method ``reset``.
1565
1566
1567.. function:: screensize(canvwidth=None, canvheight=None, bg=None)
1568
Georg Brandlff2ad0e2009-04-27 16:51:45 +00001569 :param canvwidth: positive integer, new width of canvas in pixels
1570 :param canvheight: positive integer, new height of canvas in pixels
1571 :param bg: colorstring or color-tuple, new background color
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001572
1573 If no arguments are given, return current (canvaswidth, canvasheight). Else
1574 resize the canvas the turtles are drawing on. Do not alter the drawing
1575 window. To observe hidden parts of the canvas, use the scrollbars. With this
1576 method, one can make visible those parts of a drawing which were outside the
1577 canvas before.
1578
R. David Murrayf877feb2009-05-05 02:08:52 +00001579 >>> screen.screensize()
1580 (400, 300)
1581 >>> screen.screensize(2000,1500)
1582 >>> screen.screensize()
1583 (2000, 1500)
1584
1585 e.g. to search for an erroneously escaped turtle ;-)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001586
1587
1588.. function:: setworldcoordinates(llx, lly, urx, ury)
1589
1590 :param llx: a number, x-coordinate of lower left corner of canvas
1591 :param lly: a number, y-coordinate of lower left corner of canvas
1592 :param urx: a number, x-coordinate of upper right corner of canvas
1593 :param ury: a number, y-coordinate of upper right corner of canvas
1594
1595 Set up user-defined coordinate system and switch to mode "world" if
1596 necessary. This performs a ``screen.reset()``. If mode "world" is already
1597 active, all drawings are redrawn according to the new coordinates.
1598
1599 **ATTENTION**: in user-defined coordinate systems angles may appear
1600 distorted.
1601
R. David Murrayf877feb2009-05-05 02:08:52 +00001602 .. doctest::
1603
1604 >>> screen.reset()
1605 >>> screen.setworldcoordinates(-50,-7.5,50,7.5)
1606 >>> for _ in range(72):
1607 ... left(10)
1608 ...
1609 >>> for _ in range(8):
1610 ... left(45); fd(2) # a regular octagon
1611
1612 .. doctest::
1613 :hide:
1614
1615 >>> screen.reset()
1616 >>> for t in turtles():
1617 ... t.reset()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001618
1619
1620Animation control
1621-----------------
1622
1623.. function:: delay(delay=None)
1624
1625 :param delay: positive integer
1626
1627 Set or return the drawing *delay* in milliseconds. (This is approximately
Georg Brandl2ee470f2008-07-16 12:55:28 +00001628 the time interval between two consecutive canvas updates.) The longer the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001629 drawing delay, the slower the animation.
1630
1631 Optional argument:
1632
R. David Murrayf877feb2009-05-05 02:08:52 +00001633 .. doctest::
1634
1635 >>> screen.delay()
1636 10
1637 >>> screen.delay(5)
1638 >>> screen.delay()
1639 5
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001640
1641
1642.. function:: tracer(n=None, delay=None)
1643
1644 :param n: nonnegative integer
1645 :param delay: nonnegative integer
1646
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001647 Turn turtle animation on/off and set delay for update drawings. If
1648 *n* is given, only each n-th regular screen update is really
1649 performed. (Can be used to accelerate the drawing of complex
1650 graphics.) When called without arguments, returns the currently
1651 stored value of n. Second argument sets delay value (see
1652 :func:`delay`).
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001653
R. David Murrayf877feb2009-05-05 02:08:52 +00001654 .. doctest::
1655
1656 >>> screen.tracer(8, 25)
1657 >>> dist = 2
1658 >>> for i in range(200):
1659 ... fd(dist)
1660 ... rt(90)
1661 ... dist += 2
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001662
1663
1664.. function:: update()
1665
1666 Perform a TurtleScreen update. To be used when tracer is turned off.
1667
1668See also the RawTurtle/Turtle method :func:`speed`.
1669
1670
1671Using screen events
1672-------------------
1673
1674.. function:: listen(xdummy=None, ydummy=None)
1675
1676 Set focus on TurtleScreen (in order to collect key-events). Dummy arguments
1677 are provided in order to be able to pass :func:`listen` to the onclick method.
1678
1679
1680.. function:: onkey(fun, key)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001681 onkeyrelease(fun, key)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001682
1683 :param fun: a function with no arguments or ``None``
1684 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1685
1686 Bind *fun* to key-release event of key. If *fun* is ``None``, event bindings
1687 are removed. Remark: in order to be able to register key-events, TurtleScreen
1688 must have the focus. (See method :func:`listen`.)
1689
R. David Murrayf877feb2009-05-05 02:08:52 +00001690 .. doctest::
1691
1692 >>> def f():
1693 ... fd(50)
1694 ... lt(60)
1695 ...
1696 >>> screen.onkey(f, "Up")
1697 >>> screen.listen()
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001698
1699
R. David Murray7fcd3de2009-06-25 14:26:19 +00001700.. function:: onkeypress(fun, key=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001701
1702 :param fun: a function with no arguments or ``None``
1703 :param key: a string: key (e.g. "a") or key-symbol (e.g. "space")
1704
1705 Bind *fun* to key-press event of key if key is given,
1706 or to any key-press-event if no key is given.
1707 Remark: in order to be able to register key-events, TurtleScreen
1708 must have focus. (See method :func:`listen`.)
1709
1710 .. doctest::
1711
1712 >>> def f():
1713 ... fd(50)
1714 ...
1715 >>> screen.onkey(f, "Up")
1716 >>> screen.listen()
1717
1718
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001719.. function:: onclick(fun, btn=1, add=None)
1720 onscreenclick(fun, btn=1, add=None)
1721
1722 :param fun: a function with two arguments which will be called with the
1723 coordinates of the clicked point on the canvas
1724 :param num: number of the mouse-button, defaults to 1 (left mouse button)
1725 :param add: ``True`` or ``False`` -- if ``True``, a new binding will be
1726 added, otherwise it will replace a former binding
1727
1728 Bind *fun* to mouse-click events on this screen. If *fun* is ``None``,
1729 existing bindings are removed.
1730
1731 Example for a TurtleScreen instance named ``screen`` and a Turtle instance
1732 named turtle:
1733
R. David Murrayf877feb2009-05-05 02:08:52 +00001734 .. doctest::
1735
1736 >>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will
1737 >>> # make the turtle move to the clicked point.
1738 >>> screen.onclick(None) # remove event binding again
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001739
1740 .. note::
1741 This TurtleScreen method is available as a global function only under the
1742 name ``onscreenclick``. The global function ``onclick`` is another one
1743 derived from the Turtle method ``onclick``.
1744
1745
1746.. function:: ontimer(fun, t=0)
1747
1748 :param fun: a function with no arguments
1749 :param t: a number >= 0
1750
1751 Install a timer that calls *fun* after *t* milliseconds.
1752
R. David Murrayf877feb2009-05-05 02:08:52 +00001753 .. doctest::
1754
1755 >>> running = True
1756 >>> def f():
1757 ... if running:
1758 ... fd(50)
1759 ... lt(60)
1760 ... screen.ontimer(f, 250)
1761 >>> f() ### makes the turtle march around
1762 >>> running = False
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001763
1764
Georg Brandleaa84ef2009-05-05 08:14:33 +00001765.. function:: mainloop()
1766
1767 Starts event loop - calling Tkinter's mainloop function.
1768 Must be the last statement in a turtle graphics program.
1769 Must *not* be used if a script is run from within IDLE in -n mode
1770 (No subprocess) - for interactive use of turtle graphics. ::
1771
1772 >>> screen.mainloop()
1773
1774
1775Input methods
1776-------------
1777
1778.. function:: textinput(title, prompt)
1779
1780 :param title: string
1781 :param prompt: string
1782
1783 Pop up a dialog window for input of a string. Parameter title is
1784 the title of the dialog window, propmt is a text mostly describing
1785 what information to input.
1786 Return the string input. If the dialog is canceled, return None. ::
1787
1788 >>> screen.textinput("NIM", "Name of first player:")
1789
1790
R. David Murray7fcd3de2009-06-25 14:26:19 +00001791.. function:: numinput(title, prompt, default=None, minval=None, maxval=None)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001792
1793 :param title: string
1794 :param prompt: string
1795 :param default: number (optional)
Alexander Belopolsky435d3062010-10-19 21:07:52 +00001796 :param minval: number (optional)
1797 :param maxval: number (optional)
Georg Brandleaa84ef2009-05-05 08:14:33 +00001798
1799 Pop up a dialog window for input of a number. title is the title of the
1800 dialog window, prompt is a text mostly describing what numerical information
1801 to input. default: default value, minval: minimum value for imput,
1802 maxval: maximum value for input
1803 The number input must be in the range minval .. maxval if these are
1804 given. If not, a hint is issued and the dialog remains open for
1805 correction.
1806 Return the number input. If the dialog is canceled, return None. ::
1807
1808 >>> screen.numinput("Poker", "Your stakes:", 1000, minval=10, maxval=10000)
1809
1810
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001811Settings and special methods
1812----------------------------
1813
1814.. function:: mode(mode=None)
1815
1816 :param mode: one of the strings "standard", "logo" or "world"
1817
1818 Set turtle mode ("standard", "logo" or "world") and perform reset. If mode
1819 is not given, current mode is returned.
1820
1821 Mode "standard" is compatible with old :mod:`turtle`. Mode "logo" is
1822 compatible with most Logo turtle graphics. Mode "world" uses user-defined
1823 "world coordinates". **Attention**: in this mode angles appear distorted if
1824 ``x/y`` unit-ratio doesn't equal 1.
1825
1826 ============ ========================= ===================
1827 Mode Initial turtle heading positive angles
1828 ============ ========================= ===================
1829 "standard" to the right (east) counterclockwise
1830 "logo" upward (north) clockwise
1831 ============ ========================= ===================
1832
R. David Murrayf877feb2009-05-05 02:08:52 +00001833 .. doctest::
1834
1835 >>> mode("logo") # resets turtle heading to north
1836 >>> mode()
1837 'logo'
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001838
1839
1840.. function:: colormode(cmode=None)
1841
1842 :param cmode: one of the values 1.0 or 255
1843
1844 Return the colormode or set it to 1.0 or 255. Subsequently *r*, *g*, *b*
1845 values of color triples have to be in the range 0..\ *cmode*.
1846
R. David Murrayf877feb2009-05-05 02:08:52 +00001847 .. doctest::
1848
1849 >>> screen.colormode(1)
1850 >>> turtle.pencolor(240, 160, 80)
1851 Traceback (most recent call last):
1852 ...
1853 TurtleGraphicsError: bad color sequence: (240, 160, 80)
1854 >>> screen.colormode()
1855 1.0
1856 >>> screen.colormode(255)
1857 >>> screen.colormode()
1858 255
1859 >>> turtle.pencolor(240,160,80)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001860
1861
1862.. function:: getcanvas()
1863
1864 Return the Canvas of this TurtleScreen. Useful for insiders who know what to
1865 do with a Tkinter Canvas.
1866
R. David Murrayf877feb2009-05-05 02:08:52 +00001867 .. doctest::
1868
1869 >>> cv = screen.getcanvas()
1870 >>> cv
1871 <turtle.ScrolledCanvas instance at 0x...>
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001872
1873
1874.. function:: getshapes()
1875
1876 Return a list of names of all currently available turtle shapes.
1877
R. David Murrayf877feb2009-05-05 02:08:52 +00001878 .. doctest::
1879
1880 >>> screen.getshapes()
1881 ['arrow', 'blank', 'circle', ..., 'turtle']
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001882
1883
1884.. function:: register_shape(name, shape=None)
1885 addshape(name, shape=None)
1886
1887 There are three different ways to call this function:
1888
1889 (1) *name* is the name of a gif-file and *shape* is ``None``: Install the
R. David Murrayf877feb2009-05-05 02:08:52 +00001890 corresponding image shape. ::
1891
1892 >>> screen.register_shape("turtle.gif")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001893
1894 .. note::
1895 Image shapes *do not* rotate when turning the turtle, so they do not
1896 display the heading of the turtle!
1897
1898 (2) *name* is an arbitrary string and *shape* is a tuple of pairs of
1899 coordinates: Install the corresponding polygon shape.
1900
R. David Murrayf877feb2009-05-05 02:08:52 +00001901 .. doctest::
1902
1903 >>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))
1904
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001905 (3) *name* is an arbitrary string and shape is a (compound) :class:`Shape`
1906 object: Install the corresponding compound shape.
1907
1908 Add a turtle shape to TurtleScreen's shapelist. Only thusly registered
1909 shapes can be used by issuing the command ``shape(shapename)``.
1910
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001911
1912.. function:: turtles()
1913
1914 Return the list of turtles on the screen.
1915
R. David Murrayf877feb2009-05-05 02:08:52 +00001916 .. doctest::
1917
1918 >>> for turtle in screen.turtles():
1919 ... turtle.color("red")
Georg Brandl116aa622007-08-15 14:28:22 +00001920
Georg Brandl116aa622007-08-15 14:28:22 +00001921
1922.. function:: window_height()
1923
R. David Murrayf877feb2009-05-05 02:08:52 +00001924 Return the height of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001925
R. David Murrayf877feb2009-05-05 02:08:52 +00001926 >>> screen.window_height()
1927 480
Georg Brandl116aa622007-08-15 14:28:22 +00001928
Georg Brandl116aa622007-08-15 14:28:22 +00001929
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001930.. function:: window_width()
1931
R. David Murrayf877feb2009-05-05 02:08:52 +00001932 Return the width of the turtle window. ::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001933
R. David Murrayf877feb2009-05-05 02:08:52 +00001934 >>> screen.window_width()
1935 640
Georg Brandl116aa622007-08-15 14:28:22 +00001936
1937
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001938.. _screenspecific:
Georg Brandl116aa622007-08-15 14:28:22 +00001939
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001940Methods specific to Screen, not inherited from TurtleScreen
1941-----------------------------------------------------------
1942
1943.. function:: bye()
1944
1945 Shut the turtlegraphics window.
Georg Brandl116aa622007-08-15 14:28:22 +00001946
1947
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001948.. function:: exitonclick()
Georg Brandl116aa622007-08-15 14:28:22 +00001949
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001950 Bind bye() method to mouse clicks on the Screen.
Georg Brandl116aa622007-08-15 14:28:22 +00001951
1952
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001953 If the value "using_IDLE" in the configuration dictionary is ``False``
1954 (default value), also enter mainloop. Remark: If IDLE with the ``-n`` switch
1955 (no subprocess) is used, this value should be set to ``True`` in
1956 :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the
1957 client script.
Georg Brandl116aa622007-08-15 14:28:22 +00001958
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001959
1960.. function:: setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"])
1961
1962 Set the size and position of the main window. Default values of arguments
Georg Brandl6faee4e2010-09-21 14:48:28 +00001963 are stored in the configuration dictionary and can be changed via a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001964 :file:`turtle.cfg` file.
1965
1966 :param width: if an integer, a size in pixels, if a float, a fraction of the
1967 screen; default is 50% of screen
1968 :param height: if an integer, the height in pixels, if a float, a fraction of
1969 the screen; default is 75% of screen
1970 :param startx: if positive, starting position in pixels from the left
1971 edge of the screen, if negative from the right edge, if None,
1972 center window horizontally
1973 :param startx: if positive, starting position in pixels from the top
1974 edge of the screen, if negative from the bottom edge, if None,
1975 center window vertically
1976
R. David Murrayf877feb2009-05-05 02:08:52 +00001977 .. doctest::
1978
1979 >>> screen.setup (width=200, height=200, startx=0, starty=0)
1980 >>> # sets window to 200x200 pixels, in upper left of screen
1981 >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
1982 >>> # sets window to 75% of screen by 50% of screen and centers
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001983
1984
1985.. function:: title(titlestring)
1986
1987 :param titlestring: a string that is shown in the titlebar of the turtle
1988 graphics window
1989
1990 Set title of turtle window to *titlestring*.
1991
R. David Murrayf877feb2009-05-05 02:08:52 +00001992 .. doctest::
1993
1994 >>> screen.title("Welcome to the turtle zoo!")
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001995
1996
Alexander Belopolsky65095992010-11-01 15:45:34 +00001997Public classes
1998==============
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00001999
2000
2001.. class:: RawTurtle(canvas)
2002 RawPen(canvas)
2003
Ezio Melotti1a263ad2010-03-14 09:51:37 +00002004 :param canvas: a :class:`tkinter.Canvas`, a :class:`ScrolledCanvas` or a
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002005 :class:`TurtleScreen`
2006
R. David Murrayf877feb2009-05-05 02:08:52 +00002007 Create a turtle. The turtle has all methods described above as "methods of
2008 Turtle/RawTurtle".
Georg Brandl116aa622007-08-15 14:28:22 +00002009
2010
2011.. class:: Turtle()
2012
R. David Murrayf877feb2009-05-05 02:08:52 +00002013 Subclass of RawTurtle, has the same interface but draws on a default
2014 :class:`Screen` object created automatically when needed for the first time.
Georg Brandl116aa622007-08-15 14:28:22 +00002015
2016
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002017.. class:: TurtleScreen(cv)
Georg Brandl116aa622007-08-15 14:28:22 +00002018
Ezio Melotti1a263ad2010-03-14 09:51:37 +00002019 :param cv: a :class:`tkinter.Canvas`
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002020
2021 Provides screen oriented methods like :func:`setbg` etc. that are described
2022 above.
2023
2024.. class:: Screen()
2025
2026 Subclass of TurtleScreen, with :ref:`four methods added <screenspecific>`.
2027
Georg Brandl48310cd2009-01-03 21:18:54 +00002028
Benjamin Petersona0dfa822009-11-13 02:25:08 +00002029.. class:: ScrolledCanvas(master)
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002030
2031 :param master: some Tkinter widget to contain the ScrolledCanvas, i.e.
2032 a Tkinter-canvas with scrollbars added
2033
2034 Used by class Screen, which thus automatically provides a ScrolledCanvas as
2035 playground for the turtles.
2036
2037.. class:: Shape(type_, data)
2038
2039 :param type\_: one of the strings "polygon", "image", "compound"
2040
2041 Data structure modeling shapes. The pair ``(type_, data)`` must follow this
2042 specification:
Georg Brandl116aa622007-08-15 14:28:22 +00002043
2044
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002045 =========== ===========
2046 *type_* *data*
2047 =========== ===========
2048 "polygon" a polygon-tuple, i.e. a tuple of pairs of coordinates
2049 "image" an image (in this form only used internally!)
Georg Brandlae2dbe22009-03-13 19:04:40 +00002050 "compound" ``None`` (a compound shape has to be constructed using the
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002051 :meth:`addcomponent` method)
2052 =========== ===========
Georg Brandl48310cd2009-01-03 21:18:54 +00002053
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002054 .. method:: addcomponent(poly, fill, outline=None)
Georg Brandl116aa622007-08-15 14:28:22 +00002055
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002056 :param poly: a polygon, i.e. a tuple of pairs of numbers
2057 :param fill: a color the *poly* will be filled with
2058 :param outline: a color for the poly's outline (if given)
Georg Brandl48310cd2009-01-03 21:18:54 +00002059
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002060 Example:
Georg Brandl116aa622007-08-15 14:28:22 +00002061
R. David Murrayf877feb2009-05-05 02:08:52 +00002062 .. doctest::
2063
2064 >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
2065 >>> s = Shape("compound")
2066 >>> s.addcomponent(poly, "red", "blue")
2067 >>> # ... add more components and then use register_shape()
Georg Brandl116aa622007-08-15 14:28:22 +00002068
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002069 See :ref:`compoundshapes`.
Georg Brandl116aa622007-08-15 14:28:22 +00002070
2071
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002072.. class:: Vec2D(x, y)
Georg Brandl116aa622007-08-15 14:28:22 +00002073
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002074 A two-dimensional vector class, used as a helper class for implementing
2075 turtle graphics. May be useful for turtle graphics programs too. Derived
2076 from tuple, so a vector is a tuple!
2077
2078 Provides (for *a*, *b* vectors, *k* number):
2079
2080 * ``a + b`` vector addition
2081 * ``a - b`` vector subtraction
2082 * ``a * b`` inner product
2083 * ``k * a`` and ``a * k`` multiplication with scalar
2084 * ``abs(a)`` absolute value of a
2085 * ``a.rotate(angle)`` rotation
2086
2087
2088Help and configuration
2089======================
2090
2091How to use help
2092---------------
2093
2094The public methods of the Screen and Turtle classes are documented extensively
2095via docstrings. So these can be used as online-help via the Python help
2096facilities:
2097
2098- When using IDLE, tooltips show the signatures and first lines of the
2099 docstrings of typed in function-/method calls.
2100
2101- Calling :func:`help` on methods or functions displays the docstrings::
2102
2103 >>> help(Screen.bgcolor)
2104 Help on method bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002105
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002106 bgcolor(self, *args) unbound turtle.Screen method
2107 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00002108
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002109 Arguments (if given): a color string or three numbers
2110 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00002111
2112
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002113 >>> screen.bgcolor("orange")
2114 >>> screen.bgcolor()
2115 "orange"
2116 >>> screen.bgcolor(0.5,0,0.5)
2117 >>> screen.bgcolor()
2118 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00002119
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002120 >>> help(Turtle.penup)
2121 Help on method penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002122
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002123 penup(self) unbound turtle.Turtle method
2124 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00002125
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002126 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00002127
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002128 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00002129
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002130 >>> turtle.penup()
2131
2132- The docstrings of the functions which are derived from methods have a modified
2133 form::
2134
2135 >>> help(bgcolor)
2136 Help on function bgcolor in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002137
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002138 bgcolor(*args)
2139 Set or return backgroundcolor of the TurtleScreen.
Georg Brandl48310cd2009-01-03 21:18:54 +00002140
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002141 Arguments (if given): a color string or three numbers
2142 in the range 0..colormode or a 3-tuple of such numbers.
Georg Brandl48310cd2009-01-03 21:18:54 +00002143
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002144 Example::
Georg Brandl48310cd2009-01-03 21:18:54 +00002145
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002146 >>> bgcolor("orange")
2147 >>> bgcolor()
2148 "orange"
2149 >>> bgcolor(0.5,0,0.5)
2150 >>> bgcolor()
2151 "#800080"
Georg Brandl48310cd2009-01-03 21:18:54 +00002152
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002153 >>> help(penup)
2154 Help on function penup in module turtle:
Georg Brandl48310cd2009-01-03 21:18:54 +00002155
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002156 penup()
2157 Pull the pen up -- no drawing when moving.
Georg Brandl48310cd2009-01-03 21:18:54 +00002158
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002159 Aliases: penup | pu | up
Georg Brandl48310cd2009-01-03 21:18:54 +00002160
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002161 No argument
Georg Brandl48310cd2009-01-03 21:18:54 +00002162
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002163 Example:
2164 >>> penup()
2165
2166These modified docstrings are created automatically together with the function
2167definitions that are derived from the methods at import time.
2168
2169
2170Translation of docstrings into different languages
2171--------------------------------------------------
2172
2173There is a utility to create a dictionary the keys of which are the method names
2174and the values of which are the docstrings of the public methods of the classes
2175Screen and Turtle.
2176
2177.. function:: write_docstringdict(filename="turtle_docstringdict")
2178
2179 :param filename: a string, used as filename
2180
2181 Create and write docstring-dictionary to a Python script with the given
2182 filename. This function has to be called explicitly (it is not used by the
2183 turtle graphics classes). The docstring dictionary will be written to the
2184 Python script :file:`{filename}.py`. It is intended to serve as a template
2185 for translation of the docstrings into different languages.
2186
2187If you (or your students) want to use :mod:`turtle` with online help in your
2188native language, you have to translate the docstrings and save the resulting
2189file as e.g. :file:`turtle_docstringdict_german.py`.
2190
2191If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary
2192will be read in at import time and will replace the original English docstrings.
2193
2194At the time of this writing there are docstring dictionaries in German and in
2195Italian. (Requests please to glingl@aon.at.)
2196
2197
2198
2199How to configure Screen and Turtles
2200-----------------------------------
2201
2202The built-in default configuration mimics the appearance and behaviour of the
2203old turtle module in order to retain best possible compatibility with it.
2204
2205If you want to use a different configuration which better reflects the features
2206of this module or which better fits to your needs, e.g. for use in a classroom,
2207you can prepare a configuration file ``turtle.cfg`` which will be read at import
2208time and modify the configuration according to its settings.
2209
2210The built in configuration would correspond to the following turtle.cfg::
2211
2212 width = 0.5
2213 height = 0.75
2214 leftright = None
2215 topbottom = None
2216 canvwidth = 400
2217 canvheight = 300
2218 mode = standard
2219 colormode = 1.0
2220 delay = 10
2221 undobuffersize = 1000
2222 shape = classic
2223 pencolor = black
2224 fillcolor = black
2225 resizemode = noresize
2226 visible = True
2227 language = english
2228 exampleturtle = turtle
2229 examplescreen = screen
2230 title = Python Turtle Graphics
2231 using_IDLE = False
2232
2233Short explanation of selected entries:
2234
2235- The first four lines correspond to the arguments of the :meth:`Screen.setup`
2236 method.
2237- Line 5 and 6 correspond to the arguments of the method
2238 :meth:`Screen.screensize`.
2239- *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more
2240 info try ``help(shape)``.
2241- If you want to use no fillcolor (i.e. make the turtle transparent), you have
2242 to write ``fillcolor = ""`` (but all nonempty strings must not have quotes in
2243 the cfg-file).
2244- If you want to reflect the turtle its state, you have to use ``resizemode =
2245 auto``.
2246- If you set e.g. ``language = italian`` the docstringdict
2247 :file:`turtle_docstringdict_italian.py` will be loaded at import time (if
2248 present on the import path, e.g. in the same directory as :mod:`turtle`.
2249- The entries *exampleturtle* and *examplescreen* define the names of these
2250 objects as they occur in the docstrings. The transformation of
2251 method-docstrings to function-docstrings will delete these names from the
2252 docstrings.
2253- *using_IDLE*: Set this to ``True`` if you regularly work with IDLE and its -n
2254 switch ("no subprocess"). This will prevent :func:`exitonclick` to enter the
2255 mainloop.
2256
2257There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is
2258stored and an additional one in the current working directory. The latter will
2259override the settings of the first one.
2260
2261The :file:`Demo/turtle` directory contains a :file:`turtle.cfg` file. You can
2262study it as an example and see its effects when running the demos (preferably
2263not from within the demo-viewer).
2264
2265
2266Demo scripts
2267============
2268
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002269There is a set of demo scripts in the :mod:`turtledemo` package. These
2270scripts can be run and viewed using the supplied demo viewer as follows::
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002271
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002272 python -m turtledemo
2273
2274Alternatively, you can run the demo scripts individually. For example,
2275
2276::
2277 python -m turtledemo.bytedesign
2278
2279The :mod:`turtledemo` package directory contains:
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002280
Georg Brandlae2dbe22009-03-13 19:04:40 +00002281- a set of 15 demo scripts demonstrating different features of the new module
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002282 :mod:`turtle`;
2283- a demo viewer :file:`__main__.py` which can be used to view the sourcecode
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002284 of the scripts and run them at the same time. 14 of the examples can be
2285 accessed via the Examples menu; all of them can also be run standalone.
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002286- The example :mod:`turtledemo.two_canvases` demonstrates the simultaneous
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002287 use of two canvases with the turtle module. Therefore it only can be run
2288 standalone.
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002289- There is a :file:`turtle.cfg` file in this directory, which serves as an
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002290 example for how to write and use such files.
2291
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002292The demo scripts are:
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002293
2294+----------------+------------------------------+-----------------------+
2295| Name | Description | Features |
2296+----------------+------------------------------+-----------------------+
2297| bytedesign | complex classical | :func:`tracer`, delay,|
Alexander Belopolskyea13d9d2010-11-01 17:39:37 +00002298| | turtle graphics pattern | :func:`update` |
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002299+----------------+------------------------------+-----------------------+
2300| chaos | graphs verhust dynamics, | world coordinates |
2301| | proves that you must not | |
2302| | trust computers' computations| |
2303+----------------+------------------------------+-----------------------+
2304| clock | analog clock showing time | turtles as clock's |
2305| | of your computer | hands, ontimer |
2306+----------------+------------------------------+-----------------------+
2307| colormixer | experiment with r, g, b | :func:`ondrag` |
2308+----------------+------------------------------+-----------------------+
2309| fractalcurves | Hilbert & Koch curves | recursion |
2310+----------------+------------------------------+-----------------------+
2311| lindenmayer | ethnomathematics | L-System |
2312| | (indian kolams) | |
2313+----------------+------------------------------+-----------------------+
2314| minimal_hanoi | Towers of Hanoi | Rectangular Turtles |
2315| | | as Hanoi discs |
2316| | | (shape, shapesize) |
2317+----------------+------------------------------+-----------------------+
Georg Brandleaa84ef2009-05-05 08:14:33 +00002318| nim | play the classical nim game | turtles as nimsticks, |
2319| | with three heaps of sticks | event driven (mouse, |
2320| | against the computer. | keyboard) |
2321+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002322| paint | super minimalistic | :func:`onclick` |
2323| | drawing program | |
2324+----------------+------------------------------+-----------------------+
2325| peace | elementary | turtle: appearance |
2326| | | and animation |
2327+----------------+------------------------------+-----------------------+
2328| penrose | aperiodic tiling with | :func:`stamp` |
2329| | kites and darts | |
2330+----------------+------------------------------+-----------------------+
2331| planet_and_moon| simulation of | compound shapes, |
2332| | gravitational system | :class:`Vec2D` |
2333+----------------+------------------------------+-----------------------+
Georg Brandleaa84ef2009-05-05 08:14:33 +00002334| round_dance | dancing turtles rotating | compound shapes, clone|
2335| | pairwise in opposite | shapesize, tilt, |
Alexander Belopolskyc08f5442010-10-21 22:29:36 +00002336| | direction | get_shapepoly, update |
Georg Brandleaa84ef2009-05-05 08:14:33 +00002337+----------------+------------------------------+-----------------------+
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002338| tree | a (graphical) breadth | :func:`clone` |
2339| | first tree (using generators)| |
2340+----------------+------------------------------+-----------------------+
2341| wikipedia | a pattern from the wikipedia | :func:`clone`, |
2342| | article on turtle graphics | :func:`undo` |
2343+----------------+------------------------------+-----------------------+
2344| yingyang | another elementary example | :func:`circle` |
2345+----------------+------------------------------+-----------------------+
2346
2347Have fun!
2348
2349
2350Changes since Python 2.6
2351========================
2352
Georg Brandl48310cd2009-01-03 21:18:54 +00002353- The methods :meth:`Turtle.tracer`, :meth:`Turtle.window_width` and
2354 :meth:`Turtle.window_height` have been eliminated.
2355 Methods with these names and functionality are now available only
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002356 as methods of :class:`Screen`. The functions derived from these remain
Georg Brandl48310cd2009-01-03 21:18:54 +00002357 available. (In fact already in Python 2.6 these methods were merely
2358 duplications of the corresponding
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002359 :class:`TurtleScreen`/:class:`Screen`-methods.)
2360
Georg Brandl48310cd2009-01-03 21:18:54 +00002361- The method :meth:`Turtle.fill` has been eliminated.
2362 The behaviour of :meth:`begin_fill` and :meth:`end_fill`
2363 have changed slightly: now every filling-process must be completed with an
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002364 ``end_fill()`` call.
Georg Brandl48310cd2009-01-03 21:18:54 +00002365
Martin v. Löwis97cf99f2008-06-10 04:44:07 +00002366- A method :meth:`Turtle.filling` has been added. It returns a boolean
2367 value: ``True`` if a filling process is under way, ``False`` otherwise.
2368 This behaviour corresponds to a ``fill()`` call without arguments in
Georg Brandl23d11d32008-09-21 07:50:52 +00002369 Python 2.6.
Georg Brandl116aa622007-08-15 14:28:22 +00002370
Georg Brandleaa84ef2009-05-05 08:14:33 +00002371Changes since Python 3.0
2372========================
2373
2374- The methods :meth:`Turtle.shearfactor`, :meth:`Turtle.shapetransform` and
2375 :meth:`Turtle.get_shapepoly` have been added. Thus the full range of
2376 regular linear transforms is now available for transforming turtle shapes.
2377 :meth:`Turtle.tiltangle` has been enhanced in functionality: it now can
2378 be used to get or set the tiltangle. :meth:`Turtle.settiltangle` has been
2379 deprecated.
2380
2381- The method :meth:`Screen.onkeypress` has been added as a complement to
2382 :meth:`Screen.onkey` which in fact binds actions to the keyrelease event.
2383 Accordingly the latter has got an alias: :meth:`Screen.onkeyrelease`.
2384
2385- The method :meth:`Screen.mainloop` has been added. So when working only
2386 with Screen and Turtle objects one must not additonally import
2387 :func:`mainloop` anymore.
2388
2389- Two input methods has been added :meth:`Screen.textinput` and
2390 :meth:`Screen.numinput`. These popup input dialogs and return
2391 strings and numbers respectively.
2392
2393- Two example scripts :file:`tdemo_nim.py` and :file:`tdemo_round_dance.py`
2394 have been added to the Demo directory (source distribution only). As usual
2395 they can be viewed and executed within the demo viewer :file:`turtleDemo.py`.
2396
R. David Murrayf877feb2009-05-05 02:08:52 +00002397
2398.. doctest::
2399 :hide:
2400
2401 >>> for turtle in turtles():
2402 ... turtle.reset()
2403 >>> turtle.penup()
2404 >>> turtle.goto(-200,25)
2405 >>> turtle.pendown()
2406 >>> turtle.write("No one expects the Spanish Inquisition!",
2407 ... font=("Arial", 20, "normal"))
2408 >>> turtle.penup()
2409 >>> turtle.goto(-100,-50)
2410 >>> turtle.pendown()
2411 >>> turtle.write("Our two chief Turtles are...",
2412 ... font=("Arial", 16, "normal"))
2413 >>> turtle.penup()
2414 >>> turtle.goto(-450,-75)
2415 >>> turtle.write(str(turtles()))