Skip to content

Commit a2a0e51

Browse files
evildmphugovk
andauthored
gh-106996: Add the basics of a turtle graphics tutorial (#107072)
Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent 102a773 commit a2a0e51

File tree

2 files changed

+134
-26
lines changed

2 files changed

+134
-26
lines changed

Doc/includes/turtle-star.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

Doc/library/turtle.rst

Lines changed: 134 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ introduced in Logo <https://en.wikipedia.org/wiki/Turtle_
2424
(robot)>`_, developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon
2525
in 1967.
2626

27+
28+
Get started
29+
===========
30+
31+
Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle``, give it the
32+
command ``turtle.forward(15)``, and it moves (on-screen!) 15 pixels in the
33+
direction it is facing, drawing a line as it moves. Give it the command
34+
``turtle.right(25)``, and it rotates in-place 25 degrees clockwise.
35+
36+
.. sidebar:: Turtle star
37+
38+
Turtle can draw intricate shapes using programs that repeat simple
39+
moves.
40+
41+
.. image:: turtle-star.*
42+
:align: center
43+
2744
In Python, turtle graphics provides a representation of a physical "turtle"
2845
(a little robot with a pen) that draws on a sheet of paper on the floor.
2946

@@ -38,26 +55,127 @@ graphical output it can be a way to do that without the overhead of
3855
introducing more complex or external libraries into their work.
3956

4057

41-
Get started
42-
===========
58+
Tutorial
59+
========
4360

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.
61+
New users should start here. In this tutorial we'll explore some of the
62+
basics of turtle drawing.
4863

49-
.. sidebar:: Turtle star
5064

51-
Turtle can draw intricate shapes using programs that repeat simple
52-
moves.
65+
Starting a turtle environment
66+
-----------------------------
5367

54-
.. image:: turtle-star.*
55-
:align: center
68+
In a Python shell, import all the objects of the ``turtle`` module::
69+
70+
from turtle import *
71+
72+
If you run into a ``No module named '_tkinter'`` error, you'll have to
73+
install the :mod:`Tk interface package <tkinter>` on your system.
74+
75+
76+
Basic drawing
77+
-------------
78+
79+
Send the turtle forward 100 steps::
80+
81+
forward(100)
82+
83+
You should see (most likely, in a new window on your display) a line
84+
drawn by the turtle, heading East. Change the direction of the turtle,
85+
so that it turns 120 degrees left (anti-clockwise)::
5686

57-
.. literalinclude:: ../includes/turtle-star.py
87+
left(120)
5888

59-
By combining together these and similar commands, intricate shapes and pictures
60-
can easily be drawn.
89+
Let's continue by drawing a triangle::
90+
91+
forward(100)
92+
left(120)
93+
forward(100)
94+
95+
Notice how the turtle, represented by an arrow, points in different
96+
directions as you steer it.
97+
98+
Experiment with those commands, and also with ``backward()`` and
99+
``right()``.
100+
101+
102+
Pen control
103+
~~~~~~~~~~~
104+
105+
Try changing the color - for example, ``color('blue')`` - and
106+
width of the line - for example, ``width(3)`` - and then drawing again.
107+
108+
You can also move the turtle around without drawing, by lifting up the pen:
109+
``up()`` before moving. To start drawing again, use ``down()``.
110+
111+
112+
The turtle's position
113+
~~~~~~~~~~~~~~~~~~~~~
114+
115+
Send your turtle back to its starting-point (useful if it has disappeared
116+
off-screen)::
117+
118+
home()
119+
120+
The home position is at the center of the turtle's screen. If you ever need to
121+
know them, get the turtle's x-y co-ordinates with::
122+
123+
pos()
124+
125+
Home is at ``(0, 0)``.
126+
127+
And after a while, it will probably help to clear the window so we can start
128+
anew::
129+
130+
clearscreen()
131+
132+
133+
Making algorithmic patterns
134+
---------------------------
135+
136+
Using loops, it's possible to build up geometric patterns::
137+
138+
for steps in range(100):
139+
for c in ('blue', 'red', 'green'):
140+
color(c)
141+
forward(steps)
142+
right(30)
143+
144+
145+
\ - which of course, are limited only by the imagination!
146+
147+
Let's draw the star shape at the top of this page. We want red lines,
148+
filled in with yellow::
149+
150+
color('red')
151+
fillcolor('yellow')
152+
153+
Just as ``up()`` and ``down()`` determine whether lines will be drawn,
154+
filling can be turned on and off::
155+
156+
begin_fill()
157+
158+
Next we'll create a loop::
159+
160+
while True:
161+
forward(200)
162+
left(170)
163+
if abs(pos()) < 1:
164+
break
165+
166+
``abs(pos()) < 1`` is a good way to know when the turtle is back at its
167+
home position.
168+
169+
Finally, complete the filling::
170+
171+
end_fill()
172+
173+
(Note that filling only actually takes place when you give the
174+
``end_fill()`` command.)
175+
176+
177+
Explanation
178+
===========
61179

62180
The :mod:`turtle` module is an extended reimplementation of the same-named
63181
module from the Python standard distribution up to version Python 2.5.
@@ -112,8 +230,8 @@ To use multiple turtles on a screen one has to use the object-oriented interface
112230
omitted here.
113231

114232

115-
Overview of available Turtle and Screen methods
116-
=================================================
233+
Turtle graphics reference
234+
=========================
117235

118236
Turtle methods
119237
--------------

0 commit comments

Comments
 (0)