forked from ramalho/jupyturtle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjupyturtle.py
242 lines (189 loc) · 6.55 KB
/
jupyturtle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import math
import time
from dataclasses import dataclass
from textwrap import dedent
from typing import NamedTuple
from IPython.display import display
from ipycanvas import MultiCanvas
# defaults
CANVAS_WIDTH = 400
CANVAS_HEIGHT = CANVAS_WIDTH // 2
CANVAS_BGCOLOR = '#F3F3F7' # "anti-flash white" (non-standard name)
class Point(NamedTuple):
x: float = 0
y: float = 0
def translated(self, dx: float, dy: float):
return Point(self.x + dx, self.y + dy)
class Line(NamedTuple):
p1: Point
p2: Point
color: str
width: int
# mapping of method names to global aliases
_commands = {}
# decorators to build procedural API with turtle commands
def command(method):
"""register method for use as a top level function in procedural API"""
_commands[method.__name__] = [] # no alias
return method
def command_alias(*names):
def decorator(method):
_commands[method.__name__] = list(names)
return method
return decorator
# defaults
TURTLE_HEADING = 0.0 # pointing to screen left, a.k.a. "east"
TURTLE_COLOR = '#63A375' # "mint" (non-standard name)
TURTLE_THICKNESS = 2 # width of circle stroke
TURTLE_DELAY = 0.01 # seconds between turtle actions
PEN_COLOR = '#663399' # rebeccapurple https://www.w3.org/TR/css-color-4/#valdef-color-rebeccapurple
PEN_WIDTH = 2
TURTLE_SVG = dedent(
"""
<g transform="rotate({heading},{x},{y}) translate({x}, {y})">
<circle stroke="{color}" stroke-width="2" fill="transparent" r="5.5" cx="0" cy="0"/>
<polygon points="0,12 2,9 -2,9" style="fill:{color};stroke:{color};stroke-width:2"/>
</g>
"""
).rstrip()
class Turtle:
def __init__(
self, *, delay: float = TURTLE_DELAY, canvas: MultiCanvas | None = None
):
self.delay = delay
if canvas:
assert isinstance(canvas, MultiCanvas)
self.canvas = canvas
else:
self.canvas = MultiCanvas(3, width=CANVAS_WIDTH, height=CANVAS_HEIGHT, sync_image_data=True)
try:
self.background = self.canvas[0]
self.drawing = self.canvas[1]
self.foreground = self.canvas[2]
except IndexError:
raise ValueError('MultiCanvas must have at least 3 layers')
self.position = Point(self.canvas.width // 2, self.canvas.height // 2)
self.heading = TURTLE_HEADING
self.color = TURTLE_COLOR
self.visible = True
self.pen_is_down = True
self.pen_color = PEN_COLOR
self.pen_width = PEN_WIDTH
self.prepare_layers()
if self.visible:
self.draw()
display(self.canvas)
def prepare_layers(self):
"""setup canvas layers"""
self.background.fill_style = CANVAS_BGCOLOR
self.background.fill_rect(0, 0, self.canvas.width, self.canvas.height)
# drawing layer
self.drawing.line_width = self.pen_width
self.drawing.stroke_style = self.pen_color
@property
def x(self) -> float:
return self.position.x
@property
def y(self) -> float:
return self.position.y
@property
def heading(self) -> float:
return self.__heading
@heading.setter
def heading(self, new_heading) -> None:
self.__heading = new_heading % 360.0
def draw(self):
"""Draw the turtle."""
x, y = self.x, self.y
self.foreground.line_width = TURTLE_THICKNESS
self.foreground.stroke_style = self.color
self.foreground.fill_style = self.color
head_x = x + 7 * math.cos(math.radians(self.heading))
head_y = y + 7 * math.sin(math.radians(self.heading))
self.foreground.clear()
self.foreground.stroke_circle(x + 0.5, y + 0.5, 5)
self.foreground.stroke_circle(head_x, head_y, 1)
@command
def hide(self):
"""Hide the turtle. It will still draw, but you won't see it."""
self.visible = False
self.foreground.clear()
@command
def show(self):
"""Show the turtle."""
self.visible = True
self.draw()
@command_alias('fd')
def forward(self, units: float):
"""Move the turtle forward by units, drawing if the pen is down."""
angle = math.radians(self.heading)
dx = units * math.cos(angle)
dy = units * math.sin(angle)
new_pos = self.position.translated(dx, dy)
if self.pen_is_down:
self.drawing.stroke_line(self.x, self.y, new_pos.x, new_pos.y)
self.position = new_pos
if self.visible:
self.draw()
if self.delay:
time.sleep(self.delay)
@command_alias('lt')
def left(self, degrees: float):
"""Turn the turtle left by degrees."""
self.heading -= degrees
if self.visible:
self.draw()
if self.delay:
time.sleep(self.delay)
@command_alias('rt')
def right(self, degrees: float):
"""Turn the turtle right by degrees."""
self.heading += degrees
if self.visible:
self.draw()
if self.delay:
time.sleep(self.delay)
@command
def penup(self):
"""Lift the pen, so the turtle leaves no trail."""
self.pen_is_down = False
@command
def pendown(self):
"""Lower the pen, so the turtle leaves a trail."""
self.pen_is_down = True
################################################## procedural API
# _install_command() will append more names when the module loads
__all__ = ['Turtle', 'make_turtle', 'get_turtle']
def __dir__():
return sorted(__all__)
_main_turtle = None
def make_turtle(*, delay=TURTLE_DELAY) -> None:
"""Makes new Turtle and sets _main_turtle."""
global _main_turtle
_main_turtle = Turtle(delay=delay)
def get_turtle() -> Turtle:
"""Gets existing _main_turtle; makes one if needed."""
global _main_turtle
if _main_turtle is None:
_main_turtle = Turtle()
return _main_turtle
def _make_command(name):
method = getattr(Turtle, name) # get unbound method
def command(*args):
turtle = get_turtle()
method(turtle, *args)
command.__name__ = name
command.__doc__ = method.__doc__
return command
def _install_command(name, function):
if name in globals():
raise ValueError(f'duplicate turtle command name: {name}')
globals()[name] = function
__all__.append(name)
def _install_commands():
for name, aliases in _commands.items():
new_command = _make_command(name)
_install_command(name, new_command)
for alias in aliases:
_install_command(alias, new_command)
_install_commands()