|
135 | 135 | 'pu', 'radians', 'right', 'reset', 'resizemode', 'rt', |
136 | 136 | 'seth', 'setheading', 'setpos', 'setposition', 'settiltangle', |
137 | 137 | 'setundobuffer', 'setx', 'sety', 'shape', 'shapesize', 'shapetransform', 'shearfactor', 'showturtle', |
138 | | - 'speed', 'st', 'stamp', 'tilt', 'tiltangle', 'towards', |
| 138 | + 'speed', 'st', 'stamp', 'teleport', 'tilt', 'tiltangle', 'towards', |
139 | 139 | 'turtlesize', 'undo', 'undobufferentries', 'up', 'width', |
140 | 140 | 'write', 'xcor', 'ycor'] |
141 | 141 | _tg_utilities = ['write_docstringdict', 'done'] |
@@ -1614,6 +1614,13 @@ def _goto(self, end): |
1614 | 1614 | """move turtle to position end.""" |
1615 | 1615 | self._position = end |
1616 | 1616 |
|
| 1617 | + def teleport(self, x=None, y=None, *, fill_gap: bool = False) -> None: |
| 1618 | + """To be overwritten by child class RawTurtle. |
| 1619 | + Includes no TPen references.""" |
| 1620 | + new_x = x if x is not None else self._position[0] |
| 1621 | + new_y = y if y is not None else self._position[1] |
| 1622 | + self._position = Vec2D(new_x, new_y) |
| 1623 | + |
1617 | 1624 | def forward(self, distance): |
1618 | 1625 | """Move the turtle forward by the specified distance. |
1619 | 1626 |
|
@@ -2293,6 +2300,15 @@ def fillcolor(self, *args): |
2293 | 2300 | else: |
2294 | 2301 | return self._color(self._fillcolor) |
2295 | 2302 |
|
| 2303 | + def teleport(self, x=None, y=None, *, fill_gap: bool = False) -> None: |
| 2304 | + """To be overwritten by child class RawTurtle. |
| 2305 | + Includes no TNavigator references. |
| 2306 | + """ |
| 2307 | + pendown = self.isdown() |
| 2308 | + if pendown: |
| 2309 | + self.pen(pendown=False) |
| 2310 | + self.pen(pendown=pendown) |
| 2311 | + |
2296 | 2312 | def showturtle(self): |
2297 | 2313 | """Makes the turtle visible. |
2298 | 2314 |
|
@@ -2710,6 +2726,54 @@ def _cc(self, args): |
2710 | 2726 | if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)): |
2711 | 2727 | raise TurtleGraphicsError("bad color sequence: %s" % str(args)) |
2712 | 2728 | return "#%02x%02x%02x" % (r, g, b) |
| 2729 | + |
| 2730 | + def teleport(self, x=None, y=None, *, fill_gap: bool = False) -> None: |
| 2731 | + """Instantly move turtle to an absolute position. |
| 2732 | +
|
| 2733 | + Arguments: |
| 2734 | + x -- a number or None |
| 2735 | + y -- a number None |
| 2736 | + fill_gap -- a boolean This argument must be specified by name. |
| 2737 | +
|
| 2738 | + call: teleport(x, y) # two coordinates |
| 2739 | + --or: teleport(x) # teleport to x position, keeping y as is |
| 2740 | + --or: teleport(y=y) # teleport to y position, keeping x as is |
| 2741 | + --or: teleport(x, y, fill_gap=True) |
| 2742 | + # teleport but fill the gap in between |
| 2743 | +
|
| 2744 | + Move turtle to an absolute position. Unlike goto(x, y), a line will not |
| 2745 | + be drawn. The turtle's orientation does not change. If currently |
| 2746 | + filling, the polygon(s) teleported from will be filled after leaving, |
| 2747 | + and filling will begin again after teleporting. This can be disabled |
| 2748 | + with fill_gap=True, which makes the imaginary line traveled during |
| 2749 | + teleporting act as a fill barrier like in goto(x, y). |
| 2750 | +
|
| 2751 | + Example (for a Turtle instance named turtle): |
| 2752 | + >>> tp = turtle.pos() |
| 2753 | + >>> tp |
| 2754 | + (0.00,0.00) |
| 2755 | + >>> turtle.teleport(60) |
| 2756 | + >>> turtle.pos() |
| 2757 | + (60.00,0.00) |
| 2758 | + >>> turtle.teleport(y=10) |
| 2759 | + >>> turtle.pos() |
| 2760 | + (60.00,10.00) |
| 2761 | + >>> turtle.teleport(20, 30) |
| 2762 | + >>> turtle.pos() |
| 2763 | + (20.00,30.00) |
| 2764 | + """ |
| 2765 | + pendown = self.isdown() |
| 2766 | + was_filling = self.filling() |
| 2767 | + if pendown: |
| 2768 | + self.pen(pendown=False) |
| 2769 | + if was_filling and not fill_gap: |
| 2770 | + self.end_fill() |
| 2771 | + new_x = x if x is not None else self._position[0] |
| 2772 | + new_y = y if y is not None else self._position[1] |
| 2773 | + self._position = Vec2D(new_x, new_y) |
| 2774 | + self.pen(pendown=pendown) |
| 2775 | + if was_filling and not fill_gap: |
| 2776 | + self.begin_fill() |
2713 | 2777 |
|
2714 | 2778 | def clone(self): |
2715 | 2779 | """Create and return a clone of the turtle. |
|
0 commit comments