Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97cf99f

Browse files
committed
Patch #3064: Port new turtle module and demos to 3.0.
1 parent b38fea3 commit 97cf99f

25 files changed

Lines changed: 7650 additions & 935 deletions

Demo/turtle/about_turtle.txt

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
========================================================
3+
A new turtle module for Python
4+
========================================================
5+
6+
Turtle graphics is a popular way for introducing programming to
7+
kids. It was part of the original Logo programming language developed
8+
by Wally Feurzig and Seymour Papert in 1966.
9+
10+
Imagine a robotic turtle starting at (0, 0) in the x-y plane. Give it
11+
the command turtle.forward(15), and it moves (on-screen!) 15 pixels in
12+
the direction it is facing, drawing a line as it moves. Give it the
13+
command turtle.left(25), and it rotates in-place 25 degrees clockwise.
14+
15+
By combining together these and similar commands, intricate shapes and
16+
pictures can easily be drawn.
17+
18+
----- turtle.py
19+
20+
This module is an extended reimplementation of turtle.py from the
21+
Python standard distribution up to Python 2.5. (See: http:\\www.python.org)
22+
23+
It tries to keep the merits of turtle.py and to be (nearly) 100%
24+
compatible with it. This means in the first place to enable the
25+
learning programmer to use all the commands, classes and methods
26+
interactively when using the module from within IDLE run with
27+
the -n switch.
28+
29+
Roughly it has the following features added:
30+
31+
- Better animation of the turtle movements, especially of turning the
32+
turtle. So the turtles can more easily be used as a visual feedback
33+
instrument by the (beginning) programmer.
34+
35+
- Different turtle shapes, gif-images as turtle shapes, user defined
36+
and user controllable turtle shapes, among them compound
37+
(multicolored) shapes. Turtle shapes can be stgretched and tilted, which
38+
makes turtles zu very versatile geometrical objects.
39+
40+
- Fine control over turtle movement and screen updates via delay(),
41+
and enhanced tracer() and speed() methods.
42+
43+
- Aliases for the most commonly used commands, like fd for forward etc.,
44+
following the early Logo traditions. This reduces the boring work of
45+
typing long sequences of commands, which often occur in a natural way
46+
when kids try to program fancy pictures on their first encounter with
47+
turtle graphcis.
48+
49+
- Turtles now have an undo()-method with configurable undo-buffer.
50+
51+
- Some simple commands/methods for creating event driven programs
52+
(mouse-, key-, timer-events). Especially useful for programming games.
53+
54+
- A scrollable Canvas class. The default scrollable Canvas can be
55+
extended interactively as needed while playing around with the turtle(s).
56+
57+
- A TurtleScreen class with methods controlling background color or
58+
background image, window and canvas size and other properties of the
59+
TurtleScreen.
60+
61+
- There is a method, setworldcoordinates(), to install a user defined
62+
coordinate-system for the TurtleScreen.
63+
64+
- The implementation uses a 2-vector class named Vec2D, derived from tuple.
65+
This class is public, so it can be imported by the application programmer,
66+
which makes certain types of computations very natural and compact.
67+
68+
- Appearance of the TurtleScreen and the Turtles at startup/import can be
69+
configured by means of a turtle.cfg configuration file.
70+
The default configuration mimics the appearance of the old turtle module.
71+
72+
- If configured appropriately the module reads in docstrings from a docstring
73+
dictionary in some different language, supplied separately and replaces
74+
the english ones by those read in. There is a utility function
75+
write_docstringdict() to write a dictionary with the original (english)
76+
docstrings to disc, so it can serve as a template for translations.

Demo/turtle/about_turtledemo.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
--------------------------------------
3+
About turtleDemo.py
4+
--------------------------------------
5+
6+
Tiny demo Viewer to view turtle graphics example scripts.
7+
8+
Quickly and dirtyly assembled by Gregor Lingl.
9+
June, 2006
10+
11+
For more information see: turtleDemo - Help
12+
13+
Have fun!

