Home | History | Annotate | Download | only in lib-tk

Lines Matching refs:Turtle

2 # turtle.py: a Tkinter based turtle graphics module for Python
26 Turtle graphics is a popular way for introducing programming to
30 Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle``, give it
31 the command turtle.forward(15), and it moves (on-screen!) 15 pixels in
33 command turtle.right(25), and it rotates in-place 25 degrees clockwise.
38 ----- turtle.py
40 This module is an extended reimplementation of turtle.py from the
43 It tries to keep the merits of turtle.py and to be (nearly) 100%
51 - Better animation of the turtle movements, especially of turning the
52 turtle. So the turtles can more easily be used as a visual feedback
55 - Different turtle shapes, gif-images as turtle shapes, user defined
56 and user controllable turtle shapes, among them compound
57 (multicolored) shapes. Turtle shapes can be stretched and tilted, which
60 - Fine control over turtle movement and screen updates via delay(),
67 turtle graphics.
75 extended interactively as needed while playing around with the turtle(s).
89 configured by means of a turtle.cfg configuration file.
90 The default configuration mimics the appearance of the old turtle module.
103 _ver = "turtle 1.0b1 - for Python 2.6 - 30. 5. 2008, 18:08"
116 from math import * ## for compatibility with old turtle module
119 'RawTurtle', 'Turtle', 'RawPen', 'Pen', 'Shape', 'Vec2D']
167 "exampleturtle": "turtle",
169 "title": "Python Turtle Graphics",
216 If there is a turtle.cfg file in the current working directory,
219 turtle.cfg and read it from the import-directory, where
220 turtle.py is located.
226 default_cfg = "turtle.cfg"
258 for implementing turtle graphics.
259 May be useful for turtle graphics programs also.
301 ### From here up to line : Tkinter - Interface for turtle.py ###
357 using turtle graphics functions or the Turtle class.
484 Interface between Tkinter and turtle.py.
486 To port turtle.py to some different graphics toolkit
630 """Bind fun to mouse-click event on turtle.
645 """Bind fun to mouse-button-release event on turtle.
650 If a turtle is clicked, first _onclick-event will be performed,
664 """Bind fun to mouse-move-event (with pressed mouse button) on turtle.
669 Every sequence of mouse-move-events on a turtle is preceded by a
670 mouse-click event on that turtle.
690 If a turtle is clicked, first _onclick-event will be performed,
755 >>> from turtle import *
756 >>> getscreen()._pointlist(getturtle().turtle._item)
795 """ Return the width and height of the turtle window.
814 This stops execution of a turtle graphics script.
815 Main purpose: use in the Demo-Viewer turtle.Demo.py.
924 "turtle" : Shape("polygon", ((0,16), (-2,14), (-1,10), (-4,7),
978 Turtle._pen = None
981 """Set turtle-mode ('standard', 'logo' or 'world') and perform reset.
986 Mode 'standard' is compatible with turtle.py.
987 Mode 'logo' is compatible with most Logo-Turtle-Graphics.
992 Mode Initial turtle heading positive angles
998 >>> mode('logo') # resets turtle heading to north
1006 raise TurtleGraphicsError("No turtle-graphics-mode %s" % mode)
1054 """Adds a turtle shape to TurtleScreen's shapelist.
1059 !! Image-shapes DO NOT rotate when turning the turtle,
1060 !! so they do not display the heading of the turtle!
1069 call: register_shape("turtle.gif")
1156 for turtle in self._turtles:
1157 turtle._setmode(self._mode)
1158 turtle.reset()
1165 [<turtle.Turtle object at 0x00E11FB0>]
1193 """Turns turtle animation on/off and set delay for update drawings.
1256 """ Return the width of the turtle window.
1265 """ Return the height of the turtle window.
1281 <turtle.ScrolledCanvas instance at 0x010742D8>
1286 """Return a list of names of all currently available turtle shapes.
1292 ['arrow', 'blank', 'circle', ... , 'turtle']
1305 and a Turtle instance named turtle):
1309 >>> # make the turtle move to the clicked point.
1333 Subsequently the turtle can be moved by repeatedly pressing
1372 >>> f() # makes the turtle marching around
1414 Example (for a Turtle instance named turtle):
1415 >>> turtle.screensize(2000,1500)
1416 >>> # e. g. to search for an erroneously escaped turtle ;-)
1427 Implements methods for turtle movement.
1448 """reset turtle to its initial values
1456 """Set turtle-mode to 'standard', 'world' or 'logo'.
1489 Example (for a Turtle instance named turtle):
1490 >>> turtle.left(90)
1491 >>> turtle.heading()
1496 >>> turtle.degrees(400.0)
1497 >>> turtle.heading()
1508 Example (for a Turtle instance named turtle):
1509 >>> turtle.heading()
1511 >>> turtle.radians()
1512 >>> turtle.heading()
1518 """move turtle forward by specified distance"""
1523 """Turn turtle counterclockwise by specified angle if angle > 0."""
1528 """move turtle to position end."""
1532 """Move the turtle forward by the specified distance.
1539 Move the turtle forward by the specified distance, in the direction
1540 the turtle is headed.
1542 Example (for a Turtle instance named turtle):
1543 >>> turtle.position()
1545 >>> turtle.forward(25)
1546 >>> turtle.position()
1548 >>> turtle.forward(-75)
1549 >>> turtle.position()
1555 """Move the turtle backward by distance.
1562 Move the turtle backward by distance ,opposite to the direction the
1563 turtle is headed. Do not change the turtle's heading.
1565 Example (for a Turtle instance named turtle):
1566 >>> turtle.position()
1568 >>> turtle.backward(30)
1569 >>> turtle.position()
1575 """Turn turtle right by angle units.
1582 Turn turtle right by angle units. (Units are by default degrees,
1586 Example (for a Turtle instance named turtle):
1587 >>> turtle.heading()
1589 >>> turtle.right(45)
1590 >>> turtle.heading()
1596 """Turn turtle left by angle units.
1603 Turn turtle left by angle units. (Units are by default degrees,
1607 Example (for a Turtle instance named turtle):
1608 >>> turtle.heading()
1610 >>> turtle.left(45)
1611 >>> turtle.heading()
1617 """Return the turtle's current location (x,y), as a Vec2D-vector.
1623 Example (for a Turtle instance named turtle):
1624 >>> turtle.pos()
1630 """ Return the turtle's x coordinate.
1634 Example (for a Turtle instance named turtle):
1636 >>> turtle.left(60)
1637 >>> turtle.forward(100)
1638 >>> print turtle.xcor()
1644 """ Return the turtle's y coordinate
1648 Example (for a Turtle instance named turtle):
1650 >>> turtle.left(60)
1651 >>> turtle.forward(100)
1652 >>> print turtle.ycor()
1659 """Move turtle to an absolute position.
1671 Move turtle to an absolute position. If the pen is down,
1672 a line will be drawn. The turtle's orientation does not change.
1674 Example (for a Turtle instance named turtle):
1675 >>> tp = turtle.pos()
1678 >>> turtle.setpos(60,30)
1679 >>> turtle.pos()
1681 >>> turtle.setpos((20,80))
1682 >>> turtle.pos()
1684 >>> turtle.setpos(tp)
1685 >>> turtle.pos()
1694 """Move turtle to the origin - coordinates (0,0).
1698 Move turtle to the origin - coordinates (0,0) and set its
1701 Example (for a Turtle instance named turtle):
1702 >>> turtle.home()
1708 """Set the turtle's first coordinate to x
1713 Set the turtle's first coordinate to x, leave second coordinate
1716 Example (for a Turtle instance named turtle):
1717 >>> turtle.position()
1719 >>> turtle.setx(10)
1720 >>> turtle.position()
1726 """Set the turtle's second coordinate to y
1731 Set the turtle's first coordinate to x, second coordinate remains
1734 Example (for a Turtle instance named turtle):
1735 >>> turtle.position()
1737 >>> turtle.sety(-10)
1738 >>> turtle.position()
1744 """Return the distance from the turtle to (x,y) in turtle step units.
1747 x -- a number or a pair/vector of numbers or a turtle instance
1753 --or: distance(mypen) # where mypen is another turtle
1755 Example (for a Turtle instance named turtle):
1756 >>> turtle.pos()
1758 >>> turtle.distance(30,40)
1760 >>> pen = Turtle()
1762 >>> turtle.distance(pen)
1776 """Return the angle of the line from the turtle's position to (x, y).
1779 x -- a number or a pair/vector of numbers or a turtle instance
1785 --or: distance(mypen) # where mypen is another turtle
1787 Return the angle, between the line from turtle-position to position
1788 specified by x, y and the turtle's start orientation. (Depends on
1791 Example (for a Turtle instance named turtle):
1792 >>> turtle.pos()
1794 >>> turtle.towards(0,0)
1811 """ Return the turtle's current heading.
1815 Example (for a Turtle instance named turtle):
1816 >>> turtle.left(67)
1817 >>> turtle.heading()
1826 """Set the orientation of the turtle to to_angle.
1833 Set the orientation of the turtle to to_angle.
1843 Example (for a Turtle instance named turtle):
1844 >>> turtle.setheading(90)
1845 >>> turtle.heading()
1862 of the turtle; extent - an angle - determines which part of the
1867 the direction of the turtle is changed by the amount of extent.
1879 Example (for a Turtle instance named turtle):
1880 >>> turtle.circle(50)
1881 >>> turtle.circle(120, 180) # semicircle
1965 - "auto" adapts the appearance of the turtle
1967 - "user" adapts the appearance of the turtle according to the
1970 - "noresize" no adaption of the turtle's appearance takes place.
1975 Examples (for a Turtle instance named turtle):
1976 >>> turtle.resizemode("noresize")
1977 >>> turtle.resizemode()
1999 Example (for a Turtle instance named turtle):
2000 >>> turtle.pensize()
2002 >>> turtle.pensize(10) # from here on lines of width 10 are drawn
2016 Example (for a Turtle instance named turtle):
2017 >>> turtle.penup()
2030 Example (for a Turtle instance named turtle):
2031 >>> turtle.pendown()
2042 Example (for a Turtle instance named turtle):
2043 >>> turtle.penup()
2044 >>> turtle.isdown()
2046 >>> turtle.pendown()
2047 >>> turtle.isdown()
2053 """ Return or set the turtle's speed.
2058 Set the turtle's speed to an integer value in the range 0 .. 10.
2070 line drawing and turtle turning.
2073 speed = 0 : *no* animation takes place. forward/back makes turtle jump
2074 and likewise left/right make the turtle turn instantly.
2076 Example (for a Turtle instance named turtle):
2077 >>> turtle.speed(3)
2113 Example (for a Turtle instance named turtle):
2114 >>> turtle.color('red', 'green')
2115 >>> turtle.color()
2158 Example (for a Turtle instance named turtle):
2159 >>> turtle.pencolor('brown')
2161 >>> turtle.pencolor(tup)
2162 >>> turtle.pencolor()
2195 Example (for a Turtle instance named turtle):
2196 >>> turtle.fillcolor('violet')
2197 >>> col = turtle.pencolor()
2198 >>> turtle.fillcolor(col)
2199 >>> turtle.fillcolor(0, .5, 0)
2210 """Makes the turtle visible.
2216 Example (for a Turtle instance named turtle):
2217 >>> turtle.hideturtle()
2218 >>> turtle.showturtle()
2223 """Makes the turtle invisible.
2231 the turtle speeds up the drawing observably.
2233 Example (for a Turtle instance named turtle):
2234 >>> turtle.hideturtle()
2239 """Return True if the Turtle is shown, False if it's hidden.
2243 Example (for a Turtle instance named turtle):
2244 >>> turtle.hideturtle()
2245 >>> print turtle.isvisible():
2277 Examples (for a Turtle instance named turtle):
2278 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
2279 >>> turtle.pen()
2283 >>> penstate=turtle.pen()
2284 >>> turtle.color("yellow","")
2285 >>> turtle.penup()
2286 >>> turtle.pen()
2386 """Helper class: Datatype to store Turtle attributes
2449 self.turtle = _TurtleImage(screen, shape)
2464 """Delete the turtle's drawings and restore its default values.
2468 Delete the turtle's drawings from the screen, re-center the turtle
2471 Example (for a Turtle instance named turtle):
2472 >>> turtle.position()
2474 >>> turtle.heading()
2476 >>> turtle.reset()
2477 >>> turtle.position()
2479 >>> turtle.heading()
2495 Size gives the maximum number of turtle-actions that can be undone
2499 Example (for a Turtle instance named turtle):
2500 >>> turtle.setundobuffer(42)
2512 Example (for a Turtle instance named turtle):
2535 """Delete the turtle's drawings from the screen. Do not move turtle.
2539 Delete the turtle's drawings from the screen. Do not move turtle.
2540 State and position of the turtle as well as drawings of other
2543 Examples (for a Turtle instance named turtle):
2544 >>> turtle.clear()
2558 """Perform a Turtle-data update.
2576 """Turns turtle animation on/off and set delay for update drawings.
2586 Example (for a Turtle instance named turtle):
2587 >>> turtle.tracer(8, 25)
2590 ... turtle.fd(dist)
2591 ... turtle.rt(90)
2618 """Create and return a clone of the turtle.
2622 Create and return a clone of the turtle with same position, heading
2623 and turtle properties.
2625 Example (for a Turtle instance named mick):
2626 mick = Turtle()
2632 turtle = self.turtle
2634 self.turtle = None # too make self deepcopy-able
2639 self.turtle = turtle
2642 q.turtle = _TurtleImage(screen, self.turtle.shapeIndex)
2645 ttype = screen._shapes[self.turtle.shapeIndex]._type
2647 q.turtle._item = screen._createpoly()
2649 q.turtle._item = screen._createimage(screen._shapes["blank"]._data)
2651 q.turtle._item = [screen._createpoly() for item in
2652 screen._shapes[self.turtle.shapeIndex]._data]
2658 """Set turtle shape to shape with given name / return current shapename.
2663 Set turtle shape to shape with given name or, if name is not given,
2667 'arrow', 'turtle', 'circle', 'square', 'triangle', 'classic'.
2670 Example (for a Turtle instance named turtle):
2671 >>> turtle.shape()
2673 >>> turtle.shape("turtle")
2674 >>> turtle.shape()
2675 'turtle'
2678 return self.turtle.shapeIndex
2681 self.turtle._setshape(name)
2685 """Set/return turtle's stretchfactors/outline. Set resizemode to "user".
2694 If and only if resizemode is set to "user", the turtle will be displayed
2700 Examples (for a Turtle instance named turtle):
2701 >>> turtle.resizemode("user")
2702 >>> turtle.shapesize(5, 5, 12)
2703 >>> turtle.shapesize(outline=8)
2729 regardless of its current tilt-angle. DO NOT change the turtle's
2733 Examples (for a Turtle instance named turtle):
2734 >>> turtle.shape("circle")
2735 >>> turtle.shapesize(5,2)
2736 >>> turtle.settiltangle(45)
2738 >>> turtle.fd(50)
2739 >>> turtle.settiltangle(-45)
2741 >>> turtle.fd(50)
2753 orientation of the turtleshape and the heading of the turtle
2756 Examples (for a Turtle instance named turtle):
2757 >>> turtle.shape("circle")
2758 >>> turtle.shapesize(5,2)
2759 >>> turtle.tilt(45)
2760 >>> turtle.tiltangle()
2772 but do NOT change the turtle's heading (direction of movement).
2774 Examples (for a Turtle instance named turtle):
2775 >>> turtle.shape("circle")
2776 >>> turtle.shapesize(5,2)
2777 >>> turtle.tilt(30)
2778 >>> turtle.fd(50)
2779 >>> turtle.tilt(30)
2780 >>> turtle.fd(50)
2797 """Manages the correct rendering of the turtle with respect to
2800 shape = screen._shapes[self.turtle.shapeIndex]
2802 titem = self.turtle._item
2856 Stamp a copy of the turtle shape onto the canvas at the current
2857 turtle position. Return a stamp_id for that stamp, which can be
2860 Example (for a Turtle instance named turtle):
2861 >>> turtle.color("blue")
2862 >>> turtle.stamp()
2864 >>> turtle.fd(50)
2867 shape = screen._shapes[self.turtle.shapeIndex]
2939 Example (for a Turtle instance named turtle):
2940 >>> turtle.color("blue")
2941 >>> astamp = turtle.stamp()
2942 >>> turtle.fd(50)
2943 >>> turtle.clearstamp(astamp)
2949 """Delete all or first/last n of turtle's stamps.
2958 Example (for a Turtle instance named turtle):
2960 ... turtle.stamp(); turtle.fd(30)
2962 >>> turtle.clearstamps(2)
2963 >>> turtle.clearstamps(-2)
2964 >>> turtle.clearstamps()
2978 if pen is down. All other methodes for turtle movement depend
3015 # Turtle now at end,
3074 # Turtle now at position old,
3139 Example (for a Turtle instance named turtle):
3140 >>> turtle.fill(True)
3141 >>> turtle.forward(100)
3142 >>> turtle.left(90)
3143 >>> turtle.forward(100)
3144 >>> turtle.left(90)
3145 >>> turtle.forward(100)
3146 >>> turtle.left(90)
3147 >>> turtle.forward(100)
3148 >>> turtle.fill(False)
3184 Example (for a Turtle instance named turtle):
3185 >>> turtle.begin_fill()
3186 >>> turtle.forward(100)
3187 >>> turtle.left(90)
3188 >>> turtle.forward(100)
3189 >>> turtle.left(90)
3190 >>> turtle.forward(100)
3191 >>> turtle.left(90)
3192 >>> turtle.forward(100)
3193 >>> turtle.end_fill()
3202 Example (for a Turtle instance named turtle):
3203 >>> turtle.begin_fill()
3204 >>> turtle.forward(100)
3205 >>> turtle.left(90)
3206 >>> turtle.forward(100)
3207 >>> turtle.left(90)
3208 >>> turtle.forward(100)
3209 >>> turtle.left(90)
3210 >>> turtle.forward(100)
3211 >>> turtle.end_fill()
3225 Example (for a Turtle instance named turtle):
3226 >>> turtle.dot()
3227 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
3277 """Write text at the current turtle position.
3286 turtle position according to align ("left", "center" or right")
3291 Example (for a Turtle instance named turtle):
3292 >>> turtle.write('Home = ', True, align="center")
3293 >>> turtle.write((0,0), True)
3310 Start recording the vertices of a polygon. Current turtle position
3313 Example (for a Turtle instance named turtle):
3314 >>> turtle.begin_poly()
3324 Stop recording the vertices of a polygon. Current turtle position is
3327 Example (for a Turtle instance named turtle):
3328 >>> turtle.end_poly()
3337 Example (for a Turtle instance named turtle):
3338 >>> p = turtle.get_poly()
3339 >>> turtle.register_shape("myFavouriteShape", p)
3346 """Return the TurtleScreen object, the turtle is drawing on.
3350 Return the TurtleScreen object, the turtle is drawing on.
3353 Example (for a Turtle instance named turtle):
3354 >>> ts = turtle.getscreen()
3356 <turtle.TurtleScreen object at 0x0106B770>
3366 Only reasonable use: as a function to return the 'anonymous turtle':
3372 <turtle.Turtle object at 0x0187D810>
3374 [<turtle.Turtle object at 0x0187D810>]
3386 """ Returns the width of the turtle window.
3397 """ Return the height of the turtle window.
3408 """Set delay value which determines speed of turtle animation.
3415 """Bind fun to mouse-click event on this turtle on canvas.
3424 Example for the anonymous turtle, i. e. the procedural way:
3429 >>> onclick(turn) # Now clicking into the turtle will turn it.
3432 self.screen._onclick(self.turtle._item, fun, btn, add)
3436 """Bind fun to mouse-button-release event on this turtle on canvas.
3444 >>> class MyTurtle(Turtle):
3457 self.screen._onrelease(self.turtle._item, fun, btn, add)
3461 """Bind fun to mouse-move event on this turtle on canvas.
3468 Every sequence of mouse-move-events on a turtle is preceded by a
3469 mouse-click event on that turtle.
3471 Example (for a Turtle instance named turtle):
3472 >>> turtle.ondrag(turtle.goto)
3474 Subsequently clicking and dragging a Turtle will move it
3478 self.screen._ondrag(self.turtle._item, fun, btn, add)
3513 """undo (repeatedly) the last turtle action.
3517 undo (repeatedly) the last turtle action.
3521 Example (for a Turtle instance named turtle):
3523 ... turtle.fd(50); turtle.lt(80)
3526 ... turtle.undo()
3551 if Turtle._screen is None:
3552 Turtle._screen = _Screen()
3553 return Turtle._screen
3564 # XXX actually, the turtle demo is injecting root window,
3624 """Set title of turtle-window
3628 turtle graphics window.
3634 >>> screen.title("Welcome to the turtle-zoo!")
3643 Turtle._pen = None
3644 Turtle._screen = None
3667 set to True in turtle.cfg. In this case IDLE's mainloop
3689 class Turtle(RawTurtle):
3692 When a Turtle object is created or a function derived from some
3693 Turtle method is called a TurtleScreen object is automatically created.
3702 if Turtle._screen is None:
3703 Turtle._screen = Screen()
3704 RawTurtle.__init__(self, Turtle._screen,
3709 Pen = Turtle
3712 """Create the 'anonymous' turtle if not already present."""
3713 if Turtle._pen is None:
3714 Turtle._pen = Turtle()
3715 return Turtle._pen
3719 if Turtle._screen is None:
3720 Turtle._screen = Screen()
3721 return Turtle._screen
3730 Has to be called explicitly, (not used by the turtle-graphics classes)
3741 key = "Turtle."+methodname
3761 Turtle and - in revised form -
3846 ## The following mechanism makes all methods of RawTurtle and Turtle available
3862 pl1, pl2 = getmethparlist(eval('Turtle.' + methodname))
3869 eval(methodname).__doc__ = _turtle_docrevise(eval('Turtle.'+methodname).__doc__)
3883 """Demo of old turtle.py - module"""
3986 shape("turtle")
3990 turtle = Turtle()
3991 turtle.resizemode("auto")
3992 turtle.shape("turtle")
3993 turtle.reset()
3994 turtle.left(90)
3995 turtle.speed(0)
3996 turtle.up()
3997 turtle.goto(280, 40)
3998 turtle.lt(30)
3999 turtle.down()
4000 turtle.speed(6)
4001 turtle.color("blue","orange")
4002 turtle.pensize(2)
4004 setheading(towards(turtle))
4006 while tri.distance(turtle) > 4:
4007 turtle.fd(3.5)
4008 turtle.lt(0.6)
4009 tri.setheading(tri.towards(turtle))
4012 turtle.stamp()
4028 turtle.undo()