Lines Matching refs:turtle
2 :mod:`turtle` --- Turtle graphics
5 .. module:: turtle
10 **Source code:** :source:`Lib/turtle.py`
14 from turtle import *
15 turtle = Turtle()
26 Imagine a robotic turtle starting at (0, 0) in the x-y plane. After an ``import turtle``, give it …
27 command ``turtle.forward(15)``, and it moves (on-screen!) 15 pixels in the
29 ``turtle.right(25)``, and it rotates in-place 25 degrees clockwise.
36 .. image:: turtle-star.*
39 .. literalinclude:: ../includes/turtle-star.py
44 The :mod:`turtle` module is an extended reimplementation of the same-named
47 It tries to keep the merits of the old turtle module and to be (nearly) 100%
52 The turtle module provides turtle graphics primitives, in both object-oriented
60 :class:`ScrolledCanvas` as argument. It should be used when :mod:`turtle` is
65 :mod:`turtle` is used as a standalone tool for doing graphics.
85 function derived from a Screen method is called. An (unnamed) turtle object is
239 ``turtle``.
249 Move the turtle forward by the specified *distance*, in the direction the
250 turtle is headed.
255 >>> turtle.position()
257 >>> turtle.forward(25)
258 >>> turtle.position()
260 >>> turtle.forward(-75)
261 >>> turtle.position()
271 Move the turtle backward by *distance*, opposite to the direction the
272 turtle is headed. Do not change the turtle's heading.
277 >>> turtle.goto(0, 0)
282 >>> turtle.position()
284 >>> turtle.backward(30)
285 >>> turtle.position()
294 Turn turtle right by *angle* units. (Units are by default degrees, but
296 orientation depends on the turtle mode, see :func:`mode`.
302 >>> turtle.setheading(22)
307 >>> turtle.heading()
309 >>> turtle.right(45)
310 >>> turtle.heading()
319 Turn turtle left by *angle* units. (Units are by default degrees, but
321 orientation depends on the turtle mode, see :func:`mode`.
327 >>> turtle.setheading(22)
332 >>> turtle.heading()
334 >>> turtle.left(45)
335 >>> turtle.heading()
349 Move turtle to an absolute position. If the pen is down, draw line. Do
350 not change the turtle's orientation.
356 >>> turtle.goto(0, 0)
361 >>> tp = turtle.pos()
364 >>> turtle.setpos(60,30)
365 >>> turtle.pos()
367 >>> turtle.setpos((20,80))
368 >>> turtle.pos()
370 >>> turtle.setpos(tp)
371 >>> turtle.pos()
379 Set the turtle's first coordinate to *x*, leave second coordinate
386 >>> turtle.goto(0, 240)
391 >>> turtle.position()
393 >>> turtle.setx(10)
394 >>> turtle.position()
402 Set the turtle's second coordinate to *y*, leave first coordinate unchanged.
408 >>> turtle.goto(0, 40)
413 >>> turtle.position()
415 >>> turtle.sety(-10)
416 >>> turtle.position()
425 Set the orientation of the turtle to *to_angle*. Here are some common
440 >>> turtle.setheading(90)
441 >>> turtle.heading()
447 Move turtle to the origin -- coordinates (0,0) -- and set its heading to
454 >>> turtle.setheading(90)
455 >>> turtle.goto(0, -10)
460 >>> turtle.heading()
462 >>> turtle.position()
464 >>> turtle.home()
465 >>> turtle.position()
467 >>> turtle.heading()
478 the turtle; *extent* -- an angle -- determines which part of the circle
483 turtle is changed by the amount of *extent*.
492 >>> turtle.home()
493 >>> turtle.position()
495 >>> turtle.heading()
497 >>> turtle.circle(50)
498 >>> turtle.position()
500 >>> turtle.heading()
502 >>> turtle.circle(120, 180) # draw a semicircle
503 >>> turtle.position()
505 >>> turtle.heading()
521 >>> turtle.home()
522 >>> turtle.dot()
523 >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
524 >>> turtle.position()
526 >>> turtle.heading()
532 Stamp a copy of the turtle shape onto the canvas at the current turtle
539 >>> turtle.color("blue")
540 >>> turtle.stamp()
542 >>> turtle.fd(50)
555 >>> turtle.position()
557 >>> turtle.color("blue")
558 >>> astamp = turtle.stamp()
559 >>> turtle.fd(50)
560 >>> turtle.position()
562 >>> turtle.clearstamp(astamp)
563 >>> turtle.position()
571 Delete all or first/last *n* of turtle's stamps. If *n* is ``None``, delete
578 ... turtle.stamp(); turtle.fd(30)
587 >>> turtle.clearstamps(2)
588 >>> turtle.clearstamps(-2)
589 >>> turtle.clearstamps()
594 Undo (repeatedly) the last turtle action(s). Number of available
601 ... turtle.fd(50); turtle.lt(80)
604 ... turtle.undo()
611 Set the turtle's speed to an integer value in the range 0..10. If no
624 and turtle turning.
627 place. forward/back makes turtle jump and likewise left/right make the
628 turtle turn instantly.
633 >>> turtle.speed()
635 >>> turtle.speed('normal')
636 >>> turtle.speed()
638 >>> turtle.speed(9)
639 >>> turtle.speed()
649 Return the turtle's current location (x,y) (as a :class:`Vec2D` vector).
654 >>> turtle.pos()
660 :param x: a number or a pair/vector of numbers or a turtle instance
663 Return the angle between the line from turtle position to position specified
664 by (x,y), the vector or the other turtle. This depends on the turtle's start
670 >>> turtle.goto(10, 10)
671 >>> turtle.towards(0,0)
677 Return the turtle's x coordinate.
682 >>> turtle.home()
683 >>> turtle.left(50)
684 >>> turtle.forward(100)
685 >>> turtle.pos()
687 >>> print(round(turtle.xcor(), 5))
693 Return the turtle's y coordinate.
698 >>> turtle.home()
699 >>> turtle.left(60)
700 >>> turtle.forward(100)
701 >>> print(turtle.pos())
703 >>> print(round(turtle.ycor(), 5))
709 Return the turtle's current heading (value depends on the turtle mode, see
715 >>> turtle.home()
716 >>> turtle.left(67)
717 >>> turtle.heading()
723 :param x: a number or a pair/vector of numbers or a turtle instance
726 Return the distance from the turtle to (x,y), the given vector, or the given
727 other turtle, in turtle step units.
732 >>> turtle.home()
733 >>> turtle.distance(30,40)
735 >>> turtle.distance((30,40))
739 >>> turtle.distance(joe)
756 >>> turtle.home()
757 >>> turtle.left(90)
758 >>> turtle.heading()
763 >>> turtle.degrees(400.0)
764 >>> turtle.heading()
766 >>> turtle.degrees(360)
767 >>> turtle.heading()
779 >>> turtle.home()
780 >>> turtle.left(90)
781 >>> turtle.heading()
783 >>> turtle.radians()
784 >>> turtle.heading()
791 >>> turtle.degrees(360)
826 >>> turtle.pensize()
828 >>> turtle.pensize(10) # from here on lines of width 10 are drawn
859 >>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
860 >>> sorted(turtle.pen().items())
865 >>> penstate=turtle.pen()
866 >>> turtle.color("yellow", "")
867 >>> turtle.penup()
868 >>> sorted(turtle.pen().items())[:3]
870 >>> turtle.pen(penstate, fillcolor="green")
871 >>> sorted(turtle.pen().items())[:3]
881 >>> turtle.penup()
882 >>> turtle.isdown()
884 >>> turtle.pendown()
885 >>> turtle.isdown()
924 >>> turtle.pencolor()
926 >>> turtle.pencolor("brown")
927 >>> turtle.pencolor()
930 >>> turtle.pencolor(tup)
931 >>> turtle.pencolor()
934 >>> turtle.pencolor()
936 >>> turtle.pencolor('#32c18f')
937 >>> turtle.pencolor()
971 >>> turtle.fillcolor("violet")
972 >>> turtle.fillcolor()
974 >>> turtle.pencolor()
976 >>> turtle.fillcolor((50, 193, 143)) # Integers, not floats
977 >>> turtle.fillcolor()
979 >>> turtle.fillcolor('#ffffff')
980 >>> turtle.fillcolor()
1010 >>> turtle.color("red", "green")
1011 >>> turtle.color()
1028 >>> turtle.home()
1037 >>> turtle.begin_fill()
1038 >>> if turtle.filling():
1039 ... turtle.pensize(5)
1041 ... turtle.pensize(3)
1062 >>> turtle.color("black", "red")
1063 >>> turtle.begin_fill()
1064 >>> turtle.circle(80)
1065 >>> turtle.end_fill()
1074 Delete the turtle's drawings from the screen, re-center the turtle and set
1080 >>> turtle.goto(0,-22)
1081 >>> turtle.left(100)
1082 >>> turtle.position()
1084 >>> turtle.heading()
1086 >>> turtle.reset()
1087 >>> turtle.position()
1089 >>> turtle.heading()
1096 Delete the turtle's drawings from the screen. Do not move turtle. State and
1097 position of the turtle as well as drawings of other turtles are not affected.
1107 Write text - the string representation of *arg* - at the current turtle
1112 >>> turtle.write("Home = ", True, align="center")
1113 >>> turtle.write((0,0), True)
1125 Make the turtle invisible. It's a good idea to do this while you're in the
1126 middle of doing some complex drawing, because hiding the turtle speeds up the
1132 >>> turtle.hideturtle()
1138 Make the turtle visible.
1143 >>> turtle.showturtle()
1150 >>> turtle.hideturtle()
1151 >>> turtle.isvisible()
1153 >>> turtle.showturtle()
1154 >>> turtle.isvisible()
1165 Set turtle shape to shape with given *name* or, if name is not given, return
1168 "turtle", "circle", "square", "triangle", "classic". To learn about how to
1174 >>> turtle.shape()
1176 >>> turtle.shape("turtle")
1177 >>> turtle.shape()
1178 'turtle'
1189 - "auto": adapts the appearance of the turtle corresponding to the value of pensize.
1190 - "user": adapts the appearance of the turtle according to the values of
1193 - "noresize": no adaption of the turtle's appearance takes place.
1200 >>> turtle.resizemode()
1202 >>> turtle.resizemode("auto")
1203 >>> turtle.resizemode()
1215 resizemode to "user". If and only if resizemode is set to "user", the turtle
1224 >>> turtle.shapesize()
1226 >>> turtle.resizemode("user")
1227 >>> turtle.shapesize(5, 5, 12)
1228 >>> turtle.shapesize()
1230 >>> turtle.shapesize(outline=8)
1231 >>> turtle.shapesize()
1241 Do *not* change the turtle's heading (direction of movement).
1244 heading of the turtle are sheared.
1249 >>> turtle.shape("circle")
1250 >>> turtle.shapesize(5,2)
1251 >>> turtle.shearfactor(0.5)
1252 >>> turtle.shearfactor()
1261 change the turtle's heading (direction of movement).
1266 >>> turtle.reset()
1267 >>> turtle.shape("circle")
1268 >>> turtle.shapesize(5,2)
1269 >>> turtle.tilt(30)
1270 >>> turtle.fd(50)
1271 >>> turtle.tilt(30)
1272 >>> turtle.fd(50)
1280 regardless of its current tilt-angle. *Do not* change the turtle's heading
1286 >>> turtle.reset()
1287 >>> turtle.shape("circle")
1288 >>> turtle.shapesize(5,2)
1289 >>> turtle.settiltangle(45)
1290 >>> turtle.fd(50)
1291 >>> turtle.settiltangle(-45)
1292 >>> turtle.fd(50)
1303 regardless of its current tilt-angle. Do *not* change the turtle's
1307 turtle (its direction of movement).
1312 >>> turtle.reset()
1313 >>> turtle.shape("circle")
1314 >>> turtle.shapesize(5,2)
1315 >>> turtle.tilt(45)
1316 >>> turtle.tiltangle()
1327 Set or return the current transformation matrix of the turtle shape.
1341 >>> turtle = Turtle()
1342 >>> turtle.shape("square")
1343 >>> turtle.shapesize(4,2)
1344 >>> turtle.shearfactor(-0.5)
1345 >>> turtle.shapetransform()
1357 >>> turtle.shape("square")
1358 >>> turtle.shapetransform(4, -1, 0, 2)
1359 >>> turtle.get_shapepoly()
1375 Bind *fun* to mouse-click events on this turtle. If *fun* is ``None``,
1376 existing bindings are removed. Example for the anonymous turtle, i.e. the
1385 >>> onclick(turn) # Now clicking into the turtle will turn it.
1397 Bind *fun* to mouse-button-release events on this turtle. If *fun* is
1409 >>> turtle = MyTurtle()
1410 >>> turtle.onclick(turtle.glow) # clicking on turtle turns fillcolor red,
1411 >>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent.
1422 Bind *fun* to mouse-move events on this turtle. If *fun* is ``None``,
1425 Remark: Every sequence of mouse-move-events on a turtle is preceded by a
1426 mouse-click event on that turtle.
1431 >>> turtle.ondrag(turtle.goto)
1442 Start recording the vertices of a polygon. Current turtle position is first
1448 Stop recording the vertices of a polygon. Current turtle position is last
1459 >>> turtle.home()
1460 >>> turtle.begin_poly()
1461 >>> turtle.fd(100)
1462 >>> turtle.left(20)
1463 >>> turtle.fd(30)
1464 >>> turtle.left(60)
1465 >>> turtle.fd(50)
1466 >>> turtle.end_poly()
1467 >>> p = turtle.get_poly()
1473 Create and return a clone of the turtle with same position, heading and
1474 turtle properties.
1487 return the "anonymous turtle":
1495 <turtle.Turtle object at 0x...>
1500 Return the :class:`TurtleScreen` object the turtle is drawing on.
1506 >>> ts = turtle.getscreen()
1508 <turtle._Screen object at 0x...>
1517 given size is installed. *size* gives the maximum number of turtle actions
1524 >>> turtle.setundobuffer(42)
1544 To use compound turtle shapes, which consist of several polygons of different
1671 e.g. to search for an erroneously escaped turtle ;-)
1736 Turn turtle animation on/off and set delay for update drawings. If
1824 named turtle:
1829 >>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will
1830 >>> # make the turtle move to the clicked point.
1855 >>> f() ### makes the turtle march around
1863 Must be the last statement in a turtle graphics program.
1865 (No subprocess) - for interactive use of turtle graphics. ::
1913 Set turtle mode ("standard", "logo" or "world") and perform reset. If mode
1916 Mode "standard" is compatible with old :mod:`turtle`. Mode "logo" is
1917 compatible with most Logo turtle graphics. Mode "world" uses user-defined
1922 Mode Initial turtle heading positive angles
1931 >>> mode("logo") # resets turtle heading to north
1947 >>> turtle.pencolor(240, 160, 80)
1956 >>> turtle.pencolor(240,160,80)
1969 <turtle.ScrolledCanvas object ...>
1974 Return a list of names of all currently available turtle shapes.
1980 ['arrow', 'blank', 'circle', ..., 'turtle']
1991 >>> screen.register_shape("turtle.gif")
1994 Image shapes *do not* rotate when turning the turtle, so they do not
1995 display the heading of the turtle!
2008 Add a turtle shape to TurtleScreen's shapelist. Only thusly registered
2019 >>> for turtle in screen.turtles():
2020 ... turtle.color("red")
2025 Return the height of the turtle window. ::
2033 Return the width of the turtle window. ::
2057 :file:`turtle.cfg`. In this case IDLE's own mainloop is active also for the
2065 :file:`turtle.cfg` file.
2089 :param titlestring: a string that is shown in the titlebar of the turtle
2092 Set title of turtle window to *titlestring*.
2097 >>> screen.title("Welcome to the turtle zoo!")
2110 Create a turtle. The turtle has all methods described above as "methods of
2179 turtle graphics. May be useful for turtle graphics programs too. Derived
2208 Help on method bgcolor in module turtle:
2210 bgcolor(self, *args) unbound turtle.Screen method
2225 Help on method penup in module turtle:
2227 penup(self) unbound turtle.Turtle method
2234 >>> turtle.penup()
2240 Help on function bgcolor in module turtle:
2258 Help on function penup in module turtle:
2287 turtle graphics classes). The docstring dictionary will be written to the
2291 If you (or your students) want to use :mod:`turtle` with online help in your
2295 If you have an appropriate entry in your :file:`turtle.cfg` file this dictionary
2307 old turtle module in order to retain best possible compatibility with it.
2311 you can prepare a configuration file ``turtle.cfg`` which will be read at import
2314 The built in configuration would correspond to the following turtle.cfg::
2332 exampleturtle = turtle
2343 - *shape* can be any of the built-in shapes, e.g: arrow, turtle, etc. For more
2345 - If you want to use no fillcolor (i.e. make the turtle transparent), you have
2348 - If you want to reflect the turtle its state, you have to use ``resizemode =
2352 present on the import path, e.g. in the same directory as :mod:`turtle`.
2361 There can be a :file:`turtle.cfg` file in the directory where :mod:`turtle` is
2365 The :file:`Lib/turtledemo` directory contains a :file:`turtle.cfg` file. You can
2374 :synopsis: A viewer for example turtle scripts
2389 - Multiple scripts demonstrating different features of the :mod:`turtle`
2392 - A :file:`turtle.cfg` file which serves as an example of how to write
2403 | | turtle graphics pattern | :func:`update` |
2434 | peace | elementary | turtle: appearance |
2457 | | article on turtle graphics | :func:`undo` |
2491 regular linear transforms is now available for transforming turtle shapes.
2516 >>> for turtle in turtles():
2517 ... turtle.reset()
2518 >>> turtle.penup()
2519 >>> turtle.goto(-200,25)
2520 >>> turtle.pendown()
2521 >>> turtle.write("No one expects the Spanish Inquisition!",
2523 >>> turtle.penup()
2524 >>> turtle.goto(-100,-50)
2525 >>> turtle.pendown()
2526 >>> turtle.write("Our two chief Turtles are...",
2528 >>> turtle.penup()
2529 >>> turtle.goto(-450,-75)
2530 >>> turtle.write(str(turtles()))