Minor turtle issues
See original GitHub issueThe existing turtle module is an excellent adaptation of CPython’s tkinter-based version that uses Scalable Vector Graphics (svg) for drawing including animations and is perfectly usable for simple demonstrations.
However, as I explored it, I found quite a few missing methods, some of which might not make much sense to implement in this browser version, but not all. I will document them here as I go along, as well as the progress done on a branch in my fork (https://github.com/aroberge/brython/tree/fix/master/turtle739).
- 1. at https://github.com/brython-dev/brython/blob/master/www/src/Lib/turtle.py#L3020 ,
document[_CFG["turtle_canvas_id"]]
can raise a KeyError exception if therestart()
function is called twice in a row. - 2.
write
(https://github.com/brython-dev/brython/blob/master/www/src/Lib/turtle.py#L2777) should allow the user to set the font. However, the font information is not passed on tosvg.text
(https://github.com/brython-dev/brython/blob/master/www/src/Lib/turtle.py#L338). Similarly for thealign
parameter. - 3. Calling Turtle with “blank” as the default shape appears to be causing an error. This is for a turtle using a gif for its image. It will be removed.
- 4. Setting speed to zero should result in immediate drawing; instead nothing is drawn. The fastest animation seems to be set to a duration of 0.1 s (https://github.com/brython-dev/brython/blob/master/www/src/Lib/turtle.py#L201), while svg supports animation duration in the range of ms (https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/dur)
- 5. Many existing configuration settings are absolutely irrelevant (and ignored); they should be removed.
- 6.
left()
appears to be turning “right” instead. - 7. The mode settings (logo, standard, world) appears to be ignored.
- 8. CPython’s version has a bug (https://bugs.python.org/issue23660) which I filed almost 3 years ago on behalf of another user; this bug is still not fixed but it should be in the Brython’s version.
- 9. In the Brython demo, it might be useful to add an output print “window” either for direct use by the turtle user or to report errors.
- 10. The code run when the module is imported as
__main__
is irrelevant for Brython and has been removed. - 11. The CPython turtle module has an
undo()
method which does not really makes sense in the current context. It, and all the associated objects (such asundobuffer
) will be removed. - 12. With the CPython turtle, in both the standard and logo mode, the y coordinate increases towards the top of the screen, whereas it increases towards the bottom with the Brython turtle.
- 13. When using CPython, the turtle itself is drawn the same when changing the line thickness; however this is not the case with Brython’s turtle.
- 14. The
dot()
method does not produce a circle and appears to suspend any running program. - 15. If
write()
is the very first method called before any other turtle drawing, nothing gets written, nor drawn afterwards. - 16. The
bgcolor()
method does not do anything. - 17. The
stamp()
method does not do anything. - 18.
begin_fill()
andend_fill()
are ignored. - 19. If two or more turtles are on the screen, only one is shown while a drawing is done; this is unlike the CPython version.
- 20. The function based version (i.e. anonymous turtle) is not available.
- 21. With
penup()
, the moving turtle is currently not drawn when moving.
Efficiency consideration
This following should be done as a second pass (perhaps in a different branch) after all the relevant methods have been done.
Currently, each time a turtle is moved, a “new” turtle shape is created and drawn on the screen as a separate svg element, moved, and, unlike in the CPython case, disappears from the screen after a drawing is done. It should be possible to repeatedly use the turtles that have been created by:
- setting
fill
tofreeze
after an animation has been done. - setting
fill
toremove
when another animation is desired. - perform the animation
- set
fill
tofreeze
again
In addition to being more efficient, as we would reuse an existing svg element rather than creating and adding a new one each time, this would likely mimic better the behavior of the CPython turtle.
A further consideration might be to see if clear
and reset
for a given turtle could not be implemented by setting the visibility attribute of previously drawn elements.
Issue Analytics
- State:
- Created 6 years ago
- Comments:14 (14 by maintainers)
Top GitHub Comments
The reddit post code using my current version. I had forgotten to call the
done()
method (which I previously namedshow()
) in my recent attempts - thus producing no output.Just because people like pictures … Using CPython’s version, I set the pen width to 30 and drew a black line, and then set the pen width to 3 and drew the same distance over: the “line” drawn has rounded edges.
Using the current version with Brython, I get the following:
This likely explains the difference between the image I obtained using the code from the reddit post and the image posted there.
I will try to change the “line” drawing method to reproduce the CPython’s case but add an additional option parameter (
rounded_lines
or similar), set to True by default but could be changed by the user.