Demo/turtle/demohelp.txt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
3+
----------------------------------------------
4+
5+
turtleDemo - Help
6+
7+
----------------------------------------------
8+
9+
This document has two sections:
10+
11+
(1) How to use the demo viewer
12+
(2) How to add your own demos to the demo repository
13+
14+
15+
(1) How to use the demo viewer.
16+
17+
Select a demoscript from the example menu.
18+
The (syntax coloured) source code appears in the left
19+
source code window. IT CANNOT BE EDITED, but ONLY VIEWED!
20+
21+
- Press START button to start the demo.
22+
- Stop execution by pressing the STOP button.
23+
- Clear screen by pressing the CLEAR button.
24+
- Restart by pressing the START button again.
25+
26+
SPECIAL demos are those which run EVENTDRIVEN.
27+
(For example clock.py - or oldTurtleDemo.py which
28+
in the end expects a mouse click.):
29+
30+
Press START button to start the demo.
31+
32+
- Until the EVENTLOOP is entered everything works
33+
as in an ordinary demo script.
34+
35+
- When the EVENTLOOP is entered, you control the
36+
application by using the mouse and/or keys (or it's
37+
controlled by some timer events)
38+
To stop it you can and must press the STOP button.
39+
40+
While the EVENTLOOP is running, the examples menu is disabled.
41+
42+
- Only after having pressed the STOP button, you may
43+
restart it or choose another example script.
44+
45+
* * * * * * * *
46+
In some rare situations there may occur interferences/conflicts
47+
between events concerning the demo script and those concerning the
48+
demo-viewer. (They run in the same process.) Strange behaviour may be
49+
the consequence and in the worst case you must close and restart the
50+
viewer.
51+
* * * * * * * *
52+
53+
54+
(2) How to add your own demos to the demo repository
55+
56+
- scriptname: must begin with tdemo_ ,
57+
so it must have the form tdemo_<your-script-name>.py
58+
59+
- place: same directory as turtleDemo.py or some
60+
subdirectory, the name of which must also begin with
61+
tdemo_.....
62+
63+
- requirements on source code:
64+
code must contain a main() function which will
65+
be executed by the viewer (see provided example scripts)
66+
main() may return a string which will be displayed
67+
in the Label below the source code window (when execution
68+
has finished.)
69+
70+
!! For programs, which are EVENT DRIVEN, main must return
71+
!! the string "EVENTLOOP". This informs the viewer, that the
72+
!! script is still running and must be stopped by the user!
73+
74+
75+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/python
2+
""" turtle-example-suite:
3+
4+
tdemo-I_dont_like_tiltdemo.py
5+
6+
Demostrates
7+
(a) use of a tilted ellipse as
8+
turtle shape
9+
(b) stamping that shape
10+
11+
We can remove it, if you don't like it.
12+
Without using reset() ;-)
13+
---------------------------------------
14+
"""
15+
from tkinter.turtle import *
16+
import time
17+
18+
def main():
19+
reset()
20+
shape("circle")
21+
resizemode("user")
22+
23+
pu(); bk(24*18/6.283); rt(90); pd()
24+
tilt(45)
25+
26+
pu()
27+
28+
turtlesize(16,10,5)
29+
color("red", "violet")
30+
for i in range(18):
31+
fd(24)
32+
lt(20)
33+
stamp()
34+
color("red", "")
35+
for i in range(18):
36+
fd(24)
37+
lt(20)
38+
stamp()
39+
40+
tilt(-15)
41+
turtlesize(3, 1, 4)
42+
color("blue", "yellow")
43+
for i in range(17):
44+
fd(24)
45+
lt(20)
46+
if i%2 == 0:
47+
stamp()
48+
time.sleep(1)
49+
while undobufferentries():
50+
undo()
51+
ht()
52+
write("OK, OVER!", align="center", font=("Courier", 18, "bold"))
53+
return "Done!"
54+
55+
if __name__=="__main__":
56+
msg = main()
57+
print(msg)
58+
# mainloop()

