blob: 1b418ebc82ff0c7976691ab2c2e6ce1a3567e2c1 [file] [log] [blame]
Guido van Rossumb241b671998-12-04 16:42:46 +00001# LogoMation-like turtle graphics
2
3from math import * # Also for export
4import Tkinter
Martin v. Löwis4b6ea792000-10-01 17:52:01 +00005class Error(Exception):
6 pass
Guido van Rossumb241b671998-12-04 16:42:46 +00007
8class RawPen:
9
10 def __init__(self, canvas):
11 self._canvas = canvas
12 self._items = []
13 self._tracing = 1
Guido van Rossum3c7a25a2001-08-09 16:42:07 +000014 self._arrow = 0
Guido van Rossumb241b671998-12-04 16:42:46 +000015 self.degrees()
16 self.reset()
17
18 def degrees(self, fullcircle=360.0):
19 self._fullcircle = fullcircle
20 self._invradian = pi / (fullcircle * 0.5)
21
22 def radians(self):
23 self.degrees(2.0*pi)
24
25 def reset(self):
26 canvas = self._canvas
Martin v. Löwis73b9b662002-09-22 13:00:26 +000027 self._canvas.update()
Guido van Rossumb241b671998-12-04 16:42:46 +000028 width = canvas.winfo_width()
29 height = canvas.winfo_height()
30 if width <= 1:
31 width = canvas['width']
32 if height <= 1:
33 height = canvas['height']
34 self._origin = float(width)/2.0, float(height)/2.0
35 self._position = self._origin
36 self._angle = 0.0
37 self._drawing = 1
38 self._width = 1
39 self._color = "black"
40 self._filling = 0
41 self._path = []
42 self._tofill = []
43 self.clear()
44 canvas._root().tkraise()
45
46 def clear(self):
47 self.fill(0)
48 canvas = self._canvas
49 items = self._items
50 self._items = []
51 for item in items:
52 canvas.delete(item)
Guido van Rossum3c7a25a2001-08-09 16:42:07 +000053 self._delete_turtle()
54 self._draw_turtle()
55
Guido van Rossumb241b671998-12-04 16:42:46 +000056
57 def tracer(self, flag):
58 self._tracing = flag
Guido van Rossum3c7a25a2001-08-09 16:42:07 +000059 if not self._tracing:
60 self._delete_turtle()
61 self._draw_turtle()
Guido van Rossumb241b671998-12-04 16:42:46 +000062
63 def forward(self, distance):
64 x0, y0 = start = self._position
65 x1 = x0 + distance * cos(self._angle*self._invradian)
66 y1 = y0 - distance * sin(self._angle*self._invradian)
67 self._goto(x1, y1)
68
69 def backward(self, distance):
70 self.forward(-distance)
71
72 def left(self, angle):
73 self._angle = (self._angle + angle) % self._fullcircle
Guido van Rossum3c7a25a2001-08-09 16:42:07 +000074 self._draw_turtle()
Guido van Rossumb241b671998-12-04 16:42:46 +000075
76 def right(self, angle):
77 self.left(-angle)
78
79 def up(self):
80 self._drawing = 0
81
82 def down(self):
83 self._drawing = 1
84
85 def width(self, width):
86 self._width = float(width)
87
88 def color(self, *args):
89 if not args:
90 raise Error, "no color arguments"
91 if len(args) == 1:
92 color = args[0]
93 if type(color) == type(""):
94 # Test the color first
95 try:
96 id = self._canvas.create_line(0, 0, 0, 0, fill=color)
Martin v. Löwis4b6ea792000-10-01 17:52:01 +000097 except Tkinter.TclError:
Guido van Rossumb241b671998-12-04 16:42:46 +000098 raise Error, "bad color string: %s" % `color`
Guido van Rossum3c7a25a2001-08-09 16:42:07 +000099 self._set_color(color)
Guido van Rossumb241b671998-12-04 16:42:46 +0000100 return
101 try:
102 r, g, b = color
103 except:
104 raise Error, "bad color sequence: %s" % `color`
105 else:
106 try:
107 r, g, b = args
108 except:
109 raise Error, "bad color arguments: %s" % `args`
110 assert 0 <= r <= 1
111 assert 0 <= g <= 1
112 assert 0 <= b <= 1
113 x = 255.0
114 y = 0.5
Guido van Rossum3c7a25a2001-08-09 16:42:07 +0000115 self._set_color("#%02x%02x%02x" % (int(r*x+y), int(g*x+y), int(b*x+y)))
116
117 def _set_color(self,color):
118 self._color = color
119 self._draw_turtle()
120
Guido van Rossumb241b671998-12-04 16:42:46 +0000121
122 def write(self, arg, move=0):
123 x, y = start = self._position
124 x = x-1 # correction -- calibrated for Windows
Fred Draked038ca82000-10-23 18:31:14 +0000125 item = self._canvas.create_text(x, y,
Guido van Rossumb241b671998-12-04 16:42:46 +0000126 text=str(arg), anchor="sw",
127 fill=self._color)
128 self._items.append(item)
129 if move:
130 x0, y0, x1, y1 = self._canvas.bbox(item)
131 self._goto(x1, y1)
Guido van Rossum3c7a25a2001-08-09 16:42:07 +0000132 self._draw_turtle()
Guido van Rossumb241b671998-12-04 16:42:46 +0000133
134 def fill(self, flag):
135 if self._filling:
136 path = tuple(self._path)
137 smooth = self._filling < 0
138 if len(path) > 2:
139 item = self._canvas._create('polygon', path,
140 {'fill': self._color,
141 'smooth': smooth})
142 self._items.append(item)
143 self._canvas.lower(item)
144 if self._tofill:
145 for item in self._tofill:
146 self._canvas.itemconfigure(item, fill=self._color)
147 self._items.append(item)
148 self._path = []
149 self._tofill = []
150 self._filling = flag
151 if flag:
152 self._path.append(self._position)
153
154 def circle(self, radius, extent=None):
155 if extent is None:
156 extent = self._fullcircle
157 x0, y0 = self._position
158 xc = x0 - radius * sin(self._angle * self._invradian)
159 yc = y0 - radius * cos(self._angle * self._invradian)
160 if radius >= 0.0:
161 start = self._angle - 90.0
162 else:
163 start = self._angle + 90.0
164 extent = -extent
165 if self._filling:
166 if abs(extent) >= self._fullcircle:
167 item = self._canvas.create_oval(xc-radius, yc-radius,
168 xc+radius, yc+radius,
169 width=self._width,
170 outline="")
171 self._tofill.append(item)
172 item = self._canvas.create_arc(xc-radius, yc-radius,
173 xc+radius, yc+radius,
174 style="chord",
175 start=start,
176 extent=extent,
177 width=self._width,
178 outline="")
179 self._tofill.append(item)
180 if self._drawing:
181 if abs(extent) >= self._fullcircle:
182 item = self._canvas.create_oval(xc-radius, yc-radius,
183 xc+radius, yc+radius,
184 width=self._width,
185 outline=self._color)
186 self._items.append(item)
187 item = self._canvas.create_arc(xc-radius, yc-radius,
188 xc+radius, yc+radius,
189 style="arc",
190 start=start,
191 extent=extent,
192 width=self._width,
193 outline=self._color)
194 self._items.append(item)
195 angle = start + extent
196 x1 = xc + abs(radius) * cos(angle * self._invradian)
197 y1 = yc - abs(radius) * sin(angle * self._invradian)
198 self._angle = (self._angle + extent) % self._fullcircle
199 self._position = x1, y1
200 if self._filling:
201 self._path.append(self._position)
Martin v. Löwis73b9b662002-09-22 13:00:26 +0000202 self._draw_turtle()
Guido van Rossumb241b671998-12-04 16:42:46 +0000203
204 def goto(self, *args):
205 if len(args) == 1:
206 try:
207 x, y = args[0]
208 except:
209 raise Error, "bad point argument: %s" % `args[0]`
210 else:
211 try:
212 x, y = args
213 except:
214 raise Error, "bad coordinates: %s" % `args[0]`
215 x0, y0 = self._origin
216 self._goto(x0+x, y0-y)
217
218 def _goto(self, x1, y1):
219 x0, y0 = start = self._position
220 self._position = map(float, (x1, y1))
221 if self._filling:
222 self._path.append(self._position)
223 if self._drawing:
Guido van Rossum3c7a25a2001-08-09 16:42:07 +0000224 if self._tracing:
Guido van Rossumb241b671998-12-04 16:42:46 +0000225 dx = float(x1 - x0)
226 dy = float(y1 - y0)
227 distance = hypot(dx, dy)
228 nhops = int(distance)
229 item = self._canvas.create_line(x0, y0, x0, y0,
230 width=self._width,
Guido van Rossumb241b671998-12-04 16:42:46 +0000231 capstyle="round",
232 fill=self._color)
233 try:
234 for i in range(1, 1+nhops):
235 x, y = x0 + dx*i/nhops, y0 + dy*i/nhops
236 self._canvas.coords(item, x0, y0, x, y)
Guido van Rossum3c7a25a2001-08-09 16:42:07 +0000237 self._draw_turtle((x,y))
Guido van Rossumb241b671998-12-04 16:42:46 +0000238 self._canvas.update()
239 self._canvas.after(10)
Guido van Rossuma659efe2001-01-01 19:11:07 +0000240 # in case nhops==0
241 self._canvas.coords(item, x0, y0, x1, y1)
Guido van Rossumb241b671998-12-04 16:42:46 +0000242 self._canvas.itemconfigure(item, arrow="none")
Martin v. Löwis4b6ea792000-10-01 17:52:01 +0000243 except Tkinter.TclError:
Guido van Rossumb241b671998-12-04 16:42:46 +0000244 # Probably the window was closed!
245 return
246 else:
247 item = self._canvas.create_line(x0, y0, x1, y1,
248 width=self._width,
249 capstyle="round",
250 fill=self._color)
251 self._items.append(item)
Guido van Rossum3c7a25a2001-08-09 16:42:07 +0000252 self._draw_turtle()
253
254 def _draw_turtle(self,position=[]):
255 if not self._tracing:
256 return
257 if position == []:
258 position = self._position
259 x,y = position
260 distance = 8
261 dx = distance * cos(self._angle*self._invradian)
262 dy = distance * sin(self._angle*self._invradian)
263 self._delete_turtle()
Martin v. Löwis4157ffb2002-03-28 15:45:57 +0000264 self._arrow = self._canvas.create_line(x-dx,y+dy,x,y,
Guido van Rossum3c7a25a2001-08-09 16:42:07 +0000265 width=self._width,
266 arrow="last",
267 capstyle="round",
268 fill=self._color)
269 self._canvas.update()
270
271 def _delete_turtle(self):
272 if self._arrow != 0:
273 self._canvas.delete(self._arrow)
274 self._arrow = 0
275
Guido van Rossumb241b671998-12-04 16:42:46 +0000276
277
278_root = None
279_canvas = None
280_pen = None
281
282class Pen(RawPen):
283
284 def __init__(self):
285 global _root, _canvas
286 if _root is None:
Martin v. Löwis4b6ea792000-10-01 17:52:01 +0000287 _root = Tkinter.Tk()
Guido van Rossumb241b671998-12-04 16:42:46 +0000288 _root.wm_protocol("WM_DELETE_WINDOW", self._destroy)
289 if _canvas is None:
290 # XXX Should have scroll bars
Martin v. Löwis4b6ea792000-10-01 17:52:01 +0000291 _canvas = Tkinter.Canvas(_root, background="white")
Guido van Rossumb241b671998-12-04 16:42:46 +0000292 _canvas.pack(expand=1, fill="both")
293 RawPen.__init__(self, _canvas)
294
295 def _destroy(self):
296 global _root, _canvas, _pen
297 root = self._canvas._root()
298 if root is _root:
299 _pen = None
300 _root = None
301 _canvas = None
302 root.destroy()
Fred Draked038ca82000-10-23 18:31:14 +0000303
Guido van Rossumb241b671998-12-04 16:42:46 +0000304
305def _getpen():
306 global _pen
307 pen = _pen
308 if not pen:
309 _pen = pen = Pen()
310 return pen
311
312def degrees(): _getpen().degrees()
313def radians(): _getpen().radians()
314def reset(): _getpen().reset()
315def clear(): _getpen().clear()
316def tracer(flag): _getpen().tracer(flag)
317def forward(distance): _getpen().forward(distance)
318def backward(distance): _getpen().backward(distance)
319def left(angle): _getpen().left(angle)
320def right(angle): _getpen().right(angle)
321def up(): _getpen().up()
322def down(): _getpen().down()
323def width(width): _getpen().width(width)
324def color(*args): apply(_getpen().color, args)
325def write(arg, move=0): _getpen().write(arg, move)
326def fill(flag): _getpen().fill(flag)
327def circle(radius, extent=None): _getpen().circle(radius, extent)
328def goto(*args): apply(_getpen().goto, args)
329
330def demo():
331 reset()
332 tracer(1)
333 up()
334 backward(100)
335 down()
336 # draw 3 squares; the last filled
337 width(3)
338 for i in range(3):
339 if i == 2:
340 fill(1)
341 for j in range(4):
342 forward(20)
343 left(90)
344 if i == 2:
345 color("maroon")
346 fill(0)
347 up()
348 forward(30)
349 down()
350 width(1)
351 color("black")
352 # move out of the way
353 tracer(0)
354 up()
355 right(90)
356 forward(100)
357 right(90)
358 forward(100)
359 right(180)
360 down()
361 # some text
362 write("startstart", 1)
363 write("start", 1)
364 color("red")
365 # staircase
366 for i in range(5):
367 forward(20)
368 left(90)
369 forward(20)
370 right(90)
371 # filled staircase
372 fill(1)
373 for i in range(5):
374 forward(20)
375 left(90)
376 forward(20)
377 right(90)
378 fill(0)
379 # more text
380 write("end")
381 if __name__ == '__main__':
382 _root.mainloop()
383
384if __name__ == '__main__':
385 demo()