19
19
Introduction
20
20
============
21
21
22
- Turtle graphics is a popular way for introducing programming to kids. It was
23
- part of the original Logo programming language developed by Wally Feurzeig,
24
- Seymour Papert and Cynthia Solomon in 1967.
25
-
26
- Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle ``, give it the
27
- command ``turtle.forward(15) ``, and it moves (on-screen!) 15 pixels in the
28
- direction it is facing, drawing a line as it moves. Give it the command
29
- ``turtle.right(25) ``, and it rotates in-place 25 degrees clockwise.
22
+ Turtle graphics is an implementation of `the popular geometric drawing tools
23
+ introduced in Logo <https://en.wikipedia.org/wiki/Turtle_
24
+ (robot)> `_, developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon
25
+ in 1967.
30
26
31
27
.. sidebar :: Turtle star
32
28
@@ -36,10 +32,141 @@ direction it is facing, drawing a line as it moves. Give it the command
36
32
.. image :: turtle-star.*
37
33
:align: center
38
34
39
- .. literalinclude :: ../includes/turtle-star.py
35
+ In Python, turtle graphics provides a representation of a physical "turtle"
36
+ (a little robot with a pen) that draws on a sheet of paper on the floor.
37
+
38
+ It's an effective and well-proven way for learners to encounter
39
+ programming concepts and interaction with software, as it provides instant,
40
+ visible feedback. It also provides convenient access to graphical output
41
+ in general.
42
+
43
+ Turtle drawing was originally created as an educational tool, to be used by
44
+ teachers in the classroom. For the programmer who needs to produce some
45
+ graphical output it can be a way to do that without the overhead of
46
+ introducing more complex or external libraries into their work.
47
+
48
+
49
+ Tutorial
50
+ ========
51
+
52
+ New users should start here. In this tutorial we'll explore some of the
53
+ basics of turtle drawing.
54
+
55
+
56
+ Starting a turtle environment
57
+ -----------------------------
58
+
59
+ In a Python shell, import all the objects of the ``turtle `` module::
60
+
61
+ from turtle import *
62
+
63
+ If you run into a ``No module named '_tkinter' `` error, you'll have to
64
+ install the :mod: `Tk interface package <tkinter> ` on your system.
65
+
66
+
67
+ Basic drawing
68
+ -------------
69
+
70
+ Send the turtle forward 100 steps::
71
+
72
+ forward(100)
73
+
74
+ You should see (most likely, in a new window on your display) a line
75
+ drawn by the turtle, heading East. Change the direction of the turtle,
76
+ so that it turns 120 degrees left (anti-clockwise)::
77
+
78
+ left(120)
79
+
80
+ Let's continue by drawing a triangle::
81
+
82
+ forward(100)
83
+ left(120)
84
+ forward(100)
85
+
86
+ Notice how the turtle, represented by an arrow, points in different
87
+ directions as you steer it.
88
+
89
+ Experiment with those commands, and also with ``backward() `` and
90
+ ``right() ``.
91
+
92
+
93
+ Pen control
94
+ ~~~~~~~~~~~
95
+
96
+ Try changing the color - for example, ``color('blue') `` - and
97
+ width of the line - for example, ``width(3) `` - and then drawing again.
98
+
99
+ You can also move the turtle around without drawing, by lifting up the pen:
100
+ ``up() `` before moving. To start drawing again, use ``down() ``.
101
+
102
+
103
+ The turtle's position
104
+ ~~~~~~~~~~~~~~~~~~~~~
105
+
106
+ Send your turtle back to its starting-point (useful if it has disappeared
107
+ off-screen)::
108
+
109
+ home()
110
+
111
+ The home position is at the center of the turtle's screen. If you ever need to
112
+ know them, get the turtle's x-y co-ordinates with::
113
+
114
+ pos()
115
+
116
+ Home is at ``(0, 0) ``.
117
+
118
+ And after a while, it will probably help to clear the window so we can start
119
+ anew::
120
+
121
+ clearscreen()
122
+
123
+
124
+ Making algorithmic patterns
125
+ ---------------------------
126
+
127
+ Using loops, it's possible to build up geometric patterns::
128
+
129
+ for steps in range(100):
130
+ for c in ('blue', 'red', 'green'):
131
+ color(c)
132
+ forward(steps)
133
+ right(30)
134
+
135
+
136
+ \ - which of course, are limited only by the imagination!
137
+
138
+ Let's draw the star shape at the top of this page. We want red lines,
139
+ filled in with yellow::
140
+
141
+ color('red')
142
+ fillcolor('yellow')
143
+
144
+ Just as ``up() `` and ``down() `` determine whether lines will be drawn,
145
+ filling can be turned on and off::
146
+
147
+ begin_fill()
148
+
149
+ Next we'll create a loop::
150
+
151
+ while True:
152
+ forward(200)
153
+ left(170)
154
+ if abs(pos()) < 1:
155
+ break
156
+
157
+ ``abs(pos()) < 1 `` is a good way to know when the turtle is back at its
158
+ home position.
159
+
160
+ Finally, complete the filling::
161
+
162
+ end_fill()
163
+
164
+ (Note that filling only actually takes place when you give the
165
+ ``end_fill() `` command.)
166
+
40
167
41
- By combining together these and similar commands, intricate shapes and pictures
42
- can easily be drawn.
168
+ Explanation
169
+ ===========
43
170
44
171
The :mod: `turtle ` module is an extended reimplementation of the same-named
45
172
module from the Python standard distribution up to version Python 2.5.
@@ -94,8 +221,8 @@ To use multiple turtles on a screen one has to use the object-oriented interface
94
221
omitted here.
95
222
96
223
97
- Overview of available Turtle and Screen methods
98
- =================================================
224
+ Turtle graphics reference
225
+ =========================
99
226
100
227
Turtle methods
101
228
--------------
0 commit comments