Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- evalkit_tf446/lib/python3.10/encodings/__pycache__/cp273.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/encodings/__pycache__/cp857.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/encodings/__pycache__/cp864.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/encodings/__pycache__/cp875.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/encodings/__pycache__/cp950.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/encodings/__pycache__/euc_jp.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/encodings/__pycache__/hp_roman8.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/encodings/__pycache__/hz.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/encodings/__pycache__/iso8859_1.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/encodings/__pycache__/iso8859_6.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/encodings/__pycache__/mac_roman.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/encodings/__pycache__/palmos.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/encodings/__pycache__/utf_32.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/encodings/__pycache__/utf_32_le.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/encodings/__pycache__/utf_8.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__init__.py +14 -0
- evalkit_tf446/lib/python3.10/turtledemo/__main__.py +398 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/__main__.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/chaos.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/clock.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/colormixer.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/forest.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/fractalcurves.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/minimal_hanoi.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/nim.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/paint.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/peace.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/planet_and_moon.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/rosette.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/round_dance.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/sorting_animate.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/tree.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/two_canvases.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/__pycache__/yinyang.cpython-310.pyc +0 -0
- evalkit_tf446/lib/python3.10/turtledemo/bytedesign.py +161 -0
- evalkit_tf446/lib/python3.10/turtledemo/chaos.py +59 -0
- evalkit_tf446/lib/python3.10/turtledemo/clock.py +131 -0
- evalkit_tf446/lib/python3.10/turtledemo/colormixer.py +58 -0
- evalkit_tf446/lib/python3.10/turtledemo/forest.py +108 -0
- evalkit_tf446/lib/python3.10/turtledemo/fractalcurves.py +138 -0
- evalkit_tf446/lib/python3.10/turtledemo/lindenmayer.py +119 -0
- evalkit_tf446/lib/python3.10/turtledemo/minimal_hanoi.py +79 -0
- evalkit_tf446/lib/python3.10/turtledemo/nim.py +226 -0
- evalkit_tf446/lib/python3.10/turtledemo/paint.py +54 -0
- evalkit_tf446/lib/python3.10/turtledemo/peace.py +61 -0
- evalkit_tf446/lib/python3.10/turtledemo/penrose.py +175 -0
- evalkit_tf446/lib/python3.10/turtledemo/planet_and_moon.py +111 -0
- evalkit_tf446/lib/python3.10/turtledemo/rosette.py +65 -0
- evalkit_tf446/lib/python3.10/turtledemo/round_dance.py +86 -0
- evalkit_tf446/lib/python3.10/turtledemo/sorting_animate.py +204 -0
evalkit_tf446/lib/python3.10/encodings/__pycache__/cp273.cpython-310.pyc
ADDED
|
Binary file (2.33 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/encodings/__pycache__/cp857.cpython-310.pyc
ADDED
|
Binary file (8.66 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/encodings/__pycache__/cp864.cpython-310.pyc
ADDED
|
Binary file (9.32 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/encodings/__pycache__/cp875.cpython-310.pyc
ADDED
|
Binary file (2.34 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/encodings/__pycache__/cp950.cpython-310.pyc
ADDED
|
Binary file (1.62 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/encodings/__pycache__/euc_jp.cpython-310.pyc
ADDED
|
Binary file (1.37 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/encodings/__pycache__/hp_roman8.cpython-310.pyc
ADDED
|
Binary file (2.55 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/encodings/__pycache__/hz.cpython-310.pyc
ADDED
|
Binary file (1.62 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/encodings/__pycache__/iso8859_1.cpython-310.pyc
ADDED
|
Binary file (2.34 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/encodings/__pycache__/iso8859_6.cpython-310.pyc
ADDED
|
Binary file (2.39 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/encodings/__pycache__/mac_roman.cpython-310.pyc
ADDED
|
Binary file (2.38 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/encodings/__pycache__/palmos.cpython-310.pyc
ADDED
|
Binary file (2.37 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/encodings/__pycache__/utf_32.cpython-310.pyc
ADDED
|
Binary file (4.99 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/encodings/__pycache__/utf_32_le.cpython-310.pyc
ADDED
|
Binary file (1.76 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/encodings/__pycache__/utf_8.cpython-310.pyc
ADDED
|
Binary file (1.85 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__init__.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
--------------------------------------
|
| 3 |
+
About this viewer
|
| 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!
|
| 14 |
+
"""
|
evalkit_tf446/lib/python3.10/turtledemo/__main__.py
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
|
| 3 |
+
"""
|
| 4 |
+
----------------------------------------------
|
| 5 |
+
turtleDemo - Help
|
| 6 |
+
----------------------------------------------
|
| 7 |
+
|
| 8 |
+
This document has two sections:
|
| 9 |
+
|
| 10 |
+
(1) How to use the demo viewer
|
| 11 |
+
(2) How to add your own demos to the demo repository
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
(1) How to use the demo viewer.
|
| 15 |
+
|
| 16 |
+
Select a demoscript from the example menu.
|
| 17 |
+
The (syntax colored) source code appears in the left
|
| 18 |
+
source code window. IT CANNOT BE EDITED, but ONLY VIEWED!
|
| 19 |
+
|
| 20 |
+
The demo viewer windows can be resized. The divider between text
|
| 21 |
+
and canvas can be moved by grabbing it with the mouse. The text font
|
| 22 |
+
size can be changed from the menu and with Control/Command '-'/'+'.
|
| 23 |
+
It can also be changed on most systems with Control-mousewheel
|
| 24 |
+
when the mouse is over the text.
|
| 25 |
+
|
| 26 |
+
Press START button to start the demo.
|
| 27 |
+
Stop execution by pressing the STOP button.
|
| 28 |
+
Clear screen by pressing the CLEAR button.
|
| 29 |
+
Restart by pressing the START button again.
|
| 30 |
+
|
| 31 |
+
SPECIAL demos, such as clock.py are those which run EVENTDRIVEN.
|
| 32 |
+
|
| 33 |
+
Press START button to start the demo.
|
| 34 |
+
|
| 35 |
+
- Until the EVENTLOOP is entered everything works
|
| 36 |
+
as in an ordinary demo script.
|
| 37 |
+
|
| 38 |
+
- When the EVENTLOOP is entered, you control the
|
| 39 |
+
application by using the mouse and/or keys (or it's
|
| 40 |
+
controlled by some timer events)
|
| 41 |
+
To stop it you can and must press the STOP button.
|
| 42 |
+
|
| 43 |
+
While the EVENTLOOP is running, the examples menu is disabled.
|
| 44 |
+
|
| 45 |
+
- Only after having pressed the STOP button, you may
|
| 46 |
+
restart it or choose another example script.
|
| 47 |
+
|
| 48 |
+
* * * * * * * *
|
| 49 |
+
In some rare situations there may occur interferences/conflicts
|
| 50 |
+
between events concerning the demo script and those concerning the
|
| 51 |
+
demo-viewer. (They run in the same process.) Strange behaviour may be
|
| 52 |
+
the consequence and in the worst case you must close and restart the
|
| 53 |
+
viewer.
|
| 54 |
+
* * * * * * * *
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
(2) How to add your own demos to the demo repository
|
| 58 |
+
|
| 59 |
+
- Place the file in the same directory as turtledemo/__main__.py
|
| 60 |
+
IMPORTANT! When imported, the demo should not modify the system
|
| 61 |
+
by calling functions in other modules, such as sys, tkinter, or
|
| 62 |
+
turtle. Global variables should be initialized in main().
|
| 63 |
+
|
| 64 |
+
- The code must contain a main() function which will
|
| 65 |
+
be executed by the viewer (see provided example scripts).
|
| 66 |
+
It may return a string which will be displayed in the Label below
|
| 67 |
+
the source code window (when execution has finished.)
|
| 68 |
+
|
| 69 |
+
- In order to run mydemo.py by itself, such as during development,
|
| 70 |
+
add the following at the end of the file:
|
| 71 |
+
|
| 72 |
+
if __name__ == '__main__':
|
| 73 |
+
main()
|
| 74 |
+
mainloop() # keep window open
|
| 75 |
+
|
| 76 |
+
python -m turtledemo.mydemo # will then run it
|
| 77 |
+
|
| 78 |
+
- If the demo is EVENT DRIVEN, main must return the string
|
| 79 |
+
"EVENTLOOP". This informs the demo viewer that the script is
|
| 80 |
+
still running and must be stopped by the user!
|
| 81 |
+
|
| 82 |
+
If an "EVENTLOOP" demo runs by itself, as with clock, which uses
|
| 83 |
+
ontimer, or minimal_hanoi, which loops by recursion, then the
|
| 84 |
+
code should catch the turtle.Terminator exception that will be
|
| 85 |
+
raised when the user presses the STOP button. (Paint is not such
|
| 86 |
+
a demo; it only acts in response to mouse clicks and movements.)
|
| 87 |
+
"""
|
| 88 |
+
import sys
|
| 89 |
+
import os
|
| 90 |
+
|
| 91 |
+
from tkinter import *
|
| 92 |
+
from idlelib.colorizer import ColorDelegator, color_config
|
| 93 |
+
from idlelib.percolator import Percolator
|
| 94 |
+
from idlelib.textview import view_text
|
| 95 |
+
from turtledemo import __doc__ as about_turtledemo
|
| 96 |
+
|
| 97 |
+
import turtle
|
| 98 |
+
|
| 99 |
+
demo_dir = os.path.dirname(os.path.abspath(__file__))
|
| 100 |
+
darwin = sys.platform == 'darwin'
|
| 101 |
+
|
| 102 |
+
STARTUP = 1
|
| 103 |
+
READY = 2
|
| 104 |
+
RUNNING = 3
|
| 105 |
+
DONE = 4
|
| 106 |
+
EVENTDRIVEN = 5
|
| 107 |
+
|
| 108 |
+
menufont = ("Arial", 12, NORMAL)
|
| 109 |
+
btnfont = ("Arial", 12, 'bold')
|
| 110 |
+
txtfont = ['Lucida Console', 10, 'normal']
|
| 111 |
+
|
| 112 |
+
MINIMUM_FONT_SIZE = 6
|
| 113 |
+
MAXIMUM_FONT_SIZE = 100
|
| 114 |
+
font_sizes = [8, 9, 10, 11, 12, 14, 18, 20, 22, 24, 30]
|
| 115 |
+
|
| 116 |
+
def getExampleEntries():
|
| 117 |
+
return [entry[:-3] for entry in os.listdir(demo_dir) if
|
| 118 |
+
entry.endswith(".py") and entry[0] != '_']
|
| 119 |
+
|
| 120 |
+
help_entries = ( # (help_label, help_doc)
|
| 121 |
+
('Turtledemo help', __doc__),
|
| 122 |
+
('About turtledemo', about_turtledemo),
|
| 123 |
+
('About turtle module', turtle.__doc__),
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
class DemoWindow(object):
|
| 128 |
+
|
| 129 |
+
def __init__(self, filename=None):
|
| 130 |
+
self.root = root = turtle._root = Tk()
|
| 131 |
+
root.title('Python turtle-graphics examples')
|
| 132 |
+
root.wm_protocol("WM_DELETE_WINDOW", self._destroy)
|
| 133 |
+
|
| 134 |
+
if darwin:
|
| 135 |
+
import subprocess
|
| 136 |
+
# Make sure we are the currently activated OS X application
|
| 137 |
+
# so that our menu bar appears.
|
| 138 |
+
subprocess.run(
|
| 139 |
+
[
|
| 140 |
+
'osascript',
|
| 141 |
+
'-e', 'tell application "System Events"',
|
| 142 |
+
'-e', 'set frontmost of the first process whose '
|
| 143 |
+
'unix id is {} to true'.format(os.getpid()),
|
| 144 |
+
'-e', 'end tell',
|
| 145 |
+
],
|
| 146 |
+
stderr=subprocess.DEVNULL,
|
| 147 |
+
stdout=subprocess.DEVNULL,)
|
| 148 |
+
|
| 149 |
+
root.grid_rowconfigure(0, weight=1)
|
| 150 |
+
root.grid_columnconfigure(0, weight=1)
|
| 151 |
+
root.grid_columnconfigure(1, minsize=90, weight=1)
|
| 152 |
+
root.grid_columnconfigure(2, minsize=90, weight=1)
|
| 153 |
+
root.grid_columnconfigure(3, minsize=90, weight=1)
|
| 154 |
+
|
| 155 |
+
self.mBar = Menu(root, relief=RAISED, borderwidth=2)
|
| 156 |
+
self.mBar.add_cascade(menu=self.makeLoadDemoMenu(self.mBar),
|
| 157 |
+
label='Examples', underline=0)
|
| 158 |
+
self.mBar.add_cascade(menu=self.makeFontMenu(self.mBar),
|
| 159 |
+
label='Fontsize', underline=0)
|
| 160 |
+
self.mBar.add_cascade(menu=self.makeHelpMenu(self.mBar),
|
| 161 |
+
label='Help', underline=0)
|
| 162 |
+
root['menu'] = self.mBar
|
| 163 |
+
|
| 164 |
+
pane = PanedWindow(orient=HORIZONTAL, sashwidth=5,
|
| 165 |
+
sashrelief=SOLID, bg='#ddd')
|
| 166 |
+
pane.add(self.makeTextFrame(pane))
|
| 167 |
+
pane.add(self.makeGraphFrame(pane))
|
| 168 |
+
pane.grid(row=0, columnspan=4, sticky='news')
|
| 169 |
+
|
| 170 |
+
self.output_lbl = Label(root, height= 1, text=" --- ", bg="#ddf",
|
| 171 |
+
font=("Arial", 16, 'normal'), borderwidth=2,
|
| 172 |
+
relief=RIDGE)
|
| 173 |
+
if darwin: # Leave Mac button colors alone - #44254.
|
| 174 |
+
self.start_btn = Button(root, text=" START ", font=btnfont,
|
| 175 |
+
fg='#00cc22', command=self.startDemo)
|
| 176 |
+
self.stop_btn = Button(root, text=" STOP ", font=btnfont,
|
| 177 |
+
fg='#00cc22', command=self.stopIt)
|
| 178 |
+
self.clear_btn = Button(root, text=" CLEAR ", font=btnfont,
|
| 179 |
+
fg='#00cc22', command = self.clearCanvas)
|
| 180 |
+
else:
|
| 181 |
+
self.start_btn = Button(root, text=" START ", font=btnfont,
|
| 182 |
+
fg="white", disabledforeground = "#fed",
|
| 183 |
+
command=self.startDemo)
|
| 184 |
+
self.stop_btn = Button(root, text=" STOP ", font=btnfont,
|
| 185 |
+
fg="white", disabledforeground = "#fed",
|
| 186 |
+
command=self.stopIt)
|
| 187 |
+
self.clear_btn = Button(root, text=" CLEAR ", font=btnfont,
|
| 188 |
+
fg="white", disabledforeground="#fed",
|
| 189 |
+
command = self.clearCanvas)
|
| 190 |
+
self.output_lbl.grid(row=1, column=0, sticky='news', padx=(0,5))
|
| 191 |
+
self.start_btn.grid(row=1, column=1, sticky='ew')
|
| 192 |
+
self.stop_btn.grid(row=1, column=2, sticky='ew')
|
| 193 |
+
self.clear_btn.grid(row=1, column=3, sticky='ew')
|
| 194 |
+
|
| 195 |
+
Percolator(self.text).insertfilter(ColorDelegator())
|
| 196 |
+
self.dirty = False
|
| 197 |
+
self.exitflag = False
|
| 198 |
+
if filename:
|
| 199 |
+
self.loadfile(filename)
|
| 200 |
+
self.configGUI(DISABLED, DISABLED, DISABLED,
|
| 201 |
+
"Choose example from menu", "black")
|
| 202 |
+
self.state = STARTUP
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
def onResize(self, event):
|
| 206 |
+
cwidth = self._canvas.winfo_width()
|
| 207 |
+
cheight = self._canvas.winfo_height()
|
| 208 |
+
self._canvas.xview_moveto(0.5*(self.canvwidth-cwidth)/self.canvwidth)
|
| 209 |
+
self._canvas.yview_moveto(0.5*(self.canvheight-cheight)/self.canvheight)
|
| 210 |
+
|
| 211 |
+
def makeTextFrame(self, root):
|
| 212 |
+
self.text_frame = text_frame = Frame(root)
|
| 213 |
+
self.text = text = Text(text_frame, name='text', padx=5,
|
| 214 |
+
wrap='none', width=45)
|
| 215 |
+
color_config(text)
|
| 216 |
+
|
| 217 |
+
self.vbar = vbar = Scrollbar(text_frame, name='vbar')
|
| 218 |
+
vbar['command'] = text.yview
|
| 219 |
+
vbar.pack(side=LEFT, fill=Y)
|
| 220 |
+
self.hbar = hbar = Scrollbar(text_frame, name='hbar', orient=HORIZONTAL)
|
| 221 |
+
hbar['command'] = text.xview
|
| 222 |
+
hbar.pack(side=BOTTOM, fill=X)
|
| 223 |
+
text['yscrollcommand'] = vbar.set
|
| 224 |
+
text['xscrollcommand'] = hbar.set
|
| 225 |
+
|
| 226 |
+
text['font'] = tuple(txtfont)
|
| 227 |
+
shortcut = 'Command' if darwin else 'Control'
|
| 228 |
+
text.bind_all('<%s-minus>' % shortcut, self.decrease_size)
|
| 229 |
+
text.bind_all('<%s-underscore>' % shortcut, self.decrease_size)
|
| 230 |
+
text.bind_all('<%s-equal>' % shortcut, self.increase_size)
|
| 231 |
+
text.bind_all('<%s-plus>' % shortcut, self.increase_size)
|
| 232 |
+
text.bind('<Control-MouseWheel>', self.update_mousewheel)
|
| 233 |
+
text.bind('<Control-Button-4>', self.increase_size)
|
| 234 |
+
text.bind('<Control-Button-5>', self.decrease_size)
|
| 235 |
+
|
| 236 |
+
text.pack(side=LEFT, fill=BOTH, expand=1)
|
| 237 |
+
return text_frame
|
| 238 |
+
|
| 239 |
+
def makeGraphFrame(self, root):
|
| 240 |
+
turtle._Screen._root = root
|
| 241 |
+
self.canvwidth = 1000
|
| 242 |
+
self.canvheight = 800
|
| 243 |
+
turtle._Screen._canvas = self._canvas = canvas = turtle.ScrolledCanvas(
|
| 244 |
+
root, 800, 600, self.canvwidth, self.canvheight)
|
| 245 |
+
canvas.adjustScrolls()
|
| 246 |
+
canvas._rootwindow.bind('<Configure>', self.onResize)
|
| 247 |
+
canvas._canvas['borderwidth'] = 0
|
| 248 |
+
|
| 249 |
+
self.screen = _s_ = turtle.Screen()
|
| 250 |
+
turtle.TurtleScreen.__init__(_s_, _s_._canvas)
|
| 251 |
+
self.scanvas = _s_._canvas
|
| 252 |
+
turtle.RawTurtle.screens = [_s_]
|
| 253 |
+
return canvas
|
| 254 |
+
|
| 255 |
+
def set_txtsize(self, size):
|
| 256 |
+
txtfont[1] = size
|
| 257 |
+
self.text['font'] = tuple(txtfont)
|
| 258 |
+
self.output_lbl['text'] = 'Font size %d' % size
|
| 259 |
+
|
| 260 |
+
def decrease_size(self, dummy=None):
|
| 261 |
+
self.set_txtsize(max(txtfont[1] - 1, MINIMUM_FONT_SIZE))
|
| 262 |
+
return 'break'
|
| 263 |
+
|
| 264 |
+
def increase_size(self, dummy=None):
|
| 265 |
+
self.set_txtsize(min(txtfont[1] + 1, MAXIMUM_FONT_SIZE))
|
| 266 |
+
return 'break'
|
| 267 |
+
|
| 268 |
+
def update_mousewheel(self, event):
|
| 269 |
+
# For wheel up, event.delta = 120 on Windows, -1 on darwin.
|
| 270 |
+
# X-11 sends Control-Button-4 event instead.
|
| 271 |
+
if (event.delta < 0) == (not darwin):
|
| 272 |
+
return self.decrease_size()
|
| 273 |
+
else:
|
| 274 |
+
return self.increase_size()
|
| 275 |
+
|
| 276 |
+
def configGUI(self, start, stop, clear, txt="", color="blue"):
|
| 277 |
+
if darwin: # Leave Mac button colors alone - #44254.
|
| 278 |
+
self.start_btn.config(state=start)
|
| 279 |
+
self.stop_btn.config(state=stop)
|
| 280 |
+
self.clear_btn.config(state=clear)
|
| 281 |
+
else:
|
| 282 |
+
self.start_btn.config(state=start,
|
| 283 |
+
bg="#d00" if start == NORMAL else "#fca")
|
| 284 |
+
self.stop_btn.config(state=stop,
|
| 285 |
+
bg="#d00" if stop == NORMAL else "#fca")
|
| 286 |
+
self.clear_btn.config(state=clear,
|
| 287 |
+
bg="#d00" if clear == NORMAL else "#fca")
|
| 288 |
+
self.output_lbl.config(text=txt, fg=color)
|
| 289 |
+
|
| 290 |
+
def makeLoadDemoMenu(self, master):
|
| 291 |
+
menu = Menu(master)
|
| 292 |
+
|
| 293 |
+
for entry in getExampleEntries():
|
| 294 |
+
def load(entry=entry):
|
| 295 |
+
self.loadfile(entry)
|
| 296 |
+
menu.add_command(label=entry, underline=0,
|
| 297 |
+
font=menufont, command=load)
|
| 298 |
+
return menu
|
| 299 |
+
|
| 300 |
+
def makeFontMenu(self, master):
|
| 301 |
+
menu = Menu(master)
|
| 302 |
+
menu.add_command(label="Decrease (C-'-')", command=self.decrease_size,
|
| 303 |
+
font=menufont)
|
| 304 |
+
menu.add_command(label="Increase (C-'+')", command=self.increase_size,
|
| 305 |
+
font=menufont)
|
| 306 |
+
menu.add_separator()
|
| 307 |
+
|
| 308 |
+
for size in font_sizes:
|
| 309 |
+
def resize(size=size):
|
| 310 |
+
self.set_txtsize(size)
|
| 311 |
+
menu.add_command(label=str(size), underline=0,
|
| 312 |
+
font=menufont, command=resize)
|
| 313 |
+
return menu
|
| 314 |
+
|
| 315 |
+
def makeHelpMenu(self, master):
|
| 316 |
+
menu = Menu(master)
|
| 317 |
+
|
| 318 |
+
for help_label, help_file in help_entries:
|
| 319 |
+
def show(help_label=help_label, help_file=help_file):
|
| 320 |
+
view_text(self.root, help_label, help_file)
|
| 321 |
+
menu.add_command(label=help_label, font=menufont, command=show)
|
| 322 |
+
return menu
|
| 323 |
+
|
| 324 |
+
def refreshCanvas(self):
|
| 325 |
+
if self.dirty:
|
| 326 |
+
self.screen.clear()
|
| 327 |
+
self.dirty=False
|
| 328 |
+
|
| 329 |
+
def loadfile(self, filename):
|
| 330 |
+
self.clearCanvas()
|
| 331 |
+
turtle.TurtleScreen._RUNNING = False
|
| 332 |
+
modname = 'turtledemo.' + filename
|
| 333 |
+
__import__(modname)
|
| 334 |
+
self.module = sys.modules[modname]
|
| 335 |
+
with open(self.module.__file__, 'r') as f:
|
| 336 |
+
chars = f.read()
|
| 337 |
+
self.text.delete("1.0", "end")
|
| 338 |
+
self.text.insert("1.0", chars)
|
| 339 |
+
self.root.title(filename + " - a Python turtle graphics example")
|
| 340 |
+
self.configGUI(NORMAL, DISABLED, DISABLED,
|
| 341 |
+
"Press start button", "red")
|
| 342 |
+
self.state = READY
|
| 343 |
+
|
| 344 |
+
def startDemo(self):
|
| 345 |
+
self.refreshCanvas()
|
| 346 |
+
self.dirty = True
|
| 347 |
+
turtle.TurtleScreen._RUNNING = True
|
| 348 |
+
self.configGUI(DISABLED, NORMAL, DISABLED,
|
| 349 |
+
"demo running...", "black")
|
| 350 |
+
self.screen.clear()
|
| 351 |
+
self.screen.mode("standard")
|
| 352 |
+
self.state = RUNNING
|
| 353 |
+
|
| 354 |
+
try:
|
| 355 |
+
result = self.module.main()
|
| 356 |
+
if result == "EVENTLOOP":
|
| 357 |
+
self.state = EVENTDRIVEN
|
| 358 |
+
else:
|
| 359 |
+
self.state = DONE
|
| 360 |
+
except turtle.Terminator:
|
| 361 |
+
if self.root is None:
|
| 362 |
+
return
|
| 363 |
+
self.state = DONE
|
| 364 |
+
result = "stopped!"
|
| 365 |
+
if self.state == DONE:
|
| 366 |
+
self.configGUI(NORMAL, DISABLED, NORMAL,
|
| 367 |
+
result)
|
| 368 |
+
elif self.state == EVENTDRIVEN:
|
| 369 |
+
self.exitflag = True
|
| 370 |
+
self.configGUI(DISABLED, NORMAL, DISABLED,
|
| 371 |
+
"use mouse/keys or STOP", "red")
|
| 372 |
+
|
| 373 |
+
def clearCanvas(self):
|
| 374 |
+
self.refreshCanvas()
|
| 375 |
+
self.screen._delete("all")
|
| 376 |
+
self.scanvas.config(cursor="")
|
| 377 |
+
self.configGUI(NORMAL, DISABLED, DISABLED)
|
| 378 |
+
|
| 379 |
+
def stopIt(self):
|
| 380 |
+
if self.exitflag:
|
| 381 |
+
self.clearCanvas()
|
| 382 |
+
self.exitflag = False
|
| 383 |
+
self.configGUI(NORMAL, DISABLED, DISABLED,
|
| 384 |
+
"STOPPED!", "red")
|
| 385 |
+
turtle.TurtleScreen._RUNNING = False
|
| 386 |
+
|
| 387 |
+
def _destroy(self):
|
| 388 |
+
turtle.TurtleScreen._RUNNING = False
|
| 389 |
+
self.root.destroy()
|
| 390 |
+
self.root = None
|
| 391 |
+
|
| 392 |
+
|
| 393 |
+
def main():
|
| 394 |
+
demo = DemoWindow()
|
| 395 |
+
demo.root.mainloop()
|
| 396 |
+
|
| 397 |
+
if __name__ == '__main__':
|
| 398 |
+
main()
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/__main__.cpython-310.pyc
ADDED
|
Binary file (13.6 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/chaos.cpython-310.pyc
ADDED
|
Binary file (1.97 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/clock.cpython-310.pyc
ADDED
|
Binary file (3.69 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/colormixer.cpython-310.pyc
ADDED
|
Binary file (2.1 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/forest.cpython-310.pyc
ADDED
|
Binary file (3.54 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/fractalcurves.cpython-310.pyc
ADDED
|
Binary file (3.15 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/minimal_hanoi.cpython-310.pyc
ADDED
|
Binary file (3.09 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/nim.cpython-310.pyc
ADDED
|
Binary file (7.66 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/paint.cpython-310.pyc
ADDED
|
Binary file (1.62 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/peace.cpython-310.pyc
ADDED
|
Binary file (1.38 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/planet_and_moon.cpython-310.pyc
ADDED
|
Binary file (3.5 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/rosette.cpython-310.pyc
ADDED
|
Binary file (1.73 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/round_dance.cpython-310.pyc
ADDED
|
Binary file (1.9 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/sorting_animate.cpython-310.pyc
ADDED
|
Binary file (6.33 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/tree.cpython-310.pyc
ADDED
|
Binary file (2 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/two_canvases.cpython-310.pyc
ADDED
|
Binary file (1.53 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/__pycache__/yinyang.cpython-310.pyc
ADDED
|
Binary file (1.07 kB). View file
|
|
|
evalkit_tf446/lib/python3.10/turtledemo/bytedesign.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
""" turtle-example-suite:
|
| 3 |
+
|
| 4 |
+
tdemo_bytedesign.py
|
| 5 |
+
|
| 6 |
+
An example adapted from the example-suite
|
| 7 |
+
of PythonCard's turtle graphics.
|
| 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 |
+
from turtle import Turtle, mainloop
|
| 26 |
+
from time import perf_counter as clock
|
| 27 |
+
|
| 28 |
+
# wrapper for any additional drawing routines
|
| 29 |
+
# that need to know about each other
|
| 30 |
+
class Designer(Turtle):
|
| 31 |
+
|
| 32 |
+
def design(self, homePos, scale):
|
| 33 |
+
self.up()
|
| 34 |
+
for i in range(5):
|
| 35 |
+
self.forward(64.65 * scale)
|
| 36 |
+
self.down()
|
| 37 |
+
self.wheel(self.position(), scale)
|
| 38 |
+
self.up()
|
| 39 |
+
self.backward(64.65 * scale)
|
| 40 |
+
self.right(72)
|
| 41 |
+
self.up()
|
| 42 |
+
self.goto(homePos)
|
| 43 |
+
self.right(36)
|
| 44 |
+
self.forward(24.5 * scale)
|
| 45 |
+
self.right(198)
|
| 46 |
+
self.down()
|
| 47 |
+
self.centerpiece(46 * scale, 143.4, scale)
|
| 48 |
+
self.getscreen().tracer(True)
|
| 49 |
+
|
| 50 |
+
def wheel(self, initpos, scale):
|
| 51 |
+
self.right(54)
|
| 52 |
+
for i in range(4):
|
| 53 |
+
self.pentpiece(initpos, scale)
|
| 54 |
+
self.down()
|
| 55 |
+
self.left(36)
|
| 56 |
+
for i in range(5):
|
| 57 |
+
self.tripiece(initpos, scale)
|
| 58 |
+
self.left(36)
|
| 59 |
+
for i in range(5):
|
| 60 |
+
self.down()
|
| 61 |
+
self.right(72)
|
| 62 |
+
self.forward(28 * scale)
|
| 63 |
+
self.up()
|
| 64 |
+
self.backward(28 * scale)
|
| 65 |
+
self.left(54)
|
| 66 |
+
self.getscreen().update()
|
| 67 |
+
|
| 68 |
+
def tripiece(self, initpos, scale):
|
| 69 |
+
oldh = self.heading()
|
| 70 |
+
self.down()
|
| 71 |
+
self.backward(2.5 * scale)
|
| 72 |
+
self.tripolyr(31.5 * scale, scale)
|
| 73 |
+
self.up()
|
| 74 |
+
self.goto(initpos)
|
| 75 |
+
self.setheading(oldh)
|
| 76 |
+
self.down()
|
| 77 |
+
self.backward(2.5 * scale)
|
| 78 |
+
self.tripolyl(31.5 * scale, scale)
|
| 79 |
+
self.up()
|
| 80 |
+
self.goto(initpos)
|
| 81 |
+
self.setheading(oldh)
|
| 82 |
+
self.left(72)
|
| 83 |
+
self.getscreen().update()
|
| 84 |
+
|
| 85 |
+
def pentpiece(self, initpos, scale):
|
| 86 |
+
oldh = self.heading()
|
| 87 |
+
self.up()
|
| 88 |
+
self.forward(29 * scale)
|
| 89 |
+
self.down()
|
| 90 |
+
for i in range(5):
|
| 91 |
+
self.forward(18 * scale)
|
| 92 |
+
self.right(72)
|
| 93 |
+
self.pentr(18 * scale, 75, scale)
|
| 94 |
+
self.up()
|
| 95 |
+
self.goto(initpos)
|
| 96 |
+
self.setheading(oldh)
|
| 97 |
+
self.forward(29 * scale)
|
| 98 |
+
self.down()
|
| 99 |
+
for i in range(5):
|
| 100 |
+
self.forward(18 * scale)
|
| 101 |
+
self.right(72)
|
| 102 |
+
self.pentl(18 * scale, 75, scale)
|
| 103 |
+
self.up()
|
| 104 |
+
self.goto(initpos)
|
| 105 |
+
self.setheading(oldh)
|
| 106 |
+
self.left(72)
|
| 107 |
+
self.getscreen().update()
|
| 108 |
+
|
| 109 |
+
def pentl(self, side, ang, scale):
|
| 110 |
+
if side < (2 * scale): return
|
| 111 |
+
self.forward(side)
|
| 112 |
+
self.left(ang)
|
| 113 |
+
self.pentl(side - (.38 * scale), ang, scale)
|
| 114 |
+
|
| 115 |
+
def pentr(self, side, ang, scale):
|
| 116 |
+
if side < (2 * scale): return
|
| 117 |
+
self.forward(side)
|
| 118 |
+
self.right(ang)
|
| 119 |
+
self.pentr(side - (.38 * scale), ang, scale)
|
| 120 |
+
|
| 121 |
+
def tripolyr(self, side, scale):
|
| 122 |
+
if side < (4 * scale): return
|
| 123 |
+
self.forward(side)
|
| 124 |
+
self.right(111)
|
| 125 |
+
self.forward(side / 1.78)
|
| 126 |
+
self.right(111)
|
| 127 |
+
self.forward(side / 1.3)
|
| 128 |
+
self.right(146)
|
| 129 |
+
self.tripolyr(side * .75, scale)
|
| 130 |
+
|
| 131 |
+
def tripolyl(self, side, scale):
|
| 132 |
+
if side < (4 * scale): return
|
| 133 |
+
self.forward(side)
|
| 134 |
+
self.left(111)
|
| 135 |
+
self.forward(side / 1.78)
|
| 136 |
+
self.left(111)
|
| 137 |
+
self.forward(side / 1.3)
|
| 138 |
+
self.left(146)
|
| 139 |
+
self.tripolyl(side * .75, scale)
|
| 140 |
+
|
| 141 |
+
def centerpiece(self, s, a, scale):
|
| 142 |
+
self.forward(s); self.left(a)
|
| 143 |
+
if s < (7.5 * scale):
|
| 144 |
+
return
|
| 145 |
+
self.centerpiece(s - (1.2 * scale), a, scale)
|
| 146 |
+
|
| 147 |
+
def main():
|
| 148 |
+
t = Designer()
|
| 149 |
+
t.speed(0)
|
| 150 |
+
t.hideturtle()
|
| 151 |
+
t.getscreen().delay(0)
|
| 152 |
+
t.getscreen().tracer(0)
|
| 153 |
+
at = clock()
|
| 154 |
+
t.design(t.position(), 2)
|
| 155 |
+
et = clock()
|
| 156 |
+
return "runtime: %.2f sec." % (et-at)
|
| 157 |
+
|
| 158 |
+
if __name__ == '__main__':
|
| 159 |
+
msg = main()
|
| 160 |
+
print(msg)
|
| 161 |
+
mainloop()
|
evalkit_tf446/lib/python3.10/turtledemo/chaos.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# File: tdemo_chaos.py
|
| 2 |
+
# Author: Gregor Lingl
|
| 3 |
+
# Date: 2009-06-24
|
| 4 |
+
|
| 5 |
+
# A demonstration of chaos
|
| 6 |
+
|
| 7 |
+
from turtle import *
|
| 8 |
+
|
| 9 |
+
N = 80
|
| 10 |
+
|
| 11 |
+
def f(x):
|
| 12 |
+
return 3.9*x*(1-x)
|
| 13 |
+
|
| 14 |
+
def g(x):
|
| 15 |
+
return 3.9*(x-x**2)
|
| 16 |
+
|
| 17 |
+
def h(x):
|
| 18 |
+
return 3.9*x-3.9*x*x
|
| 19 |
+
|
| 20 |
+
def jumpto(x, y):
|
| 21 |
+
penup(); goto(x,y)
|
| 22 |
+
|
| 23 |
+
def line(x1, y1, x2, y2):
|
| 24 |
+
jumpto(x1, y1)
|
| 25 |
+
pendown()
|
| 26 |
+
goto(x2, y2)
|
| 27 |
+
|
| 28 |
+
def coosys():
|
| 29 |
+
line(-1, 0, N+1, 0)
|
| 30 |
+
line(0, -0.1, 0, 1.1)
|
| 31 |
+
|
| 32 |
+
def plot(fun, start, color):
|
| 33 |
+
pencolor(color)
|
| 34 |
+
x = start
|
| 35 |
+
jumpto(0, x)
|
| 36 |
+
pendown()
|
| 37 |
+
dot(5)
|
| 38 |
+
for i in range(N):
|
| 39 |
+
x=fun(x)
|
| 40 |
+
goto(i+1,x)
|
| 41 |
+
dot(5)
|
| 42 |
+
|
| 43 |
+
def main():
|
| 44 |
+
reset()
|
| 45 |
+
setworldcoordinates(-1.0,-0.1, N+1, 1.1)
|
| 46 |
+
speed(0)
|
| 47 |
+
hideturtle()
|
| 48 |
+
coosys()
|
| 49 |
+
plot(f, 0.35, "blue")
|
| 50 |
+
plot(g, 0.35, "green")
|
| 51 |
+
plot(h, 0.35, "red")
|
| 52 |
+
# Now zoom in:
|
| 53 |
+
for s in range(100):
|
| 54 |
+
setworldcoordinates(0.5*s,-0.1, N+1, 1.1)
|
| 55 |
+
return "Done!"
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
main()
|
| 59 |
+
mainloop()
|
evalkit_tf446/lib/python3.10/turtledemo/clock.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# -*- coding: cp1252 -*-
|
| 3 |
+
""" turtle-example-suite:
|
| 4 |
+
|
| 5 |
+
tdemo_clock.py
|
| 6 |
+
|
| 7 |
+
Enhanced clock-program, showing date
|
| 8 |
+
and time
|
| 9 |
+
------------------------------------
|
| 10 |
+
Press STOP to exit the program!
|
| 11 |
+
------------------------------------
|
| 12 |
+
"""
|
| 13 |
+
from turtle import *
|
| 14 |
+
from datetime import datetime
|
| 15 |
+
|
| 16 |
+
def jump(distanz, winkel=0):
|
| 17 |
+
penup()
|
| 18 |
+
right(winkel)
|
| 19 |
+
forward(distanz)
|
| 20 |
+
left(winkel)
|
| 21 |
+
pendown()
|
| 22 |
+
|
| 23 |
+
def hand(laenge, spitze):
|
| 24 |
+
fd(laenge*1.15)
|
| 25 |
+
rt(90)
|
| 26 |
+
fd(spitze/2.0)
|
| 27 |
+
lt(120)
|
| 28 |
+
fd(spitze)
|
| 29 |
+
lt(120)
|
| 30 |
+
fd(spitze)
|
| 31 |
+
lt(120)
|
| 32 |
+
fd(spitze/2.0)
|
| 33 |
+
|
| 34 |
+
def make_hand_shape(name, laenge, spitze):
|
| 35 |
+
reset()
|
| 36 |
+
jump(-laenge*0.15)
|
| 37 |
+
begin_poly()
|
| 38 |
+
hand(laenge, spitze)
|
| 39 |
+
end_poly()
|
| 40 |
+
hand_form = get_poly()
|
| 41 |
+
register_shape(name, hand_form)
|
| 42 |
+
|
| 43 |
+
def clockface(radius):
|
| 44 |
+
reset()
|
| 45 |
+
pensize(7)
|
| 46 |
+
for i in range(60):
|
| 47 |
+
jump(radius)
|
| 48 |
+
if i % 5 == 0:
|
| 49 |
+
fd(25)
|
| 50 |
+
jump(-radius-25)
|
| 51 |
+
else:
|
| 52 |
+
dot(3)
|
| 53 |
+
jump(-radius)
|
| 54 |
+
rt(6)
|
| 55 |
+
|
| 56 |
+
def setup():
|
| 57 |
+
global second_hand, minute_hand, hour_hand, writer
|
| 58 |
+
mode("logo")
|
| 59 |
+
make_hand_shape("second_hand", 125, 25)
|
| 60 |
+
make_hand_shape("minute_hand", 130, 25)
|
| 61 |
+
make_hand_shape("hour_hand", 90, 25)
|
| 62 |
+
clockface(160)
|
| 63 |
+
second_hand = Turtle()
|
| 64 |
+
second_hand.shape("second_hand")
|
| 65 |
+
second_hand.color("gray20", "gray80")
|
| 66 |
+
minute_hand = Turtle()
|
| 67 |
+
minute_hand.shape("minute_hand")
|
| 68 |
+
minute_hand.color("blue1", "red1")
|
| 69 |
+
hour_hand = Turtle()
|
| 70 |
+
hour_hand.shape("hour_hand")
|
| 71 |
+
hour_hand.color("blue3", "red3")
|
| 72 |
+
for hand in second_hand, minute_hand, hour_hand:
|
| 73 |
+
hand.resizemode("user")
|
| 74 |
+
hand.shapesize(1, 1, 3)
|
| 75 |
+
hand.speed(0)
|
| 76 |
+
ht()
|
| 77 |
+
writer = Turtle()
|
| 78 |
+
#writer.mode("logo")
|
| 79 |
+
writer.ht()
|
| 80 |
+
writer.pu()
|
| 81 |
+
writer.bk(85)
|
| 82 |
+
|
| 83 |
+
def wochentag(t):
|
| 84 |
+
wochentag = ["Monday", "Tuesday", "Wednesday",
|
| 85 |
+
"Thursday", "Friday", "Saturday", "Sunday"]
|
| 86 |
+
return wochentag[t.weekday()]
|
| 87 |
+
|
| 88 |
+
def datum(z):
|
| 89 |
+
monat = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "June",
|
| 90 |
+
"July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."]
|
| 91 |
+
j = z.year
|
| 92 |
+
m = monat[z.month - 1]
|
| 93 |
+
t = z.day
|
| 94 |
+
return "%s %d %d" % (m, t, j)
|
| 95 |
+
|
| 96 |
+
def tick():
|
| 97 |
+
t = datetime.today()
|
| 98 |
+
sekunde = t.second + t.microsecond*0.000001
|
| 99 |
+
minute = t.minute + sekunde/60.0
|
| 100 |
+
stunde = t.hour + minute/60.0
|
| 101 |
+
try:
|
| 102 |
+
tracer(False) # Terminator can occur here
|
| 103 |
+
writer.clear()
|
| 104 |
+
writer.home()
|
| 105 |
+
writer.forward(65)
|
| 106 |
+
writer.write(wochentag(t),
|
| 107 |
+
align="center", font=("Courier", 14, "bold"))
|
| 108 |
+
writer.back(150)
|
| 109 |
+
writer.write(datum(t),
|
| 110 |
+
align="center", font=("Courier", 14, "bold"))
|
| 111 |
+
writer.forward(85)
|
| 112 |
+
second_hand.setheading(6*sekunde) # or here
|
| 113 |
+
minute_hand.setheading(6*minute)
|
| 114 |
+
hour_hand.setheading(30*stunde)
|
| 115 |
+
tracer(True)
|
| 116 |
+
ontimer(tick, 100)
|
| 117 |
+
except Terminator:
|
| 118 |
+
pass # turtledemo user pressed STOP
|
| 119 |
+
|
| 120 |
+
def main():
|
| 121 |
+
tracer(False)
|
| 122 |
+
setup()
|
| 123 |
+
tracer(True)
|
| 124 |
+
tick()
|
| 125 |
+
return "EVENTLOOP"
|
| 126 |
+
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
mode("logo")
|
| 129 |
+
msg = main()
|
| 130 |
+
print(msg)
|
| 131 |
+
mainloop()
|
evalkit_tf446/lib/python3.10/turtledemo/colormixer.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# colormixer
|
| 2 |
+
|
| 3 |
+
from turtle import Screen, Turtle, mainloop
|
| 4 |
+
|
| 5 |
+
class ColorTurtle(Turtle):
|
| 6 |
+
|
| 7 |
+
def __init__(self, x, y):
|
| 8 |
+
Turtle.__init__(self)
|
| 9 |
+
self.shape("turtle")
|
| 10 |
+
self.resizemode("user")
|
| 11 |
+
self.shapesize(3,3,5)
|
| 12 |
+
self.pensize(10)
|
| 13 |
+
self._color = [0,0,0]
|
| 14 |
+
self.x = x
|
| 15 |
+
self._color[x] = y
|
| 16 |
+
self.color(self._color)
|
| 17 |
+
self.speed(0)
|
| 18 |
+
self.left(90)
|
| 19 |
+
self.pu()
|
| 20 |
+
self.goto(x,0)
|
| 21 |
+
self.pd()
|
| 22 |
+
self.sety(1)
|
| 23 |
+
self.pu()
|
| 24 |
+
self.sety(y)
|
| 25 |
+
self.pencolor("gray25")
|
| 26 |
+
self.ondrag(self.shift)
|
| 27 |
+
|
| 28 |
+
def shift(self, x, y):
|
| 29 |
+
self.sety(max(0,min(y,1)))
|
| 30 |
+
self._color[self.x] = self.ycor()
|
| 31 |
+
self.fillcolor(self._color)
|
| 32 |
+
setbgcolor()
|
| 33 |
+
|
| 34 |
+
def setbgcolor():
|
| 35 |
+
screen.bgcolor(red.ycor(), green.ycor(), blue.ycor())
|
| 36 |
+
|
| 37 |
+
def main():
|
| 38 |
+
global screen, red, green, blue
|
| 39 |
+
screen = Screen()
|
| 40 |
+
screen.delay(0)
|
| 41 |
+
screen.setworldcoordinates(-1, -0.3, 3, 1.3)
|
| 42 |
+
|
| 43 |
+
red = ColorTurtle(0, .5)
|
| 44 |
+
green = ColorTurtle(1, .5)
|
| 45 |
+
blue = ColorTurtle(2, .5)
|
| 46 |
+
setbgcolor()
|
| 47 |
+
|
| 48 |
+
writer = Turtle()
|
| 49 |
+
writer.ht()
|
| 50 |
+
writer.pu()
|
| 51 |
+
writer.goto(1,1.15)
|
| 52 |
+
writer.write("DRAG!",align="center",font=("Arial",30,("bold","italic")))
|
| 53 |
+
return "EVENTLOOP"
|
| 54 |
+
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
msg = main()
|
| 57 |
+
print(msg)
|
| 58 |
+
mainloop()
|
evalkit_tf446/lib/python3.10/turtledemo/forest.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
""" turtlegraphics-example-suite:
|
| 3 |
+
|
| 4 |
+
tdemo_forest.py
|
| 5 |
+
|
| 6 |
+
Displays a 'forest' of 3 breadth-first-trees
|
| 7 |
+
similar to the one in tree.
|
| 8 |
+
For further remarks see tree.py
|
| 9 |
+
|
| 10 |
+
This example is a 'breadth-first'-rewrite of
|
| 11 |
+
a Logo program written by Erich Neuwirth. See
|
| 12 |
+
http://homepage.univie.ac.at/erich.neuwirth/
|
| 13 |
+
"""
|
| 14 |
+
from turtle import Turtle, colormode, tracer, mainloop
|
| 15 |
+
from random import randrange
|
| 16 |
+
from time import perf_counter as clock
|
| 17 |
+
|
| 18 |
+
def symRandom(n):
|
| 19 |
+
return randrange(-n,n+1)
|
| 20 |
+
|
| 21 |
+
def randomize( branchlist, angledist, sizedist ):
|
| 22 |
+
return [ (angle+symRandom(angledist),
|
| 23 |
+
sizefactor*1.01**symRandom(sizedist))
|
| 24 |
+
for angle, sizefactor in branchlist ]
|
| 25 |
+
|
| 26 |
+
def randomfd( t, distance, parts, angledist ):
|
| 27 |
+
for i in range(parts):
|
| 28 |
+
t.left(symRandom(angledist))
|
| 29 |
+
t.forward( (1.0 * distance)/parts )
|
| 30 |
+
|
| 31 |
+
def tree(tlist, size, level, widthfactor, branchlists, angledist=10, sizedist=5):
|
| 32 |
+
# benutzt Liste von turtles und Liste von Zweiglisten,
|
| 33 |
+
# fuer jede turtle eine!
|
| 34 |
+
if level > 0:
|
| 35 |
+
lst = []
|
| 36 |
+
brs = []
|
| 37 |
+
for t, branchlist in list(zip(tlist,branchlists)):
|
| 38 |
+
t.pensize( size * widthfactor )
|
| 39 |
+
t.pencolor( 255 - (180 - 11 * level + symRandom(15)),
|
| 40 |
+
180 - 11 * level + symRandom(15),
|
| 41 |
+
0 )
|
| 42 |
+
t.pendown()
|
| 43 |
+
randomfd(t, size, level, angledist )
|
| 44 |
+
yield 1
|
| 45 |
+
for angle, sizefactor in branchlist:
|
| 46 |
+
t.left(angle)
|
| 47 |
+
lst.append(t.clone())
|
| 48 |
+
brs.append(randomize(branchlist, angledist, sizedist))
|
| 49 |
+
t.right(angle)
|
| 50 |
+
for x in tree(lst, size*sizefactor, level-1, widthfactor, brs,
|
| 51 |
+
angledist, sizedist):
|
| 52 |
+
yield None
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def start(t,x,y):
|
| 56 |
+
colormode(255)
|
| 57 |
+
t.reset()
|
| 58 |
+
t.speed(0)
|
| 59 |
+
t.hideturtle()
|
| 60 |
+
t.left(90)
|
| 61 |
+
t.penup()
|
| 62 |
+
t.setpos(x,y)
|
| 63 |
+
t.pendown()
|
| 64 |
+
|
| 65 |
+
def doit1(level, pen):
|
| 66 |
+
pen.hideturtle()
|
| 67 |
+
start(pen, 20, -208)
|
| 68 |
+
t = tree( [pen], 80, level, 0.1, [[ (45,0.69), (0,0.65), (-45,0.71) ]] )
|
| 69 |
+
return t
|
| 70 |
+
|
| 71 |
+
def doit2(level, pen):
|
| 72 |
+
pen.hideturtle()
|
| 73 |
+
start(pen, -135, -130)
|
| 74 |
+
t = tree( [pen], 120, level, 0.1, [[ (45,0.69), (-45,0.71) ]] )
|
| 75 |
+
return t
|
| 76 |
+
|
| 77 |
+
def doit3(level, pen):
|
| 78 |
+
pen.hideturtle()
|
| 79 |
+
start(pen, 190, -90)
|
| 80 |
+
t = tree( [pen], 100, level, 0.1, [[ (45,0.7), (0,0.72), (-45,0.65) ]] )
|
| 81 |
+
return t
|
| 82 |
+
|
| 83 |
+
# Hier 3 Baumgeneratoren:
|
| 84 |
+
def main():
|
| 85 |
+
p = Turtle()
|
| 86 |
+
p.ht()
|
| 87 |
+
tracer(75,0)
|
| 88 |
+
u = doit1(6, Turtle(undobuffersize=1))
|
| 89 |
+
s = doit2(7, Turtle(undobuffersize=1))
|
| 90 |
+
t = doit3(5, Turtle(undobuffersize=1))
|
| 91 |
+
a = clock()
|
| 92 |
+
while True:
|
| 93 |
+
done = 0
|
| 94 |
+
for b in u,s,t:
|
| 95 |
+
try:
|
| 96 |
+
b.__next__()
|
| 97 |
+
except:
|
| 98 |
+
done += 1
|
| 99 |
+
if done == 3:
|
| 100 |
+
break
|
| 101 |
+
|
| 102 |
+
tracer(1,10)
|
| 103 |
+
b = clock()
|
| 104 |
+
return "runtime: %.2f sec." % (b-a)
|
| 105 |
+
|
| 106 |
+
if __name__ == '__main__':
|
| 107 |
+
main()
|
| 108 |
+
mainloop()
|
evalkit_tf446/lib/python3.10/turtledemo/fractalcurves.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
""" turtle-example-suite:
|
| 3 |
+
|
| 4 |
+
tdemo_fractalCurves.py
|
| 5 |
+
|
| 6 |
+
This program draws two fractal-curve-designs:
|
| 7 |
+
(1) A hilbert curve (in a box)
|
| 8 |
+
(2) A combination of Koch-curves.
|
| 9 |
+
|
| 10 |
+
The CurvesTurtle class and the fractal-curve-
|
| 11 |
+
methods are taken from the PythonCard example
|
| 12 |
+
scripts for turtle-graphics.
|
| 13 |
+
"""
|
| 14 |
+
from turtle import *
|
| 15 |
+
from time import sleep, perf_counter as clock
|
| 16 |
+
|
| 17 |
+
class CurvesTurtle(Pen):
|
| 18 |
+
# example derived from
|
| 19 |
+
# Turtle Geometry: The Computer as a Medium for Exploring Mathematics
|
| 20 |
+
# by Harold Abelson and Andrea diSessa
|
| 21 |
+
# p. 96-98
|
| 22 |
+
def hilbert(self, size, level, parity):
|
| 23 |
+
if level == 0:
|
| 24 |
+
return
|
| 25 |
+
# rotate and draw first subcurve with opposite parity to big curve
|
| 26 |
+
self.left(parity * 90)
|
| 27 |
+
self.hilbert(size, level - 1, -parity)
|
| 28 |
+
# interface to and draw second subcurve with same parity as big curve
|
| 29 |
+
self.forward(size)
|
| 30 |
+
self.right(parity * 90)
|
| 31 |
+
self.hilbert(size, level - 1, parity)
|
| 32 |
+
# third subcurve
|
| 33 |
+
self.forward(size)
|
| 34 |
+
self.hilbert(size, level - 1, parity)
|
| 35 |
+
# fourth subcurve
|
| 36 |
+
self.right(parity * 90)
|
| 37 |
+
self.forward(size)
|
| 38 |
+
self.hilbert(size, level - 1, -parity)
|
| 39 |
+
# a final turn is needed to make the turtle
|
| 40 |
+
# end up facing outward from the large square
|
| 41 |
+
self.left(parity * 90)
|
| 42 |
+
|
| 43 |
+
# Visual Modeling with Logo: A Structural Approach to Seeing
|
| 44 |
+
# by James Clayson
|
| 45 |
+
# Koch curve, after Helge von Koch who introduced this geometric figure in 1904
|
| 46 |
+
# p. 146
|
| 47 |
+
def fractalgon(self, n, rad, lev, dir):
|
| 48 |
+
import math
|
| 49 |
+
|
| 50 |
+
# if dir = 1 turn outward
|
| 51 |
+
# if dir = -1 turn inward
|
| 52 |
+
edge = 2 * rad * math.sin(math.pi / n)
|
| 53 |
+
self.pu()
|
| 54 |
+
self.fd(rad)
|
| 55 |
+
self.pd()
|
| 56 |
+
self.rt(180 - (90 * (n - 2) / n))
|
| 57 |
+
for i in range(n):
|
| 58 |
+
self.fractal(edge, lev, dir)
|
| 59 |
+
self.rt(360 / n)
|
| 60 |
+
self.lt(180 - (90 * (n - 2) / n))
|
| 61 |
+
self.pu()
|
| 62 |
+
self.bk(rad)
|
| 63 |
+
self.pd()
|
| 64 |
+
|
| 65 |
+
# p. 146
|
| 66 |
+
def fractal(self, dist, depth, dir):
|
| 67 |
+
if depth < 1:
|
| 68 |
+
self.fd(dist)
|
| 69 |
+
return
|
| 70 |
+
self.fractal(dist / 3, depth - 1, dir)
|
| 71 |
+
self.lt(60 * dir)
|
| 72 |
+
self.fractal(dist / 3, depth - 1, dir)
|
| 73 |
+
self.rt(120 * dir)
|
| 74 |
+
self.fractal(dist / 3, depth - 1, dir)
|
| 75 |
+
self.lt(60 * dir)
|
| 76 |
+
self.fractal(dist / 3, depth - 1, dir)
|
| 77 |
+
|
| 78 |
+
def main():
|
| 79 |
+
ft = CurvesTurtle()
|
| 80 |
+
|
| 81 |
+
ft.reset()
|
| 82 |
+
ft.speed(0)
|
| 83 |
+
ft.ht()
|
| 84 |
+
ft.getscreen().tracer(1,0)
|
| 85 |
+
ft.pu()
|
| 86 |
+
|
| 87 |
+
size = 6
|
| 88 |
+
ft.setpos(-33*size, -32*size)
|
| 89 |
+
ft.pd()
|
| 90 |
+
|
| 91 |
+
ta=clock()
|
| 92 |
+
ft.fillcolor("red")
|
| 93 |
+
ft.begin_fill()
|
| 94 |
+
ft.fd(size)
|
| 95 |
+
|
| 96 |
+
ft.hilbert(size, 6, 1)
|
| 97 |
+
|
| 98 |
+
# frame
|
| 99 |
+
ft.fd(size)
|
| 100 |
+
for i in range(3):
|
| 101 |
+
ft.lt(90)
|
| 102 |
+
ft.fd(size*(64+i%2))
|
| 103 |
+
ft.pu()
|
| 104 |
+
for i in range(2):
|
| 105 |
+
ft.fd(size)
|
| 106 |
+
ft.rt(90)
|
| 107 |
+
ft.pd()
|
| 108 |
+
for i in range(4):
|
| 109 |
+
ft.fd(size*(66+i%2))
|
| 110 |
+
ft.rt(90)
|
| 111 |
+
ft.end_fill()
|
| 112 |
+
tb=clock()
|
| 113 |
+
res = "Hilbert: %.2fsec. " % (tb-ta)
|
| 114 |
+
|
| 115 |
+
sleep(3)
|
| 116 |
+
|
| 117 |
+
ft.reset()
|
| 118 |
+
ft.speed(0)
|
| 119 |
+
ft.ht()
|
| 120 |
+
ft.getscreen().tracer(1,0)
|
| 121 |
+
|
| 122 |
+
ta=clock()
|
| 123 |
+
ft.color("black", "blue")
|
| 124 |
+
ft.begin_fill()
|
| 125 |
+
ft.fractalgon(3, 250, 4, 1)
|
| 126 |
+
ft.end_fill()
|
| 127 |
+
ft.begin_fill()
|
| 128 |
+
ft.color("red")
|
| 129 |
+
ft.fractalgon(3, 200, 4, -1)
|
| 130 |
+
ft.end_fill()
|
| 131 |
+
tb=clock()
|
| 132 |
+
res += "Koch: %.2fsec." % (tb-ta)
|
| 133 |
+
return res
|
| 134 |
+
|
| 135 |
+
if __name__ == '__main__':
|
| 136 |
+
msg = main()
|
| 137 |
+
print(msg)
|
| 138 |
+
mainloop()
|
evalkit_tf446/lib/python3.10/turtledemo/lindenmayer.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
""" turtle-example-suite:
|
| 3 |
+
|
| 4 |
+
xtx_lindenmayer_indian.py
|
| 5 |
+
|
| 6 |
+
Each morning women in Tamil Nadu, in southern
|
| 7 |
+
India, place designs, created by using rice
|
| 8 |
+
flour and known as kolam on the thresholds of
|
| 9 |
+
their homes.
|
| 10 |
+
|
| 11 |
+
These can be described by Lindenmayer systems,
|
| 12 |
+
which can easily be implemented with turtle
|
| 13 |
+
graphics and Python.
|
| 14 |
+
|
| 15 |
+
Two examples are shown here:
|
| 16 |
+
(1) the snake kolam
|
| 17 |
+
(2) anklets of Krishna
|
| 18 |
+
|
| 19 |
+
Taken from Marcia Ascher: Mathematics
|
| 20 |
+
Elsewhere, An Exploration of Ideas Across
|
| 21 |
+
Cultures
|
| 22 |
+
|
| 23 |
+
"""
|
| 24 |
+
################################
|
| 25 |
+
# Mini Lindenmayer tool
|
| 26 |
+
###############################
|
| 27 |
+
|
| 28 |
+
from turtle import *
|
| 29 |
+
|
| 30 |
+
def replace( seq, replacementRules, n ):
|
| 31 |
+
for i in range(n):
|
| 32 |
+
newseq = ""
|
| 33 |
+
for element in seq:
|
| 34 |
+
newseq = newseq + replacementRules.get(element,element)
|
| 35 |
+
seq = newseq
|
| 36 |
+
return seq
|
| 37 |
+
|
| 38 |
+
def draw( commands, rules ):
|
| 39 |
+
for b in commands:
|
| 40 |
+
try:
|
| 41 |
+
rules[b]()
|
| 42 |
+
except TypeError:
|
| 43 |
+
try:
|
| 44 |
+
draw(rules[b], rules)
|
| 45 |
+
except:
|
| 46 |
+
pass
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def main():
|
| 50 |
+
################################
|
| 51 |
+
# Example 1: Snake kolam
|
| 52 |
+
################################
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def r():
|
| 56 |
+
right(45)
|
| 57 |
+
|
| 58 |
+
def l():
|
| 59 |
+
left(45)
|
| 60 |
+
|
| 61 |
+
def f():
|
| 62 |
+
forward(7.5)
|
| 63 |
+
|
| 64 |
+
snake_rules = {"-":r, "+":l, "f":f, "b":"f+f+f--f--f+f+f"}
|
| 65 |
+
snake_replacementRules = {"b": "b+f+b--f--b+f+b"}
|
| 66 |
+
snake_start = "b--f--b--f"
|
| 67 |
+
|
| 68 |
+
drawing = replace(snake_start, snake_replacementRules, 3)
|
| 69 |
+
|
| 70 |
+
reset()
|
| 71 |
+
speed(3)
|
| 72 |
+
tracer(1,0)
|
| 73 |
+
ht()
|
| 74 |
+
up()
|
| 75 |
+
backward(195)
|
| 76 |
+
down()
|
| 77 |
+
draw(drawing, snake_rules)
|
| 78 |
+
|
| 79 |
+
from time import sleep
|
| 80 |
+
sleep(3)
|
| 81 |
+
|
| 82 |
+
################################
|
| 83 |
+
# Example 2: Anklets of Krishna
|
| 84 |
+
################################
|
| 85 |
+
|
| 86 |
+
def A():
|
| 87 |
+
color("red")
|
| 88 |
+
circle(10,90)
|
| 89 |
+
|
| 90 |
+
def B():
|
| 91 |
+
from math import sqrt
|
| 92 |
+
color("black")
|
| 93 |
+
l = 5/sqrt(2)
|
| 94 |
+
forward(l)
|
| 95 |
+
circle(l, 270)
|
| 96 |
+
forward(l)
|
| 97 |
+
|
| 98 |
+
def F():
|
| 99 |
+
color("green")
|
| 100 |
+
forward(10)
|
| 101 |
+
|
| 102 |
+
krishna_rules = {"a":A, "b":B, "f":F}
|
| 103 |
+
krishna_replacementRules = {"a" : "afbfa", "b" : "afbfbfbfa" }
|
| 104 |
+
krishna_start = "fbfbfbfb"
|
| 105 |
+
|
| 106 |
+
reset()
|
| 107 |
+
speed(0)
|
| 108 |
+
tracer(3,0)
|
| 109 |
+
ht()
|
| 110 |
+
left(45)
|
| 111 |
+
drawing = replace(krishna_start, krishna_replacementRules, 3)
|
| 112 |
+
draw(drawing, krishna_rules)
|
| 113 |
+
tracer(1)
|
| 114 |
+
return "Done!"
|
| 115 |
+
|
| 116 |
+
if __name__=='__main__':
|
| 117 |
+
msg = main()
|
| 118 |
+
print(msg)
|
| 119 |
+
mainloop()
|
evalkit_tf446/lib/python3.10/turtledemo/minimal_hanoi.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
""" turtle-example-suite:
|
| 3 |
+
|
| 4 |
+
tdemo_minimal_hanoi.py
|
| 5 |
+
|
| 6 |
+
A minimal 'Towers of Hanoi' animation:
|
| 7 |
+
A tower of 6 discs is transferred from the
|
| 8 |
+
left to the right peg.
|
| 9 |
+
|
| 10 |
+
An imho quite elegant and concise
|
| 11 |
+
implementation using a tower class, which
|
| 12 |
+
is derived from the built-in type list.
|
| 13 |
+
|
| 14 |
+
Discs are turtles with shape "square", but
|
| 15 |
+
stretched to rectangles by shapesize()
|
| 16 |
+
---------------------------------------
|
| 17 |
+
To exit press STOP button
|
| 18 |
+
---------------------------------------
|
| 19 |
+
"""
|
| 20 |
+
from turtle import *
|
| 21 |
+
|
| 22 |
+
class Disc(Turtle):
|
| 23 |
+
def __init__(self, n):
|
| 24 |
+
Turtle.__init__(self, shape="square", visible=False)
|
| 25 |
+
self.pu()
|
| 26 |
+
self.shapesize(1.5, n*1.5, 2) # square-->rectangle
|
| 27 |
+
self.fillcolor(n/6., 0, 1-n/6.)
|
| 28 |
+
self.st()
|
| 29 |
+
|
| 30 |
+
class Tower(list):
|
| 31 |
+
"Hanoi tower, a subclass of built-in type list"
|
| 32 |
+
def __init__(self, x):
|
| 33 |
+
"create an empty tower. x is x-position of peg"
|
| 34 |
+
self.x = x
|
| 35 |
+
def push(self, d):
|
| 36 |
+
d.setx(self.x)
|
| 37 |
+
d.sety(-150+34*len(self))
|
| 38 |
+
self.append(d)
|
| 39 |
+
def pop(self):
|
| 40 |
+
d = list.pop(self)
|
| 41 |
+
d.sety(150)
|
| 42 |
+
return d
|
| 43 |
+
|
| 44 |
+
def hanoi(n, from_, with_, to_):
|
| 45 |
+
if n > 0:
|
| 46 |
+
hanoi(n-1, from_, to_, with_)
|
| 47 |
+
to_.push(from_.pop())
|
| 48 |
+
hanoi(n-1, with_, from_, to_)
|
| 49 |
+
|
| 50 |
+
def play():
|
| 51 |
+
onkey(None,"space")
|
| 52 |
+
clear()
|
| 53 |
+
try:
|
| 54 |
+
hanoi(6, t1, t2, t3)
|
| 55 |
+
write("press STOP button to exit",
|
| 56 |
+
align="center", font=("Courier", 16, "bold"))
|
| 57 |
+
except Terminator:
|
| 58 |
+
pass # turtledemo user pressed STOP
|
| 59 |
+
|
| 60 |
+
def main():
|
| 61 |
+
global t1, t2, t3
|
| 62 |
+
ht(); penup(); goto(0, -225) # writer turtle
|
| 63 |
+
t1 = Tower(-250)
|
| 64 |
+
t2 = Tower(0)
|
| 65 |
+
t3 = Tower(250)
|
| 66 |
+
# make tower of 6 discs
|
| 67 |
+
for i in range(6,0,-1):
|
| 68 |
+
t1.push(Disc(i))
|
| 69 |
+
# prepare spartanic user interface ;-)
|
| 70 |
+
write("press spacebar to start game",
|
| 71 |
+
align="center", font=("Courier", 16, "bold"))
|
| 72 |
+
onkey(play, "space")
|
| 73 |
+
listen()
|
| 74 |
+
return "EVENTLOOP"
|
| 75 |
+
|
| 76 |
+
if __name__=="__main__":
|
| 77 |
+
msg = main()
|
| 78 |
+
print(msg)
|
| 79 |
+
mainloop()
|
evalkit_tf446/lib/python3.10/turtledemo/nim.py
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" turtle-example-suite:
|
| 2 |
+
|
| 3 |
+
tdemo_nim.py
|
| 4 |
+
|
| 5 |
+
Play nim against the computer. The player
|
| 6 |
+
who takes the last stick is the winner.
|
| 7 |
+
|
| 8 |
+
Implements the model-view-controller
|
| 9 |
+
design pattern.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
import turtle
|
| 14 |
+
import random
|
| 15 |
+
import time
|
| 16 |
+
|
| 17 |
+
SCREENWIDTH = 640
|
| 18 |
+
SCREENHEIGHT = 480
|
| 19 |
+
|
| 20 |
+
MINSTICKS = 7
|
| 21 |
+
MAXSTICKS = 31
|
| 22 |
+
|
| 23 |
+
HUNIT = SCREENHEIGHT // 12
|
| 24 |
+
WUNIT = SCREENWIDTH // ((MAXSTICKS // 5) * 11 + (MAXSTICKS % 5) * 2)
|
| 25 |
+
|
| 26 |
+
SCOLOR = (63, 63, 31)
|
| 27 |
+
HCOLOR = (255, 204, 204)
|
| 28 |
+
COLOR = (204, 204, 255)
|
| 29 |
+
|
| 30 |
+
def randomrow():
|
| 31 |
+
return random.randint(MINSTICKS, MAXSTICKS)
|
| 32 |
+
|
| 33 |
+
def computerzug(state):
|
| 34 |
+
xored = state[0] ^ state[1] ^ state[2]
|
| 35 |
+
if xored == 0:
|
| 36 |
+
return randommove(state)
|
| 37 |
+
for z in range(3):
|
| 38 |
+
s = state[z] ^ xored
|
| 39 |
+
if s <= state[z]:
|
| 40 |
+
move = (z, s)
|
| 41 |
+
return move
|
| 42 |
+
|
| 43 |
+
def randommove(state):
|
| 44 |
+
m = max(state)
|
| 45 |
+
while True:
|
| 46 |
+
z = random.randint(0,2)
|
| 47 |
+
if state[z] > (m > 1):
|
| 48 |
+
break
|
| 49 |
+
rand = random.randint(m > 1, state[z]-1)
|
| 50 |
+
return z, rand
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class NimModel(object):
|
| 54 |
+
def __init__(self, game):
|
| 55 |
+
self.game = game
|
| 56 |
+
|
| 57 |
+
def setup(self):
|
| 58 |
+
if self.game.state not in [Nim.CREATED, Nim.OVER]:
|
| 59 |
+
return
|
| 60 |
+
self.sticks = [randomrow(), randomrow(), randomrow()]
|
| 61 |
+
self.player = 0
|
| 62 |
+
self.winner = None
|
| 63 |
+
self.game.view.setup()
|
| 64 |
+
self.game.state = Nim.RUNNING
|
| 65 |
+
|
| 66 |
+
def move(self, row, col):
|
| 67 |
+
maxspalte = self.sticks[row]
|
| 68 |
+
self.sticks[row] = col
|
| 69 |
+
self.game.view.notify_move(row, col, maxspalte, self.player)
|
| 70 |
+
if self.game_over():
|
| 71 |
+
self.game.state = Nim.OVER
|
| 72 |
+
self.winner = self.player
|
| 73 |
+
self.game.view.notify_over()
|
| 74 |
+
elif self.player == 0:
|
| 75 |
+
self.player = 1
|
| 76 |
+
row, col = computerzug(self.sticks)
|
| 77 |
+
self.move(row, col)
|
| 78 |
+
self.player = 0
|
| 79 |
+
|
| 80 |
+
def game_over(self):
|
| 81 |
+
return self.sticks == [0, 0, 0]
|
| 82 |
+
|
| 83 |
+
def notify_move(self, row, col):
|
| 84 |
+
if self.sticks[row] <= col:
|
| 85 |
+
return
|
| 86 |
+
self.move(row, col)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
class Stick(turtle.Turtle):
|
| 90 |
+
def __init__(self, row, col, game):
|
| 91 |
+
turtle.Turtle.__init__(self, visible=False)
|
| 92 |
+
self.row = row
|
| 93 |
+
self.col = col
|
| 94 |
+
self.game = game
|
| 95 |
+
x, y = self.coords(row, col)
|
| 96 |
+
self.shape("square")
|
| 97 |
+
self.shapesize(HUNIT/10.0, WUNIT/20.0)
|
| 98 |
+
self.speed(0)
|
| 99 |
+
self.pu()
|
| 100 |
+
self.goto(x,y)
|
| 101 |
+
self.color("white")
|
| 102 |
+
self.showturtle()
|
| 103 |
+
|
| 104 |
+
def coords(self, row, col):
|
| 105 |
+
packet, remainder = divmod(col, 5)
|
| 106 |
+
x = (3 + 11 * packet + 2 * remainder) * WUNIT
|
| 107 |
+
y = (2 + 3 * row) * HUNIT
|
| 108 |
+
return x - SCREENWIDTH // 2 + WUNIT // 2, SCREENHEIGHT // 2 - y - HUNIT // 2
|
| 109 |
+
|
| 110 |
+
def makemove(self, x, y):
|
| 111 |
+
if self.game.state != Nim.RUNNING:
|
| 112 |
+
return
|
| 113 |
+
self.game.controller.notify_move(self.row, self.col)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
class NimView(object):
|
| 117 |
+
def __init__(self, game):
|
| 118 |
+
self.game = game
|
| 119 |
+
self.screen = game.screen
|
| 120 |
+
self.model = game.model
|
| 121 |
+
self.screen.colormode(255)
|
| 122 |
+
self.screen.tracer(False)
|
| 123 |
+
self.screen.bgcolor((240, 240, 255))
|
| 124 |
+
self.writer = turtle.Turtle(visible=False)
|
| 125 |
+
self.writer.pu()
|
| 126 |
+
self.writer.speed(0)
|
| 127 |
+
self.sticks = {}
|
| 128 |
+
for row in range(3):
|
| 129 |
+
for col in range(MAXSTICKS):
|
| 130 |
+
self.sticks[(row, col)] = Stick(row, col, game)
|
| 131 |
+
self.display("... a moment please ...")
|
| 132 |
+
self.screen.tracer(True)
|
| 133 |
+
|
| 134 |
+
def display(self, msg1, msg2=None):
|
| 135 |
+
self.screen.tracer(False)
|
| 136 |
+
self.writer.clear()
|
| 137 |
+
if msg2 is not None:
|
| 138 |
+
self.writer.goto(0, - SCREENHEIGHT // 2 + 48)
|
| 139 |
+
self.writer.pencolor("red")
|
| 140 |
+
self.writer.write(msg2, align="center", font=("Courier",18,"bold"))
|
| 141 |
+
self.writer.goto(0, - SCREENHEIGHT // 2 + 20)
|
| 142 |
+
self.writer.pencolor("black")
|
| 143 |
+
self.writer.write(msg1, align="center", font=("Courier",14,"bold"))
|
| 144 |
+
self.screen.tracer(True)
|
| 145 |
+
|
| 146 |
+
def setup(self):
|
| 147 |
+
self.screen.tracer(False)
|
| 148 |
+
for row in range(3):
|
| 149 |
+
for col in range(self.model.sticks[row]):
|
| 150 |
+
self.sticks[(row, col)].color(SCOLOR)
|
| 151 |
+
for row in range(3):
|
| 152 |
+
for col in range(self.model.sticks[row], MAXSTICKS):
|
| 153 |
+
self.sticks[(row, col)].color("white")
|
| 154 |
+
self.display("Your turn! Click leftmost stick to remove.")
|
| 155 |
+
self.screen.tracer(True)
|
| 156 |
+
|
| 157 |
+
def notify_move(self, row, col, maxspalte, player):
|
| 158 |
+
if player == 0:
|
| 159 |
+
farbe = HCOLOR
|
| 160 |
+
for s in range(col, maxspalte):
|
| 161 |
+
self.sticks[(row, s)].color(farbe)
|
| 162 |
+
else:
|
| 163 |
+
self.display(" ... thinking ... ")
|
| 164 |
+
time.sleep(0.5)
|
| 165 |
+
self.display(" ... thinking ... aaah ...")
|
| 166 |
+
farbe = COLOR
|
| 167 |
+
for s in range(maxspalte-1, col-1, -1):
|
| 168 |
+
time.sleep(0.2)
|
| 169 |
+
self.sticks[(row, s)].color(farbe)
|
| 170 |
+
self.display("Your turn! Click leftmost stick to remove.")
|
| 171 |
+
|
| 172 |
+
def notify_over(self):
|
| 173 |
+
if self.game.model.winner == 0:
|
| 174 |
+
msg2 = "Congrats. You're the winner!!!"
|
| 175 |
+
else:
|
| 176 |
+
msg2 = "Sorry, the computer is the winner."
|
| 177 |
+
self.display("To play again press space bar. To leave press ESC.", msg2)
|
| 178 |
+
|
| 179 |
+
def clear(self):
|
| 180 |
+
if self.game.state == Nim.OVER:
|
| 181 |
+
self.screen.clear()
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
class NimController(object):
|
| 185 |
+
|
| 186 |
+
def __init__(self, game):
|
| 187 |
+
self.game = game
|
| 188 |
+
self.sticks = game.view.sticks
|
| 189 |
+
self.BUSY = False
|
| 190 |
+
for stick in self.sticks.values():
|
| 191 |
+
stick.onclick(stick.makemove)
|
| 192 |
+
self.game.screen.onkey(self.game.model.setup, "space")
|
| 193 |
+
self.game.screen.onkey(self.game.view.clear, "Escape")
|
| 194 |
+
self.game.view.display("Press space bar to start game")
|
| 195 |
+
self.game.screen.listen()
|
| 196 |
+
|
| 197 |
+
def notify_move(self, row, col):
|
| 198 |
+
if self.BUSY:
|
| 199 |
+
return
|
| 200 |
+
self.BUSY = True
|
| 201 |
+
self.game.model.notify_move(row, col)
|
| 202 |
+
self.BUSY = False
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
class Nim(object):
|
| 206 |
+
CREATED = 0
|
| 207 |
+
RUNNING = 1
|
| 208 |
+
OVER = 2
|
| 209 |
+
def __init__(self, screen):
|
| 210 |
+
self.state = Nim.CREATED
|
| 211 |
+
self.screen = screen
|
| 212 |
+
self.model = NimModel(self)
|
| 213 |
+
self.view = NimView(self)
|
| 214 |
+
self.controller = NimController(self)
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
def main():
|
| 218 |
+
mainscreen = turtle.Screen()
|
| 219 |
+
mainscreen.mode("standard")
|
| 220 |
+
mainscreen.setup(SCREENWIDTH, SCREENHEIGHT)
|
| 221 |
+
nim = Nim(mainscreen)
|
| 222 |
+
return "EVENTLOOP"
|
| 223 |
+
|
| 224 |
+
if __name__ == "__main__":
|
| 225 |
+
main()
|
| 226 |
+
turtle.mainloop()
|
evalkit_tf446/lib/python3.10/turtledemo/paint.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
""" turtle-example-suite:
|
| 3 |
+
|
| 4 |
+
tdemo_paint.py
|
| 5 |
+
|
| 6 |
+
A simple event-driven paint program
|
| 7 |
+
|
| 8 |
+
- left mouse button moves turtle
|
| 9 |
+
- middle mouse button changes color
|
| 10 |
+
- right mouse button toggles between pen up
|
| 11 |
+
(no line drawn when the turtle moves) and
|
| 12 |
+
pen down (line is drawn). If pen up follows
|
| 13 |
+
at least two pen-down moves, the polygon that
|
| 14 |
+
includes the starting point is filled.
|
| 15 |
+
-------------------------------------------
|
| 16 |
+
Play around by clicking into the canvas
|
| 17 |
+
using all three mouse buttons.
|
| 18 |
+
-------------------------------------------
|
| 19 |
+
To exit press STOP button
|
| 20 |
+
-------------------------------------------
|
| 21 |
+
"""
|
| 22 |
+
from turtle import *
|
| 23 |
+
|
| 24 |
+
def switchupdown(x=0, y=0):
|
| 25 |
+
if pen()["pendown"]:
|
| 26 |
+
end_fill()
|
| 27 |
+
up()
|
| 28 |
+
else:
|
| 29 |
+
down()
|
| 30 |
+
begin_fill()
|
| 31 |
+
|
| 32 |
+
def changecolor(x=0, y=0):
|
| 33 |
+
global colors
|
| 34 |
+
colors = colors[1:]+colors[:1]
|
| 35 |
+
color(colors[0])
|
| 36 |
+
|
| 37 |
+
def main():
|
| 38 |
+
global colors
|
| 39 |
+
shape("circle")
|
| 40 |
+
resizemode("user")
|
| 41 |
+
shapesize(.5)
|
| 42 |
+
width(3)
|
| 43 |
+
colors=["red", "green", "blue", "yellow"]
|
| 44 |
+
color(colors[0])
|
| 45 |
+
switchupdown()
|
| 46 |
+
onscreenclick(goto,1)
|
| 47 |
+
onscreenclick(changecolor,2)
|
| 48 |
+
onscreenclick(switchupdown,3)
|
| 49 |
+
return "EVENTLOOP"
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
msg = main()
|
| 53 |
+
print(msg)
|
| 54 |
+
mainloop()
|
evalkit_tf446/lib/python3.10/turtledemo/peace.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
""" turtle-example-suite:
|
| 3 |
+
|
| 4 |
+
tdemo_peace.py
|
| 5 |
+
|
| 6 |
+
A simple drawing suitable as a beginner's
|
| 7 |
+
programming example. Aside from the
|
| 8 |
+
peacecolors assignment and the for loop,
|
| 9 |
+
it only uses turtle commands.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
from turtle import *
|
| 13 |
+
|
| 14 |
+
def main():
|
| 15 |
+
peacecolors = ("red3", "orange", "yellow",
|
| 16 |
+
"seagreen4", "orchid4",
|
| 17 |
+
"royalblue1", "dodgerblue4")
|
| 18 |
+
|
| 19 |
+
reset()
|
| 20 |
+
Screen()
|
| 21 |
+
up()
|
| 22 |
+
goto(-320,-195)
|
| 23 |
+
width(70)
|
| 24 |
+
|
| 25 |
+
for pcolor in peacecolors:
|
| 26 |
+
color(pcolor)
|
| 27 |
+
down()
|
| 28 |
+
forward(640)
|
| 29 |
+
up()
|
| 30 |
+
backward(640)
|
| 31 |
+
left(90)
|
| 32 |
+
forward(66)
|
| 33 |
+
right(90)
|
| 34 |
+
|
| 35 |
+
width(25)
|
| 36 |
+
color("white")
|
| 37 |
+
goto(0,-170)
|
| 38 |
+
down()
|
| 39 |
+
|
| 40 |
+
circle(170)
|
| 41 |
+
left(90)
|
| 42 |
+
forward(340)
|
| 43 |
+
up()
|
| 44 |
+
left(180)
|
| 45 |
+
forward(170)
|
| 46 |
+
right(45)
|
| 47 |
+
down()
|
| 48 |
+
forward(170)
|
| 49 |
+
up()
|
| 50 |
+
backward(170)
|
| 51 |
+
left(90)
|
| 52 |
+
down()
|
| 53 |
+
forward(170)
|
| 54 |
+
up()
|
| 55 |
+
|
| 56 |
+
goto(0,300) # vanish if hideturtle() is not available ;-)
|
| 57 |
+
return "Done!"
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
main()
|
| 61 |
+
mainloop()
|
evalkit_tf446/lib/python3.10/turtledemo/penrose.py
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
""" xturtle-example-suite:
|
| 3 |
+
|
| 4 |
+
xtx_kites_and_darts.py
|
| 5 |
+
|
| 6 |
+
Constructs two aperiodic penrose-tilings,
|
| 7 |
+
consisting of kites and darts, by the method
|
| 8 |
+
of inflation in six steps.
|
| 9 |
+
|
| 10 |
+
Starting points are the patterns "sun"
|
| 11 |
+
consisting of five kites and "star"
|
| 12 |
+
consisting of five darts.
|
| 13 |
+
|
| 14 |
+
For more information see:
|
| 15 |
+
http://en.wikipedia.org/wiki/Penrose_tiling
|
| 16 |
+
-------------------------------------------
|
| 17 |
+
"""
|
| 18 |
+
from turtle import *
|
| 19 |
+
from math import cos, pi
|
| 20 |
+
from time import perf_counter as clock, sleep
|
| 21 |
+
|
| 22 |
+
f = (5**0.5-1)/2.0 # (sqrt(5)-1)/2 -- golden ratio
|
| 23 |
+
d = 2 * cos(3*pi/10)
|
| 24 |
+
|
| 25 |
+
def kite(l):
|
| 26 |
+
fl = f * l
|
| 27 |
+
lt(36)
|
| 28 |
+
fd(l)
|
| 29 |
+
rt(108)
|
| 30 |
+
fd(fl)
|
| 31 |
+
rt(36)
|
| 32 |
+
fd(fl)
|
| 33 |
+
rt(108)
|
| 34 |
+
fd(l)
|
| 35 |
+
rt(144)
|
| 36 |
+
|
| 37 |
+
def dart(l):
|
| 38 |
+
fl = f * l
|
| 39 |
+
lt(36)
|
| 40 |
+
fd(l)
|
| 41 |
+
rt(144)
|
| 42 |
+
fd(fl)
|
| 43 |
+
lt(36)
|
| 44 |
+
fd(fl)
|
| 45 |
+
rt(144)
|
| 46 |
+
fd(l)
|
| 47 |
+
rt(144)
|
| 48 |
+
|
| 49 |
+
def inflatekite(l, n):
|
| 50 |
+
if n == 0:
|
| 51 |
+
px, py = pos()
|
| 52 |
+
h, x, y = int(heading()), round(px,3), round(py,3)
|
| 53 |
+
tiledict[(h,x,y)] = True
|
| 54 |
+
return
|
| 55 |
+
fl = f * l
|
| 56 |
+
lt(36)
|
| 57 |
+
inflatedart(fl, n-1)
|
| 58 |
+
fd(l)
|
| 59 |
+
rt(144)
|
| 60 |
+
inflatekite(fl, n-1)
|
| 61 |
+
lt(18)
|
| 62 |
+
fd(l*d)
|
| 63 |
+
rt(162)
|
| 64 |
+
inflatekite(fl, n-1)
|
| 65 |
+
lt(36)
|
| 66 |
+
fd(l)
|
| 67 |
+
rt(180)
|
| 68 |
+
inflatedart(fl, n-1)
|
| 69 |
+
lt(36)
|
| 70 |
+
|
| 71 |
+
def inflatedart(l, n):
|
| 72 |
+
if n == 0:
|
| 73 |
+
px, py = pos()
|
| 74 |
+
h, x, y = int(heading()), round(px,3), round(py,3)
|
| 75 |
+
tiledict[(h,x,y)] = False
|
| 76 |
+
return
|
| 77 |
+
fl = f * l
|
| 78 |
+
inflatekite(fl, n-1)
|
| 79 |
+
lt(36)
|
| 80 |
+
fd(l)
|
| 81 |
+
rt(180)
|
| 82 |
+
inflatedart(fl, n-1)
|
| 83 |
+
lt(54)
|
| 84 |
+
fd(l*d)
|
| 85 |
+
rt(126)
|
| 86 |
+
inflatedart(fl, n-1)
|
| 87 |
+
fd(l)
|
| 88 |
+
rt(144)
|
| 89 |
+
|
| 90 |
+
def draw(l, n, th=2):
|
| 91 |
+
clear()
|
| 92 |
+
l = l * f**n
|
| 93 |
+
shapesize(l/100.0, l/100.0, th)
|
| 94 |
+
for k in tiledict:
|
| 95 |
+
h, x, y = k
|
| 96 |
+
setpos(x, y)
|
| 97 |
+
setheading(h)
|
| 98 |
+
if tiledict[k]:
|
| 99 |
+
shape("kite")
|
| 100 |
+
color("black", (0, 0.75, 0))
|
| 101 |
+
else:
|
| 102 |
+
shape("dart")
|
| 103 |
+
color("black", (0.75, 0, 0))
|
| 104 |
+
stamp()
|
| 105 |
+
|
| 106 |
+
def sun(l, n):
|
| 107 |
+
for i in range(5):
|
| 108 |
+
inflatekite(l, n)
|
| 109 |
+
lt(72)
|
| 110 |
+
|
| 111 |
+
def star(l,n):
|
| 112 |
+
for i in range(5):
|
| 113 |
+
inflatedart(l, n)
|
| 114 |
+
lt(72)
|
| 115 |
+
|
| 116 |
+
def makeshapes():
|
| 117 |
+
tracer(0)
|
| 118 |
+
begin_poly()
|
| 119 |
+
kite(100)
|
| 120 |
+
end_poly()
|
| 121 |
+
register_shape("kite", get_poly())
|
| 122 |
+
begin_poly()
|
| 123 |
+
dart(100)
|
| 124 |
+
end_poly()
|
| 125 |
+
register_shape("dart", get_poly())
|
| 126 |
+
tracer(1)
|
| 127 |
+
|
| 128 |
+
def start():
|
| 129 |
+
reset()
|
| 130 |
+
ht()
|
| 131 |
+
pu()
|
| 132 |
+
makeshapes()
|
| 133 |
+
resizemode("user")
|
| 134 |
+
|
| 135 |
+
def test(l=200, n=4, fun=sun, startpos=(0,0), th=2):
|
| 136 |
+
global tiledict
|
| 137 |
+
goto(startpos)
|
| 138 |
+
setheading(0)
|
| 139 |
+
tiledict = {}
|
| 140 |
+
tracer(0)
|
| 141 |
+
fun(l, n)
|
| 142 |
+
draw(l, n, th)
|
| 143 |
+
tracer(1)
|
| 144 |
+
nk = len([x for x in tiledict if tiledict[x]])
|
| 145 |
+
nd = len([x for x in tiledict if not tiledict[x]])
|
| 146 |
+
print("%d kites and %d darts = %d pieces." % (nk, nd, nk+nd))
|
| 147 |
+
|
| 148 |
+
def demo(fun=sun):
|
| 149 |
+
start()
|
| 150 |
+
for i in range(8):
|
| 151 |
+
a = clock()
|
| 152 |
+
test(300, i, fun)
|
| 153 |
+
b = clock()
|
| 154 |
+
t = b - a
|
| 155 |
+
if t < 2:
|
| 156 |
+
sleep(2 - t)
|
| 157 |
+
|
| 158 |
+
def main():
|
| 159 |
+
#title("Penrose-tiling with kites and darts.")
|
| 160 |
+
mode("logo")
|
| 161 |
+
bgcolor(0.3, 0.3, 0)
|
| 162 |
+
demo(sun)
|
| 163 |
+
sleep(2)
|
| 164 |
+
demo(star)
|
| 165 |
+
pencolor("black")
|
| 166 |
+
goto(0,-200)
|
| 167 |
+
pencolor(0.7,0.7,1)
|
| 168 |
+
write("Please wait...",
|
| 169 |
+
align="center", font=('Arial Black', 36, 'bold'))
|
| 170 |
+
test(600, 8, startpos=(70, 117))
|
| 171 |
+
return "Done"
|
| 172 |
+
|
| 173 |
+
if __name__ == "__main__":
|
| 174 |
+
msg = main()
|
| 175 |
+
mainloop()
|
evalkit_tf446/lib/python3.10/turtledemo/planet_and_moon.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
""" turtle-example-suite:
|
| 3 |
+
|
| 4 |
+
tdemo_planets_and_moon.py
|
| 5 |
+
|
| 6 |
+
Gravitational system simulation using the
|
| 7 |
+
approximation method from Feynman-lectures,
|
| 8 |
+
p.9-8, using turtlegraphics.
|
| 9 |
+
|
| 10 |
+
Example: heavy central body, light planet,
|
| 11 |
+
very light moon!
|
| 12 |
+
Planet has a circular orbit, moon a stable
|
| 13 |
+
orbit around the planet.
|
| 14 |
+
|
| 15 |
+
You can hold the movement temporarily by
|
| 16 |
+
pressing the left mouse button with the
|
| 17 |
+
mouse over the scrollbar of the canvas.
|
| 18 |
+
|
| 19 |
+
"""
|
| 20 |
+
from turtle import Shape, Turtle, mainloop, Vec2D as Vec
|
| 21 |
+
|
| 22 |
+
G = 8
|
| 23 |
+
|
| 24 |
+
class GravSys(object):
|
| 25 |
+
def __init__(self):
|
| 26 |
+
self.planets = []
|
| 27 |
+
self.t = 0
|
| 28 |
+
self.dt = 0.01
|
| 29 |
+
def init(self):
|
| 30 |
+
for p in self.planets:
|
| 31 |
+
p.init()
|
| 32 |
+
def start(self):
|
| 33 |
+
for i in range(10000):
|
| 34 |
+
self.t += self.dt
|
| 35 |
+
for p in self.planets:
|
| 36 |
+
p.step()
|
| 37 |
+
|
| 38 |
+
class Star(Turtle):
|
| 39 |
+
def __init__(self, m, x, v, gravSys, shape):
|
| 40 |
+
Turtle.__init__(self, shape=shape)
|
| 41 |
+
self.penup()
|
| 42 |
+
self.m = m
|
| 43 |
+
self.setpos(x)
|
| 44 |
+
self.v = v
|
| 45 |
+
gravSys.planets.append(self)
|
| 46 |
+
self.gravSys = gravSys
|
| 47 |
+
self.resizemode("user")
|
| 48 |
+
self.pendown()
|
| 49 |
+
def init(self):
|
| 50 |
+
dt = self.gravSys.dt
|
| 51 |
+
self.a = self.acc()
|
| 52 |
+
self.v = self.v + 0.5*dt*self.a
|
| 53 |
+
def acc(self):
|
| 54 |
+
a = Vec(0,0)
|
| 55 |
+
for planet in self.gravSys.planets:
|
| 56 |
+
if planet != self:
|
| 57 |
+
v = planet.pos()-self.pos()
|
| 58 |
+
a += (G*planet.m/abs(v)**3)*v
|
| 59 |
+
return a
|
| 60 |
+
def step(self):
|
| 61 |
+
dt = self.gravSys.dt
|
| 62 |
+
self.setpos(self.pos() + dt*self.v)
|
| 63 |
+
if self.gravSys.planets.index(self) != 0:
|
| 64 |
+
self.setheading(self.towards(self.gravSys.planets[0]))
|
| 65 |
+
self.a = self.acc()
|
| 66 |
+
self.v = self.v + dt*self.a
|
| 67 |
+
|
| 68 |
+
## create compound yellow/blue turtleshape for planets
|
| 69 |
+
|
| 70 |
+
def main():
|
| 71 |
+
s = Turtle()
|
| 72 |
+
s.reset()
|
| 73 |
+
s.getscreen().tracer(0,0)
|
| 74 |
+
s.ht()
|
| 75 |
+
s.pu()
|
| 76 |
+
s.fd(6)
|
| 77 |
+
s.lt(90)
|
| 78 |
+
s.begin_poly()
|
| 79 |
+
s.circle(6, 180)
|
| 80 |
+
s.end_poly()
|
| 81 |
+
m1 = s.get_poly()
|
| 82 |
+
s.begin_poly()
|
| 83 |
+
s.circle(6,180)
|
| 84 |
+
s.end_poly()
|
| 85 |
+
m2 = s.get_poly()
|
| 86 |
+
|
| 87 |
+
planetshape = Shape("compound")
|
| 88 |
+
planetshape.addcomponent(m1,"orange")
|
| 89 |
+
planetshape.addcomponent(m2,"blue")
|
| 90 |
+
s.getscreen().register_shape("planet", planetshape)
|
| 91 |
+
s.getscreen().tracer(1,0)
|
| 92 |
+
|
| 93 |
+
## setup gravitational system
|
| 94 |
+
gs = GravSys()
|
| 95 |
+
sun = Star(1000000, Vec(0,0), Vec(0,-2.5), gs, "circle")
|
| 96 |
+
sun.color("yellow")
|
| 97 |
+
sun.shapesize(1.8)
|
| 98 |
+
sun.pu()
|
| 99 |
+
earth = Star(12500, Vec(210,0), Vec(0,195), gs, "planet")
|
| 100 |
+
earth.pencolor("green")
|
| 101 |
+
earth.shapesize(0.8)
|
| 102 |
+
moon = Star(1, Vec(220,0), Vec(0,295), gs, "planet")
|
| 103 |
+
moon.pencolor("blue")
|
| 104 |
+
moon.shapesize(0.5)
|
| 105 |
+
gs.init()
|
| 106 |
+
gs.start()
|
| 107 |
+
return "Done!"
|
| 108 |
+
|
| 109 |
+
if __name__ == '__main__':
|
| 110 |
+
main()
|
| 111 |
+
mainloop()
|
evalkit_tf446/lib/python3.10/turtledemo/rosette.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" turtle-example-suite:
|
| 2 |
+
|
| 3 |
+
tdemo_wikipedia3.py
|
| 4 |
+
|
| 5 |
+
This example is
|
| 6 |
+
inspired by the Wikipedia article on turtle
|
| 7 |
+
graphics. (See example wikipedia1 for URLs)
|
| 8 |
+
|
| 9 |
+
First we create (ne-1) (i.e. 35 in this
|
| 10 |
+
example) copies of our first turtle p.
|
| 11 |
+
Then we let them perform their steps in
|
| 12 |
+
parallel.
|
| 13 |
+
|
| 14 |
+
Followed by a complete undo().
|
| 15 |
+
"""
|
| 16 |
+
from turtle import Screen, Turtle, mainloop
|
| 17 |
+
from time import perf_counter as clock, sleep
|
| 18 |
+
|
| 19 |
+
def mn_eck(p, ne,sz):
|
| 20 |
+
turtlelist = [p]
|
| 21 |
+
#create ne-1 additional turtles
|
| 22 |
+
for i in range(1,ne):
|
| 23 |
+
q = p.clone()
|
| 24 |
+
q.rt(360.0/ne)
|
| 25 |
+
turtlelist.append(q)
|
| 26 |
+
p = q
|
| 27 |
+
for i in range(ne):
|
| 28 |
+
c = abs(ne/2.0-i)/(ne*.7)
|
| 29 |
+
# let those ne turtles make a step
|
| 30 |
+
# in parallel:
|
| 31 |
+
for t in turtlelist:
|
| 32 |
+
t.rt(360./ne)
|
| 33 |
+
t.pencolor(1-c,0,c)
|
| 34 |
+
t.fd(sz)
|
| 35 |
+
|
| 36 |
+
def main():
|
| 37 |
+
s = Screen()
|
| 38 |
+
s.bgcolor("black")
|
| 39 |
+
p=Turtle()
|
| 40 |
+
p.speed(0)
|
| 41 |
+
p.hideturtle()
|
| 42 |
+
p.pencolor("red")
|
| 43 |
+
p.pensize(3)
|
| 44 |
+
|
| 45 |
+
s.tracer(36,0)
|
| 46 |
+
|
| 47 |
+
at = clock()
|
| 48 |
+
mn_eck(p, 36, 19)
|
| 49 |
+
et = clock()
|
| 50 |
+
z1 = et-at
|
| 51 |
+
|
| 52 |
+
sleep(1)
|
| 53 |
+
|
| 54 |
+
at = clock()
|
| 55 |
+
while any(t.undobufferentries() for t in s.turtles()):
|
| 56 |
+
for t in s.turtles():
|
| 57 |
+
t.undo()
|
| 58 |
+
et = clock()
|
| 59 |
+
return "runtime: %.3f sec" % (z1+et-at)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
if __name__ == '__main__':
|
| 63 |
+
msg = main()
|
| 64 |
+
print(msg)
|
| 65 |
+
mainloop()
|
evalkit_tf446/lib/python3.10/turtledemo/round_dance.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
""" turtle-example-suite:
|
| 2 |
+
|
| 3 |
+
tdemo_round_dance.py
|
| 4 |
+
|
| 5 |
+
(Needs version 1.1 of the turtle module that
|
| 6 |
+
comes with Python 3.1)
|
| 7 |
+
|
| 8 |
+
Dancing turtles have a compound shape
|
| 9 |
+
consisting of a series of triangles of
|
| 10 |
+
decreasing size.
|
| 11 |
+
|
| 12 |
+
Turtles march along a circle while rotating
|
| 13 |
+
pairwise in opposite direction, with one
|
| 14 |
+
exception. Does that breaking of symmetry
|
| 15 |
+
enhance the attractiveness of the example?
|
| 16 |
+
|
| 17 |
+
Press any key to stop the animation.
|
| 18 |
+
|
| 19 |
+
Technically: demonstrates use of compound
|
| 20 |
+
shapes, transformation of shapes as well as
|
| 21 |
+
cloning turtles. The animation is
|
| 22 |
+
controlled through update().
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
from turtle import *
|
| 26 |
+
|
| 27 |
+
def stop():
|
| 28 |
+
global running
|
| 29 |
+
running = False
|
| 30 |
+
|
| 31 |
+
def main():
|
| 32 |
+
global running
|
| 33 |
+
clearscreen()
|
| 34 |
+
bgcolor("gray10")
|
| 35 |
+
tracer(False)
|
| 36 |
+
shape("triangle")
|
| 37 |
+
f = 0.793402
|
| 38 |
+
phi = 9.064678
|
| 39 |
+
s = 5
|
| 40 |
+
c = 1
|
| 41 |
+
# create compound shape
|
| 42 |
+
sh = Shape("compound")
|
| 43 |
+
for i in range(10):
|
| 44 |
+
shapesize(s)
|
| 45 |
+
p =get_shapepoly()
|
| 46 |
+
s *= f
|
| 47 |
+
c *= f
|
| 48 |
+
tilt(-phi)
|
| 49 |
+
sh.addcomponent(p, (c, 0.25, 1-c), "black")
|
| 50 |
+
register_shape("multitri", sh)
|
| 51 |
+
# create dancers
|
| 52 |
+
shapesize(1)
|
| 53 |
+
shape("multitri")
|
| 54 |
+
pu()
|
| 55 |
+
setpos(0, -200)
|
| 56 |
+
dancers = []
|
| 57 |
+
for i in range(180):
|
| 58 |
+
fd(7)
|
| 59 |
+
tilt(-4)
|
| 60 |
+
lt(2)
|
| 61 |
+
update()
|
| 62 |
+
if i % 12 == 0:
|
| 63 |
+
dancers.append(clone())
|
| 64 |
+
home()
|
| 65 |
+
# dance
|
| 66 |
+
running = True
|
| 67 |
+
onkeypress(stop)
|
| 68 |
+
listen()
|
| 69 |
+
cs = 1
|
| 70 |
+
while running:
|
| 71 |
+
ta = -4
|
| 72 |
+
for dancer in dancers:
|
| 73 |
+
dancer.fd(7)
|
| 74 |
+
dancer.lt(2)
|
| 75 |
+
dancer.tilt(ta)
|
| 76 |
+
ta = -4 if ta > 0 else 2
|
| 77 |
+
if cs < 180:
|
| 78 |
+
right(4)
|
| 79 |
+
shapesize(cs)
|
| 80 |
+
cs *= 1.005
|
| 81 |
+
update()
|
| 82 |
+
return "DONE!"
|
| 83 |
+
|
| 84 |
+
if __name__=='__main__':
|
| 85 |
+
print(main())
|
| 86 |
+
mainloop()
|
evalkit_tf446/lib/python3.10/turtledemo/sorting_animate.py
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
|
| 4 |
+
sorting_animation.py
|
| 5 |
+
|
| 6 |
+
A minimal sorting algorithm animation:
|
| 7 |
+
Sorts a shelf of 10 blocks using insertion
|
| 8 |
+
sort, selection sort and quicksort.
|
| 9 |
+
|
| 10 |
+
Shelfs are implemented using builtin lists.
|
| 11 |
+
|
| 12 |
+
Blocks are turtles with shape "square", but
|
| 13 |
+
stretched to rectangles by shapesize()
|
| 14 |
+
---------------------------------------
|
| 15 |
+
To exit press space button
|
| 16 |
+
---------------------------------------
|
| 17 |
+
"""
|
| 18 |
+
from turtle import *
|
| 19 |
+
import random
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class Block(Turtle):
|
| 23 |
+
|
| 24 |
+
def __init__(self, size):
|
| 25 |
+
self.size = size
|
| 26 |
+
Turtle.__init__(self, shape="square", visible=False)
|
| 27 |
+
self.pu()
|
| 28 |
+
self.shapesize(size * 1.5, 1.5, 2) # square-->rectangle
|
| 29 |
+
self.fillcolor("black")
|
| 30 |
+
self.st()
|
| 31 |
+
|
| 32 |
+
def glow(self):
|
| 33 |
+
self.fillcolor("red")
|
| 34 |
+
|
| 35 |
+
def unglow(self):
|
| 36 |
+
self.fillcolor("black")
|
| 37 |
+
|
| 38 |
+
def __repr__(self):
|
| 39 |
+
return "Block size: {0}".format(self.size)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class Shelf(list):
|
| 43 |
+
|
| 44 |
+
def __init__(self, y):
|
| 45 |
+
"create a shelf. y is y-position of first block"
|
| 46 |
+
self.y = y
|
| 47 |
+
self.x = -150
|
| 48 |
+
|
| 49 |
+
def push(self, d):
|
| 50 |
+
width, _, _ = d.shapesize()
|
| 51 |
+
# align blocks by the bottom edge
|
| 52 |
+
y_offset = width / 2 * 20
|
| 53 |
+
d.sety(self.y + y_offset)
|
| 54 |
+
d.setx(self.x + 34 * len(self))
|
| 55 |
+
self.append(d)
|
| 56 |
+
|
| 57 |
+
def _close_gap_from_i(self, i):
|
| 58 |
+
for b in self[i:]:
|
| 59 |
+
xpos, _ = b.pos()
|
| 60 |
+
b.setx(xpos - 34)
|
| 61 |
+
|
| 62 |
+
def _open_gap_from_i(self, i):
|
| 63 |
+
for b in self[i:]:
|
| 64 |
+
xpos, _ = b.pos()
|
| 65 |
+
b.setx(xpos + 34)
|
| 66 |
+
|
| 67 |
+
def pop(self, key):
|
| 68 |
+
b = list.pop(self, key)
|
| 69 |
+
b.glow()
|
| 70 |
+
b.sety(200)
|
| 71 |
+
self._close_gap_from_i(key)
|
| 72 |
+
return b
|
| 73 |
+
|
| 74 |
+
def insert(self, key, b):
|
| 75 |
+
self._open_gap_from_i(key)
|
| 76 |
+
list.insert(self, key, b)
|
| 77 |
+
b.setx(self.x + 34 * key)
|
| 78 |
+
width, _, _ = b.shapesize()
|
| 79 |
+
# align blocks by the bottom edge
|
| 80 |
+
y_offset = width / 2 * 20
|
| 81 |
+
b.sety(self.y + y_offset)
|
| 82 |
+
b.unglow()
|
| 83 |
+
|
| 84 |
+
def isort(shelf):
|
| 85 |
+
length = len(shelf)
|
| 86 |
+
for i in range(1, length):
|
| 87 |
+
hole = i
|
| 88 |
+
while hole > 0 and shelf[i].size < shelf[hole - 1].size:
|
| 89 |
+
hole = hole - 1
|
| 90 |
+
shelf.insert(hole, shelf.pop(i))
|
| 91 |
+
return
|
| 92 |
+
|
| 93 |
+
def ssort(shelf):
|
| 94 |
+
length = len(shelf)
|
| 95 |
+
for j in range(0, length - 1):
|
| 96 |
+
imin = j
|
| 97 |
+
for i in range(j + 1, length):
|
| 98 |
+
if shelf[i].size < shelf[imin].size:
|
| 99 |
+
imin = i
|
| 100 |
+
if imin != j:
|
| 101 |
+
shelf.insert(j, shelf.pop(imin))
|
| 102 |
+
|
| 103 |
+
def partition(shelf, left, right, pivot_index):
|
| 104 |
+
pivot = shelf[pivot_index]
|
| 105 |
+
shelf.insert(right, shelf.pop(pivot_index))
|
| 106 |
+
store_index = left
|
| 107 |
+
for i in range(left, right): # range is non-inclusive of ending value
|
| 108 |
+
if shelf[i].size < pivot.size:
|
| 109 |
+
shelf.insert(store_index, shelf.pop(i))
|
| 110 |
+
store_index = store_index + 1
|
| 111 |
+
shelf.insert(store_index, shelf.pop(right)) # move pivot to correct position
|
| 112 |
+
return store_index
|
| 113 |
+
|
| 114 |
+
def qsort(shelf, left, right):
|
| 115 |
+
if left < right:
|
| 116 |
+
pivot_index = left
|
| 117 |
+
pivot_new_index = partition(shelf, left, right, pivot_index)
|
| 118 |
+
qsort(shelf, left, pivot_new_index - 1)
|
| 119 |
+
qsort(shelf, pivot_new_index + 1, right)
|
| 120 |
+
|
| 121 |
+
def randomize():
|
| 122 |
+
disable_keys()
|
| 123 |
+
clear()
|
| 124 |
+
target = list(range(10))
|
| 125 |
+
random.shuffle(target)
|
| 126 |
+
for i, t in enumerate(target):
|
| 127 |
+
for j in range(i, len(s)):
|
| 128 |
+
if s[j].size == t + 1:
|
| 129 |
+
s.insert(i, s.pop(j))
|
| 130 |
+
show_text(instructions1)
|
| 131 |
+
show_text(instructions2, line=1)
|
| 132 |
+
enable_keys()
|
| 133 |
+
|
| 134 |
+
def show_text(text, line=0):
|
| 135 |
+
line = 20 * line
|
| 136 |
+
goto(0,-250 - line)
|
| 137 |
+
write(text, align="center", font=("Courier", 16, "bold"))
|
| 138 |
+
|
| 139 |
+
def start_ssort():
|
| 140 |
+
disable_keys()
|
| 141 |
+
clear()
|
| 142 |
+
show_text("Selection Sort")
|
| 143 |
+
ssort(s)
|
| 144 |
+
clear()
|
| 145 |
+
show_text(instructions1)
|
| 146 |
+
show_text(instructions2, line=1)
|
| 147 |
+
enable_keys()
|
| 148 |
+
|
| 149 |
+
def start_isort():
|
| 150 |
+
disable_keys()
|
| 151 |
+
clear()
|
| 152 |
+
show_text("Insertion Sort")
|
| 153 |
+
isort(s)
|
| 154 |
+
clear()
|
| 155 |
+
show_text(instructions1)
|
| 156 |
+
show_text(instructions2, line=1)
|
| 157 |
+
enable_keys()
|
| 158 |
+
|
| 159 |
+
def start_qsort():
|
| 160 |
+
disable_keys()
|
| 161 |
+
clear()
|
| 162 |
+
show_text("Quicksort")
|
| 163 |
+
qsort(s, 0, len(s) - 1)
|
| 164 |
+
clear()
|
| 165 |
+
show_text(instructions1)
|
| 166 |
+
show_text(instructions2, line=1)
|
| 167 |
+
enable_keys()
|
| 168 |
+
|
| 169 |
+
def init_shelf():
|
| 170 |
+
global s
|
| 171 |
+
s = Shelf(-200)
|
| 172 |
+
vals = (4, 2, 8, 9, 1, 5, 10, 3, 7, 6)
|
| 173 |
+
for i in vals:
|
| 174 |
+
s.push(Block(i))
|
| 175 |
+
|
| 176 |
+
def disable_keys():
|
| 177 |
+
onkey(None, "s")
|
| 178 |
+
onkey(None, "i")
|
| 179 |
+
onkey(None, "q")
|
| 180 |
+
onkey(None, "r")
|
| 181 |
+
|
| 182 |
+
def enable_keys():
|
| 183 |
+
onkey(start_isort, "i")
|
| 184 |
+
onkey(start_ssort, "s")
|
| 185 |
+
onkey(start_qsort, "q")
|
| 186 |
+
onkey(randomize, "r")
|
| 187 |
+
onkey(bye, "space")
|
| 188 |
+
|
| 189 |
+
def main():
|
| 190 |
+
getscreen().clearscreen()
|
| 191 |
+
ht(); penup()
|
| 192 |
+
init_shelf()
|
| 193 |
+
show_text(instructions1)
|
| 194 |
+
show_text(instructions2, line=1)
|
| 195 |
+
enable_keys()
|
| 196 |
+
listen()
|
| 197 |
+
return "EVENTLOOP"
|
| 198 |
+
|
| 199 |
+
instructions1 = "press i for insertion sort, s for selection sort, q for quicksort"
|
| 200 |
+
instructions2 = "spacebar to quit, r to randomize"
|
| 201 |
+
|
| 202 |
+
if __name__=="__main__":
|
| 203 |
+
msg = main()
|
| 204 |
+
mainloop()
|