Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-88773: Added teleport method to Turtle library #103974

Merged
merged 26 commits into from
Apr 30, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a51d835
Working teleport method
liam-gersten Apr 27, 2023
7986926
Handle filling with teleport method
liam-gersten Apr 28, 2023
1054b34
Teleport method documentation
liam-gersten Apr 28, 2023
af7c1c6
Merge branch 'main' into TurtleTeleportMethod
liam-gersten Apr 28, 2023
5871c2c
📜🤖 Added by blurb_it.
blurb-it[bot] Apr 28, 2023
494f478
Fixed NEWS entry
liam-gersten Apr 28, 2023
2fd8eda
Update Misc/NEWS.d/next/Library/2023-04-28-18-04-23.gh-issue-88773.xX…
terryjreedy Apr 28, 2023
e89549e
Merge branch 'main' into TurtleTeleportMethod
liam-gersten Apr 28, 2023
ba2920e
Made new coord assignment more concise
liam-gersten Apr 28, 2023
01dd697
Merge branch 'TurtleTeleportMethod' of https://github.com/liam-gerste…
liam-gersten Apr 28, 2023
f8a9681
Teleport documentation in Doc/library/turtle.rst
liam-gersten Apr 28, 2023
7cec9b6
Update Doc/library/turtle.rst
liam-gersten Apr 28, 2023
eec5411
Update Misc/NEWS.d/next/Library/2023-04-28-18-04-23.gh-issue-88773.xX…
liam-gersten Apr 28, 2023
ca3d9c5
Partial teleport method in TNavigator for testing in TestTNavigator
liam-gersten Apr 28, 2023
02712b3
Partial teleport method in TPen for testing in TestTPen
liam-gersten Apr 28, 2023
8db9bf3
Teleport tests in TestTNavigator and TestTPen
liam-gersten Apr 28, 2023
4272101
Merge branch 'main' into TurtleTeleportMethod
liam-gersten Apr 28, 2023
ae01a3e
Made teleport appear in alphabetical order
liam-gersten Apr 28, 2023
43f8a69
Merge branch 'TurtleTeleportMethod' of https://github.com/liam-gerste…
liam-gersten Apr 28, 2023
07ae3da
Attempt to fix the doctests.
gpshead Apr 29, 2023
d648f52
Mention fill_gap in the teleport prototype.
gpshead Apr 29, 2023
2baaf85
Make fill_gap a keyword only argument.
gpshead Apr 29, 2023
203a0f3
Align docs with fill_gap being keyword only.
gpshead Apr 29, 2023
dac054e
Testing changes for fill_gap arg and assertFalse
liam-gersten Apr 30, 2023
9a38e8a
Merge branch 'main' into TurtleTeleportMethod
liam-gersten Apr 30, 2023
fc880c8
Merge branch 'main' into TurtleTeleportMethod
liam-gersten Apr 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Doc/library/turtle.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Turtle motion
| :func:`right` | :func:`rt`
| :func:`left` | :func:`lt`
| :func:`goto` | :func:`setpos` | :func:`setposition`
| :func:`teleport`
| :func:`setx`
| :func:`sety`
| :func:`setheading` | :func:`seth`
Expand Down Expand Up @@ -372,6 +373,44 @@ Turtle motion
(0.00,0.00)


.. function:: teleport(x, y=None)

:param x: a number or ``None``
:param y: a number or ``None``
:param fill_gap: a boolean

Move turtle to an absolute position. Unlike goto(x, y), a line will not
be drawn. The turtle's orientation does not change. If currently
filling, the polygon(s) teleported from will be filled after leaving,
and filling will begin again after teleporting. This can be disabled
with fill_gap=True, which makes the imaginary line traveled during
liam-gersten marked this conversation as resolved.
Show resolved Hide resolved
teleporting act as a fill barrier like in goto(x, y).

.. doctest::
:skipif: _tkinter is None
:hide:

>>> turtle.goto(0, 0)

.. doctest::
:skipif: _tkinter is None

>>> tp = turtle.pos()
>>> tp
(0.00, 0.00)
>>> turtle.teleport(60)
>>> turtle.pos()
(60.00,0.00)
>>> turtle.teleport(y=10)
>>> turtle.pos()
(60.00,10.00)
>>> turtle.teleport(20, 30)
>>> turtle.pos()
(20.00,30.00)

.. versionadded: 3.12


.. function:: setx(x)

:param x: a number (integer or float)
Expand Down
50 changes: 49 additions & 1 deletion Lib/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
'setworldcoordinates', 'textinput', 'title', 'tracer', 'turtles', 'update',
'window_height', 'window_width']
_tg_turtle_functions = ['back', 'backward', 'begin_fill', 'begin_poly', 'bk',
'circle', 'clear', 'clearstamp', 'clearstamps', 'clone', 'color',
'circle', 'clear', 'clearstamp', 'clearstamps', 'clone', 'color', 'teleport',
liam-gersten marked this conversation as resolved.
Show resolved Hide resolved
'degrees', 'distance', 'dot', 'down', 'end_fill', 'end_poly', 'fd',
'fillcolor', 'filling', 'forward', 'get_poly', 'getpen', 'getscreen', 'get_shapepoly',
'getturtle', 'goto', 'heading', 'hideturtle', 'home', 'ht', 'isdown',
Expand Down Expand Up @@ -2710,6 +2710,54 @@ def _cc(self, args):
if not ((0 <= r <= 255) and (0 <= g <= 255) and (0 <= b <= 255)):
raise TurtleGraphicsError("bad color sequence: %s" % str(args))
return "#%02x%02x%02x" % (r, g, b)

def teleport(self, x=None, y=None, fill_gap: bool = False) -> None:
"""Instantly move turtle to an absolute position.

Arguments:
x -- a number or None
y -- a number None
fill_gap -- a boolean None

call: teleport(x, y) # two coordinates
--or: teleport(x) # teleport to x position, keeping y as is
--or: teleport(y=y) # teleport to y position, keeping x as is
--or: teleport(x, y, fill_gap=True)
# teleport but fill the gap in between

Move turtle to an absolute position. Unlike goto(x, y), a line will not
be drawn. The turtle's orientation does not change. If currently
filling, the polygon(s) teleported from will be filled after leaving,
and filling will begin again after teleporting. This can be disabled
with fill_gap=True, which makes the imaginary line traveled during
teleporting act as a fill barrier like in goto(x, y).

Example (for a Turtle instance named turtle):
>>> tp = turtle.pos()
>>> tp
(0.00, 0.00)
>>> turtle.teleport(60)
>>> turtle.pos()
(60.00,0.00)
>>> turtle.teleport(y=10)
>>> turtle.pos()
(60.00,10.00)
>>> turtle.teleport(20, 30)
>>> turtle.pos()
(20.00,30.00)
"""
pendown = self.isdown()
was_filling = self.filling()
if pendown:
self.pen(pendown=False)
if was_filling and not fill_gap:
self.end_fill()
new_x = x if x is not None else self._position[0]
new_y = y if y is not None else self._position[1]
self._position = Vec2D(new_x, new_y)
self.pen(pendown=pendown)
if was_filling and not fill_gap:
self.begin_fill()

def clone(self):
"""Create and return a clone of the turtle.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added :func:`turtle.teleport` to turtle library to move a turtle to a new point without tracing a line, visible or invisible. Patch by Liam Gersten.
liam-gersten marked this conversation as resolved.
Show resolved Hide resolved