@@ -24,6 +24,14 @@ introduced in Logo <https://en.wikipedia.org/wiki/Turtle_
24
24
(robot)> `_, developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon
25
25
in 1967.
26
26
27
+ .. sidebar :: Turtle star
28
+
29
+ Turtle can draw intricate shapes using programs that repeat simple
30
+ moves.
31
+
32
+ .. image :: turtle-star.*
33
+ :align: center
34
+
27
35
In Python, turtle graphics provides a representation of a physical "turtle"
28
36
(a little robot with a pen) that draws on a sheet of paper on the floor.
29
37
@@ -38,26 +46,115 @@ graphical output it can be a way to do that without the overhead of
38
46
introducing more complex or external libraries into their work.
39
47
40
48
41
- Get started
42
- ===========
49
+ Tutorial
50
+ ========
43
51
44
- Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle ``, give it the
45
- command ``turtle.forward(15) ``, and it moves (on-screen!) 15 pixels in the
46
- direction it is facing, drawing a line as it moves. Give it the command
47
- ``turtle.right(25) ``, and it rotates in-place 25 degrees clockwise.
52
+ New users should start here. In this tutorial we'll explore some of the
53
+ basics of turtle drawing.
48
54
49
- .. sidebar :: Turtle star
50
55
51
- Turtle can draw intricate shapes using programs that repeat simple
52
- moves.
56
+ Starting a turtle environment
57
+ -----------------------------
53
58
54
- .. image :: turtle-star.*
55
- :align: center
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.
56
88
57
- .. literalinclude :: ../includes/turtle-star.py
89
+ Experiment with those commands, and also with ``backward() `` and
90
+ ``right() ``.
58
91
59
- By combining together these and similar commands, intricate shapes and pictures
60
- can easily be drawn.
92
+ Try changing the color - for example, ``color('blue') `` - and
93
+ width of the line - for example, ``width(3) ``.
94
+
95
+ Send your turtle back to its starting-point (useful if it has disappeared
96
+ off-screen)::
97
+
98
+ home()
99
+
100
+ The home position is at the center of the turtle's screen. If you ever need to
101
+ know them, get the turtle's x-y co-ordinates with::
102
+
103
+ pos()
104
+
105
+ And after a while, it will probably help to clear the window so we can start
106
+ anew::
107
+
108
+ clearscreen()
109
+
110
+
111
+ Making algorithmic patterns
112
+ ---------------------------
113
+
114
+ Using loops, it's possible to build up geometric patterns::
115
+
116
+ steps = 1
117
+ while steps < 100:
118
+ for c in ('blue', 'red', 'green'):
119
+ color(c)
120
+ forward(steps)
121
+ right(30)
122
+ x += 1
123
+
124
+
125
+ \ - which of course, are limited only by the imagination!
126
+
127
+ Try this::
128
+
129
+ color('red')
130
+ fillcolor('yellow')
131
+
132
+ This means that the turtle will draw red lines, and use yellow to fill
133
+ between the lines. It needs to know to at which point filling should
134
+ begin::
135
+
136
+ begin_fill()
137
+
138
+ Next we'll create a loop::
139
+
140
+ while True:
141
+ forward(200)
142
+ left(170)
143
+ if abs(pos()) < 1:
144
+ break
145
+
146
+ ``abs(pos()) < 1 `` is a good way to know when the turtle is back at its
147
+ home position.
148
+
149
+ Finally, complete the filling::
150
+
151
+ end_fill()
152
+
153
+ This will draw the star shape at the top of this page.
154
+
155
+
156
+ Explanation
157
+ ===========
61
158
62
159
The :mod: `turtle ` module is an extended reimplementation of the same-named
63
160
module from the Python standard distribution up to version Python 2.5.
@@ -112,8 +209,8 @@ To use multiple turtles on a screen one has to use the object-oriented interface
112
209
omitted here.
113
210
114
211
115
- Overview of available Turtle and Screen methods
116
- =================================================
212
+ Turtle graphics reference
213
+ =========================
117
214
118
215
Turtle methods
119
216
--------------
0 commit comments