10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# turtle.py: a Tkinter based turtle graphics module for Python
30a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Version 1.0.1 - 24. 9. 2009
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Copyright (C) 2006 - 2010  Gregor Lingl
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# email: glingl@aon.at
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# This software is provided 'as-is', without any express or implied
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# warranty.  In no event will the authors be held liable for any damages
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# arising from the use of this software.
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Permission is granted to anyone to use this software for any purpose,
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# including commercial applications, and to alter it and redistribute it
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# freely, subject to the following restrictions:
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 1. The origin of this software must not be misrepresented; you must not
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#    claim that you wrote the original software. If you use this software
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#    in a product, an acknowledgment in the product documentation would be
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#    appreciated but is not required.
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 2. Altered source versions must be plainly marked as such, and must not be
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#    misrepresented as being the original software.
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# 3. This notice may not be removed or altered from any source distribution.
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""
260a8c90248264a8b26970b4473770bcc3df8515fJosh GaoTurtle graphics is a popular way for introducing programming to
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gaokids. It was part of the original Logo programming language developed
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoby Wally Feurzig and Seymour Papert in 1966.
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
300a8c90248264a8b26970b4473770bcc3df8515fJosh GaoImagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle``, give it
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gaothe command turtle.forward(15), and it moves (on-screen!) 15 pixels in
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gaothe direction it is facing, drawing a line as it moves. Give it the
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gaocommand turtle.right(25), and it rotates in-place 25 degrees clockwise.
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
350a8c90248264a8b26970b4473770bcc3df8515fJosh GaoBy combining together these and similar commands, intricate shapes and
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gaopictures can easily be drawn.
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao----- turtle.py
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
400a8c90248264a8b26970b4473770bcc3df8515fJosh GaoThis module is an extended reimplementation of turtle.py from the
410a8c90248264a8b26970b4473770bcc3df8515fJosh GaoPython standard distribution up to Python 2.5. (See: http://www.python.org)
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
430a8c90248264a8b26970b4473770bcc3df8515fJosh GaoIt tries to keep the merits of turtle.py and to be (nearly) 100%
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gaocompatible with it. This means in the first place to enable the
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gaolearning programmer to use all the commands, classes and methods
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gaointeractively when using the module from within IDLE run with
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gaothe -n switch.
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
490a8c90248264a8b26970b4473770bcc3df8515fJosh GaoRoughly it has the following features added:
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao- Better animation of the turtle movements, especially of turning the
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  turtle. So the turtles can more easily be used as a visual feedback
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  instrument by the (beginning) programmer.
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao- Different turtle shapes, gif-images as turtle shapes, user defined
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  and user controllable turtle shapes, among them compound
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  (multicolored) shapes. Turtle shapes can be stretched and tilted, which
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  makes turtles very versatile geometrical objects.
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao- Fine control over turtle movement and screen updates via delay(),
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  and enhanced tracer() and speed() methods.
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao- Aliases for the most commonly used commands, like fd for forward etc.,
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  following the early Logo traditions. This reduces the boring work of
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  typing long sequences of commands, which often occur in a natural way
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  when kids try to program fancy pictures on their first encounter with
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  turtle graphics.
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao- Turtles now have an undo()-method with configurable undo-buffer.
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao- Some simple commands/methods for creating event driven programs
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  (mouse-, key-, timer-events). Especially useful for programming games.
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao- A scrollable Canvas class. The default scrollable Canvas can be
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  extended interactively as needed while playing around with the turtle(s).
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao- A TurtleScreen class with methods controlling background color or
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  background image, window and canvas size and other properties of the
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  TurtleScreen.
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao- There is a method, setworldcoordinates(), to install a user defined
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  coordinate-system for the TurtleScreen.
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao- The implementation uses a 2-vector class named Vec2D, derived from tuple.
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  This class is public, so it can be imported by the application programmer,
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  which makes certain types of computations very natural and compact.
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao- Appearance of the TurtleScreen and the Turtles at startup/import can be
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  configured by means of a turtle.cfg configuration file.
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  The default configuration mimics the appearance of the old turtle module.
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao- If configured appropriately the module reads in docstrings from a docstring
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  dictionary in some different language, supplied separately  and replaces
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  the English ones by those read in. There is a utility function
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  write_docstringdict() to write a dictionary with the original (English)
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao  docstrings to disc, so it can serve as a template for translations.
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
980a8c90248264a8b26970b4473770bcc3df8515fJosh GaoBehind the scenes there are some features included with possible
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoextensions in mind. These will be commented and documented elsewhere.
1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""
1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_ver = "turtle 1.0b1 - for Python 2.6   -  30. 5. 2008, 18:08"
1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#print _ver
1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport Tkinter as TK
1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport types
1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport math
1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport time
1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport os
1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom os.path import isfile, split, join
1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom copy import deepcopy
1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom math import *    ## for compatibility with old turtle module
1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_tg_classes = ['ScrolledCanvas', 'TurtleScreen', 'Screen',
1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               'RawTurtle', 'Turtle', 'RawPen', 'Pen', 'Shape', 'Vec2D']
1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_tg_screen_functions = ['addshape', 'bgcolor', 'bgpic', 'bye',
1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'clearscreen', 'colormode', 'delay', 'exitonclick', 'getcanvas',
1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'getshapes', 'listen', 'mode', 'onkey', 'onscreenclick', 'ontimer',
1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'register_shape', 'resetscreen', 'screensize', 'setup',
1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'setworldcoordinates', 'title', 'tracer', 'turtles', 'update',
1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'window_height', 'window_width']
1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_tg_turtle_functions = ['back', 'backward', 'begin_fill', 'begin_poly', 'bk',
1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'circle', 'clear', 'clearstamp', 'clearstamps', 'clone', 'color',
1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'degrees', 'distance', 'dot', 'down', 'end_fill', 'end_poly', 'fd',
1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'fill', 'fillcolor', 'forward', 'get_poly', 'getpen', 'getscreen',
1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'getturtle', 'goto', 'heading', 'hideturtle', 'home', 'ht', 'isdown',
1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'isvisible', 'left', 'lt', 'onclick', 'ondrag', 'onrelease', 'pd',
1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'pen', 'pencolor', 'pendown', 'pensize', 'penup', 'pos', 'position',
1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'pu', 'radians', 'right', 'reset', 'resizemode', 'rt',
1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'seth', 'setheading', 'setpos', 'setposition', 'settiltangle',
1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'setundobuffer', 'setx', 'sety', 'shape', 'shapesize', 'showturtle',
1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'speed', 'st', 'stamp', 'tilt', 'tiltangle', 'towards', 'tracer',
1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'turtlesize', 'undo', 'undobufferentries', 'up', 'width',
1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'window_height', 'window_width', 'write', 'xcor', 'ycor']
1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_tg_utilities = ['write_docstringdict', 'done', 'mainloop']
1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_math_functions = ['acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh',
1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log',
1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'log10', 'modf', 'pi', 'pow', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +
1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           _tg_utilities + _math_functions)
1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_alias_list = ['addshape', 'backward', 'bk', 'fd', 'ht', 'lt', 'pd', 'pos',
1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               'pu', 'rt', 'seth', 'setpos', 'setposition', 'st',
1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               'turtlesize', 'up', 'width']
1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_CFG = {"width" : 0.5,               # Screen
1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "height" : 0.75,
1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "canvwidth" : 400,
1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "canvheight": 300,
1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "leftright": None,
1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "topbottom": None,
1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "mode": "standard",          # TurtleScreen
1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "colormode": 1.0,
1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "delay": 10,
1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "undobuffersize": 1000,      # RawTurtle
1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "shape": "classic",
1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "pencolor" : "black",
1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "fillcolor" : "black",
1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "resizemode" : "noresize",
1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "visible" : True,
1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "language": "english",        # docstrings
1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "exampleturtle": "turtle",
1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "examplescreen": "screen",
1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "title": "Python Turtle Graphics",
1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "using_IDLE": False
1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao       }
1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##print "cwd:", os.getcwd()
1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##print "__file__:", __file__
1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##
1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##def show(dictionary):
1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##    print "=========================="
1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##    for key in sorted(dictionary.keys()):
1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##        print key, ":", dictionary[key]
1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##    print "=========================="
1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##    print
1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef config_dict(filename):
1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Convert content of config-file into dictionary."""
1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    f = open(filename, "r")
1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    cfglines = f.readlines()
1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    f.close()
1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    cfgdict = {}
1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for line in cfglines:
1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        line = line.strip()
1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not line or line.startswith("#"):
1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            continue
1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            key, value = line.split("=")
1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except:
1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            print "Bad line in config-file %s:\n%s" % (filename,line)
1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            continue
1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        key = key.strip()
1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        value = value.strip()
2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if value in ["True", "False", "None", "''", '""']:
2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            value = eval(value)
2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if "." in value:
2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    value = float(value)
2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                else:
2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    value = int(value)
2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except:
2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass # value need not be converted
2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cfgdict[key] = value
2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return cfgdict
2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef readconfig(cfgdict):
2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Read config-files, change configuration-dict accordingly.
2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    If there is a turtle.cfg file in the current working directory,
2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    read it from there. If this contains an importconfig-value,
2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    say 'myway', construct filename turtle_mayway.cfg else use
2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    turtle.cfg and read it from the import-directory, where
2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    turtle.py is located.
2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Update configuration dictionary first according to config-file,
2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    in the import directory, then according to config-file in the
2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    current working directory.
2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    If no config-file is found, the default configuration is used.
2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    default_cfg = "turtle.cfg"
2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    cfgdict1 = {}
2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    cfgdict2 = {}
2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if isfile(default_cfg):
2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cfgdict1 = config_dict(default_cfg)
2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #print "1. Loading config-file %s from: %s" % (default_cfg, os.getcwd())
2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if "importconfig" in cfgdict1:
2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        default_cfg = "turtle_%s.cfg" % cfgdict1["importconfig"]
2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    try:
2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        head, tail = split(__file__)
2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cfg_file2 = join(head, default_cfg)
2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    except:
2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cfg_file2 = ""
2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if isfile(cfg_file2):
2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #print "2. Loading config-file %s:" % cfg_file2
2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cfgdict2 = config_dict(cfg_file2)
2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##    show(_CFG)
2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##    show(cfgdict2)
2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    _CFG.update(cfgdict2)
2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##    show(_CFG)
2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##    show(cfgdict1)
2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    _CFG.update(cfgdict1)
2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##    show(_CFG)
2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotry:
2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    readconfig(_CFG)
2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoexcept:
2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    print "No configfile read, reason unknown"
2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Vec2D(tuple):
2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """A 2 dimensional vector class, used as a helper class
2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for implementing turtle graphics.
2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    May be useful for turtle graphics programs also.
2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Derived from tuple, so a vector is a tuple!
2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Provides (for a, b vectors, k number):
2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao       a+b vector addition
2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao       a-b vector subtraction
2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao       a*b inner product
2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao       k*a and a*k multiplication with scalar
2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao       |a| absolute value of a
2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao       a.rotate(angle) rotation
2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __new__(cls, x, y):
2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return tuple.__new__(cls, (x, y))
2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __add__(self, other):
2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return Vec2D(self[0]+other[0], self[1]+other[1])
2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __mul__(self, other):
2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(other, Vec2D):
2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self[0]*other[0]+self[1]*other[1]
2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return Vec2D(self[0]*other, self[1]*other)
2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __rmul__(self, other):
2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(other, int) or isinstance(other, float):
2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return Vec2D(self[0]*other, self[1]*other)
2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __sub__(self, other):
2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return Vec2D(self[0]-other[0], self[1]-other[1])
2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __neg__(self):
2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return Vec2D(-self[0], -self[1])
2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __abs__(self):
2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return (self[0]**2 + self[1]**2)**0.5
2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def rotate(self, angle):
2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """rotate self counterclockwise by angle
2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        perp = Vec2D(-self[1], self[0])
2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        angle = angle * math.pi / 180.0
2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        c, s = math.cos(angle), math.sin(angle)
2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return Vec2D(self[0]*c+perp[0]*s, self[1]*c+perp[1]*s)
2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __getnewargs__(self):
2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return (self[0], self[1])
2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __repr__(self):
2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return "(%.2f,%.2f)" % self
2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##############################################################################
3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao### From here up to line    : Tkinter - Interface for turtle.py            ###
3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao### May be replaced by an interface to some different graphics toolkit     ###
3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##############################################################################
3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao## helper functions for Scrolled Canvas, to forward Canvas-methods
3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao## to ScrolledCanvas class
3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef __methodDict(cls, _dict):
3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """helper function for Scrolled Canvas"""
3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    baseList = list(cls.__bases__)
3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    baseList.reverse()
3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for _super in baseList:
3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        __methodDict(_super, _dict)
3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for key, value in cls.__dict__.items():
3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if type(value) == types.FunctionType:
3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            _dict[key] = value
3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef __methods(cls):
3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """helper function for Scrolled Canvas"""
3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    _dict = {}
3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    __methodDict(cls, _dict)
3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return _dict.keys()
3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__stringBody = (
3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    'def %(method)s(self, *args, **kw): return ' +
3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    'self.%(attribute)s.%(method)s(*args, **kw)')
3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef __forwardmethods(fromClass, toClass, toPart, exclude = ()):
3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Helper functions for Scrolled Canvas, used to forward
3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ScrolledCanvas-methods to Tkinter.Canvas class.
3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    _dict = {}
3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    __methodDict(toClass, _dict)
3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for ex in _dict.keys():
3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if ex[:1] == '_' or ex[-1:] == '_':
3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            del _dict[ex]
3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for ex in exclude:
3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if ex in _dict:
3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            del _dict[ex]
3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for ex in __methods(fromClass):
3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if ex in _dict:
3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            del _dict[ex]
3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for method, func in _dict.items():
3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        d = {'method': method, 'func': func}
3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if type(toPart) == types.StringType:
3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            execString = \
3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                __stringBody % {'method' : method, 'attribute' : toPart}
3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        exec execString in d
3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fromClass.__dict__[method] = d[method]
3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass ScrolledCanvas(TK.Frame):
3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Modeled after the scrolled canvas class from Grayons's Tkinter book.
3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Used as the default canvas, which pops up automatically when
3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    using turtle graphics functions or the Turtle class.
3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, master, width=500, height=350,
3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                          canvwidth=600, canvheight=500):
3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        TK.Frame.__init__(self, master, width=width, height=height)
3620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._rootwindow = self.winfo_toplevel()
3630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.width, self.height = width, height
3640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.canvwidth, self.canvheight = canvwidth, canvheight
3650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.bg = "white"
3660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._canvas = TK.Canvas(master, width=width, height=height,
3670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                 bg=self.bg, relief=TK.SUNKEN, borderwidth=2)
3680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.hscroll = TK.Scrollbar(master, command=self._canvas.xview,
3690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                    orient=TK.HORIZONTAL)
3700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.vscroll = TK.Scrollbar(master, command=self._canvas.yview)
3710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._canvas.configure(xscrollcommand=self.hscroll.set,
3720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                               yscrollcommand=self.vscroll.set)
3730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.rowconfigure(0, weight=1, minsize=0)
3740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.columnconfigure(0, weight=1, minsize=0)
3750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._canvas.grid(padx=1, in_ = self, pady=1, row=0,
3760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                column=0, rowspan=1, columnspan=1, sticky='news')
3770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.vscroll.grid(padx=1, in_ = self, pady=1, row=0,
3780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                column=1, rowspan=1, columnspan=1, sticky='news')
3790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.hscroll.grid(padx=1, in_ = self, pady=1, row=1,
3800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                column=0, rowspan=1, columnspan=1, sticky='news')
3810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.reset()
3820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._rootwindow.bind('<Configure>', self.onResize)
3830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def reset(self, canvwidth=None, canvheight=None, bg = None):
3850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Adjust canvas and scrollbars according to given canvas size."""
3860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if canvwidth:
3870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.canvwidth = canvwidth
3880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if canvheight:
3890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.canvheight = canvheight
3900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if bg:
3910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.bg = bg
3920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._canvas.config(bg=bg,
3930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        scrollregion=(-self.canvwidth//2, -self.canvheight//2,
3940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                       self.canvwidth//2, self.canvheight//2))
3950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._canvas.xview_moveto(0.5*(self.canvwidth - self.width + 30) /
3960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                               self.canvwidth)
3970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._canvas.yview_moveto(0.5*(self.canvheight- self.height + 30) /
3980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                              self.canvheight)
3990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.adjustScrolls()
4000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def adjustScrolls(self):
4030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Adjust scrollbars according to window- and canvas-size.
4040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
4050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cwidth = self._canvas.winfo_width()
4060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cheight = self._canvas.winfo_height()
4070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._canvas.xview_moveto(0.5*(self.canvwidth-cwidth)/self.canvwidth)
4080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._canvas.yview_moveto(0.5*(self.canvheight-cheight)/self.canvheight)
4090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if cwidth < self.canvwidth or cheight < self.canvheight:
4100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.hscroll.grid(padx=1, in_ = self, pady=1, row=1,
4110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              column=0, rowspan=1, columnspan=1, sticky='news')
4120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.vscroll.grid(padx=1, in_ = self, pady=1, row=0,
4130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              column=1, rowspan=1, columnspan=1, sticky='news')
4140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
4150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.hscroll.grid_forget()
4160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.vscroll.grid_forget()
4170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def onResize(self, event):
4190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """self-explanatory"""
4200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.adjustScrolls()
4210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def bbox(self, *args):
4230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ 'forward' method, which canvas itself has inherited...
4240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
4250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._canvas.bbox(*args)
4260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def cget(self, *args, **kwargs):
4280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ 'forward' method, which canvas itself has inherited...
4290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
4300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._canvas.cget(*args, **kwargs)
4310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def config(self, *args, **kwargs):
4330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ 'forward' method, which canvas itself has inherited...
4340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
4350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._canvas.config(*args, **kwargs)
4360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def bind(self, *args, **kwargs):
4380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ 'forward' method, which canvas itself has inherited...
4390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
4400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._canvas.bind(*args, **kwargs)
4410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def unbind(self, *args, **kwargs):
4430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ 'forward' method, which canvas itself has inherited...
4440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
4450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._canvas.unbind(*args, **kwargs)
4460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def focus_force(self):
4480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ 'forward' method, which canvas itself has inherited...
4490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
4500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._canvas.focus_force()
4510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__forwardmethods(ScrolledCanvas, TK.Canvas, '_canvas')
4530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4550a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass _Root(TK.Tk):
4560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Root class for Screen based on Tkinter."""
4570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self):
4580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        TK.Tk.__init__(self)
4590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def setupcanvas(self, width, height, cwidth, cheight):
4610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._canvas = ScrolledCanvas(self, width, height, cwidth, cheight)
4620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._canvas.pack(expand=1, fill="both")
4630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _getcanvas(self):
4650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._canvas
4660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def set_geometry(self, width, height, startx, starty):
4680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.geometry("%dx%d%+d%+d"%(width, height, startx, starty))
4690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def ondestroy(self, destroy):
4710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.wm_protocol("WM_DELETE_WINDOW", destroy)
4720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def win_width(self):
4740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.winfo_screenwidth()
4750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def win_height(self):
4770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.winfo_screenheight()
4780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4790a8c90248264a8b26970b4473770bcc3df8515fJosh GaoCanvas = TK.Canvas
4800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4820a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TurtleScreenBase(object):
4830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Provide the basic graphics functionality.
4840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao       Interface between Tkinter and turtle.py.
4850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao       To port turtle.py to some different graphics toolkit
4870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao       a corresponding TurtleScreenBase class has to be implemented.
4880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
4890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    @staticmethod
4910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _blankimage():
4920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """return a blank image object
4930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
4940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        img = TK.PhotoImage(width=1, height=1)
4950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        img.blank()
4960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return img
4970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    @staticmethod
4990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _image(filename):
5000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """return an image object containing the
5010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        imagedata from a gif-file named filename.
5020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
5030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return TK.PhotoImage(file=filename)
5040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, cv):
5060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cv = cv
5070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(cv, ScrolledCanvas):
5080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            w = self.cv.canvwidth
5090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            h = self.cv.canvheight
5100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:  # expected: ordinary TK.Canvas
5110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            w = int(self.cv.cget("width"))
5120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            h = int(self.cv.cget("height"))
5130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.config(scrollregion = (-w//2, -h//2, w//2, h//2 ))
5140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.canvwidth = w
5150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.canvheight = h
5160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.xscale = self.yscale = 1.0
5170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _createpoly(self):
5190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Create an invisible polygon item on canvas self.cv)
5200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
5210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.cv.create_polygon((0, 0, 0, 0, 0, 0), fill="", outline="")
5220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _drawpoly(self, polyitem, coordlist, fill=None,
5240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                  outline=None, width=None, top=False):
5250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Configure polygonitem polyitem according to provided
5260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        arguments:
5270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        coordlist is sequence of coordinates
5280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fill is filling color
5290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        outline is outline color
5300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        top is a boolean value, which specifies if polyitem
5310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        will be put on top of the canvas' displaylist so it
5320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        will not be covered by other items.
5330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
5340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cl = []
5350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for x, y in coordlist:
5360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            cl.append(x * self.xscale)
5370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            cl.append(-y * self.yscale)
5380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cv.coords(polyitem, *cl)
5390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if fill is not None:
5400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.itemconfigure(polyitem, fill=fill)
5410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if outline is not None:
5420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.itemconfigure(polyitem, outline=outline)
5430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if width is not None:
5440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.itemconfigure(polyitem, width=width)
5450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if top:
5460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.tag_raise(polyitem)
5470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _createline(self):
5490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Create an invisible line item on canvas self.cv)
5500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
5510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.cv.create_line(0, 0, 0, 0, fill="", width=2,
5520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   capstyle = TK.ROUND)
5530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _drawline(self, lineitem, coordlist=None,
5550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                  fill=None, width=None, top=False):
5560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Configure lineitem according to provided arguments:
5570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        coordlist is sequence of coordinates
5580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fill is drawing color
5590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        width is width of drawn line.
5600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        top is a boolean value, which specifies if polyitem
5610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        will be put on top of the canvas' displaylist so it
5620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        will not be covered by other items.
5630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
5640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if coordlist is not None:
5650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            cl = []
5660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for x, y in coordlist:
5670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                cl.append(x * self.xscale)
5680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                cl.append(-y * self.yscale)
5690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.coords(lineitem, *cl)
5700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if fill is not None:
5710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.itemconfigure(lineitem, fill=fill)
5720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if width is not None:
5730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.itemconfigure(lineitem, width=width)
5740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if top:
5750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.tag_raise(lineitem)
5760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _delete(self, item):
5780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Delete graphics item from canvas.
5790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If item is"all" delete all graphics items.
5800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
5810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cv.delete(item)
5820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _update(self):
5840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Redraw graphics items on canvas
5850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
5860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cv.update()
5870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _delay(self, delay):
5890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Delay subsequent canvas actions for delay ms."""
5900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cv.after(delay)
5910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _iscolorstring(self, color):
5930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Check if the string color is a legal Tkinter color string.
5940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
5950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
5960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            rgb = self.cv.winfo_rgb(color)
5970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            ok = True
5980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except TK.TclError:
5990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            ok = False
6000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return ok
6010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _bgcolor(self, color=None):
6030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set canvas' backgroundcolor if color is not None,
6040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else return backgroundcolor."""
6050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if color is not None:
6060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.config(bg = color)
6070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._update()
6080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
6090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self.cv.cget("bg")
6100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _write(self, pos, txt, align, font, pencolor):
6120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Write txt at pos in canvas with specified font
6130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        and color.
6140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Return text item and x-coord of right bottom corner
6150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        of text's bounding box."""
6160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x, y = pos
6170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x = x * self.xscale
6180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        y = y * self.yscale
6190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        anchor = {"left":"sw", "center":"s", "right":"se" }
6200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        item = self.cv.create_text(x-1, -y, text = txt, anchor = anchor[align],
6210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                        fill = pencolor, font = font)
6220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x0, y0, x1, y1 = self.cv.bbox(item)
6230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cv.update()
6240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return item, x1-1
6250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##    def _dot(self, pos, size, color):
6270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##        """may be implemented for some other graphics toolkit"""
6280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _onclick(self, item, fun, num=1, add=None):
6300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Bind fun to mouse-click event on turtle.
6310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fun must be a function with two arguments, the coordinates
6320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        of the clicked point on the canvas.
6330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        num, the number of the mouse-button defaults to 1
6340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
6350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if fun is None:
6360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.tag_unbind(item, "<Button-%s>" % num)
6370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
6380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def eventfun(event):
6390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                x, y = (self.cv.canvasx(event.x)/self.xscale,
6400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        -self.cv.canvasy(event.y)/self.yscale)
6410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                fun(x, y)
6420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.tag_bind(item, "<Button-%s>" % num, eventfun, add)
6430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _onrelease(self, item, fun, num=1, add=None):
6450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Bind fun to mouse-button-release event on turtle.
6460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fun must be a function with two arguments, the coordinates
6470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        of the point on the canvas where mouse button is released.
6480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        num, the number of the mouse-button defaults to 1
6490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If a turtle is clicked, first _onclick-event will be performed,
6510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        then _onscreensclick-event.
6520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
6530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if fun is None:
6540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.tag_unbind(item, "<Button%s-ButtonRelease>" % num)
6550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
6560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def eventfun(event):
6570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                x, y = (self.cv.canvasx(event.x)/self.xscale,
6580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        -self.cv.canvasy(event.y)/self.yscale)
6590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                fun(x, y)
6600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.tag_bind(item, "<Button%s-ButtonRelease>" % num,
6610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                             eventfun, add)
6620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _ondrag(self, item, fun, num=1, add=None):
6640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Bind fun to mouse-move-event (with pressed mouse button) on turtle.
6650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fun must be a function with two arguments, the coordinates of the
6660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        actual mouse position on the canvas.
6670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        num, the number of the mouse-button defaults to 1
6680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Every sequence of mouse-move-events on a turtle is preceded by a
6700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mouse-click event on that turtle.
6710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
6720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if fun is None:
6730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.tag_unbind(item, "<Button%s-Motion>" % num)
6740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
6750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def eventfun(event):
6760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                try:
6770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    x, y = (self.cv.canvasx(event.x)/self.xscale,
6780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                           -self.cv.canvasy(event.y)/self.yscale)
6790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    fun(x, y)
6800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except:
6810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    pass
6820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.tag_bind(item, "<Button%s-Motion>" % num, eventfun, add)
6830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _onscreenclick(self, fun, num=1, add=None):
6850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Bind fun to mouse-click event on canvas.
6860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fun must be a function with two arguments, the coordinates
6870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        of the clicked point on the canvas.
6880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        num, the number of the mouse-button defaults to 1
6890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If a turtle is clicked, first _onclick-event will be performed,
6910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        then _onscreensclick-event.
6920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
6930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if fun is None:
6940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.unbind("<Button-%s>" % num)
6950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
6960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def eventfun(event):
6970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                x, y = (self.cv.canvasx(event.x)/self.xscale,
6980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        -self.cv.canvasy(event.y)/self.yscale)
6990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                fun(x, y)
7000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.bind("<Button-%s>" % num, eventfun, add)
7010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _onkey(self, fun, key):
7030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Bind fun to key-release event of key.
7040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Canvas must have focus. See method listen
7050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
7060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if fun is None:
7070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.unbind("<KeyRelease-%s>" % key, None)
7080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
7090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def eventfun(event):
7100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                fun()
7110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.bind("<KeyRelease-%s>" % key, eventfun)
7120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _listen(self):
7140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set focus on canvas (in order to collect key-events)
7150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
7160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cv.focus_force()
7170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _ontimer(self, fun, t):
7190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Install a timer, which calls fun after t milliseconds.
7200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
7210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if t == 0:
7220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.after_idle(fun)
7230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
7240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.after(t, fun)
7250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _createimage(self, image):
7270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Create and return image item on canvas.
7280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
7290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.cv.create_image(0, 0, image=image)
7300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _drawimage(self, item, (x, y), image):
7320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Configure image item as to draw image object
7330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        at position (x,y) on canvas)
7340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
7350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cv.coords(item, (x * self.xscale, -y * self.yscale))
7360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cv.itemconfig(item, image=image)
7370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _setbgpic(self, item, image):
7390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Configure image item as to draw image object
7400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        at center of canvas. Set item to the first item
7410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        in the displaylist, so it will be drawn below
7420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        any other item ."""
7430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cv.itemconfig(item, image=image)
7440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cv.tag_lower(item)
7450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _type(self, item):
7470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return 'line' or 'polygon' or 'image' depending on
7480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        type of item.
7490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
7500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.cv.type(item)
7510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _pointlist(self, item):
7530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """returns list of coordinate-pairs of points of item
7540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for insiders):
7550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> from turtle import *
7560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> getscreen()._pointlist(getturtle().turtle._item)
7570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        [(0.0, 9.9999999999999982), (0.0, -9.9999999999999982),
7580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (9.9999999999999982, 0.0)]
7590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> """
7600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cl = self.cv.coords(item)
7610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pl = [(cl[i], -cl[i+1]) for i in range(0, len(cl), 2)]
7620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return  pl
7630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _setscrollregion(self, srx1, sry1, srx2, sry2):
7650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cv.config(scrollregion=(srx1, sry1, srx2, sry2))
7660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _rescale(self, xscalefactor, yscalefactor):
7680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        items = self.cv.find_all()
7690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for item in items:
7700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            coordinates = self.cv.coords(item)
7710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            newcoordlist = []
7720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            while coordinates:
7730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                x, y = coordinates[:2]
7740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                newcoordlist.append(x * xscalefactor)
7750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                newcoordlist.append(y * yscalefactor)
7760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                coordinates = coordinates[2:]
7770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.cv.coords(item, *newcoordlist)
7780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _resize(self, canvwidth=None, canvheight=None, bg=None):
7800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Resize the canvas the turtles are drawing on. Does
7810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        not alter the drawing window.
7820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
7830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # needs amendment
7840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not isinstance(self.cv, ScrolledCanvas):
7850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self.canvwidth, self.canvheight
7860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if canvwidth is canvheight is bg is None:
7870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self.cv.canvwidth, self.cv.canvheight
7880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if canvwidth is not None:
7890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.canvwidth = canvwidth
7900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if canvheight is not None:
7910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.canvheight = canvheight
7920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cv.reset(canvwidth, canvheight, bg)
7930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _window_size(self):
7950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Return the width and height of the turtle window.
7960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
7970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        width = self.cv.winfo_width()
7980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if width <= 1:  # the window isn't managed by a geometry manager
7990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            width = self.cv['width']
8000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        height = self.cv.winfo_height()
8010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if height <= 1: # the window isn't managed by a geometry manager
8020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            height = self.cv['height']
8030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return width, height
8040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##############################################################################
8070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao###                  End of Tkinter - interface                            ###
8080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##############################################################################
8090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Terminator (Exception):
8120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Will be raised in TurtleScreen.update, if _RUNNING becomes False.
8130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    This stops execution of a turtle graphics script.
8150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Main purpose: use in the Demo-Viewer turtle.Demo.py.
8160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
8170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    pass
8180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8200a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TurtleGraphicsError(Exception):
8210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Some TurtleGraphics Error
8220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
8230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8250a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Shape(object):
8260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Data structure modeling shapes.
8270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    attribute _type is one of "polygon", "image", "compound"
8290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    attribute _data is - depending on _type a poygon-tuple,
8300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    an image or a list constructed using the addcomponent method.
8310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
8320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, type_, data=None):
8330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._type = type_
8340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if type_ == "polygon":
8350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if isinstance(data, list):
8360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                data = tuple(data)
8370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif type_ == "image":
8380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if isinstance(data, str):
8390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if data.lower().endswith(".gif") and isfile(data):
8400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    data = TurtleScreen._image(data)
8410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # else data assumed to be Photoimage
8420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif type_ == "compound":
8430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            data = []
8440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
8450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise TurtleGraphicsError("There is no shape type %s" % type_)
8460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._data = data
8470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def addcomponent(self, poly, fill, outline=None):
8490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Add component to a shape of type compound.
8500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments: poly is a polygon, i. e. a tuple of number pairs.
8520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fill is the fillcolor of the component,
8530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        outline is the outline color of the component.
8540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        call (for a Shapeobject namend s):
8560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        --   s.addcomponent(((0,0), (10,10), (-10,10)), "red", "blue")
8570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example:
8590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
8600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> s = Shape("compound")
8610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> s.addcomponent(poly, "red", "blue")
8620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> # .. add more components and then use register_shape()
8630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
8640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._type != "compound":
8650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise TurtleGraphicsError("Cannot add component to %s Shape"
8660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                                % self._type)
8670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if outline is None:
8680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            outline = fill
8690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._data.append([poly, fill, outline])
8700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8720a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Tbuffer(object):
8730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Ring buffer used as undobuffer for RawTurtle objects."""
8740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, bufsize=10):
8750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.bufsize = bufsize
8760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.buffer = [[None]] * bufsize
8770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.ptr = -1
8780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.cumulate = False
8790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def reset(self, bufsize=None):
8800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if bufsize is None:
8810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for i in range(self.bufsize):
8820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.buffer[i] = [None]
8830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
8840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.bufsize = bufsize
8850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.buffer = [[None]] * bufsize
8860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.ptr = -1
8870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def push(self, item):
8880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.bufsize > 0:
8890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if not self.cumulate:
8900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.ptr = (self.ptr + 1) % self.bufsize
8910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.buffer[self.ptr] = item
8920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
8930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.buffer[self.ptr].append(item)
8940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def pop(self):
8950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.bufsize > 0:
8960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            item = self.buffer[self.ptr]
8970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if item is None:
8980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return None
8990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
9000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.buffer[self.ptr] = [None]
9010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.ptr = (self.ptr - 1) % self.bufsize
9020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return (item)
9030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def nr_of_items(self):
9040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.bufsize - self.buffer.count([None])
9050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __repr__(self):
9060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return str(self.buffer) + " " + str(self.ptr)
9070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TurtleScreen(TurtleScreenBase):
9110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Provides screen oriented methods like setbg etc.
9120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Only relies upon the methods of TurtleScreenBase and NOT
9140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    upon components of the underlying graphics toolkit -
9150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    which is Tkinter in this case.
9160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
9170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#    _STANDARD_DELAY = 5
9180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    _RUNNING = True
9190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, cv, mode=_CFG["mode"],
9210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 colormode=_CFG["colormode"], delay=_CFG["delay"]):
9220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._shapes = {
9230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   "arrow" : Shape("polygon", ((-10,0), (10,0), (0,10))),
9240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                  "turtle" : Shape("polygon", ((0,16), (-2,14), (-1,10), (-4,7),
9250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              (-7,9), (-9,8), (-6,5), (-7,1), (-5,-3), (-8,-6),
9260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              (-6,-8), (-4,-5), (0,-7), (4,-5), (6,-8), (8,-6),
9270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              (5,-3), (7,1), (6,5), (9,8), (7,9), (4,7), (1,10),
9280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              (2,14))),
9290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                  "circle" : Shape("polygon", ((10,0), (9.51,3.09), (8.09,5.88),
9300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              (5.88,8.09), (3.09,9.51), (0,10), (-3.09,9.51),
9310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              (-5.88,8.09), (-8.09,5.88), (-9.51,3.09), (-10,0),
9320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              (-9.51,-3.09), (-8.09,-5.88), (-5.88,-8.09),
9330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              (-3.09,-9.51), (-0.00,-10.00), (3.09,-9.51),
9340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              (5.88,-8.09), (8.09,-5.88), (9.51,-3.09))),
9350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                  "square" : Shape("polygon", ((10,-10), (10,10), (-10,10),
9360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              (-10,-10))),
9370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "triangle" : Shape("polygon", ((10,-5.77), (0,11.55),
9380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              (-10,-5.77))),
9390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                  "classic": Shape("polygon", ((0,0),(-5,-9),(0,-7),(5,-9))),
9400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   "blank" : Shape("image", self._blankimage())
9410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                  }
9420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._bgpics = {"nopic" : ""}
9440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        TurtleScreenBase.__init__(self, cv)
9460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._mode = mode
9470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._delayvalue = delay
9480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._colormode = _CFG["colormode"]
9490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._keys = []
9500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.clear()
9510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def clear(self):
9530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Delete all drawings and all turtles from the TurtleScreen.
9540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Reset empty TurtleScreen to its initial state: white background,
9560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        no backgroundimage, no eventbindings and tracing on.
9570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
9590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
9610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.clear()
9620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Note: this method is not available as function.
9640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
9650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._delayvalue = _CFG["delay"]
9660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._colormode = _CFG["colormode"]
9670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._delete("all")
9680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._bgpic = self._createimage("")
9690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._bgpicname = "nopic"
9700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._tracing = 1
9710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._updatecounter = 0
9720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._turtles = []
9730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.bgcolor("white")
9740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for btn in 1, 2, 3:
9750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.onclick(None, btn)
9760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for key in self._keys[:]:
9770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.onkey(None, key)
9780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Turtle._pen = None
9790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def mode(self, mode=None):
9810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set turtle-mode ('standard', 'logo' or 'world') and perform reset.
9820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Optional argument:
9840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mode -- on of the strings 'standard', 'logo' or 'world'
9850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Mode 'standard' is compatible with turtle.py.
9870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Mode 'logo' is compatible with most Logo-Turtle-Graphics.
9880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Mode 'world' uses userdefined 'worldcoordinates'. *Attention*: in
9890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        this mode angles appear distorted if x/y unit-ratio doesn't equal 1.
9900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If mode is not given, return the current mode.
9910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             Mode      Initial turtle heading     positive angles
9930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         ------------|-------------------------|-------------------
9940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          'standard'    to the right (east)       counterclockwise
9950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            'logo'        upward    (north)         clockwise
9960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Examples:
9980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> mode('logo')   # resets turtle heading to north
9990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> mode()
10000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'logo'
10010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
10020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if mode is None:
10030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self._mode
10040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mode = mode.lower()
10050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if mode not in ["standard", "logo", "world"]:
10060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise TurtleGraphicsError("No turtle-graphics-mode %s" % mode)
10070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._mode = mode
10080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if mode in ["standard", "logo"]:
10090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._setscrollregion(-self.canvwidth//2, -self.canvheight//2,
10100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                       self.canvwidth//2, self.canvheight//2)
10110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.xscale = self.yscale = 1.0
10120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.reset()
10130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def setworldcoordinates(self, llx, lly, urx, ury):
10150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set up a user defined coordinate-system.
10160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
10180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        llx -- a number, x-coordinate of lower left corner of canvas
10190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        lly -- a number, y-coordinate of lower left corner of canvas
10200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        urx -- a number, x-coordinate of upper right corner of canvas
10210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ury -- a number, y-coordinate of upper right corner of canvas
10220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Set up user coodinat-system and switch to mode 'world' if necessary.
10240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        This performs a screen.reset. If mode 'world' is already active,
10250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        all drawings are redrawn according to the new coordinates.
10260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        But ATTENTION: in user-defined coordinatesystems angles may appear
10280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        distorted. (see Screen.mode())
10290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
10310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.setworldcoordinates(-10,-0.5,50,1.5)
10320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> for _ in range(36):
10330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     left(10)
10340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     forward(0.5)
10350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
10360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.mode() != "world":
10370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.mode("world")
10380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        xspan = float(urx - llx)
10390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        yspan = float(ury - lly)
10400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        wx, wy = self._window_size()
10410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.screensize(wx-20, wy-20)
10420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        oldxscale, oldyscale = self.xscale, self.yscale
10430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.xscale = self.canvwidth / xspan
10440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.yscale = self.canvheight / yspan
10450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        srx1 = llx * self.xscale
10460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sry1 = -ury * self.yscale
10470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        srx2 = self.canvwidth + srx1
10480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sry2 = self.canvheight + sry1
10490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._setscrollregion(srx1, sry1, srx2, sry2)
10500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._rescale(self.xscale/oldxscale, self.yscale/oldyscale)
10510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.update()
10520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def register_shape(self, name, shape=None):
10540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Adds a turtle shape to TurtleScreen's shapelist.
10550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
10570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (1) name is the name of a gif-file and shape is None.
10580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            Installs the corresponding image shape.
10590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            !! Image-shapes DO NOT rotate when turning the turtle,
10600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            !! so they do not display the heading of the turtle!
10610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (2) name is an arbitrary string and shape is a tuple
10620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            of pairs of coordinates. Installs the corresponding
10630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            polygon shape
10640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (3) name is an arbitrary string and shape is a
10650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            (compound) Shape object. Installs the corresponding
10660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            compound shape.
10670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        To use a shape, you have to issue the command shape(shapename).
10680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        call: register_shape("turtle.gif")
10700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        --or: register_shape("tri", ((0,0), (10,10), (-10,10)))
10710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
10730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.register_shape("triangle", ((5,-3),(0,5),(-5,-3)))
10740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
10760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if shape is None:
10770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # image
10780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if name.lower().endswith(".gif"):
10790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                shape = Shape("image", self._image(name))
10800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
10810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise TurtleGraphicsError("Bad arguments for register_shape.\n"
10820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                          + "Use  help(register_shape)" )
10830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif isinstance(shape, tuple):
10840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            shape = Shape("polygon", shape)
10850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ## else shape assumed to be Shape-instance
10860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._shapes[name] = shape
10870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # print "shape added:" , self._shapes
10880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _colorstr(self, color):
10900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return color string corresponding to args.
10910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Argument may be a string or a tuple of three
10930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        numbers corresponding to actual colormode,
10940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        i.e. in the range 0<=n<=colormode.
10950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
10960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If the argument doesn't represent a color,
10970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        an error is raised.
10980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
10990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if len(color) == 1:
11000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            color = color[0]
11010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(color, str):
11020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if self._iscolorstring(color) or color == "":
11030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return color
11040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
11050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise TurtleGraphicsError("bad color string: %s" % str(color))
11060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
11070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            r, g, b = color
11080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except:
11090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise TurtleGraphicsError("bad color arguments: %s" % str(color))
11100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._colormode == 1.0:
11110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            r, g, b = [round(255.0*x) for x in (r, g, b)]
11120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)):
11130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise TurtleGraphicsError("bad color sequence: %s" % str(color))
11140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return "#%02x%02x%02x" % (r, g, b)
11150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _color(self, cstr):
11170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not cstr.startswith("#"):
11180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return cstr
11190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if len(cstr) == 7:
11200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            cl = [int(cstr[i:i+2], 16) for i in (1, 3, 5)]
11210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif len(cstr) == 4:
11220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            cl = [16*int(cstr[h], 16) for h in cstr[1:]]
11230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
11240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise TurtleGraphicsError("bad colorstring: %s" % cstr)
11250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return tuple([c * self._colormode/255 for c in cl])
11260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def colormode(self, cmode=None):
11280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return the colormode or set it to 1.0 or 255.
11290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Optional argument:
11310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cmode -- one of the values 1.0 or 255
11320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        r, g, b values of colortriples have to be in range 0..cmode.
11340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
11360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.colormode()
11370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        1.0
11380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.colormode(255)
11390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> pencolor(240,160,80)
11400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
11410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if cmode is None:
11420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self._colormode
11430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if cmode == 1.0:
11440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._colormode = float(cmode)
11450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif cmode == 255:
11460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._colormode = int(cmode)
11470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def reset(self):
11490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Reset all Turtles on the Screen to their initial state.
11500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
11520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
11540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.reset()
11550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
11560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for turtle in self._turtles:
11570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            turtle._setmode(self._mode)
11580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            turtle.reset()
11590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def turtles(self):
11610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return the list of turtles on the screen.
11620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
11640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.turtles()
11650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        [<turtle.Turtle object at 0x00E11FB0>]
11660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
11670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._turtles
11680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def bgcolor(self, *args):
11700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set or return backgroundcolor of the TurtleScreen.
11710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments (if given): a color string or three numbers
11730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        in the range 0..colormode or a 3-tuple of such numbers.
11740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
11760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.bgcolor("orange")
11770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.bgcolor()
11780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'orange'
11790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.bgcolor(0.5,0,0.5)
11800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.bgcolor()
11810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        '#800080'
11820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
11830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if args:
11840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            color = self._colorstr(args)
11850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
11860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            color = None
11870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        color = self._bgcolor(color)
11880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if color is not None:
11890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            color = self._color(color)
11900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return color
11910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tracer(self, n=None, delay=None):
11930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Turns turtle animation on/off and set delay for update drawings.
11940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Optional arguments:
11960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        n -- nonnegative  integer
11970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        delay -- nonnegative  integer
11980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
11990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If n is given, only each n-th regular screen update is really performed.
12000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (Can be used to accelerate the drawing of complex graphics.)
12010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Second arguments sets delay value (see RawTurtle.delay())
12020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
12040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.tracer(8, 25)
12050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> dist = 2
12060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> for i in range(200):
12070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     fd(dist)
12080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     rt(90)
12090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     dist += 2
12100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
12110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if n is None:
12120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self._tracing
12130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._tracing = int(n)
12140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._updatecounter = 0
12150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if delay is not None:
12160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._delayvalue = int(delay)
12170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._tracing:
12180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.update()
12190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def delay(self, delay=None):
12210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Return or set the drawing delay in milliseconds.
12220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Optional argument:
12240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        delay -- positive integer
12250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
12270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.delay(15)
12280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.delay()
12290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        15
12300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
12310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if delay is None:
12320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self._delayvalue
12330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._delayvalue = int(delay)
12340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _incrementudc(self):
12360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Increment upadate counter."""
12370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not TurtleScreen._RUNNING:
12380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            TurtleScreen._RUNNNING = True
12390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise Terminator
12400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._tracing > 0:
12410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._updatecounter += 1
12420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._updatecounter %= self._tracing
12430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def update(self):
12450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Perform a TurtleScreen update.
12460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
12470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tracing = self._tracing
12480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._tracing = True
12490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for t in self.turtles():
12500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            t._update_data()
12510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            t._drawturtle()
12520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._tracing = tracing
12530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._update()
12540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def window_width(self):
12560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Return the width of the turtle window.
12570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
12590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.window_width()
12600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        640
12610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
12620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._window_size()[0]
12630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def window_height(self):
12650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Return the height of the turtle window.
12660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
12680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.window_height()
12690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        480
12700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
12710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._window_size()[1]
12720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def getcanvas(self):
12740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return the Canvas of this TurtleScreen.
12750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
12770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Screen instance named screen):
12790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> cv = screen.getcanvas()
12800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> cv
12810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        <turtle.ScrolledCanvas instance at 0x010742D8>
12820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
12830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.cv
12840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def getshapes(self):
12860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return a list of names of all currently available turtle shapes.
12870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
12890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
12910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.getshapes()
12920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ['arrow', 'blank', 'circle', ... , 'turtle']
12930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
12940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return sorted(self._shapes.keys())
12950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def onclick(self, fun, btn=1, add=None):
12970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Bind fun to mouse-click event on canvas.
12980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
12990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
13000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fun -- a function with two arguments, the coordinates of the
13010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               clicked point on the canvas.
13020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        num -- the number of the mouse-button, defaults to 1
13030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen
13050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        and a Turtle instance named turtle):
13060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.onclick(goto)
13080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> # Subsequently clicking into the TurtleScreen will
13090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> # make the turtle move to the clicked point.
13100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.onclick(None)
13110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
13120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._onscreenclick(fun, btn, add)
13130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def onkey(self, fun, key):
13150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Bind fun to key-release event of key.
13160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
13180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fun -- a function with no arguments
13190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        key -- a string: key (e.g. "a") or key-symbol (e.g. "space")
13200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        In order to be able to register key-events, TurtleScreen
13220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        must have focus. (See method listen.)
13230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
13250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> def f():
13270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     fd(50)
13280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     lt(60)
13290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...
13300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.onkey(f, "Up")
13310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.listen()
13320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Subsequently the turtle can be moved by repeatedly pressing
13340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        the up-arrow key, consequently drawing a hexagon
13350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
13370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if fun is None:
13380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if key in self._keys:
13390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._keys.remove(key)
13400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif key not in self._keys:
13410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._keys.append(key)
13420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._onkey(fun, key)
13430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def listen(self, xdummy=None, ydummy=None):
13450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set focus on TurtleScreen (in order to collect key-events)
13460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No arguments.
13480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Dummy arguments are provided in order
13490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        to be able to pass listen to the onclick method.
13500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
13520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.listen()
13530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
13540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._listen()
13550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def ontimer(self, fun, t=0):
13570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Install a timer, which calls fun after t milliseconds.
13580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
13600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fun -- a function with no arguments.
13610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        t -- a number >= 0
13620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
13640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> running = True
13660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> def f():
13670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     if running:
13680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...             fd(50)
13690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...             lt(60)
13700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...             screen.ontimer(f, 250)
13710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...
13720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> f()   # makes the turtle marching around
13730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> running = False
13740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
13750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._ontimer(fun, t)
13760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def bgpic(self, picname=None):
13780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set background image or return name of current backgroundimage.
13790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Optional argument:
13810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        picname -- a string, name of a gif-file or "nopic".
13820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If picname is a filename, set the corresponding image as background.
13840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If picname is "nopic", delete backgroundimage, if present.
13850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If picname is None, return the filename of the current backgroundimage.
13860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
13870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
13880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.bgpic()
13890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'nopic'
13900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.bgpic("landscape.gif")
13910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.bgpic()
13920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'landscape.gif'
13930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
13940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if picname is None:
13950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self._bgpicname
13960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if picname not in self._bgpics:
13970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._bgpics[picname] = self._image(picname)
13980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._setbgpic(self._bgpic, self._bgpics[picname])
13990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._bgpicname = picname
14000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def screensize(self, canvwidth=None, canvheight=None, bg=None):
14020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Resize the canvas the turtles are drawing on.
14030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Optional arguments:
14050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        canvwidth -- positive integer, new width of canvas in pixels
14060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        canvheight --  positive integer, new height of canvas in pixels
14070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        bg -- colorstring or color-tuple, new backgroundcolor
14080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If no arguments are given, return current (canvaswidth, canvasheight)
14090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Do not alter the drawing window. To observe hidden parts of
14110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        the canvas use the scrollbars. (Can make visible those parts
14120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        of a drawing, which were outside the canvas before!)
14130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
14150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.screensize(2000,1500)
14160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> # e. g. to search for an erroneously escaped turtle ;-)
14170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
14180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._resize(canvwidth, canvheight, bg)
14190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    onscreenclick = onclick
14210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    resetscreen = reset
14220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    clearscreen = clear
14230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    addshape = register_shape
14240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14250a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TNavigator(object):
14260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Navigation part of the RawTurtle.
14270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Implements methods for turtle movement.
14280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
14290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    START_ORIENTATION = {
14300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "standard": Vec2D(1.0, 0.0),
14310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "world"   : Vec2D(1.0, 0.0),
14320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        "logo"    : Vec2D(0.0, 1.0)  }
14330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    DEFAULT_MODE = "standard"
14340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    DEFAULT_ANGLEOFFSET = 0
14350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    DEFAULT_ANGLEORIENT = 1
14360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, mode=DEFAULT_MODE):
14380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._angleOffset = self.DEFAULT_ANGLEOFFSET
14390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._angleOrient = self.DEFAULT_ANGLEORIENT
14400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._mode = mode
14410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.undobuffer = None
14420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.degrees()
14430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._mode = None
14440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._setmode(mode)
14450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        TNavigator.reset(self)
14460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def reset(self):
14480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """reset turtle to its initial values
14490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Will be overwritten by parent class
14510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
14520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._position = Vec2D(0.0, 0.0)
14530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._orient =  TNavigator.START_ORIENTATION[self._mode]
14540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _setmode(self, mode=None):
14560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set turtle-mode to 'standard', 'world' or 'logo'.
14570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
14580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if mode is None:
14590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self._mode
14600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if mode not in ["standard", "logo", "world"]:
14610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
14620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._mode = mode
14630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if mode in ["standard", "world"]:
14640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._angleOffset = 0
14650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._angleOrient = 1
14660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else: # mode == "logo":
14670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._angleOffset = self._fullcircle/4.
14680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._angleOrient = -1
14690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _setDegreesPerAU(self, fullcircle):
14710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Helper function for degrees() and radians()"""
14720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._fullcircle = fullcircle
14730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._degreesPerAU = 360/fullcircle
14740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._mode == "standard":
14750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._angleOffset = 0
14760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
14770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._angleOffset = fullcircle/4.
14780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def degrees(self, fullcircle=360.0):
14800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Set angle measurement units to degrees.
14810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Optional argument:
14830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fullcircle -  a number
14840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Set angle measurement units, i. e. set number
14860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        of 'degrees' for a full circle. Dafault value is
14870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        360 degrees.
14880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
14900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.left(90)
14910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.heading()
14920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        90
14930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
14940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Change angle measurement unit to grad (also known as gon,
14950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        grade, or gradian and equals 1/100-th of the right angle.)
14960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.degrees(400.0)
14970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.heading()
14980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        100
14990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
15010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._setDegreesPerAU(fullcircle)
15020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def radians(self):
15040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Set the angle measurement units to radians.
15050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No arguments.
15070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
15090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.heading()
15100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        90
15110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.radians()
15120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.heading()
15130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        1.5707963267948966
15140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
15150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._setDegreesPerAU(2*math.pi)
15160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _go(self, distance):
15180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """move turtle forward by specified distance"""
15190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ende = self._position + self._orient * distance
15200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._goto(ende)
15210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _rotate(self, angle):
15230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Turn turtle counterclockwise by specified angle if angle > 0."""
15240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        angle *= self._degreesPerAU
15250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._orient = self._orient.rotate(angle)
15260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _goto(self, end):
15280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """move turtle to position end."""
15290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._position = end
15300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def forward(self, distance):
15320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Move the turtle forward by the specified distance.
15330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Aliases: forward | fd
15350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Argument:
15370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        distance -- a number (integer or float)
15380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Move the turtle forward by the specified distance, in the direction
15400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        the turtle is headed.
15410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
15430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.position()
15440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (0.00, 0.00)
15450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(25)
15460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.position()
15470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (25.00,0.00)
15480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(-75)
15490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.position()
15500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (-50.00,0.00)
15510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
15520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._go(distance)
15530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def back(self, distance):
15550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Move the turtle backward by distance.
15560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Aliases: back | backward | bk
15580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Argument:
15600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        distance -- a number
15610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Move the turtle backward by distance ,opposite to the direction the
15630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle is headed. Do not change the turtle's heading.
15640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
15660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.position()
15670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (0.00, 0.00)
15680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.backward(30)
15690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.position()
15700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (-30.00, 0.00)
15710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
15720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._go(-distance)
15730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def right(self, angle):
15750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Turn turtle right by angle units.
15760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Aliases: right | rt
15780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Argument:
15800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        angle -- a number (integer or float)
15810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Turn turtle right by angle units. (Units are by default degrees,
15830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        but can be set via the degrees() and radians() functions.)
15840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Angle orientation depends on mode. (See this.)
15850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
15870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.heading()
15880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        22.0
15890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.right(45)
15900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.heading()
15910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        337.0
15920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
15930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._rotate(-angle)
15940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def left(self, angle):
15960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Turn turtle left by angle units.
15970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
15980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Aliases: left | lt
15990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Argument:
16010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        angle -- a number (integer or float)
16020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Turn turtle left by angle units. (Units are by default degrees,
16040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        but can be set via the degrees() and radians() functions.)
16050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Angle orientation depends on mode. (See this.)
16060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
16080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.heading()
16090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        22.0
16100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.left(45)
16110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.heading()
16120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        67.0
16130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
16140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._rotate(angle)
16150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def pos(self):
16170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return the turtle's current location (x,y), as a Vec2D-vector.
16180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Aliases: pos | position
16200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No arguments.
16220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
16240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pos()
16250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (0.00, 240.00)
16260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
16270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._position
16280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def xcor(self):
16300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Return the turtle's x coordinate.
16310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No arguments.
16330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
16350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> reset()
16360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.left(60)
16370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(100)
16380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> print turtle.xcor()
16390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        50.0
16400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
16410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._position[0]
16420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def ycor(self):
16440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Return the turtle's y coordinate
16450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ---
16460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No arguments.
16470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
16490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> reset()
16500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.left(60)
16510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(100)
16520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> print turtle.ycor()
16530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        86.6025403784
16540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
16550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._position[1]
16560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def goto(self, x, y=None):
16590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Move turtle to an absolute position.
16600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Aliases: setpos | setposition | goto:
16620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
16640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x -- a number      or     a pair/vector of numbers
16650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        y -- a number             None
16660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        call: goto(x, y)         # two coordinates
16680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        --or: goto((x, y))       # a pair (tuple) of coordinates
16690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        --or: goto(vec)          # e.g. as returned by pos()
16700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Move turtle to an absolute position. If the pen is down,
16720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        a line will be drawn. The turtle's orientation does not change.
16730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
16750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> tp = turtle.pos()
16760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> tp
16770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (0.00, 0.00)
16780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.setpos(60,30)
16790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pos()
16800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (60.00,30.00)
16810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.setpos((20,80))
16820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pos()
16830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (20.00,80.00)
16840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.setpos(tp)
16850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pos()
16860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (0.00,0.00)
16870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
16880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if y is None:
16890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._goto(Vec2D(*x))
16900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
16910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._goto(Vec2D(x, y))
16920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def home(self):
16940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Move turtle to the origin - coordinates (0,0).
16950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No arguments.
16970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
16980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Move turtle to the origin - coordinates (0,0) and set its
16990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        heading to its start-orientation (which depends on mode).
17000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
17020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.home()
17030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
17040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.goto(0, 0)
17050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.setheading(0)
17060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def setx(self, x):
17080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set the turtle's first coordinate to x
17090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Argument:
17110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x -- a number (integer or float)
17120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Set the turtle's first coordinate to x, leave second coordinate
17140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        unchanged.
17150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
17170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.position()
17180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (0.00, 240.00)
17190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.setx(10)
17200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.position()
17210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (10.00, 240.00)
17220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
17230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._goto(Vec2D(x, self._position[1]))
17240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def sety(self, y):
17260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set the turtle's second coordinate to y
17270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Argument:
17290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        y -- a number (integer or float)
17300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Set the turtle's first coordinate to x, second coordinate remains
17320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        unchanged.
17330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
17350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.position()
17360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (0.00, 40.00)
17370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.sety(-10)
17380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.position()
17390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (0.00, -10.00)
17400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
17410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._goto(Vec2D(self._position[0], y))
17420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def distance(self, x, y=None):
17440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return the distance from the turtle to (x,y) in turtle step units.
17450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
17470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x -- a number   or  a pair/vector of numbers   or   a turtle instance
17480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        y -- a number       None                            None
17490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        call: distance(x, y)         # two coordinates
17510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        --or: distance((x, y))       # a pair (tuple) of coordinates
17520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        --or: distance(vec)          # e.g. as returned by pos()
17530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        --or: distance(mypen)        # where mypen is another turtle
17540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
17560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pos()
17570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (0.00, 0.00)
17580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.distance(30,40)
17590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        50.0
17600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> pen = Turtle()
17610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> pen.forward(77)
17620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.distance(pen)
17630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        77.0
17640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
17650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if y is not None:
17660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pos = Vec2D(x, y)
17670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(x, Vec2D):
17680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pos = x
17690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif isinstance(x, tuple):
17700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pos = Vec2D(*x)
17710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif isinstance(x, TNavigator):
17720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pos = x._position
17730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return abs(pos - self._position)
17740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def towards(self, x, y=None):
17760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return the angle of the line from the turtle's position to (x, y).
17770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
17790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x -- a number   or  a pair/vector of numbers   or   a turtle instance
17800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        y -- a number       None                            None
17810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        call: distance(x, y)         # two coordinates
17830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        --or: distance((x, y))       # a pair (tuple) of coordinates
17840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        --or: distance(vec)          # e.g. as returned by pos()
17850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        --or: distance(mypen)        # where mypen is another turtle
17860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Return the angle, between the line from turtle-position to position
17880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        specified by x, y and the turtle's start orientation. (Depends on
17890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        modes - "standard" or "logo")
17900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
17910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
17920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pos()
17930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (10.00, 10.00)
17940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.towards(0,0)
17950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        225.0
17960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
17970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if y is not None:
17980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pos = Vec2D(x, y)
17990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(x, Vec2D):
18000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pos = x
18010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif isinstance(x, tuple):
18020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pos = Vec2D(*x)
18030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif isinstance(x, TNavigator):
18040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pos = x._position
18050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x, y = pos - self._position
18060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        result = round(math.atan2(y, x)*180.0/math.pi, 10) % 360.0
18070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        result /= self._degreesPerAU
18080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return (self._angleOffset + self._angleOrient*result) % self._fullcircle
18090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
18100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def heading(self):
18110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Return the turtle's current heading.
18120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
18130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No arguments.
18140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
18150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
18160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.left(67)
18170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.heading()
18180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        67.0
18190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
18200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x, y = self._orient
18210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        result = round(math.atan2(y, x)*180.0/math.pi, 10) % 360.0
18220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        result /= self._degreesPerAU
18230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return (self._angleOffset + self._angleOrient*result) % self._fullcircle
18240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
18250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def setheading(self, to_angle):
18260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set the orientation of the turtle to to_angle.
18270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
18280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Aliases:  setheading | seth
18290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
18300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Argument:
18310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        to_angle -- a number (integer or float)
18320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
18330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Set the orientation of the turtle to to_angle.
18340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Here are some common directions in degrees:
18350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
18360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         standard - mode:          logo-mode:
18370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        -------------------|--------------------
18380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           0 - east                0 - north
18390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          90 - north              90 - east
18400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         180 - west              180 - south
18410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao         270 - south             270 - west
18420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
18430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
18440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.setheading(90)
18450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.heading()
18460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        90
18470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
18480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        angle = (to_angle - self.heading())*self._angleOrient
18490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        full = self._fullcircle
18500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        angle = (angle+full/2.)%full - full/2.
18510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._rotate(angle)
18520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
18530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def circle(self, radius, extent = None, steps = None):
18540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Draw a circle with given radius.
18550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
18560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
18570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        radius -- a number
18580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        extent (optional) -- a number
18590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        steps (optional) -- an integer
18600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
18610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Draw a circle with given radius. The center is radius units left
18620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        of the turtle; extent - an angle - determines which part of the
18630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        circle is drawn. If extent is not given, draw the entire circle.
18640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If extent is not a full circle, one endpoint of the arc is the
18650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        current pen position. Draw the arc in counterclockwise direction
18660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if radius is positive, otherwise in clockwise direction. Finally
18670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        the direction of the turtle is changed by the amount of extent.
18680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
18690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        As the circle is approximated by an inscribed regular polygon,
18700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        steps determines the number of steps to use. If not given,
18710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        it will be calculated automatically. Maybe used to draw regular
18720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        polygons.
18730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
18740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        call: circle(radius)                  # full circle
18750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        --or: circle(radius, extent)          # arc
18760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        --or: circle(radius, extent, steps)
18770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        --or: circle(radius, steps=6)         # 6-sided polygon
18780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
18790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
18800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.circle(50)
18810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.circle(120, 180)  # semicircle
18820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
18830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.undobuffer:
18840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.undobuffer.push(["seq"])
18850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.undobuffer.cumulate = True
18860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        speed = self.speed()
18870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if extent is None:
18880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            extent = self._fullcircle
18890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if steps is None:
18900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            frac = abs(extent)/self._fullcircle
18910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            steps = 1+int(min(11+abs(radius)/6.0, 59.0)*frac)
18920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        w = 1.0 * extent / steps
18930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        w2 = 0.5 * w
18940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        l = 2.0 * radius * math.sin(w2*math.pi/180.0*self._degreesPerAU)
18950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if radius < 0:
18960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            l, w, w2 = -l, -w, -w2
18970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tr = self.tracer()
18980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        dl = self._delay()
18990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if speed == 0:
19000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.tracer(0, 0)
19010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
19020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.speed(0)
19030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._rotate(w2)
19040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for i in range(steps):
19050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.speed(speed)
19060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._go(l)
19070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.speed(0)
19080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._rotate(w)
19090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._rotate(-w2)
19100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if speed == 0:
19110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.tracer(tr, dl)
19120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.speed(speed)
19130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.undobuffer:
19140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.undobuffer.cumulate = False
19150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao## three dummy methods to be implemented by child class:
19170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def speed(self, s=0):
19190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """dummy method - to be overwritten by child class"""
19200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tracer(self, a=None, b=None):
19210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """dummy method - to be overwritten by child class"""
19220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _delay(self, n=None):
19230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """dummy method - to be overwritten by child class"""
19240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    fd = forward
19260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    bk = back
19270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    backward = back
19280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    rt = right
19290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    lt = left
19300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    position = pos
19310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    setpos = goto
19320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    setposition = goto
19330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    seth = setheading
19340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19360a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TPen(object):
19370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Drawing part of the RawTurtle.
19380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Implements drawing properties.
19390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
19400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, resizemode=_CFG["resizemode"]):
19410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._resizemode = resizemode # or "user" or "noresize"
19420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.undobuffer = None
19430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        TPen._reset(self)
19440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _reset(self, pencolor=_CFG["pencolor"],
19460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     fillcolor=_CFG["fillcolor"]):
19470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._pensize = 1
19480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._shown = True
19490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._pencolor = pencolor
19500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._fillcolor = fillcolor
19510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._drawing = True
19520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._speed = 3
19530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._stretchfactor = (1, 1)
19540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._tilt = 0
19550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._outlinewidth = 1
19560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ### self.screen = None  # to override by child class
19570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def resizemode(self, rmode=None):
19590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set resizemode to one of the values: "auto", "user", "noresize".
19600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (Optional) Argument:
19620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        rmode -- one of the strings "auto", "user", "noresize"
19630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Different resizemodes have the following effects:
19650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          - "auto" adapts the appearance of the turtle
19660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   corresponding to the value of pensize.
19670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          - "user" adapts the appearance of the turtle according to the
19680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   values of stretchfactor and outlinewidth (outline),
19690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                   which are set by shapesize()
19700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          - "noresize" no adaption of the turtle's appearance takes place.
19710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If no argument is given, return current resizemode.
19720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        resizemode("user") is called by a call of shapesize with arguments.
19730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Examples (for a Turtle instance named turtle):
19760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.resizemode("noresize")
19770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.resizemode()
19780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'noresize'
19790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
19800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if rmode is None:
19810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self._resizemode
19820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        rmode = rmode.lower()
19830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if rmode in ["auto", "user", "noresize"]:
19840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.pen(resizemode=rmode)
19850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def pensize(self, width=None):
19870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set or return the line thickness.
19880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Aliases:  pensize | width
19900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Argument:
19920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        width -- positive number
19930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Set the line thickness to width or return it. If resizemode is set
19950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        to "auto" and turtleshape is a polygon, that polygon is drawn with
19960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        the same line thickness. If no argument is given, current pensize
19970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        is returned.
19980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
19990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
20000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pensize()
20010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        1
20020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pensize(10)   # from here on lines of width 10 are drawn
20030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
20040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if width is None:
20050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self._pensize
20060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.pen(pensize=width)
20070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def penup(self):
20100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Pull the pen up -- no drawing when moving.
20110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Aliases: penup | pu | up
20130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument
20150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
20170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.penup()
20180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
20190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not self._drawing:
20200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
20210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.pen(pendown=False)
20220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def pendown(self):
20240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Pull the pen down -- drawing when moving.
20250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Aliases: pendown | pd | down
20270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
20290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
20310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pendown()
20320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
20330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._drawing:
20340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
20350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.pen(pendown=True)
20360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def isdown(self):
20380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return True if pen is down, False if it's up.
20390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
20410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
20430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.penup()
20440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.isdown()
20450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        False
20460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pendown()
20470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.isdown()
20480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        True
20490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
20500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._drawing
20510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def speed(self, speed=None):
20530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Return or set the turtle's speed.
20540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Optional argument:
20560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        speed -- an integer in the range 0..10 or a speedstring (see below)
20570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Set the turtle's speed to an integer value in the range 0 .. 10.
20590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If no argument is given: return current speed.
20600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If input is a number greater than 10 or smaller than 0.5,
20620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        speed is set to 0.
20630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Speedstrings  are mapped to speedvalues in the following way:
20640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            'fastest' :  0
20650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            'fast'    :  10
20660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            'normal'  :  6
20670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            'slow'    :  3
20680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            'slowest' :  1
20690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        speeds from 1 to 10 enforce increasingly faster animation of
20700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        line drawing and turtle turning.
20710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Attention:
20730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        speed = 0 : *no* animation takes place. forward/back makes turtle jump
20740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        and likewise left/right make the turtle turn instantly.
20750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
20770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.speed(3)
20780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
20790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        speeds = {'fastest':0, 'fast':10, 'normal':6, 'slow':3, 'slowest':1 }
20800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if speed is None:
20810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self._speed
20820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if speed in speeds:
20830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            speed = speeds[speed]
20840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif 0.5 < speed < 10.5:
20850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            speed = int(round(speed))
20860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
20870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            speed = 0
20880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.pen(speed=speed)
20890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def color(self, *args):
20910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return or set the pencolor and fillcolor.
20920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
20940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Several input formats are allowed.
20950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        They use 0, 1, 2, or 3 arguments as follows:
20960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        color()
20980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            Return the current pencolor and the current fillcolor
20990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            as a pair of color specification strings as are returned
21000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            by pencolor and fillcolor.
21010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        color(colorstring), color((r,g,b)), color(r,g,b)
21020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            inputs as in pencolor, set both, fillcolor and pencolor,
21030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            to the given value.
21040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        color(colorstring1, colorstring2),
21050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        color((r1,g1,b1), (r2,g2,b2))
21060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            equivalent to pencolor(colorstring1) and fillcolor(colorstring2)
21070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            and analogously, if the other input format is used.
21080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
21090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If turtleshape is a polygon, outline and interior of that polygon
21100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        is drawn with the newly set colors.
21110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        For mor info see: pencolor, fillcolor
21120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
21130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
21140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.color('red', 'green')
21150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.color()
21160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('red', 'green')
21170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> colormode(255)
21180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> color((40, 80, 120), (160, 200, 240))
21190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> color()
21200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('#285078', '#a0c8f0')
21210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
21220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if args:
21230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            l = len(args)
21240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if l == 1:
21250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pcolor = fcolor = args[0]
21260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            elif l == 2:
21270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pcolor, fcolor = args
21280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            elif l == 3:
21290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pcolor = fcolor = args
21300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pcolor = self._colorstr(pcolor)
21310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            fcolor = self._colorstr(fcolor)
21320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.pen(pencolor=pcolor, fillcolor=fcolor)
21330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
21340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self._color(self._pencolor), self._color(self._fillcolor)
21350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
21360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def pencolor(self, *args):
21370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Return or set the pencolor.
21380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
21390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
21400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Four input formats are allowed:
21410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          - pencolor()
21420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            Return the current pencolor as color specification string,
21430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            possibly in hex-number format (see example).
21440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            May be used as input to another color/pencolor/fillcolor call.
21450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          - pencolor(colorstring)
21460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            s is a Tk color specification string, such as "red" or "yellow"
21470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          - pencolor((r, g, b))
21480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            *a tuple* of r, g, and b, which represent, an RGB color,
21490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            and each of r, g, and b are in the range 0..colormode,
21500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            where colormode is either 1.0 or 255
21510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          - pencolor(r, g, b)
21520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            r, g, and b represent an RGB color, and each of r, g, and b
21530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            are in the range 0..colormode
21540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
21550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If turtleshape is a polygon, the outline of that polygon is drawn
21560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with the newly set pencolor.
21570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
21580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
21590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pencolor('brown')
21600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> tup = (0.2, 0.8, 0.55)
21610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pencolor(tup)
21620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pencolor()
21630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        '#33cc8c'
21640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
21650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if args:
21660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            color = self._colorstr(args)
21670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if color == self._pencolor:
21680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return
21690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.pen(pencolor=color)
21700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
21710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self._color(self._pencolor)
21720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
21730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def fillcolor(self, *args):
21740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Return or set the fillcolor.
21750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
21760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
21770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Four input formats are allowed:
21780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          - fillcolor()
21790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            Return the current fillcolor as color specification string,
21800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            possibly in hex-number format (see example).
21810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            May be used as input to another color/pencolor/fillcolor call.
21820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          - fillcolor(colorstring)
21830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            s is a Tk color specification string, such as "red" or "yellow"
21840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          - fillcolor((r, g, b))
21850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            *a tuple* of r, g, and b, which represent, an RGB color,
21860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            and each of r, g, and b are in the range 0..colormode,
21870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            where colormode is either 1.0 or 255
21880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          - fillcolor(r, g, b)
21890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            r, g, and b represent an RGB color, and each of r, g, and b
21900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            are in the range 0..colormode
21910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
21920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If turtleshape is a polygon, the interior of that polygon is drawn
21930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with the newly set fillcolor.
21940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
21950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
21960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.fillcolor('violet')
21970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> col = turtle.pencolor()
21980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.fillcolor(col)
21990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.fillcolor(0, .5, 0)
22000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
22010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if args:
22020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            color = self._colorstr(args)
22030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if color == self._fillcolor:
22040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return
22050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.pen(fillcolor=color)
22060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
22070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self._color(self._fillcolor)
22080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def showturtle(self):
22100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Makes the turtle visible.
22110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Aliases: showturtle | st
22130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
22150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
22170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.hideturtle()
22180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.showturtle()
22190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
22200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.pen(shown=True)
22210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def hideturtle(self):
22230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Makes the turtle invisible.
22240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Aliases: hideturtle | ht
22260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
22280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        It's a good idea to do this while you're in the
22300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        middle of a complicated drawing, because hiding
22310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        the turtle speeds up the drawing observably.
22320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
22340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.hideturtle()
22350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
22360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.pen(shown=False)
22370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def isvisible(self):
22390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return True if the Turtle is shown, False if it's hidden.
22400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
22420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
22440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.hideturtle()
22450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> print turtle.isvisible():
22460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        False
22470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
22480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self._shown
22490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def pen(self, pen=None, **pendict):
22510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return or set the pen's attributes.
22520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
22540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pen -- a dictionary with some or all of the below listed keys.
22550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            **pendict -- one or more keyword-arguments with the below
22560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         listed keys as keywords.
22570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Return or set the pen's attributes in a 'pen-dictionary'
22590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with the following key/value pairs:
22600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "shown"      :   True/False
22610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "pendown"    :   True/False
22620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "pencolor"   :   color-string or color-tuple
22630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "fillcolor"  :   color-string or color-tuple
22640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "pensize"    :   positive number
22650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "speed"      :   number in range 0..10
22660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "resizemode" :   "auto" or "user" or "noresize"
22670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "stretchfactor": (positive number, positive number)
22680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "outline"    :   positive number
22690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           "tilt"       :   number
22700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        This dictionary can be used as argument for a subsequent
22720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pen()-call to restore the former pen-state. Moreover one
22730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        or more of these attributes can be provided as keyword-arguments.
22740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        This can be used to set several pen attributes in one statement.
22750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
22770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Examples (for a Turtle instance named turtle):
22780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
22790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pen()
22800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
22810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'pencolor': 'red', 'pendown': True, 'fillcolor': 'black',
22820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'stretchfactor': (1,1), 'speed': 3}
22830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> penstate=turtle.pen()
22840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.color("yellow","")
22850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.penup()
22860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.pen()
22870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
22880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'pencolor': 'yellow', 'pendown': False, 'fillcolor': '',
22890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'stretchfactor': (1,1), 'speed': 3}
22900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> p.pen(penstate, fillcolor="green")
22910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> p.pen()
22920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        {'pensize': 10, 'shown': True, 'resizemode': 'auto', 'outline': 1,
22930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'pencolor': 'red', 'pendown': True, 'fillcolor': 'green',
22940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'stretchfactor': (1,1), 'speed': 3}
22950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
22960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        _pd =  {"shown"         : self._shown,
22970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "pendown"       : self._drawing,
22980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "pencolor"      : self._pencolor,
22990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "fillcolor"     : self._fillcolor,
23000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "pensize"       : self._pensize,
23010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "speed"         : self._speed,
23020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "resizemode"    : self._resizemode,
23030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "stretchfactor" : self._stretchfactor,
23040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "outline"       : self._outlinewidth,
23050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "tilt"          : self._tilt
23060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               }
23070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
23080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not (pen or pendict):
23090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return _pd
23100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
23110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(pen, dict):
23120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            p = pen
23130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
23140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            p = {}
23150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        p.update(pendict)
23160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
23170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        _p_buf = {}
23180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for key in p:
23190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            _p_buf[key] = _pd[key]
23200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
23210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.undobuffer:
23220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.undobuffer.push(("pen", _p_buf))
23230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
23240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        newLine = False
23250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if "pendown" in p:
23260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if self._drawing != p["pendown"]:
23270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                newLine = True
23280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if "pencolor" in p:
23290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if isinstance(p["pencolor"], tuple):
23300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                p["pencolor"] = self._colorstr((p["pencolor"],))
23310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if self._pencolor != p["pencolor"]:
23320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                newLine = True
23330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if "pensize" in p:
23340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if self._pensize != p["pensize"]:
23350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                newLine = True
23360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if newLine:
23370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._newLine()
23380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if "pendown" in p:
23390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._drawing = p["pendown"]
23400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if "pencolor" in p:
23410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._pencolor = p["pencolor"]
23420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if "pensize" in p:
23430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._pensize = p["pensize"]
23440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if "fillcolor" in p:
23450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if isinstance(p["fillcolor"], tuple):
23460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                p["fillcolor"] = self._colorstr((p["fillcolor"],))
23470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._fillcolor = p["fillcolor"]
23480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if "speed" in p:
23490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._speed = p["speed"]
23500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if "resizemode" in p:
23510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._resizemode = p["resizemode"]
23520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if "stretchfactor" in p:
23530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            sf = p["stretchfactor"]
23540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if isinstance(sf, (int, float)):
23550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                sf = (sf, sf)
23560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._stretchfactor = sf
23570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if "outline" in p:
23580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._outlinewidth = p["outline"]
23590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if "shown" in p:
23600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._shown = p["shown"]
23610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if "tilt" in p:
23620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._tilt = p["tilt"]
23630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._update()
23640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
23650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao## three dummy methods to be implemented by child class:
23660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
23670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _newLine(self, usePos = True):
23680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """dummy method - to be overwritten by child class"""
23690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _update(self, count=True, forced=False):
23700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """dummy method - to be overwritten by child class"""
23710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _color(self, args):
23720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """dummy method - to be overwritten by child class"""
23730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _colorstr(self, args):
23740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """dummy method - to be overwritten by child class"""
23750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
23760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    width = pensize
23770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    up = penup
23780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    pu = penup
23790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    pd = pendown
23800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    down = pendown
23810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    st = showturtle
23820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ht = hideturtle
23830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
23840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
23850a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass _TurtleImage(object):
23860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Helper class: Datatype to store Turtle attributes
23870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
23880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
23890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, screen, shapeIndex):
23900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.screen = screen
23910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._type = None
23920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._setshape(shapeIndex)
23930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
23940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _setshape(self, shapeIndex):
23950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        screen = self.screen # RawTurtle.screens[self.screenIndex]
23960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.shapeIndex = shapeIndex
23970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._type == "polygon" == screen._shapes[shapeIndex]._type:
23980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
23990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._type == "image" == screen._shapes[shapeIndex]._type:
24000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
24010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._type in ["image", "polygon"]:
24020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            screen._delete(self._item)
24030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif self._type == "compound":
24040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for item in self._item:
24050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                screen._delete(item)
24060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._type = screen._shapes[shapeIndex]._type
24070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._type == "polygon":
24080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._item = screen._createpoly()
24090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif self._type == "image":
24100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._item = screen._createimage(screen._shapes["blank"]._data)
24110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif self._type == "compound":
24120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._item = [screen._createpoly() for item in
24130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                          screen._shapes[shapeIndex]._data]
24140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
24150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
24160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass RawTurtle(TPen, TNavigator):
24170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Animation part of the RawTurtle.
24180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Puts RawTurtle upon a TurtleScreen and provides tools for
24190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    its animation.
24200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
24210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    screens = []
24220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
24230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, canvas=None,
24240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 shape=_CFG["shape"],
24250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 undobuffersize=_CFG["undobuffersize"],
24260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 visible=_CFG["visible"]):
24270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(canvas, _Screen):
24280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.screen = canvas
24290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif isinstance(canvas, TurtleScreen):
24300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if canvas not in RawTurtle.screens:
24310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                RawTurtle.screens.append(canvas)
24320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.screen = canvas
24330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif isinstance(canvas, (ScrolledCanvas, Canvas)):
24340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for screen in RawTurtle.screens:
24350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if screen.cv == canvas:
24360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.screen = screen
24370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    break
24380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
24390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.screen = TurtleScreen(canvas)
24400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                RawTurtle.screens.append(self.screen)
24410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
24420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise TurtleGraphicsError("bad cavas argument %s" % canvas)
24430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
24440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        screen = self.screen
24450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        TNavigator.__init__(self, screen.mode())
24460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        TPen.__init__(self)
24470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        screen._turtles.append(self)
24480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.drawingLineItem = screen._createline()
24490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.turtle = _TurtleImage(screen, shape)
24500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._poly = None
24510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._creatingPoly = False
24520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._fillitem = self._fillpath = None
24530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._shown = visible
24540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._hidden_from_screen = False
24550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.currentLineItem = screen._createline()
24560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.currentLine = [self._position]
24570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.items = [self.currentLineItem]
24580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.stampItems = []
24590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._undobuffersize = undobuffersize
24600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.undobuffer = Tbuffer(undobuffersize)
24610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._update()
24620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
24630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def reset(self):
24640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Delete the turtle's drawings and restore its default values.
24650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
24660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
24670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao,
24680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Delete the turtle's drawings from the screen, re-center the turtle
24690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        and set variables to the default values.
24700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
24710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
24720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.position()
24730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (0.00,-22.00)
24740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.heading()
24750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        100.0
24760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.reset()
24770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.position()
24780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (0.00,0.00)
24790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.heading()
24800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        0.0
24810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
24820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        TNavigator.reset(self)
24830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        TPen._reset(self)
24840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._clear()
24850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._drawturtle()
24860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._update()
24870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
24880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def setundobuffer(self, size):
24890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set or disable undobuffer.
24900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
24910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Argument:
24920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        size -- an integer or None
24930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
24940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If size is an integer an empty undobuffer of given size is installed.
24950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Size gives the maximum number of turtle-actions that can be undone
24960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        by the undo() function.
24970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If size is None, no undobuffer is present.
24980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
24990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
25000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.setundobuffer(42)
25010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
25020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if size is None:
25030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.undobuffer = None
25040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
25050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.undobuffer = Tbuffer(size)
25060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def undobufferentries(self):
25080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return count of entries in the undobuffer.
25090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
25110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
25130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> while undobufferentries():
25140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     undo()
25150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
25160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.undobuffer is None:
25170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return 0
25180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.undobuffer.nr_of_items()
25190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _clear(self):
25210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Delete all of pen's drawings"""
25220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._fillitem = self._fillpath = None
25230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for item in self.items:
25240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.screen._delete(item)
25250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.currentLineItem = self.screen._createline()
25260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.currentLine = []
25270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._drawing:
25280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.currentLine.append(self._position)
25290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.items = [self.currentLineItem]
25300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.clearstamps()
25310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.setundobuffer(self._undobuffersize)
25320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def clear(self):
25350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Delete the turtle's drawings from the screen. Do not move turtle.
25360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No arguments.
25380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Delete the turtle's drawings from the screen. Do not move turtle.
25400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        State and position of the turtle as well as drawings of other
25410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtles are not affected.
25420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Examples (for a Turtle instance named turtle):
25440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.clear()
25450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
25460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._clear()
25470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._update()
25480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _update_data(self):
25500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.screen._incrementudc()
25510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.screen._updatecounter != 0:
25520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
25530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if len(self.currentLine)>1:
25540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.screen._drawline(self.currentLineItem, self.currentLine,
25550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                  self._pencolor, self._pensize)
25560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _update(self):
25580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Perform a Turtle-data update.
25590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
25600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        screen = self.screen
25610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if screen._tracing == 0:
25620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
25630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif screen._tracing == 1:
25640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._update_data()
25650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._drawturtle()
25660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            screen._update()                  # TurtleScreenBase
25670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            screen._delay(screen._delayvalue) # TurtleScreenBase
25680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
25690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._update_data()
25700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if screen._updatecounter == 0:
25710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                for t in screen.turtles():
25720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    t._drawturtle()
25730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                screen._update()
25740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tracer(self, flag=None, delay=None):
25760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Turns turtle animation on/off and set delay for update drawings.
25770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Optional arguments:
25790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        n -- nonnegative  integer
25800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        delay -- nonnegative  integer
25810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If n is given, only each n-th regular screen update is really performed.
25830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (Can be used to accelerate the drawing of complex graphics.)
25840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Second arguments sets delay value (see RawTurtle.delay())
25850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
25870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.tracer(8, 25)
25880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> dist = 2
25890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> for i in range(200):
25900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     turtle.fd(dist)
25910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     turtle.rt(90)
25920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     dist += 2
25930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
25940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.screen.tracer(flag, delay)
25950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _color(self, args):
25970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.screen._color(args)
25980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
25990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _colorstr(self, args):
26000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.screen._colorstr(args)
26010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _cc(self, args):
26030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Convert colortriples to hexstrings.
26040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
26050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(args, str):
26060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return args
26070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
26080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            r, g, b = args
26090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except:
26100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise TurtleGraphicsError("bad color arguments: %s" % str(args))
26110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.screen._colormode == 1.0:
26120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            r, g, b = [round(255.0*x) for x in (r, g, b)]
26130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)):
26140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise TurtleGraphicsError("bad color sequence: %s" % str(args))
26150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return "#%02x%02x%02x" % (r, g, b)
26160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def clone(self):
26180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Create and return a clone of the turtle.
26190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
26210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Create and return a clone of the turtle with same position, heading
26230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        and turtle properties.
26240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named mick):
26260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mick = Turtle()
26270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        joe = mick.clone()
26280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
26290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        screen = self.screen
26300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._newLine(self._drawing)
26310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle = self.turtle
26330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.screen = None
26340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.turtle = None  # too make self deepcopy-able
26350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        q = deepcopy(self)
26370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.screen = screen
26390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.turtle = turtle
26400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        q.screen = screen
26420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        q.turtle = _TurtleImage(screen, self.turtle.shapeIndex)
26430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        screen._turtles.append(q)
26450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ttype = screen._shapes[self.turtle.shapeIndex]._type
26460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if ttype == "polygon":
26470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            q.turtle._item = screen._createpoly()
26480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif ttype == "image":
26490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            q.turtle._item = screen._createimage(screen._shapes["blank"]._data)
26500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif ttype == "compound":
26510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            q.turtle._item = [screen._createpoly() for item in
26520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              screen._shapes[self.turtle.shapeIndex]._data]
26530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        q.currentLineItem = screen._createline()
26540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        q._update()
26550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return q
26560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def shape(self, name=None):
26580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set turtle shape to shape with given name / return current shapename.
26590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Optional argument:
26610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        name -- a string, which is a valid shapename
26620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Set turtle shape to shape with given name or, if name is not given,
26640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return name of current shape.
26650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Shape with name must exist in the TurtleScreen's shape dictionary.
26660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Initially there are the following polygon shapes:
26670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'arrow', 'turtle', 'circle', 'square', 'triangle', 'classic'.
26680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        To learn about how to deal with shapes see Screen-method register_shape.
26690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
26710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.shape()
26720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'arrow'
26730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.shape("turtle")
26740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.shape()
26750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        'turtle'
26760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
26770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if name is None:
26780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self.turtle.shapeIndex
26790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not name in self.screen.getshapes():
26800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise TurtleGraphicsError("There is no shape named %s" % name)
26810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.turtle._setshape(name)
26820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._update()
26830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def shapesize(self, stretch_wid=None, stretch_len=None, outline=None):
26850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set/return turtle's stretchfactors/outline. Set resizemode to "user".
26860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Optinonal arguments:
26880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           stretch_wid : positive number
26890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           stretch_len : positive number
26900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           outline  : positive number
26910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
26920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Return or set the pen's attributes x/y-stretchfactors and/or outline.
26930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Set resizemode to "user".
26940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If and only if resizemode is set to "user", the turtle will be displayed
26950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        stretched according to its stretchfactors:
26960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        stretch_wid is stretchfactor perpendicular to orientation
26970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        stretch_len is stretchfactor in direction of turtles orientation.
26980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        outline determines the width of the shapes's outline.
26990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Examples (for a Turtle instance named turtle):
27010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.resizemode("user")
27020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.shapesize(5, 5, 12)
27030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.shapesize(outline=8)
27040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
27050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if stretch_wid is stretch_len is outline is None:
27060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            stretch_wid, stretch_len = self._stretchfactor
27070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return stretch_wid, stretch_len, self._outlinewidth
27080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if stretch_wid is not None:
27090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if stretch_len is None:
27100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                stretchfactor = stretch_wid, stretch_wid
27110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
27120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                stretchfactor = stretch_wid, stretch_len
27130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif stretch_len is not None:
27140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            stretchfactor = self._stretchfactor[0], stretch_len
27150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
27160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            stretchfactor = self._stretchfactor
27170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if outline is None:
27180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            outline = self._outlinewidth
27190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.pen(resizemode="user",
27200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 stretchfactor=stretchfactor, outline=outline)
27210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def settiltangle(self, angle):
27230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Rotate the turtleshape to point in the specified direction
27240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Optional argument:
27260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        angle -- number
27270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Rotate the turtleshape to point in the direction specified by angle,
27290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        regardless of its current tilt-angle. DO NOT change the turtle's
27300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        heading (direction of movement).
27310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Examples (for a Turtle instance named turtle):
27340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.shape("circle")
27350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.shapesize(5,2)
27360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.settiltangle(45)
27370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> stamp()
27380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.fd(50)
27390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.settiltangle(-45)
27400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> stamp()
27410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.fd(50)
27420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
27430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tilt = -angle * self._degreesPerAU * self._angleOrient
27440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tilt = (tilt * math.pi / 180.0) % (2*math.pi)
27450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.pen(resizemode="user", tilt=tilt)
27460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tiltangle(self):
27480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return the current tilt-angle.
27490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
27510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Return the current tilt-angle, i. e. the angle between the
27530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        orientation of the turtleshape and the heading of the turtle
27540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (its direction of movement).
27550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Examples (for a Turtle instance named turtle):
27570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.shape("circle")
27580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.shapesize(5,2)
27590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.tilt(45)
27600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.tiltangle()
27610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
27620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tilt = -self._tilt * (180.0/math.pi) * self._angleOrient
27630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return (tilt / self._degreesPerAU) % self._fullcircle
27640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tilt(self, angle):
27660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Rotate the turtleshape by angle.
27670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Argument:
27690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        angle - a number
27700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Rotate the turtleshape by angle from its current tilt-angle,
27720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        but do NOT change the turtle's heading (direction of movement).
27730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Examples (for a Turtle instance named turtle):
27750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.shape("circle")
27760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.shapesize(5,2)
27770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.tilt(30)
27780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.fd(50)
27790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.tilt(30)
27800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.fd(50)
27810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
27820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.settiltangle(angle + self.tiltangle())
27830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _polytrafo(self, poly):
27850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Computes transformed polygon shapes from a shape
27860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        according to current position and heading.
27870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
27880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        screen = self.screen
27890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        p0, p1 = self._position
27900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e0, e1 = self._orient
27910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e = Vec2D(e0, e1 * screen.yscale / screen.xscale)
27920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        e0, e1 = (1.0 / abs(e)) * e
27930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return [(p0+(e1*x+e0*y)/screen.xscale, p1+(-e0*x+e1*y)/screen.yscale)
27940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                           for (x, y) in poly]
27950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
27960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _drawturtle(self):
27970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Manages the correct rendering of the turtle with respect to
27980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        its shape, resizemode, stretch and tilt etc."""
27990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        screen = self.screen
28000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        shape = screen._shapes[self.turtle.shapeIndex]
28010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ttype = shape._type
28020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        titem = self.turtle._item
28030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._shown and screen._updatecounter == 0 and screen._tracing > 0:
28040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._hidden_from_screen = False
28050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            tshape = shape._data
28060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if ttype == "polygon":
28070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if self._resizemode == "noresize":
28080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    w = 1
28090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    shape = tshape
28100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                else:
28110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    if self._resizemode == "auto":
28120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        lx = ly = max(1, self._pensize/5.0)
28130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        w = self._pensize
28140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        tiltangle = 0
28150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    elif self._resizemode == "user":
28160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        lx, ly = self._stretchfactor
28170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        w = self._outlinewidth
28180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        tiltangle = self._tilt
28190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    shape = [(lx*x, ly*y) for (x, y) in tshape]
28200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    t0, t1 = math.sin(tiltangle), math.cos(tiltangle)
28210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    shape = [(t1*x+t0*y, -t0*x+t1*y) for (x, y) in shape]
28220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                shape = self._polytrafo(shape)
28230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                fc, oc = self._fillcolor, self._pencolor
28240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                screen._drawpoly(titem, shape, fill=fc, outline=oc,
28250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                      width=w, top=True)
28260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            elif ttype == "image":
28270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                screen._drawimage(titem, self._position, tshape)
28280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            elif ttype == "compound":
28290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                lx, ly = self._stretchfactor
28300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                w = self._outlinewidth
28310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                for item, (poly, fc, oc) in zip(titem, tshape):
28320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    poly = [(lx*x, ly*y) for (x, y) in poly]
28330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    poly = self._polytrafo(poly)
28340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    screen._drawpoly(item, poly, fill=self._cc(fc),
28350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                     outline=self._cc(oc), width=w, top=True)
28360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
28370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if self._hidden_from_screen:
28380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return
28390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if ttype == "polygon":
28400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                screen._drawpoly(titem, ((0, 0), (0, 0), (0, 0)), "", "")
28410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            elif ttype == "image":
28420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                screen._drawimage(titem, self._position,
28430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                          screen._shapes["blank"]._data)
28440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            elif ttype == "compound":
28450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                for item in titem:
28460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    screen._drawpoly(item, ((0, 0), (0, 0), (0, 0)), "", "")
28470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._hidden_from_screen = True
28480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
28490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao##############################  stamp stuff  ###############################
28500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
28510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def stamp(self):
28520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Stamp a copy of the turtleshape onto the canvas and return its id.
28530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
28540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
28550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
28560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Stamp a copy of the turtle shape onto the canvas at the current
28570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle position. Return a stamp_id for that stamp, which can be
28580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        used to delete it by calling clearstamp(stamp_id).
28590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
28600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
28610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.color("blue")
28620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.stamp()
28630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        13
28640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.fd(50)
28650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
28660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        screen = self.screen
28670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        shape = screen._shapes[self.turtle.shapeIndex]
28680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ttype = shape._type
28690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tshape = shape._data
28700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if ttype == "polygon":
28710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            stitem = screen._createpoly()
28720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if self._resizemode == "noresize":
28730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                w = 1
28740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                shape = tshape
28750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
28760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if self._resizemode == "auto":
28770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    lx = ly = max(1, self._pensize/5.0)
28780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    w = self._pensize
28790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    tiltangle = 0
28800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                elif self._resizemode == "user":
28810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    lx, ly = self._stretchfactor
28820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    w = self._outlinewidth
28830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    tiltangle = self._tilt
28840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                shape = [(lx*x, ly*y) for (x, y) in tshape]
28850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                t0, t1 = math.sin(tiltangle), math.cos(tiltangle)
28860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                shape = [(t1*x+t0*y, -t0*x+t1*y) for (x, y) in shape]
28870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            shape = self._polytrafo(shape)
28880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            fc, oc = self._fillcolor, self._pencolor
28890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            screen._drawpoly(stitem, shape, fill=fc, outline=oc,
28900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                  width=w, top=True)
28910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif ttype == "image":
28920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            stitem = screen._createimage("")
28930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            screen._drawimage(stitem, self._position, tshape)
28940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif ttype == "compound":
28950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            stitem = []
28960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for element in tshape:
28970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                item = screen._createpoly()
28980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                stitem.append(item)
28990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            stitem = tuple(stitem)
29000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            lx, ly = self._stretchfactor
29010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            w = self._outlinewidth
29020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for item, (poly, fc, oc) in zip(stitem, tshape):
29030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                poly = [(lx*x, ly*y) for (x, y) in poly]
29040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                poly = self._polytrafo(poly)
29050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                screen._drawpoly(item, poly, fill=self._cc(fc),
29060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                 outline=self._cc(oc), width=w, top=True)
29070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.stampItems.append(stitem)
29080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.undobuffer.push(("stamp", stitem))
29090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return stitem
29100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
29110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _clearstamp(self, stampid):
29120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """does the work for clearstamp() and clearstamps()
29130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
29140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if stampid in self.stampItems:
29150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if isinstance(stampid, tuple):
29160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                for subitem in stampid:
29170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.screen._delete(subitem)
29180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
29190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.screen._delete(stampid)
29200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.stampItems.remove(stampid)
29210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Delete stampitem from undobuffer if necessary
29220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # if clearstamp is called directly.
29230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        item = ("stamp", stampid)
29240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        buf = self.undobuffer
29250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if item not in buf.buffer:
29260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
29270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        index = buf.buffer.index(item)
29280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        buf.buffer.remove(item)
29290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if index <= buf.ptr:
29300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            buf.ptr = (buf.ptr - 1) % buf.bufsize
29310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        buf.buffer.insert((buf.ptr+1)%buf.bufsize, [None])
29320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
29330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def clearstamp(self, stampid):
29340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Delete stamp with given stampid
29350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
29360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Argument:
29370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        stampid - an integer, must be return value of previous stamp() call.
29380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
29390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
29400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.color("blue")
29410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> astamp = turtle.stamp()
29420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.fd(50)
29430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.clearstamp(astamp)
29440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
29450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._clearstamp(stampid)
29460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._update()
29470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
29480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def clearstamps(self, n=None):
29490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Delete all or first/last n of turtle's stamps.
29500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
29510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Optional argument:
29520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        n -- an integer
29530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
29540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If n is None, delete all of pen's stamps,
29550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else if n > 0 delete first n stamps
29560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else if n < 0 delete last n stamps.
29570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
29580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
29590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> for i in range(8):
29600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     turtle.stamp(); turtle.fd(30)
29610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...
29620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.clearstamps(2)
29630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.clearstamps(-2)
29640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.clearstamps()
29650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
29660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if n is None:
29670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            toDelete = self.stampItems[:]
29680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif n >= 0:
29690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            toDelete = self.stampItems[:n]
29700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
29710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            toDelete = self.stampItems[n:]
29720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for item in toDelete:
29730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._clearstamp(item)
29740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._update()
29750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
29760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _goto(self, end):
29770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Move the pen to the point end, thereby drawing a line
29780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if pen is down. All other methodes for turtle movement depend
29790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        on this one.
29800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
29810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ## Version mit undo-stuff
29820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        go_modes = ( self._drawing,
29830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     self._pencolor,
29840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     self._pensize,
29850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     isinstance(self._fillpath, list))
29860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        screen = self.screen
29870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        undo_entry = ("go", self._position, end, go_modes,
29880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                      (self.currentLineItem,
29890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                      self.currentLine[:],
29900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                      screen._pointlist(self.currentLineItem),
29910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                      self.items[:])
29920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                      )
29930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.undobuffer:
29940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.undobuffer.push(undo_entry)
29950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        start = self._position
29960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._speed and screen._tracing == 1:
29970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            diff = (end-start)
29980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            diffsq = (diff[0]*screen.xscale)**2 + (diff[1]*screen.yscale)**2
29990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            nhops = 1+int((diffsq**0.5)/(3*(1.1**self._speed)*self._speed))
30000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            delta = diff * (1.0/nhops)
30010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for n in range(1, nhops):
30020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if n == 1:
30030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    top = True
30040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                else:
30050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    top = False
30060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._position = start + delta * n
30070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if self._drawing:
30080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    screen._drawline(self.drawingLineItem,
30090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                     (start, self._position),
30100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                     self._pencolor, self._pensize, top)
30110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._update()
30120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if self._drawing:
30130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                screen._drawline(self.drawingLineItem, ((0, 0), (0, 0)),
30140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                               fill="", width=self._pensize)
30150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Turtle now at end,
30160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._drawing: # now update currentLine
30170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.currentLine.append(end)
30180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(self._fillpath, list):
30190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._fillpath.append(end)
30200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ######    vererbung!!!!!!!!!!!!!!!!!!!!!!
30210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._position = end
30220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._creatingPoly:
30230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._poly.append(end)
30240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if len(self.currentLine) > 42: # 42! answer to the ultimate question
30250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                       # of life, the universe and everything
30260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._newLine()
30270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._update() #count=True)
30280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _undogoto(self, entry):
30300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Reverse a _goto. Used for undo()
30310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
30320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        old, new, go_modes, coodata = entry
30330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        drawing, pc, ps, filling = go_modes
30340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cLI, cL, pl, items = coodata
30350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        screen = self.screen
30360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if abs(self._position - new) > 0.5:
30370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            print "undogoto: HALLO-DA-STIMMT-WAS-NICHT!"
30380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # restore former situation
30390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.currentLineItem = cLI
30400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.currentLine = cL
30410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if pl == [(0, 0), (0, 0)]:
30430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            usepc = ""
30440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
30450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            usepc = pc
30460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        screen._drawline(cLI, pl, fill=usepc, width=ps)
30470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        todelete = [i for i in self.items if (i not in items) and
30490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                       (screen._type(i) == "line")]
30500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for i in todelete:
30510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            screen._delete(i)
30520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.items.remove(i)
30530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        start = old
30550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._speed and screen._tracing == 1:
30560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            diff = old - new
30570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            diffsq = (diff[0]*screen.xscale)**2 + (diff[1]*screen.yscale)**2
30580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            nhops = 1+int((diffsq**0.5)/(3*(1.1**self._speed)*self._speed))
30590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            delta = diff * (1.0/nhops)
30600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for n in range(1, nhops):
30610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if n == 1:
30620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    top = True
30630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                else:
30640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    top = False
30650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._position = new + delta * n
30660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if drawing:
30670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    screen._drawline(self.drawingLineItem,
30680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                     (start, self._position),
30690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                     pc, ps, top)
30700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._update()
30710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if drawing:
30720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                screen._drawline(self.drawingLineItem, ((0, 0), (0, 0)),
30730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                               fill="", width=ps)
30740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Turtle now at position old,
30750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._position = old
30760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ##  if undo is done during creating a polygon, the last vertex
30770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ##  will be deleted. if the polygon is entirely deleted,
30780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ##  creatingPoly will be set to False.
30790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ##  Polygons created before the last one will not be affected by undo()
30800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._creatingPoly:
30810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if len(self._poly) > 0:
30820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._poly.pop()
30830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if self._poly == []:
30840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._creatingPoly = False
30850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._poly = None
30860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if filling:
30870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if self._fillpath == []:
30880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._fillpath = None
30890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                print "Unwahrscheinlich in _undogoto!"
30900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            elif self._fillpath is not None:
30910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._fillpath.pop()
30920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._update() #count=True)
30930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _rotate(self, angle):
30950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Turns pen clockwise by angle.
30960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
30970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.undobuffer:
30980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.undobuffer.push(("rot", angle, self._degreesPerAU))
30990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        angle *= self._degreesPerAU
31000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        neworient = self._orient.rotate(angle)
31010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tracing = self.screen._tracing
31020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if tracing == 1 and self._speed > 0:
31030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            anglevel = 3.0 * self._speed
31040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            steps = 1 + int(abs(angle)/anglevel)
31050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            delta = 1.0*angle/steps
31060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for _ in range(steps):
31070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._orient = self._orient.rotate(delta)
31080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._update()
31090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._orient = neworient
31100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._update()
31110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
31120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _newLine(self, usePos=True):
31130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Closes current line item and starts a new one.
31140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           Remark: if current line became too long, animation
31150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao           performance (via _drawline) slowed down considerably.
31160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
31170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if len(self.currentLine) > 1:
31180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.screen._drawline(self.currentLineItem, self.currentLine,
31190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                      self._pencolor, self._pensize)
31200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.currentLineItem = self.screen._createline()
31210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.items.append(self.currentLineItem)
31220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
31230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.screen._drawline(self.currentLineItem, top=True)
31240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.currentLine = []
31250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if usePos:
31260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.currentLine = [self._position]
31270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
31280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def fill(self, flag=None):
31290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Call fill(True) before drawing a shape to fill, fill(False) when done.
31300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
31310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Optional argument:
31320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        flag -- True/False (or 1/0 respectively)
31330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
31340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Call fill(True) before drawing the shape you want to fill,
31350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        and  fill(False) when done.
31360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        When used without argument: return fillstate (True if filling,
31370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        False else)
31380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
31390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
31400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.fill(True)
31410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(100)
31420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.left(90)
31430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(100)
31440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.left(90)
31450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(100)
31460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.left(90)
31470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(100)
31480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.fill(False)
31490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
31500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        filling = isinstance(self._fillpath, list)
31510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if flag is None:
31520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return filling
31530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        screen = self.screen
31540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        entry1 = entry2 = ()
31550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if filling:
31560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if len(self._fillpath) > 2:
31570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.screen._drawpoly(self._fillitem, self._fillpath,
31580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                      fill=self._fillcolor)
31590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                entry1 = ("dofill", self._fillitem)
31600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if flag:
31610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._fillitem = self.screen._createpoly()
31620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.items.append(self._fillitem)
31630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._fillpath = [self._position]
31640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            entry2 = ("beginfill", self._fillitem) # , self._fillpath)
31650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._newLine()
31660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
31670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._fillitem = self._fillpath = None
31680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.undobuffer:
31690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if entry1 == ():
31700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if entry2 != ():
31710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.undobuffer.push(entry2)
31720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
31730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if entry2 == ():
31740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.undobuffer.push(entry1)
31750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                else:
31760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.undobuffer.push(["seq", entry1, entry2])
31770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._update()
31780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
31790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def begin_fill(self):
31800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Called just before drawing a shape to be filled.
31810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
31820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
31830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
31840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
31850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.begin_fill()
31860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(100)
31870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.left(90)
31880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(100)
31890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.left(90)
31900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(100)
31910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.left(90)
31920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(100)
31930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.end_fill()
31940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
31950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.fill(True)
31960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
31970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def end_fill(self):
31980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Fill the shape drawn after the call begin_fill().
31990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
32000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
32010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
32020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
32030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.begin_fill()
32040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(100)
32050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.left(90)
32060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(100)
32070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.left(90)
32080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(100)
32090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.left(90)
32100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.forward(100)
32110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.end_fill()
32120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
32130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.fill(False)
32140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
32150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def dot(self, size=None, *color):
32160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Draw a dot with diameter size, using color.
32170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
32180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Optional arguments:
32190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        size -- an integer >= 1 (if given)
32200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        color -- a colorstring or a numeric color tuple
32210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
32220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Draw a circular dot with diameter size, using color.
32230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If size is not given, the maximum of pensize+4 and 2*pensize is used.
32240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
32250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
32260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.dot()
32270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
32280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
32290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #print "dot-1:", size, color
32300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not color:
32310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if isinstance(size, (str, tuple)):
32320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                color = self._colorstr(size)
32330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                size = self._pensize + max(self._pensize, 4)
32340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
32350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                color = self._pencolor
32360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if not size:
32370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    size = self._pensize + max(self._pensize, 4)
32380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
32390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if size is None:
32400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                size = self._pensize + max(self._pensize, 4)
32410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            color = self._colorstr(color)
32420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #print "dot-2:", size, color
32430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if hasattr(self.screen, "_dot"):
32440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            item = self.screen._dot(self._position, size, color)
32450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            #print "dot:", size, color, "item:", item
32460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.items.append(item)
32470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if self.undobuffer:
32480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.undobuffer.push(("dot", item))
32490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
32500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pen = self.pen()
32510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if self.undobuffer:
32520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.undobuffer.push(["seq"])
32530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.undobuffer.cumulate = True
32540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
32550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if self.resizemode() == 'auto':
32560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.ht()
32570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.pendown()
32580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.pensize(size)
32590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.pencolor(color)
32600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.forward(0)
32610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            finally:
32620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.pen(pen)
32630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if self.undobuffer:
32640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.undobuffer.cumulate = False
32650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
32660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _write(self, txt, align, font):
32670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Performs the writing for write()
32680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
32690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        item, end = self.screen._write(self._position, txt, align, font,
32700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                          self._pencolor)
32710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.items.append(item)
32720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.undobuffer:
32730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.undobuffer.push(("wri", item))
32740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return end
32750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
32760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def write(self, arg, move=False, align="left", font=("Arial", 8, "normal")):
32770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Write text at the current turtle position.
32780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
32790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
32800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        arg -- info, which is to be written to the TurtleScreen
32810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        move (optional) -- True/False
32820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        align (optional) -- one of the strings "left", "center" or right"
32830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        font (optional) -- a triple (fontname, fontsize, fonttype)
32840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
32850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Write text - the string representation of arg - at the current
32860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle position according to align ("left", "center" or right")
32870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        and with the given font.
32880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If move is True, the pen is moved to the bottom-right corner
32890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        of the text. By default, move is False.
32900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
32910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
32920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.write('Home = ', True, align="center")
32930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.write((0,0), True)
32940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
32950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.undobuffer:
32960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.undobuffer.push(["seq"])
32970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.undobuffer.cumulate = True
32980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        end = self._write(str(arg), align.lower(), font)
32990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if move:
33000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            x, y = self.pos()
33010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.setpos(end, y)
33020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.undobuffer:
33030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.undobuffer.cumulate = False
33040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def begin_poly(self):
33060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Start recording the vertices of a polygon.
33070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
33090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Start recording the vertices of a polygon. Current turtle position
33110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        is first point of polygon.
33120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
33140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.begin_poly()
33150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
33160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._poly = [self._position]
33170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._creatingPoly = True
33180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def end_poly(self):
33200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Stop recording the vertices of a polygon.
33210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
33230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Stop recording the vertices of a polygon. Current turtle position is
33250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        last point of polygon. This will be connected with the first point.
33260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
33280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.end_poly()
33290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
33300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._creatingPoly = False
33310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def get_poly(self):
33330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return the lastly recorded polygon.
33340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
33360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
33380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> p = turtle.get_poly()
33390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.register_shape("myFavouriteShape", p)
33400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
33410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ## check if there is any poly?  -- 1st solution:
33420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self._poly is not None:
33430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return tuple(self._poly)
33440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def getscreen(self):
33460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return the TurtleScreen object, the turtle is drawing  on.
33470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
33490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Return the TurtleScreen object, the turtle is drawing  on.
33510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        So TurtleScreen-methods can be called for that object.
33520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
33540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> ts = turtle.getscreen()
33550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> ts
33560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        <turtle.TurtleScreen object at 0x0106B770>
33570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> ts.bgcolor("pink")
33580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
33590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.screen
33600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def getturtle(self):
33620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Return the Turtleobject itself.
33630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
33650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Only reasonable use: as a function to return the 'anonymous turtle':
33670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example:
33690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> pet = getturtle()
33700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> pet.fd(50)
33710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> pet
33720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        <turtle.Turtle object at 0x0187D810>
33730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtles()
33740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        [<turtle.Turtle object at 0x0187D810>]
33750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
33760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self
33770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    getpen = getturtle
33790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ################################################################
33820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ### screen oriented methods recurring to methods of TurtleScreen
33830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ################################################################
33840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def window_width(self):
33860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Returns the width of the turtle window.
33870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
33890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
33910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.window_width()
33920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        640
33930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
33940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.screen._window_size()[0]
33950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def window_height(self):
33970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Return the height of the turtle window.
33980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
33990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
34000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
34020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.window_height()
34030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        480
34040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
34050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.screen._window_size()[1]
34060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _delay(self, delay=None):
34080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set delay value which determines speed of turtle animation.
34090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
34100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.screen.delay(delay)
34110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    #####   event binding methods   #####
34130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def onclick(self, fun, btn=1, add=None):
34150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Bind fun to mouse-click event on this turtle on canvas.
34160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
34180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fun --  a function with two arguments, to which will be assigned
34190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                the coordinates of the clicked point on the canvas.
34200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        num --  number of the mouse-button defaults to 1 (left mouse button).
34210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        add --  True or False. If True, new binding will be added, otherwise
34220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                it will replace a former binding.
34230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example for the anonymous turtle, i. e. the procedural way:
34250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> def turn(x, y):
34270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     left(360)
34280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...
34290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> onclick(turn)  # Now clicking into the turtle will turn it.
34300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> onclick(None)  # event-binding will be removed
34310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
34320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.screen._onclick(self.turtle._item, fun, btn, add)
34330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._update()
34340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def onrelease(self, fun, btn=1, add=None):
34360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Bind fun to mouse-button-release event on this turtle on canvas.
34370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
34390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fun -- a function with two arguments, to which will be assigned
34400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                the coordinates of the clicked point on the canvas.
34410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        num --  number of the mouse-button defaults to 1 (left mouse button).
34420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a MyTurtle instance named joe):
34440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> class MyTurtle(Turtle):
34450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     def glow(self,x,y):
34460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...             self.fillcolor("red")
34470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     def unglow(self,x,y):
34480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...             self.fillcolor("")
34490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...
34500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> joe = MyTurtle()
34510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> joe.onclick(joe.glow)
34520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> joe.onrelease(joe.unglow)
34530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Clicking on joe turns fillcolor red, unclicking turns it to
34550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        transparent.
34560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
34570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.screen._onrelease(self.turtle._item, fun, btn, add)
34580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._update()
34590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def ondrag(self, fun, btn=1, add=None):
34610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Bind fun to mouse-move event on this turtle on canvas.
34620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
34640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fun -- a function with two arguments, to which will be assigned
34650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               the coordinates of the clicked point on the canvas.
34660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        num -- number of the mouse-button defaults to 1 (left mouse button).
34670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Every sequence of mouse-move-events on a turtle is preceded by a
34690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mouse-click event on that turtle.
34700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
34720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> turtle.ondrag(turtle.goto)
34730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Subsequently clicking and dragging a Turtle will move it
34750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        across the screen thereby producing handdrawings (if pen is
34760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        down).
34770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
34780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.screen._ondrag(self.turtle._item, fun, btn, add)
34790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
34810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _undo(self, action, data):
34820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Does the main part of the work for undo()
34830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
34840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.undobuffer is None:
34850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
34860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if action == "rot":
34870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            angle, degPAU = data
34880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._rotate(-angle*degPAU/self._degreesPerAU)
34890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            dummy = self.undobuffer.pop()
34900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif action == "stamp":
34910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            stitem = data[0]
34920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.clearstamp(stitem)
34930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif action == "go":
34940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._undogoto(data)
34950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif action in ["wri", "dot"]:
34960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            item = data[0]
34970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.screen._delete(item)
34980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.items.remove(item)
34990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif action == "dofill":
35000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            item = data[0]
35010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.screen._drawpoly(item, ((0, 0),(0, 0),(0, 0)),
35020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                  fill="", outline="")
35030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif action == "beginfill":
35040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            item = data[0]
35050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._fillitem = self._fillpath = None
35060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.screen._delete(item)
35070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.items.remove(item)
35080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        elif action == "pen":
35090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            TPen.pen(self, data[0])
35100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.undobuffer.pop()
35110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
35120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def undo(self):
35130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """undo (repeatedly) the last turtle action.
35140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
35150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No argument.
35160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
35170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        undo (repeatedly) the last turtle action.
35180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Number of available undo actions is determined by the size of
35190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        the undobuffer.
35200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
35210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Turtle instance named turtle):
35220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> for i in range(4):
35230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     turtle.fd(50); turtle.lt(80)
35240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...
35250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> for i in range(8):
35260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...     turtle.undo()
35270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ...
35280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
35290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.undobuffer is None:
35300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
35310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        item = self.undobuffer.pop()
35320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        action = item[0]
35330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        data = item[1:]
35340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if action == "seq":
35350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            while data:
35360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                item = data.pop()
35370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self._undo(item[0], item[1:])
35380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
35390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._undo(action, data)
35400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
35410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    turtlesize = shapesize
35420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
35430a8c90248264a8b26970b4473770bcc3df8515fJosh GaoRawPen = RawTurtle
35440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
35450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao###  Screen - Singleton  ########################
35460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
35470a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef Screen():
35480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Return the singleton screen object.
35490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    If none exists at the moment, create a new one and return it,
35500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    else return the existing one."""
35510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if Turtle._screen is None:
35520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Turtle._screen = _Screen()
35530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return Turtle._screen
35540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
35550a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass _Screen(TurtleScreen):
35560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
35570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    _root = None
35580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    _canvas = None
35590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    _title = _CFG["title"]
35600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
35610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self):
35620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # XXX there is no need for this code to be conditional,
35630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # as there will be only a single _Screen instance, anyway
35640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # XXX actually, the turtle demo is injecting root window,
35650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # so perhaps the conditional creation of a root should be
35660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # preserved (perhaps by passing it as an optional parameter)
35670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if _Screen._root is None:
35680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            _Screen._root = self._root = _Root()
35690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._root.title(_Screen._title)
35700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._root.ondestroy(self._destroy)
35710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if _Screen._canvas is None:
35720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            width = _CFG["width"]
35730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            height = _CFG["height"]
35740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            canvwidth = _CFG["canvwidth"]
35750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            canvheight = _CFG["canvheight"]
35760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            leftright = _CFG["leftright"]
35770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            topbottom = _CFG["topbottom"]
35780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self._root.setupcanvas(width, height, canvwidth, canvheight)
35790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            _Screen._canvas = self._root._getcanvas()
35800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            TurtleScreen.__init__(self, _Screen._canvas)
35810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.setup(width, height, leftright, topbottom)
35820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
35830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def setup(self, width=_CFG["width"], height=_CFG["height"],
35840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao              startx=_CFG["leftright"], starty=_CFG["topbottom"]):
35850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """ Set the size and position of the main window.
35860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
35870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Arguments:
35880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        width: as integer a size in pixels, as float a fraction of the screen.
35890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          Default is 50% of screen.
35900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        height: as integer the height in pixels, as float a fraction of the
35910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          screen. Default is 75% of screen.
35920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        startx: if positive, starting position in pixels from the left
35930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          edge of the screen, if negative from the right edge
35940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          Default, startx=None is to center window horizontally.
35950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        starty: if positive, starting position in pixels from the top
35960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          edge of the screen, if negative from the bottom edge
35970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao          Default, starty=None is to center window vertically.
35980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
35990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Examples (for a Screen instance named screen):
36000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.setup (width=200, height=200, startx=0, starty=0)
36010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sets window to 200x200 pixels, in upper left of screen
36030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
36050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sets window to 75% of screen by 50% of screen and centers
36070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
36080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not hasattr(self._root, "set_geometry"):
36090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
36100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sw = self._root.win_width()
36110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sh = self._root.win_height()
36120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(width, float) and 0 <= width <= 1:
36130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            width = sw*width
36140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if startx is None:
36150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            startx = (sw - width) / 2
36160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isinstance(height, float) and 0 <= height <= 1:
36170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            height = sh*height
36180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if starty is None:
36190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            starty = (sh - height) / 2
36200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._root.set_geometry(width, height, startx, starty)
36210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.update()
36220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def title(self, titlestring):
36240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Set title of turtle-window
36250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Argument:
36270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        titlestring -- a string, to appear in the titlebar of the
36280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                       turtle graphics window.
36290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        This is a method of Screen-class. Not available for TurtleScreen-
36310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        objects.
36320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Screen instance named screen):
36340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.title("Welcome to the turtle-zoo!")
36350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
36360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if _Screen._root is not None:
36370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            _Screen._root.title(titlestring)
36380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        _Screen._title = titlestring
36390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _destroy(self):
36410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        root = self._root
36420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if root is _Screen._root:
36430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            Turtle._pen = None
36440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            Turtle._screen = None
36450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            _Screen._root = None
36460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            _Screen._canvas = None
36470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        TurtleScreen._RUNNING = True
36480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        root.destroy()
36490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def bye(self):
36510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Shut the turtlegraphics window.
36520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a TurtleScreen instance named screen):
36540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.bye()
36550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
36560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self._destroy()
36570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def exitonclick(self):
36590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Go into mainloop until the mouse is clicked.
36600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        No arguments.
36620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Bind bye() method to mouseclick on TurtleScreen.
36640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If "using_IDLE" - value in configuration dictionary is False
36650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (default value), enter mainloop.
36660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        If IDLE with -n switch (no subprocess) is used, this value should be
36670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        set to True in turtle.cfg. In this case IDLE's mainloop
36680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        is active also for the client script.
36690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        This is a method of the Screen-class and not available for
36710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        TurtleScreen instances.
36720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Example (for a Screen instance named screen):
36740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        >>> screen.exitonclick()
36750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """
36770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def exitGracefully(x, y):
36780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            """Screen.bye() with two dummy-parameters"""
36790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.bye()
36800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.onclick(exitGracefully)
36810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if _CFG["using_IDLE"]:
36820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return
36830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
36840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            mainloop()
36850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except AttributeError:
36860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            exit(0)
36870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36890a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Turtle(RawTurtle):
36900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """RawTurtle auto-creating (scrolled) canvas.
36910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    When a Turtle object is created or a function derived from some
36930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Turtle method is called a TurtleScreen object is automatically created.
36940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
36950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    _pen = None
36960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    _screen = None
36970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
36980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self,
36990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 shape=_CFG["shape"],
37000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 undobuffersize=_CFG["undobuffersize"],
37010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 visible=_CFG["visible"]):
37020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if Turtle._screen is None:
37030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            Turtle._screen = Screen()
37040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        RawTurtle.__init__(self, Turtle._screen,
37050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                           shape=shape,
37060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                           undobuffersize=undobuffersize,
37070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                           visible=visible)
37080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
37090a8c90248264a8b26970b4473770bcc3df8515fJosh GaoPen = Turtle
37100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
37110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _getpen():
37120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Create the 'anonymous' turtle if not already present."""
37130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if Turtle._pen is None:
37140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Turtle._pen = Turtle()
37150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return Turtle._pen
37160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
37170a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _getscreen():
37180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Create a TurtleScreen if not already present."""
37190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if Turtle._screen is None:
37200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Turtle._screen = Screen()
37210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return Turtle._screen
37220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
37230a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef write_docstringdict(filename="turtle_docstringdict"):
37240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Create and write docstring-dictionary to file.
37250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
37260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Optional argument:
37270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    filename -- a string, used as filename
37280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                default value is turtle_docstringdict
37290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
37300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Has to be called explicitly, (not used by the turtle-graphics classes)
37310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    The docstring dictionary will be written to the Python script <filname>.py
37320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    It is intended to serve as a template for translation of the docstrings
37330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    into different languages.
37340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
37350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    docsdict = {}
37360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
37370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for methodname in _tg_screen_functions:
37380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        key = "_Screen."+methodname
37390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        docsdict[key] = eval(key).__doc__
37400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for methodname in _tg_turtle_functions:
37410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        key = "Turtle."+methodname
37420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        docsdict[key] = eval(key).__doc__
37430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
37440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    f = open("%s.py" % filename,"w")
37450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    keys = sorted([x for x in docsdict.keys()
37460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        if x.split('.')[1] not in _alias_list])
37470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    f.write('docsdict = {\n\n')
37480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for key in keys[:-1]:
37490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        f.write('%s :\n' % repr(key))
37500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        f.write('        """%s\n""",\n\n' % docsdict[key])
37510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    key = keys[-1]
37520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    f.write('%s :\n' % repr(key))
37530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    f.write('        """%s\n"""\n\n' % docsdict[key])
37540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    f.write("}\n")
37550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    f.close()
37560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
37570a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef read_docstrings(lang):
37580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Read in docstrings from lang-specific docstring dictionary.
37590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
37600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    Transfer docstrings, translated to lang, from a dictionary-file
37610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    to the methods of classes Screen and Turtle and - in revised form -
37620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    to the corresponding functions.
37630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
37640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    modname = "turtle_docstringdict_%(language)s" % {'language':lang.lower()}
37650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    module = __import__(modname)
37660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    docsdict = module.docsdict
37670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    for key in docsdict:
37680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        #print key
37690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
37700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            eval(key).im_func.__doc__ = docsdict[key]
37710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except:
37720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            print "Bad docstring-entry: %s" % key
37730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
37740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao_LANGUAGE = _CFG["language"]
37750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
37760a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotry:
37770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if _LANGUAGE != "english":
37780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        read_docstrings(_LANGUAGE)
37790a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoexcept ImportError:
37800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    print "Cannot find docsdict for", _LANGUAGE
37810a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoexcept:
37820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    print ("Unknown Error when trying to import %s-docstring-dictionary" %
37830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                                  _LANGUAGE)
37840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
37850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
37860a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef getmethparlist(ob):
37870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    "Get strings describing the arguments for the given object"
37880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    argText1 = argText2 = ""
37890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # bit of a hack for methods - turn it into a function
37900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # but we drop the "self" param.
37910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if type(ob)==types.MethodType:
37920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fob = ob.im_func
37930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        argOffset = 1
37940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    else:
37950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fob = ob
37960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        argOffset = 0
37970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Try and build one for Python defined functions
37980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if type(fob) in [types.FunctionType, types.LambdaType]:
37990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
38000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            counter = fob.func_code.co_argcount
38010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            items2 = list(fob.func_code.co_varnames[argOffset:counter])
38020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            realArgs = fob.func_code.co_varnames[argOffset:counter]
38030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            defaults = fob.func_defaults or []
38040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            defaults = list(map(lambda name: "=%s" % repr(name), defaults))
38050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            defaults = [""] * (len(realArgs)-len(defaults)) + defaults
38060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            items1 = map(lambda arg, dflt: arg+dflt, realArgs, defaults)
38070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if fob.func_code.co_flags & 0x4:
38080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                items1.append("*"+fob.func_code.co_varnames[counter])
38090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                items2.append("*"+fob.func_code.co_varnames[counter])
38100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                counter += 1
38110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if fob.func_code.co_flags & 0x8:
38120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                items1.append("**"+fob.func_code.co_varnames[counter])
38130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                items2.append("**"+fob.func_code.co_varnames[counter])
38140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            argText1 = ", ".join(items1)
38150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            argText1 = "(%s)" % argText1
38160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            argText2 = ", ".join(items2)
38170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            argText2 = "(%s)" % argText2
38180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except:
38190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
38200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return argText1, argText2
38210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
38220a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _turtle_docrevise(docstr):
38230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """To reduce docstrings from RawTurtle class for functions
38240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
38250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    import re
38260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if docstr is None:
38270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return None
38280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    turtlename = _CFG["exampleturtle"]
38290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    newdocstr = docstr.replace("%s." % turtlename,"")
38300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    parexp = re.compile(r' \(.+ %s\):' % turtlename)
38310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    newdocstr = parexp.sub(":", newdocstr)
38320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return newdocstr
38330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
38340a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef _screen_docrevise(docstr):
38350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """To reduce docstrings from TurtleScreen class for functions
38360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """
38370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    import re
38380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if docstr is None:
38390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return None
38400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    screenname = _CFG["examplescreen"]
38410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    newdocstr = docstr.replace("%s." % screenname,"")
38420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    parexp = re.compile(r' \(.+ %s\):' % screenname)
38430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    newdocstr = parexp.sub(":", newdocstr)
38440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return newdocstr
38450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
38460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao## The following mechanism makes all methods of RawTurtle and Turtle available
38470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao## as functions. So we can enhance, change, add, delete methods to these
38480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao## classes and do not need to change anything here.
38490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
38500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
38510a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofor methodname in _tg_screen_functions:
38520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    pl1, pl2 = getmethparlist(eval('_Screen.' + methodname))
38530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if pl1 == "":
38540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        print ">>>>>>", pl1, pl2
38550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        continue
38560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    defstr = ("def %(key)s%(pl1)s: return _getscreen().%(key)s%(pl2)s" %
38570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   {'key':methodname, 'pl1':pl1, 'pl2':pl2})
38580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    exec defstr
38590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    eval(methodname).__doc__ = _screen_docrevise(eval('_Screen.'+methodname).__doc__)
38600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
38610a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofor methodname in _tg_turtle_functions:
38620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    pl1, pl2 = getmethparlist(eval('Turtle.' + methodname))
38630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    if pl1 == "":
38640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        print ">>>>>>", pl1, pl2
38650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        continue
38660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    defstr = ("def %(key)s%(pl1)s: return _getpen().%(key)s%(pl2)s" %
38670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                   {'key':methodname, 'pl1':pl1, 'pl2':pl2})
38680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    exec defstr
38690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    eval(methodname).__doc__ = _turtle_docrevise(eval('Turtle.'+methodname).__doc__)
38700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
38710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
38720a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodone = mainloop = TK.mainloop
38730a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodel pl1, pl2, defstr
38740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
38750a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == "__main__":
38760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def switchpen():
38770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if isdown():
38780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pu()
38790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
38800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pd()
38810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
38820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def demo1():
38830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Demo of old turtle.py - module"""
38840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        reset()
38850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tracer(True)
38860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        up()
38870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        backward(100)
38880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        down()
38890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # draw 3 squares; the last filled
38900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        width(3)
38910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for i in range(3):
38920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if i == 2:
38930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                fill(1)
38940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for _ in range(4):
38950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                forward(20)
38960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                left(90)
38970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if i == 2:
38980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                color("maroon")
38990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                fill(0)
39000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            up()
39010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            forward(30)
39020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            down()
39030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        width(1)
39040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        color("black")
39050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # move out of the way
39060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tracer(False)
39070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        up()
39080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        right(90)
39090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        forward(100)
39100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        right(90)
39110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        forward(100)
39120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        right(180)
39130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        down()
39140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # some text
39150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        write("startstart", 1)
39160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        write("start", 1)
39170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        color("red")
39180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # staircase
39190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for i in range(5):
39200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            forward(20)
39210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            left(90)
39220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            forward(20)
39230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            right(90)
39240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # filled staircase
39250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tracer(True)
39260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fill(1)
39270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for i in range(5):
39280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            forward(20)
39290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            left(90)
39300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            forward(20)
39310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            right(90)
39320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fill(0)
39330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # more text
39340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
39350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def demo2():
39360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        """Demo of some new features."""
39370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        speed(1)
39380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        st()
39390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pensize(3)
39400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        setheading(towards(0, 0))
39410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        radius = distance(0, 0)/2.0
39420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        rt(90)
39430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for _ in range(18):
39440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            switchpen()
39450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            circle(radius, 10)
39460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        write("wait a moment...")
39470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        while undobufferentries():
39480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            undo()
39490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        reset()
39500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        lt(90)
39510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        colormode(255)
39520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        laenge = 10
39530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pencolor("green")
39540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pensize(3)
39550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        lt(180)
39560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for i in range(-2, 16):
39570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if i > 0:
39580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                begin_fill()
39590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                fillcolor(255-15*i, 0, 15*i)
39600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for _ in range(3):
39610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                fd(laenge)
39620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                lt(120)
39630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            laenge += 10
39640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            lt(15)
39650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            speed((speed()+1)%12)
39660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        end_fill()
39670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
39680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        lt(120)
39690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pu()
39700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fd(70)
39710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        rt(30)
39720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pd()
39730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        color("red","yellow")
39740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        speed(0)
39750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fill(1)
39760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for _ in range(4):
39770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            circle(50, 90)
39780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            rt(90)
39790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            fd(30)
39800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            rt(90)
39810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fill(0)
39820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        lt(90)
39830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pu()
39840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        fd(30)
39850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pd()
39860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        shape("turtle")
39870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
39880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tri = getturtle()
39890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tri.resizemode("auto")
39900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle = Turtle()
39910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle.resizemode("auto")
39920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle.shape("turtle")
39930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle.reset()
39940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle.left(90)
39950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle.speed(0)
39960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle.up()
39970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle.goto(280, 40)
39980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle.lt(30)
39990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle.down()
40000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle.speed(6)
40010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle.color("blue","orange")
40020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        turtle.pensize(2)
40030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tri.speed(6)
40040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        setheading(towards(turtle))
40050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        count = 1
40060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        while tri.distance(turtle) > 4:
40070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            turtle.fd(3.5)
40080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            turtle.lt(0.6)
40090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            tri.setheading(tri.towards(turtle))
40100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            tri.fd(4)
40110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if count % 20 == 0:
40120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                turtle.stamp()
40130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                tri.stamp()
40140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                switchpen()
40150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            count += 1
40160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tri.write("CAUGHT! ", font=("Arial", 16, "bold"), align="right")
40170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tri.pencolor("black")
40180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tri.pencolor("red")
40190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
40200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def baba(xdummy, ydummy):
40210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            clearscreen()
40220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            bye()
40230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
40240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        time.sleep(2)
40250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
40260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        while undobufferentries():
40270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            tri.undo()
40280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            turtle.undo()
40290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tri.fd(50)
40300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tri.write("  Click me!", font = ("Courier", 12, "bold") )
40310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tri.onclick(baba, 1)
40320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
40330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    demo1()
40340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    demo2()
40350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    exitonclick()
4036