# LogoMation-like turtle graphics
from math
import * # Also for export
def __init__(self
, canvas
):
def degrees(self
, fullcircle
=360.0):
self
._fullcircle
= fullcircle
self
._invradian
= pi
/ (fullcircle
* 0.5)
width
= canvas
.winfo_width()
height
= canvas
.winfo_height()
height
= canvas
['height']
self
._origin
= float(width
)/2.0, float(height
)/2.0
self
._position
= self
._origin
def forward(self
, distance
):
x0
, y0
= start
= self
._position
x1
= x0
+ distance
* cos(self
._angle
*self
._invradian
)
y1
= y0
- distance
* sin(self
._angle
*self
._invradian
)
def backward(self
, distance
):
self
._angle
= (self
._angle
+ angle
) % self
._fullcircle
self
._width
= float(width
)
raise Error
, "no color arguments"
if type(color
) == type(""):
id = self
._canvas
.create_line(0, 0, 0, 0, fill
=color
)
raise Error
, "bad color string: %r" % (color
,)
raise Error
, "bad color sequence: %r" % (color
,)
raise Error
, "bad color arguments: %r" % (args
,)
self
._set
_color
("#%02x%02x%02x" % (int(r
*x
+y
), int(g
*x
+y
), int(b
*x
+y
)))
def _set_color(self
,color
):
def write(self
, arg
, move
=0):
x
, y
= start
= self
._position
x
= x
-1 # correction -- calibrated for Windows
item
= self
._canvas
.create_text(x
, y
,
text
=str(arg
), anchor
="sw",
x0
, y0
, x1
, y1
= self
._canvas
.bbox(item
)
smooth
= self
._filling
< 0
item
= self
._canvas
._create
('polygon', path
,
for item
in self
._tofill
:
self
._canvas
.itemconfigure(item
, fill
=self
._color
)
self
._path
.append(self
._position
)
def circle(self
, radius
, extent
=None):
extent
= self
._fullcircle
xc
= x0
- radius
* sin(self
._angle
* self
._invradian
)
yc
= y0
- radius
* cos(self
._angle
* self
._invradian
)
start
= self
._angle
- 90.0
start
= self
._angle
+ 90.0
if abs(extent
) >= self
._fullcircle
:
item
= self
._canvas
.create_oval(xc
-radius
, yc
-radius
,
self
._tofill
.append(item
)
item
= self
._canvas
.create_arc(xc
-radius
, yc
-radius
,
self
._tofill
.append(item
)
if abs(extent
) >= self
._fullcircle
:
item
= self
._canvas
.create_oval(xc
-radius
, yc
-radius
,
item
= self
._canvas
.create_arc(xc
-radius
, yc
-radius
,
x1
= xc
+ abs(radius
) * cos(angle
* self
._invradian
)
y1
= yc
- abs(radius
) * sin(angle
* self
._invradian
)
self
._angle
= (self
._angle
+ extent
) % self
._fullcircle
self
._path
.append(self
._position
)
def setheading(self
, angle
):
width
= self
._canvas
.winfo_width()
if width
<= 1: # the window isn't managed by a geometry manager
width
= self
._canvas
['width']
height
= self
._canvas
.winfo_height()
if height
<= 1: # the window isn't managed by a geometry manager
height
= self
._canvas
['height']
raise Error
, "bad point argument: %r" % (args
[0],)
raise Error
, "bad coordinates: %r" % (args
[0],)
x0
, y0
= start
= self
._position
self
._position
= map(float, (x1
, y1
))
self
._path
.append(self
._position
)
item
= self
._canvas
.create_line(x0
, y0
, x0
, y0
,
for i
in range(1, 1+nhops
):
x
, y
= x0
+ dx
*i
/nhops
, y0
+ dy
*i
/nhops
self
._canvas
.coords(item
, x0
, y0
, x
, y
)
self
._canvas
.coords(item
, x0
, y0
, x1
, y1
)
self
._canvas
.itemconfigure(item
, arrow
="none")
# Probably the window was closed!
item
= self
._canvas
.create_line(x0
, y0
, x1
, y1
,
def _draw_turtle(self
,position
=[]):
position
= self
._position
dx
= distance
* cos(self
._angle
*self
._invradian
)
dy
= distance
* sin(self
._angle
*self
._invradian
)
self
._arrow
= self
._canvas
.create_line(x
-dx
,y
+dy
,x
,y
,
def _delete_turtle(self
):
self
._canvas
.delete(self
._arrow
)
_root
.wm_protocol("WM_DELETE_WINDOW", self
._destroy
)
# XXX Should have scroll bars
_canvas
= Tkinter
.Canvas(_root
, background
="white")
_canvas
.pack(expand
=1, fill
="both")
RawPen
.__init
__(self
, _canvas
)
global _root
, _canvas
, _pen
root
= self
._canvas
._root
()
def degrees(): _getpen().degrees()
def radians(): _getpen().radians()
def reset(): _getpen().reset()
def clear(): _getpen().clear()
def tracer(flag
): _getpen().tracer(flag
)
def forward(distance
): _getpen().forward(distance
)
def backward(distance
): _getpen().backward(distance
)
def left(angle
): _getpen().left(angle
)
def right(angle
): _getpen().right(angle
)
def down(): _getpen().down()
def width(width
): _getpen().width(width
)
def color(*args
): _getpen().color(*args
)
def write(arg
, move
=0): _getpen().write(arg
, move
)
def fill(flag
): _getpen().fill(flag
)
def circle(radius
, extent
=None): _getpen().circle(radius
, extent
)
def goto(*args
): _getpen().goto(*args
)
def heading(): return _getpen().heading()
def setheading(angle
): _getpen().setheading(angle
)
def position(): return _getpen().position()
def window_width(): return _getpen().window_width()
def window_height(): return _getpen().window_height()
def setx(xpos
): _getpen().setx(xpos
)
def sety(ypos
): _getpen().sety(ypos
)
# draw 3 squares; the last filled
if __name__
== '__main__':
if __name__
== '__main__':