titem = self.turtle._item
if self._shown and screen._updatecounter == 0 and screen._tracing > 0:
self._hidden_from_screen = False
if self._resizemode == "noresize": w = 1
elif self._resizemode == "auto": w = self._pensize
else: w =self._outlinewidth
shape = self._polytrafo(self._getshapepoly(tshape))
fc, oc = self._fillcolor, self._pencolor
screen._drawpoly(titem, shape, fill=fc, outline=oc,
screen._drawimage(titem, self._position, tshape)
elif ttype == "compound":
for item, (poly, fc, oc) in zip(titem, tshape):
poly = self._polytrafo(self._getshapepoly(poly, True))
screen._drawpoly(item, poly, fill=self._cc(fc),
outline=self._cc(oc), width=self._outlinewidth, top=True)
if self._hidden_from_screen:
screen._drawpoly(titem, ((0, 0), (0, 0), (0, 0)), "", "")
screen._drawimage(titem, self._position,
screen._shapes["blank"]._data)
elif ttype == "compound":
screen._drawpoly(item, ((0, 0), (0, 0), (0, 0)), "", "")
self._hidden_from_screen = True
############################## stamp stuff ###############################
"""Stamp a copy of the turtleshape onto the canvas and return its id.
Stamp a copy of the turtle shape onto the canvas at the current
turtle position. Return a stamp_id for that stamp, which can be
used to delete it by calling clearstamp(stamp_id).
Example (for a Turtle instance named turtle):
shape = screen._shapes[self.turtle.shapeIndex]
stitem = screen._createpoly()
if self._resizemode == "noresize": w = 1
elif self._resizemode == "auto": w = self._pensize
else: w =self._outlinewidth
shape = self._polytrafo(self._getshapepoly(tshape))
fc, oc = self._fillcolor, self._pencolor
screen._drawpoly(stitem, shape, fill=fc, outline=oc,
stitem = screen._createimage("")
screen._drawimage(stitem, self._position, tshape)
elif ttype == "compound":
item = screen._createpoly()
for item, (poly, fc, oc) in zip(stitem, tshape):
poly = self._polytrafo(self._getshapepoly(poly, True))
screen._drawpoly(item, poly, fill=self._cc(fc),
outline=self._cc(oc), width=self._outlinewidth, top=True)
self.stampItems.append(stitem)
self.undobuffer.push(("stamp", stitem))
def _clearstamp(self, stampid):
"""does the work for clearstamp() and clearstamps()
if stampid in self.stampItems:
if isinstance(stampid, tuple):
self.screen._delete(subitem)
self.screen._delete(stampid)
self.stampItems.remove(stampid)
# Delete stampitem from undobuffer if necessary
# if clearstamp is called directly.
item = ("stamp", stampid)
if item not in buf.buffer:
index = buf.buffer.index(item)
buf.ptr = (buf.ptr - 1) % buf.bufsize
buf.buffer.insert((buf.ptr+1)%buf.bufsize, [None])
def clearstamp(self, stampid):
"""Delete stamp with given stampid
stampid - an integer, must be return value of previous stamp() call.
Example (for a Turtle instance named turtle):
>>> astamp = turtle.stamp()
>>> turtle.clearstamp(astamp)
self._clearstamp(stampid)
def clearstamps(self, n=None):
"""Delete all or first/last n of turtle's stamps.
If n is None, delete all of pen's stamps,
else if n > 0 delete first n stamps
else if n < 0 delete last n stamps.
Example (for a Turtle instance named turtle):
... turtle.stamp(); turtle.fd(30)
>>> turtle.clearstamps(2)
>>> turtle.clearstamps(-2)
toDelete = self.stampItems[:]
toDelete = self.stampItems[:n]
toDelete = self.stampItems[n:]
"""Move the pen to the point end, thereby drawing a line
if pen is down. All other methods for turtle movement depend
## Version with undo-stuff
go_modes = ( self._drawing,
isinstance(self._fillpath, list))
undo_entry = ("go", self._position, end, go_modes,
screen._pointlist(self.currentLineItem),
self.undobuffer.push(undo_entry)
if self._speed and screen._tracing == 1:
diffsq = (diff[0]*screen.xscale)**2 + (diff[1]*screen.yscale)**2
nhops = 1+int((diffsq**0.5)/(3*(1.1**self._speed)*self._speed))
delta = diff * (1.0/nhops)
for n in range(1, nhops):
self._position = start + delta * n
screen._drawline(self.drawingLineItem,
self._pencolor, self._pensize, top)
screen._drawline(self.drawingLineItem, ((0, 0), (0, 0)),
fill="", width=self._pensize)
if self._drawing: # now update currentLine
self.currentLine.append(end)
if isinstance(self._fillpath, list):
self._fillpath.append(end)
###### vererbung!!!!!!!!!!!!!!!!!!!!!!
if len(self.currentLine) > 42: # 42! answer to the ultimate question
# of life, the universe and everything
self._update() #count=True)
def _undogoto(self, entry):
"""Reverse a _goto. Used for undo()
old, new, go_modes, coodata = entry
drawing, pc, ps, filling = go_modes
cLI, cL, pl, items = coodata
if abs(self._position - new) > 0.5:
print ("undogoto: HALLO-DA-STIMMT-WAS-NICHT!")
# restore former situation
self.currentLineItem = cLI
if pl == [(0, 0), (0, 0)]:
screen._drawline(cLI, pl, fill=usepc, width=ps)
todelete = [i for i in self.items if (i not in items) and
(screen._type(i) == "line")]
if self._speed and screen._tracing == 1:
diffsq = (diff[0]*screen.xscale)**2 + (diff[1]*screen.yscale)**2
nhops = 1+int((diffsq**0.5)/(3*(1.1**self._speed)*self._speed))
delta = diff * (1.0/nhops)
for n in range(1, nhops):
self._position = new + delta * n
screen._drawline(self.drawingLineItem,
screen._drawline(self.drawingLineItem, ((0, 0), (0, 0)),
# Turtle now at position old,
## if undo is done during creating a polygon, the last vertex
## will be deleted. if the polygon is entirely deleted,
## creatingPoly will be set to False.
## Polygons created before the last one will not be affected by undo()
self._creatingPoly = False
print("Unwahrscheinlich in _undogoto!")
elif self._fillpath is not None:
self._update() #count=True)
def _rotate(self, angle):
"""Turns pen clockwise by angle.
self.undobuffer.push(("rot", angle, self._degreesPerAU))
angle *= self._degreesPerAU
neworient = self._orient.rotate(angle)
tracing = self.screen._tracing
if tracing == 1 and self._speed > 0:
anglevel = 3.0 * self._speed
steps = 1 + int(abs(angle)/anglevel)
self._orient = self._orient.rotate(delta)
def _newLine(self, usePos=True):
"""Closes current line item and starts a new one.
Remark: if current line became too long, animation
performance (via _drawline) slowed down considerably.
if len(self.currentLine) > 1:
self.screen._drawline(self.currentLineItem, self.currentLine,
self._pencolor, self._pensize)
self.currentLineItem = self.screen._createline()
self.items.append(self.currentLineItem)
self.screen._drawline(self.currentLineItem, top=True)
self.currentLine = [self._position]
"""Return fillstate (True if filling, False else).
Example (for a Turtle instance named turtle):
return isinstance(self._fillpath, list)
"""Called just before drawing a shape to be filled.
Example (for a Turtle instance named turtle):
>>> turtle.color("black", "red")
self._fillitem = self.screen._createpoly()
self.items.append(self._fillitem)
self._fillpath = [self._position]
self.undobuffer.push(("beginfill", self._fillitem))
"""Fill the shape drawn after the call begin_fill().
Example (for a Turtle instance named turtle):
>>> turtle.color("black", "red")
if len(self._fillpath) > 2:
self.screen._drawpoly(self._fillitem, self._fillpath,
self.undobuffer.push(("dofill", self._fillitem))
self._fillitem = self._fillpath = None
def dot(self, size=None, *color):
"""Draw a dot with diameter size, using color.
size -- an integer >= 1 (if given)
color -- a colorstring or a numeric color tuple
Draw a circular dot with diameter size, using color.
If size is not given, the maximum of pensize+4 and 2*pensize is used.
Example (for a Turtle instance named turtle):
>>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
if isinstance(size, (str, tuple)):
color = self._colorstr(size)
size = self._pensize + max(self._pensize, 4)
size = self._pensize + max(self._pensize, 4)
size = self._pensize + max(self._pensize, 4)
color = self._colorstr(color)
if hasattr(self.screen, "_dot"):
item = self.screen._dot(self._position, size, color)
self.undobuffer.push(("dot", item))
self.undobuffer.push(["seq"])
self.undobuffer.cumulate = True
if self.resizemode() == 'auto':
self.undobuffer.cumulate = False
def _write(self, txt, align, font):
"""Performs the writing for write()
item, end = self.screen._write(self._position, txt, align, font,
self.undobuffer.push(("wri", item))
def write(self, arg, move=False, align="left", font=("Arial", 8, "normal")):
"""Write text at the current turtle position.
arg -- info, which is to be written to the TurtleScreen
move (optional) -- True/False
align (optional) -- one of the strings "left", "center" or right"
font (optional) -- a triple (fontname, fontsize, fonttype)
Write text - the string representation of arg - at the current
turtle position according to align ("left", "center" or right")
If move is True, the pen is moved to the bottom-right corner
of the text. By default, move is False.
Example (for a Turtle instance named turtle):
>>> turtle.write('Home = ', True, align="center")
>>> turtle.write((0,0), True)
self.undobuffer.push(["seq"])
self.undobuffer.cumulate = True
end = self._write(str(arg), align.lower(), font)
self.undobuffer.cumulate = False
"""Start recording the vertices of a polygon.
Start recording the vertices of a polygon. Current turtle position
is first point of polygon.
Example (for a Turtle instance named turtle):
self._poly = [self._position]
self._creatingPoly = True
"""Stop recording the vertices of a polygon.
Stop recording the vertices of a polygon. Current turtle position is
last point of polygon. This will be connected with the first point.
Example (for a Turtle instance named turtle):
self._creatingPoly = False
"""Return the lastly recorded polygon.
Example (for a Turtle instance named turtle):
>>> p = turtle.get_poly()
>>> turtle.register_shape("myFavouriteShape", p)
## check if there is any poly?
if self._poly is not None:
"""Return the TurtleScreen object, the turtle is drawing on.
Return the TurtleScreen object, the turtle is drawing on.
So TurtleScreen-methods can be called for that object.
Example (for a Turtle instance named turtle):
>>> ts = turtle.getscreen()
<turtle.TurtleScreen object at 0x0106B770>
"""Return the Turtleobject itself.
Only reasonable use: as a function to return the 'anonymous turtle':