Lines Matching refs:turtle
2 :mod:`turtle` --- Turtle graphics for Tk
5 .. module:: turtle
11 from turtle import *
12 turtle = Turtle()
21 Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle``, give it …
22 command ``turtle.forward(15)``, and it moves (on-screen!) 15 pixels in the
24 ``turtle.right(25)``, and it rotates in-place 25 degrees clockwise.
29 The :mod:`turtle` module is an extended reimplementation of the same-named
32 It tries to keep the merits of the old turtle module and to be (nearly) 100%
37 The turtle module provides turtle graphics primitives, in both object-oriented
45 :class:`ScrolledCanvas` as argument. It should be used when :mod:`turtle` is
50 :mod:`turtle` is used as a standalone tool for doing graphics.
70 function derived from a Screen method is called. An (unnamed) turtle object is
219 ``turtle``.
229 Move the turtle forward by the specified *distance*, in the direction the
230 turtle is headed.
234 >>> turtle.position()
236 >>> turtle.forward(25)
237 >>> turtle.position()
239 >>> turtle.forward(-75)
240 >>> turtle.position()
250 Move the turtle backward by *distance*, opposite to the direction the
251 turtle is headed. Do not change the turtle's heading.
256 >>> turtle.goto(0, 0)
260 >>> turtle.position()
262 >>> turtle.backward(30)
263 >>> turtle.position()
272 Turn turtle right by *angle* units. (Units are by default degrees, but
274 orientation depends on the turtle mode, see :func:`mode`.
279 >>> turtle.setheading(22)
283 >>> turtle.heading()
285 >>> turtle.right(45)
286 >>> turtle.heading()
295 Turn turtle left by *angle* units. (Units are by default degrees, but
297 orientation depends on the turtle mode, see :func:`mode`.
302 >>> turtle.setheading(22)
306 >>> turtle.heading()
308 >>> turtle.left(45)
309 >>> turtle.heading()
323 Move turtle to an absolute position. If the pen is down, draw line. Do
324 not change the turtle's orientation.
329 >>> turtle.goto(0, 0)
333 >>> tp = turtle.pos()
336 >>> turtle.setpos(60,30)
337 >>> turtle.pos()
339 >>> turtle.setpos((20,80))
340 >>> turtle.pos()
342 >>> turtle.setpos(tp)
343 >>> turtle.pos()
351 Set the turtle's first coordinate to *x*, leave second coordinate
357 >>> turtle.goto(0, 240)
361 >>> turtle.position()
363 >>> turtle.setx(10)
364 >>> turtle.position()
372 Set the turtle's second coordinate to *y*, leave first coordinate unchanged.
377 >>> turtle.goto(0, 40)
381 >>> turtle.position()
383 >>> turtle.sety(-10)
384 >>> turtle.position()
393 Set the orientation of the turtle to *to_angle*. Here are some common
407 >>> turtle.setheading(90)
408 >>> turtle.heading()
414 Move turtle to the origin -- coordinates (0,0) -- and set its heading to
420 >>> turtle.setheading(90)
421 >>> turtle.goto(0, -10)
425 >>> turtle.heading()
427 >>> turtle.position()
429 >>> turtle.home()
430 >>> turtle.position()
432 >>> turtle.heading()
443 the turtle; *extent* -- an angle -- determines which part of the circle
448 turtle is changed by the amount of *extent*.
456 >>> turtle.home()
457 >>> turtle.position()
459 >>> turtle.heading()
461 >>> turtle.circle(50)
462 >>> turtle.position()
464 >>> turtle.heading()
466 >>> turtle.circle(120, 180) # draw a semicircle
467 >>> turtle.position()
469 >>> turtle.heading()
484 >>> turtle.home()
485 >>> turtle.dot()
486 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
487 >>> turtle.position()
489 >>> turtle.heading()
495 Stamp a copy of the turtle shape onto the canvas at the current turtle
501 >>> turtle.color("blue")
502 >>> turtle.stamp()
504 >>> turtle.fd(50)
516 >>> turtle.position()
518 >>> turtle.color("blue")
519 >>> astamp = turtle.stamp()
520 >>> turtle.fd(50)
521 >>> turtle.position()
523 >>> turtle.clearstamp(astamp)
524 >>> turtle.position()
532 Delete all or first/last *n* of turtle's stamps. If *n* is ``None``, delete
539 ... turtle.stamp(); turtle.fd(30)
548 >>> turtle.clearstamps(2)
549 >>> turtle.clearstamps(-2)
550 >>> turtle.clearstamps()
555 Undo (repeatedly) the last turtle action(s). Number of available
561 ... turtle.fd(50); turtle.lt(80)
564 ... turtle.undo()
571 Set the turtle's speed to an integer value in the range 0..10. If no
584 and turtle turning.
587 place. forward/back makes turtle jump and likewise left/right make the
588 turtle turn instantly.
592 >>> turtle.speed()
594 >>> turtle.speed('normal')
595 >>> turtle.speed()
597 >>> turtle.speed(9)
598 >>> turtle.speed()
608 Return the turtle's current location (x,y) (as a :class:`Vec2D` vector).
612 >>> turtle.pos()
618 :param x: a number or a pair/vector of numbers or a turtle instance
621 Return the angle between the line from turtle position to position specified
622 by (x,y), the vector or the other turtle. This depends on the turtle's start
627 >>> turtle.goto(10, 10)
628 >>> turtle.towards(0,0)
634 Return the turtle's x coordinate.
638 >>> turtle.home()
639 >>> turtle.left(50)
640 >>> turtle.forward(100)
641 >>> turtle.pos()
643 >>> print turtle.xcor()
649 Return the turtle's y coordinate.
653 >>> turtle.home()
654 >>> turtle.left(60)
655 >>> turtle.forward(100)
656 >>> print turtle.pos()
658 >>> print turtle.ycor()
664 Return the turtle's current heading (value depends on the turtle mode, see
669 >>> turtle.home()
670 >>> turtle.left(67)
671 >>> turtle.heading()
677 :param x: a number or a pair/vector of numbers or a turtle instance
680 Return the distance from the turtle to (x,y), the given vector, or the given
681 other turtle, in turtle step units.
685 >>> turtle.home()
686 >>> turtle.distance(30,40)
688 >>> turtle.distance((30,40))
692 >>> turtle.distance(joe)
708 >>> turtle.home()
709 >>> turtle.left(90)
710 >>> turtle.heading()
715 >>> turtle.degrees(400.0)
716 >>> turtle.heading()
718 >>> turtle.degrees(360)
719 >>> turtle.heading()
730 >>> turtle.home()
731 >>> turtle.left(90)
732 >>> turtle.heading()
734 >>> turtle.radians()
735 >>> turtle.heading()
741 >>> turtle.degrees(360)
775 >>> turtle.pensize()
777 >>> turtle.pensize(10) # from here on lines of width 10 are drawn
807 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
808 >>> sorted(turtle.pen().items())
812 >>> penstate=turtle.pen()
813 >>> turtle.color("yellow", "")
814 >>> turtle.penup()
815 >>> sorted(turtle.pen().items())
819 >>> turtle.pen(penstate, fillcolor="green")
820 >>> sorted(turtle.pen().items())
832 >>> turtle.penup()
833 >>> turtle.isdown()
835 >>> turtle.pendown()
836 >>> turtle.isdown()
874 >>> turtle.pencolor()
876 >>> turtle.pencolor("brown")
877 >>> turtle.pencolor()
880 >>> turtle.pencolor(tup)
881 >>> turtle.pencolor()
884 >>> turtle.pencolor()
886 >>> turtle.pencolor('#32c18f')
887 >>> turtle.pencolor()
920 >>> turtle.fillcolor("violet")
921 >>> turtle.fillcolor()
923 >>> col = turtle.pencolor()
926 >>> turtle.fillcolor(col)
927 >>> turtle.fillcolor()
929 >>> turtle.fillcolor('#ffffff')
930 >>> turtle.fillcolor()
959 >>> turtle.color("red", "green")
960 >>> turtle.color()
976 >>> turtle.home()
988 >>> turtle.fill(True)
990 ... turtle.forward(100)
991 ... turtle.left(120)
993 >>> turtle.fill(False)
1008 >>> turtle.color("black", "red")
1009 >>> turtle.begin_fill()
1010 >>> turtle.circle(80)
1011 >>> turtle.end_fill()
1019 Delete the turtle's drawings from the screen, re-center the turtle and set
1024 >>> turtle.goto(0,-22)
1025 >>> turtle.left(100)
1026 >>> turtle.position()
1028 >>> turtle.heading()
1030 >>> turtle.reset()
1031 >>> turtle.position()
1033 >>> turtle.heading()
1039 Delete the turtle's drawings from the screen. Do not move turtle. State and
1040 position of the turtle as well as drawings of other turtles are not affected.
1050 Write text - the string representation of *arg* - at the current turtle
1055 >>> turtle.write("Home = ", True, align="center")
1056 >>> turtle.write((0,0), True)
1068 Make the turtle invisible. It's a good idea to do this while you're in the
1069 middle of doing some complex drawing, because hiding the turtle speeds up the
1074 >>> turtle.hideturtle()
1080 Make the turtle visible.
1084 >>> turtle.showturtle()
1091 >>> turtle.hideturtle()
1092 >>> turtle.isvisible()
1094 >>> turtle.showturtle()
1095 >>> turtle.isvisible()
1106 Set turtle shape to shape with given *name* or, if name is not given, return
1109 "turtle", "circle", "square", "triangle", "classic". To learn about how to
1114 >>> turtle.shape()
1116 >>> turtle.shape("turtle")
1117 >>> turtle.shape()
1118 'turtle'
1129 - "auto": adapts the appearance of the turtle corresponding to the value of pensize.
1130 - "user": adapts the appearance of the turtle according to the values of
1133 - "noresize": no adaption of the turtle's appearance takes place.
1139 >>> turtle.resizemode()
1141 >>> turtle.resizemode("auto")
1142 >>> turtle.resizemode()
1154 resizemode to "user". If and only if resizemode is set to "user", the turtle
1162 >>> turtle.shapesize()
1164 >>> turtle.resizemode("user")
1165 >>> turtle.shapesize(5, 5, 12)
1166 >>> turtle.shapesize()
1168 >>> turtle.shapesize(outline=8)
1169 >>> turtle.shapesize()
1178 change the turtle's heading (direction of movement).
1182 >>> turtle.reset()
1183 >>> turtle.shape("circle")
1184 >>> turtle.shapesize(5,2)
1185 >>> turtle.tilt(30)
1186 >>> turtle.fd(50)
1187 >>> turtle.tilt(30)
1188 >>> turtle.fd(50)
1196 regardless of its current tilt-angle. *Do not* change the turtle's heading
1201 >>> turtle.reset()
1202 >>> turtle.shape("circle")
1203 >>> turtle.shapesize(5,2)
1204 >>> turtle.settiltangle(45)
1205 >>> turtle.fd(50)
1206 >>> turtle.settiltangle(-45)
1207 >>> turtle.fd(50)
1213 turtleshape and the heading of the turtle (its direction of movement).
1217 >>> turtle.reset()
1218 >>> turtle.shape("circle")
1219 >>> turtle.shapesize(5,2)
1220 >>> turtle.tilt(45)
1221 >>> turtle.tiltangle()
1236 Bind *fun* to mouse-click events on this turtle. If *fun* is ``None``,
1237 existing bindings are removed. Example for the anonymous turtle, i.e. the
1245 >>> onclick(turn) # Now clicking into the turtle will turn it.
1257 Bind *fun* to mouse-button-release events on this turtle. If *fun* is
1268 >>> turtle = MyTurtle()
1269 >>> turtle.onclick(turtle.glow) # clicking on turtle turns fillcolor red,
1270 >>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent.
1281 Bind *fun* to mouse-move events on this turtle. If *fun* is ``None``,
1284 Remark: Every sequence of mouse-move-events on a turtle is preceded by a
1285 mouse-click event on that turtle.
1289 >>> turtle.ondrag(turtle.goto)
1299 statement in a turtle graphics program.
1301 >>> turtle.mainloop()
1309 Start recording the vertices of a polygon. Current turtle position is first
1315 Stop recording the vertices of a polygon. Current turtle position is last
1325 >>> turtle.home()
1326 >>> turtle.begin_poly()
1327 >>> turtle.fd(100)
1328 >>> turtle.left(20)
1329 >>> turtle.fd(30)
1330 >>> turtle.left(60)
1331 >>> turtle.fd(50)
1332 >>> turtle.end_poly()
1333 >>> p = turtle.get_poly()
1339 Create and return a clone of the turtle with same position, heading and
1340 turtle properties.
1352 return the "anonymous turtle":
1359 <turtle.Turtle object at 0x...>
1364 Return the :class:`TurtleScreen` object the turtle is drawing on.
1369 >>> ts = turtle.getscreen()
1371 <turtle._Screen object at 0x...>
1380 given size is installed. *size* gives the maximum number of turtle actions
1386 >>> turtle.setundobuffer(42)
1419 To use compound turtle shapes, which consist of several polygons of different
1542 e.g. to search for an erroneously escaped turtle ;-)
1604 Turn turtle animation on/off and set delay for update drawings. If *n* is
1667 named turtle:
1671 >>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will
1672 >>> # make the turtle move to the clicked point.
1696 >>> f() ### makes the turtle march around
1707 Set turtle mode ("standard", "logo" or "world") and perform reset. If mode
1710 Mode "standard" is compatible with old :mod:`turtle`. Mode "logo" is
1711 compatible with most Logo turtle graphics. Mode "world" uses user-defined
1716 Mode Initial turtle heading positive angles
1724 >>> mode("logo") # resets turtle heading to north
1739 >>> turtle.pencolor(240, 160, 80)
1748 >>> turtle.pencolor(240,160,80)
1760 <turtle.ScrolledCanvas instance at 0x...>
1765 Return a list of names of all currently available turtle shapes.
1770 ['arrow', 'blank', 'circle', ..., 'turtle']
1781 >>> screen.register_shape("turtle.gif")
1784 Image shapes *do not* rotate when turning the turtle, so they do not
1785 display the heading of the turtle!
1797 Add a turtle shape to TurtleScreen's shapelist. Only thusly registered
1807 >>> for turtle in screen.turtles():
1808 ... turtle.color("red")
1813 Return the height of the turtle window. ::
1821 Return the width of the turtle window. ::
1845 :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the
1853 :file:`turtle.cfg` file.
1876 :param titlestring: a string that is shown in the titlebar of the turtle
1879 Set title of turtle window to *titlestring*.
1883 >>> screen.title("Welcome to the turtle zoo!")
1886 The public classes of the module :mod:`turtle`
1896 Create a turtle. The turtle has all methods described above as "methods of
1964 turtle graphics. May be useful for turtle graphics programs too. Derived
1993 Help on method bgcolor in module turtle:
1995 bgcolor(self, *args) unbound turtle.Screen method
2010 Help on method penup in module turtle:
2012 penup(self) unbound turtle.Turtle method
2019 >>> turtle.penup()
2025 Help on function bgcolor in module turtle:
2043 Help on function penup in module turtle:
2072 turtle graphics classes). The docstring dictionary will be written to the
2076 If you (or your students) want to use :mod:`turtle` with online help in your
2080 If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary
2092 old turtle module in order to retain best possible compatibility with it.
2096 you can prepare a configuration file ``turtle.cfg`` which will be read at import
2099 The built in configuration would correspond to the following turtle.cfg::
2117 exampleturtle = turtle
2128 - *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more
2130 - If you want to use no fillcolor (i.e. make the turtle transparent), you have
2133 - If you want to reflect the turtle its state, you have to use ``resizemode =
2137 present on the import path, e.g. in the same directory as :mod:`turtle`.
2146 There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is
2150 The :file:`Demo/turtle` directory contains a :file:`turtle.cfg` file. You can
2159 :file:`Demo/turtle` directory in the source distribution.
2164 :mod:`turtle`
2169 use of two canvases with the turtle module. Therefore it only can be run
2171 - There is a :file:`turtle.cfg` file in this directory, which also serves as an
2207 | peace | elementary | turtle: appearance |
2220 | | article on turtle graphics | :func:`undo` |
2230 >>> for turtle in turtles():
2231 ... turtle.reset()
2232 >>> turtle.penup()
2233 >>> turtle.goto(-200,25)
2234 >>> turtle.pendown()
2235 >>> turtle.write("No one expects the Spanish Inquisition!",
2237 >>> turtle.penup()
2238 >>> turtle.goto(-100,-50)
2239 >>> turtle.pendown()
2240 >>> turtle.write("Our two chief Turtles are...",
2242 >>> turtle.penup()
2243 >>> turtle.goto(-450,-75)
2244 >>> turtle.write(str(turtles()))