Demo/turtle/tdemo_bytedesign.py

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#!/usr/bin/python
2+
""" turtle-example-suite:
3+
4+
tdemo_bytedesign.py
5+
6+
An example adapted from the example-suite
7+
of PythonCard's turtle graphcis.
8+
9+
It's based on an article in BYTE magazine
10+
Problem Solving with Logo: Using Turtle
11+
Graphics to Redraw a Design
12+
November 1982, p. 118 - 134
13+
14+
-------------------------------------------
15+
16+
Due to the statement
17+
18+
t.delay(0)
19+
20+
in line 152, which sets the animation delay
21+
to 0, this animation runs in "line per line"
22+
mode as fast as possible.
23+
"""
24+
25+
import math
26+
from tkinter.turtle import Turtle, mainloop
27+
from time import clock
28+
29+
# wrapper for any additional drawing routines
30+
# that need to know about each other
31+
class Designer(Turtle):
32+
33+
def design(self, homePos, scale):
34+
self.up()
35+
for i in range(5):
36+
self.forward(64.65 * scale)
37+
self.down()
38+
self.wheel(self.position(), scale)
39+
self.up()
40+
self.backward(64.65 * scale)
41+
self.right(72)
42+
self.up()
43+
self.goto(homePos)
44+
self.right(36)
45+
self.forward(24.5 * scale)
46+
self.right(198)
47+
self.down()
48+
self.centerpiece(46 * scale, 143.4, scale)
49+
self.getscreen().tracer(True)
50+
51+
def wheel(self, initpos, scale):
52+
self.right(54)
53+
for i in range(4):
54+
self.pentpiece(initpos, scale)
55+
self.down()
56+
self.left(36)
57+
for i in range(5):
58+
self.tripiece(initpos, scale)
59+
self.left(36)
60+
for i in range(5):
61+
self.down()
62+
self.right(72)
63+
self.forward(28 * scale)
64+
self.up()
65+
self.backward(28 * scale)
66+
self.left(54)
67+
self.getscreen().update()
68+
69+
def tripiece(self, initpos, scale):
70+
oldh = self.heading()
71+
self.down()
72+
self.backward(2.5 * scale)
73+
self.tripolyr(31.5 * scale, scale)
74+
self.up()
75+
self.goto(initpos)
76+
self.setheading(oldh)
77+
self.down()
78+
self.backward(2.5 * scale)
79+
self.tripolyl(31.5 * scale, scale)
80+
self.up()
81+
self.goto(initpos)
82+
self.setheading(oldh)
83+
self.left(72)
84+
self.getscreen().update()
85+
86+
def pentpiece(self, initpos, scale):
87+
oldh = self.heading()
88+
self.up()
89+
self.forward(29 * scale)
90+
self.down()
91+
for i in range(5):
92+
self.forward(18 * scale)
93+
self.right(72)
94+
self.pentr(18 * scale, 75, scale)
95+
self.up()
96+
self.goto(initpos)
97+
self.setheading(oldh)
98+
self.forward(29 * scale)
99+
self.down()
100+
for i in range(5):
101+
self.forward(18 * scale)
102+
self.right(72)
103+
self.pentl(18 * scale, 75, scale)
104+
self.up()
105+
self.goto(initpos)
106+
self.setheading(oldh)
107+
self.left(72)
108+
self.getscreen().update()
109+
110+
def pentl(self, side, ang, scale):
111+
if side < (2 * scale): return
112+
self.forward(side)
113+
self.left(ang)
114+
self.pentl(side - (.38 * scale), ang, scale)
115+
116+
def pentr(self, side, ang, scale):
117+
if side < (2 * scale): return
118+
self.forward(side)
119+
self.right(ang)
120+
self.pentr(side - (.38 * scale), ang, scale)
121+
122+
def tripolyr(self, side, scale):
123+
if side < (4 * scale): return
124+
self.forward(side)
125+
self.right(111)
126+
self.forward(side / 1.78)
127+
self.right(111)
128+
self.forward(side / 1.3)
129+
self.right(146)
130+
self.tripolyr(side * .75, scale)
131+
132+
def tripolyl(self, side, scale):
133+
if side < (4 * scale): return
134+
self.forward(side)
135+
self.left(111)
136+
self.forward(side / 1.78)
137+
self.left(111)
138+
self.forward(side / 1.3)
139+
self.left(146)
140+
self.tripolyl(side * .75, scale)
141+
142+
def centerpiece(self, s, a, scale):
143+
self.forward(s); self.left(a)
144+
if s < (7.5 * scale):
145+
return
146+
self.centerpiece(s - (1.2 * scale), a, scale)
147+
148+
def main():
149+
t = Designer()
150+
t.speed(0)
151+
t.hideturtle()
152+
t.getscreen().delay(0)
153+
t.getscreen().tracer(0)
154+
at = clock()
155+
t.design(t.position(), 2)
156+
et = clock()
157+
return "runtime: %.2f sec." % (et-at)
158+
159+
if __name__ == '__main__':
160+
msg = main()
161+
print(msg)
162+
mainloop()

0 commit comments

Comments
 (0)