Skip to content

Commit

Permalink
Merge pull request #132 from chris-greening/add-canvas-move-feature
Browse files Browse the repository at this point in the history
Add screen coordinate feature
  • Loading branch information
chris-greening authored Apr 6, 2023
2 parents 5e7d8f1 + 1a7e075 commit 5f70476
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
13 changes: 7 additions & 6 deletions spyrograph/core/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@ def _draw_animation(
shapes_arr, screen_size: Tuple[Number, Number] = (1000, 1000),
screen_color: str = "white", exit_on_click: bool = False,
color: str = "black", width: Number = 1,
frame_pause: Number = 0.1, screen: "turtle.Screen" = None
frame_pause: Number = 0.1, screen: "turtle.Screen" = None,
screen_coords = (0, 0)
) -> None:
for shape in shapes_arr:
if screen is not None:
screen.clear()
screen.setup(*screen_size)
screen.bgcolor(screen_color)
screen = shape.trace(
turtles.shape_turtle.clear()
# screen.setup(*screen_size)
# screen.bgcolor(screen_color)
screen, turtles = shape.trace(
screen = screen, screen_size = screen_size,
screen_color = screen_color,
color = color, width=width
color = color, width=width, screen_coords=screen_coords
)
time.sleep(frame_pause)
if exit_on_click:
Expand Down
23 changes: 14 additions & 9 deletions spyrograph/core/_trochoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def trace(
show_circles: bool = False, frame_pause: Number = 0,
screen: "turtle.Screen" = None, circle_color: str = "black",
show_full_path: bool = False, full_path_color: str = "grey",
repeat: bool = False
repeat: bool = False, screen_coords = (0, 0)
) -> "turtle.Screen":
"""
Trace the shape using the turtle graphics library and return the turtle.Screen object.
Expand All @@ -234,7 +234,7 @@ def trace(
screen_color : str, optional
The color of the background screen, default is "white".
exit_on_click : bool, optional
If True, pause the final animation until the user clicks to exit
If True, pause the final animation until the user clicks to exit
the window, default is False.
color : str, optional
The color of the primary tracing, default is "black".
Expand All @@ -255,8 +255,10 @@ def trace(
full_path_color : str, optional
The color of the full path drawing, default is "grey".
repeat : bool, optional
If True, infinitely repeat the animation so it starts over from the
If True, infinitely repeat the animation so it starts over from the
beginning, default is False.
screen_coords : Tuple[int, int] = (0, 0)
Location of the screen coordinates
Returns
-------
Expand All @@ -271,9 +273,8 @@ def trace(
>>> screen = shape.trace(show_circles=True, exit_on_click=True)
"""
# pylint: disable=no-member,too-many-locals
screen = self._init_screen(screen, screen_size, screen_color)
screen = self._init_screen(screen, screen_size, screen_color, screen_coords)
turtle.tracer(False)

turtles = self._init_turtles(color, circle_color, full_path_color, hide_turtle, width)

if show_full_path:
Expand Down Expand Up @@ -310,7 +311,7 @@ def trace(
turtles.shape_turtle.clear()
if exit_on_click:
turtle.exitonclick()
return screen
return screen, turtles

@classmethod
def animate(
Expand All @@ -321,7 +322,7 @@ def animate(
screen_size: Tuple[Number, Number] = (1000, 1000),
screen_color: str = "white", exit_on_click: bool = False,
color: str = "black", width: Number = 1,
frame_pause: Number = 0.1, screen: "turtle.Screen" = None
frame_pause: Number = 0.1, screen: "turtle.Screen" = None, screen_coords = (0, 0)
) -> List["_Trochoid"]:
"""
Animate a sequence of _Trochoid shapes with varying input parameters,
Expand Down Expand Up @@ -389,7 +390,8 @@ def animate(
_draw_animation(
shapes_arr=shapes_arr, screen_size=screen_size,
screen_color=screen_color, exit_on_click=exit_on_click, color=color,
width=width, frame_pause=frame_pause, screen=screen
width=width, frame_pause=frame_pause, screen=screen,
screen_coords=screen_coords
)
return shapes_arr

Expand Down Expand Up @@ -524,13 +526,16 @@ def _show_full_path(self, pre_draw_turtle: "turtle.Turtle") -> None:

def _init_screen(
self, screen: "turtle.Screen", screen_size: Tuple[Number, Number],
screen_color: str
screen_color: str, screen_coords: Tuple[Number, Number]
) -> "turtle.Screen":
"""Initializes the turtle screen with the given size and color"""
if screen is None:
screen = turtle.Screen()
screen.setup(*screen_size)
screen.bgcolor(screen_color)
canvas = screen.getcanvas()
root = canvas.winfo_toplevel()
root.geometry(f"+{screen_coords[0]}+{screen_coords[1]}")
return screen

def _init_turtles(
Expand Down

0 comments on commit 5f70476

Please sign in to comment.