Is there any easy way for beginners to use the turtle Python module on Py5 ?
It's in the title :
import py5 import turtle as tt
def setup(): py5.size(500,500) lea = tt.Turtle()
def draw(): lea.forward(1) lea.left(10) if py5.frame_count > 20 : py5.no_loop()
py5.run_sketch()
I know I can code myself a simple Turtle class, or use translate, rotate, etc. but just using the turtle CPython module ? Maybe somebody has already a py5.Turtle class available ? Thanks.
This is an interesting idea and is worth thinking about. I don't know much about python's turtle library. It probably has its own graphics back end that it is built to work with.
py5 doesn't have anything like this right now. It would be possible for py5 to provide something very similar, perhaps as a context manager. That would be a good start. Also we could provide something like the py5bot Jupyter notebook kernel to make it easy to use.
Thank you for the suggestion. I will work on this in a future release.
I saw the comment on the Hariss Spiral post first... so I replied with some code there. Implementing basic turtle functionality can be done like @vsquared did, with Py5Vector objects tracking the pen position and heading, or as I did, manipulating the coordinate system... I translated into English some proof of concept code I had. https://github.com/py5coding/py5generator/discussions/615#discussioncomment-13337260
Natalie Freed created a rather simple utility to port turtle graphics to Processing and this may be rather easily 'pythonized'. The java (.pde) version of this .js example is what was used to create the Harriss spiral in py5: https://gist.github.com/nataliefreed/161b38a3e948ecf9b2655d40af1547df
Example code for your original post:
# Uses Imported mode for py5
loc = Py5Vector(0.0,0.0)
start = Py5Vector(0.0,0.0)
end = Py5Vector(0.0,0.0)
orientation = radians(90)
def setup():
global loc
size(500, 500)
window_title("Turtle Demo")
loc = Py5Vector(width/2 + 50, height/2)
fill(0)
def draw():
if(frame_count < 20):
fwd(10)
lt(radians(10))
# === UTILITY FUNCTIONS ===
def fwd(len):
global loc
start = loc
end = loc + polar(len,orientation)
line(start.x,start.y, end.x, end.y)
loc = end
def back(len):
global loc
end = loc - polar(len,orientation)
loc = end
def lt(theta):
global orientation
orientation += theta
def rt(theta):
global orientation
orientation -= theta
def jumpTo(x,y):
loc = Py5Vector(x, y)
# converts an angle and radius into a vector
def polar(r, theta):
return Py5Vector(r*cos(theta), r*sin(-theta))
Output:
Yes, I had already translated this code in Python, it's a first step but it lacks turtle shapes et show/hide_turtle features so important when playing with several turtles.
Yes I replied to this post. The problem is show/hide turtle with some usual turtle shapes (ball, square, arrow, turtle). Mostly for beginner games with 1 ou many turtles... Thanks, -jpr
Le 2 juin 2025 à 02:05, Alexandre B A Villares @.***> a écrit :
villares left a comment (py5coding/py5generator#660) I saw the comment on the Hariss Spiral post first... so I replied with some code there. Implementing basic turtle functionality can be done like @vsquared did, with Py5Vector objects tracking the pen position and heading, or as I did, manipulating the coordinate system... I translated into English some proof of concept code I had. #615 (reply in thread) — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>
Py5 has a cursor function where you can customize the cursor with whatever image you want.
The thing is that cursor() changes the mouse pointer/cursor... To provide a shape to indicate current turtle position would involve caching the canvas without the turtle, so it can be redrawn and the turtle shape won't leave a trail of copies... It can be done. For multiple turtles we would have to adopt the OO strategies similar to the turtle module I suppose... It cab be done, but might be a lot of work.
@villares Would it be feasible to draw a 'cursor' object when the line is drawn? The coordinates would be either at the start or end of the line. That way it would follow the line being drawn.
@villares Would it be feasible to draw a 'cursor' object when the line is drawn? The coordinates would be either at the start or end of the line.
We could use a normal Py5Shape or raster image. The trouble is that you usually want to have only one of these for each turtle (unless the 'trail mode' is active). My intuition would be to cache, to keep a copy of the canvas without the turtle cursor, in order to be able to clean them up and re-add them at the end of each frame. Something that a py5 extension could do as pre-draw and post-draw operations.
I forgot about the 'trail'; now I see what you are talking about. This image was made by drawing a small circle at the start of each line segment:
Perhaps we can try out some "Turtle" library for Java Processing, which can draw on the Processing's canvas: https://GitHub.com/leahbuechley/Turtle
@GoToLoop In the examples I don't see code for a cursor; haven't looked in the library yet but so far it looks similar to Natalie Freed's work, except that the library needs to be imported and Freed used inline functions.
@villares Would something like this work? I used Py5Vector arrays for point coordinates and then added line segments in draw(). When it reached the end of the arc I added the 'cursor' last.
# Uses Imported mode for py5
loc = Py5Vector(0.0,0.0)
start = Py5Vector(0.0,0.0)
end = Py5Vector(0.0,0.0)
orientation = radians(90)
s = []
e = []
def setup():
global loc
size(500, 500)
window_title("Turtle Demo")
loc = Py5Vector(width/2 + 50, height/2)
fill(0)
def draw():
if(frame_count < 20):
fwd(10)
lt(radians(10))
print("start =",s[frame_count-1])
print("end =", e[frame_count-1])
line(s[frame_count-1].x, s[frame_count-1].y, e[frame_count-1].x, e[frame_count-1].y )
if(frame_count == 20):
circle(e[frame_count-2].x, e[frame_count-2].y, 5)
# === UTILITY FUNCTIONS (modified Natalie Freed) ===
def fwd(len):
global loc
global v
start = loc
end = loc + polar(len,orientation)
s.append(start)
e.append(end)
loc = end
def back(len):
global loc
end = loc - polar(len,orientation)
loc = end
def lt(theta):
global orientation
orientation += theta
def rt(theta):
global orientation
orientation -= theta
def jumpTo(x,y):
loc = Py5Vector(x, y)
# converts an angle and radius into a vector
def polar(r, theta):
return Py5Vector(r*cos(theta), r*sin(-theta))
Output:
@villares Would something like this work? I used Py5Vector arrays for point coordinates and then added line segments in draw(). When it reached the end of the arc I added the 'cursor' last.
Hmm, I unfortunately don't have the time and energy to pursue this, but we would have to think of something more "invisible" for the end user.
Conceptually I was thinking of something like this:
from py5_turtle import Turtle
# or from py5_turtle import *
def setup():
size(500, 500)
this = get_current_sketch() # better if we could avoid this
alice_turtle = Turtle(this)
bob_turtle = Turtle(this)
def pre_draw(): # hidden, called by py5
<revert canvas to drawing without turtles, if there is a snapshot>
def draw():
... # the user normal drawing commands
def post_draw(): # hidden, called by py5
<take a snapshot of the drawing without any turtle>
<add the turtle or turtles (in case of multiple instances) at their current positions>
Hmm, I unfortunately don't have the time and energy to pursue this, but we would have to think of something more "invisible" for the end user.
Interesting ideas! I need to learn more about Python's turtle library. This could be an interesting teaching tool. I agree with @villares in that this should be a py5 extension library.
Another possible approach that would avoid the snapshot issue is by having each turtle draw to a Py5Graphics object. That might work, we will have to see.
I woke up this morning thinking about py5_turtle, first I thought about P3D and some turtle drawings in a Py5Graphics object overlapping some 3D stuff, the phrase "turtles in space" came to my mind. Then I thought, turtles in 3D space could be a killer feature I haven't seen on other turtle implementations! (I swear I just had some coffee)