|
17 | 17 |
|
18 | 18 | -------------- |
19 | 19 |
|
20 | | -Introduction |
21 | | -============ |
22 | | - |
23 | | -Turtle graphics is an implementation of `the popular geometric drawing tools |
24 | | -introduced in Logo <https://en.wikipedia.org/wiki/Turtle_ |
25 | | -(robot)>`_, developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon |
26 | | -in 1967. |
27 | | - |
28 | | - |
29 | | -Get started |
30 | | -=========== |
31 | | - |
32 | | -Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle``, give it the |
33 | | -command ``turtle.forward(15)``, and it moves (on-screen!) 15 pixels in the |
34 | | -direction it is facing, drawing a line as it moves. Give it the command |
35 | | -``turtle.right(25)``, and it rotates in-place 25 degrees clockwise. |
36 | | - |
37 | 20 | .. sidebar:: Turtle star |
38 | 21 |
|
39 | 22 | Turtle can draw intricate shapes using programs that repeat simple |
@@ -42,21 +25,40 @@ direction it is facing, drawing a line as it moves. Give it the command |
42 | 25 | .. image:: turtle-star.* |
43 | 26 | :align: center |
44 | 27 |
|
45 | | -In Python, turtle graphics provides a representation of a physical "turtle" |
46 | | -(a little robot with a pen) that draws on a sheet of paper on the floor. |
| 28 | +Imagine a robotic turtle starting at (0, 0) in the x-y plane. |
| 29 | +After an ``import turtle``, give it the command ``turtle.forward(15)``, and it |
| 30 | +moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as |
| 31 | +it moves. Give it the command ``turtle.right(25)``, and it rotates in-place 25 |
| 32 | +degrees clockwise. |
| 33 | + |
| 34 | +Turtle graphics is an implementation of `the drawing tools introduced in Logo |
| 35 | +<https://en.wikipedia.org/wiki/Turtle_(robot)>`_ in 1967. It was created as an |
| 36 | +educational tool, and its instant, visible feedback makes it an effective way |
| 37 | +for learners to encounter programming concepts. It is also a convenient way to |
| 38 | +produce simple graphical output without bringing in external libraries. |
| 39 | + |
| 40 | +This document includes four main sections: |
47 | 41 |
|
48 | | -It's an effective and well-proven way for learners to encounter |
49 | | -programming concepts and interaction with software, as it provides instant, |
50 | | -visible feedback. It also provides convenient access to graphical output |
51 | | -in general. |
| 42 | +* :ref:`turtle-tutorial` teaches the basics of turtle drawing. |
| 43 | +* :ref:`turtle-reference` describes the functions, methods and classes this |
| 44 | + module defines. |
| 45 | +* :ref:`turtle-howtos` details how to handle specific tasks. |
| 46 | +* :ref:`turtle-explanation` provides background on the object-oriented |
| 47 | + interface. |
| 48 | + |
| 49 | +.. note:: |
52 | 50 |
|
53 | | -Turtle drawing was originally created as an educational tool, to be used by |
54 | | -teachers in the classroom. For the programmer who needs to produce some |
55 | | -graphical output it can be a way to do that without the overhead of |
56 | | -introducing more complex or external libraries into their work. |
| 51 | + Turtle graphics requires the :mod:`tkinter` optional module. |
| 52 | + The python.org installers for Windows and macOS include it, but some |
| 53 | + Linux distributions and other platforms may package it separately. If |
| 54 | + ``import turtle`` fails with an error mentioning ``_tkinter``, look for |
| 55 | + documentation from your distributor (that is, whoever provided Python to you). |
| 56 | + Check this in advance if you're planning to use turtle graphics with a learner. |
57 | 57 |
|
58 | 58 |
|
59 | 59 | .. _turtle-tutorial: |
| 60 | +.. _get-started: |
| 61 | +.. _get-started-as-quickly-as-possible: |
60 | 62 |
|
61 | 63 | Tutorial |
62 | 64 | ======== |
@@ -99,7 +101,8 @@ Notice how the turtle, represented by an arrow, points in different |
99 | 101 | directions as you steer it. |
100 | 102 |
|
101 | 103 | Experiment with those commands, and also with ``backward()`` and |
102 | | -``right()``. |
| 104 | +``right()``. Many commands also have terser aliases, such as ``fd()`` for |
| 105 | +:func:`forward`. |
103 | 106 |
|
104 | 107 |
|
105 | 108 | Pen control |
@@ -179,38 +182,16 @@ Finally, complete the filling:: |
179 | 182 | ``end_fill()`` command.) |
180 | 183 |
|
181 | 184 |
|
| 185 | +.. _turtle-howtos: |
182 | 186 | .. _turtle-how-to: |
| 187 | +.. _how-to: |
183 | 188 |
|
184 | | -How to... |
185 | | -========= |
| 189 | +How-to guides |
| 190 | +============= |
186 | 191 |
|
187 | 192 | This section covers some typical turtle use-cases and approaches. |
188 | 193 |
|
189 | 194 |
|
190 | | -Get started as quickly as possible |
191 | | ----------------------------------- |
192 | | - |
193 | | -One of the joys of turtle graphics is the immediate, visual feedback that's |
194 | | -available from simple commands - it's an excellent way to introduce children |
195 | | -to programming ideas, with a minimum of overhead (not just children, of |
196 | | -course). |
197 | | - |
198 | | -The turtle module makes this possible by exposing all its basic functionality |
199 | | -as functions, available with ``from turtle import *``. The :ref:`turtle |
200 | | -graphics tutorial <turtle-tutorial>` covers this approach. |
201 | | - |
202 | | -It's worth noting that many of the turtle commands also have even more terse |
203 | | -equivalents, such as ``fd()`` for :func:`forward`. These are especially |
204 | | -useful when working with learners for whom typing is not a skill. |
205 | | - |
206 | | -.. _note: |
207 | | - |
208 | | - You'll need to have the :mod:`Tk interface package <tkinter>` installed on |
209 | | - your system for turtle graphics to work. Be warned that this is not |
210 | | - always straightforward, so check this in advance if you're planning to |
211 | | - use turtle graphics with a learner. |
212 | | - |
213 | | - |
214 | 195 | Use the ``turtle`` module namespace |
215 | 196 | ----------------------------------- |
216 | 197 |
|
@@ -289,8 +270,11 @@ The turtle's screen can be customised, for example:: |
289 | 270 | t.screen.bgcolor("orange") |
290 | 271 |
|
291 | 272 |
|
292 | | -Turtle graphics reference |
293 | | -========================= |
| 273 | +.. _turtle-reference: |
| 274 | +.. _turtle-graphics-reference: |
| 275 | + |
| 276 | +Reference |
| 277 | +========= |
294 | 278 |
|
295 | 279 | .. note:: |
296 | 280 |
|
|