brython-dev / brython

Brython (Browser Python) is an implementation of Python 3 running in the browser
BSD 3-Clause "New" or "Revised" License
6.37k stars 508 forks source link

Minor turtle issues #739

Closed aroberge closed 4 years ago

aroberge commented 6 years ago

The 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).

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:

  1. setting fill to freeze after an animation has been done.
  2. setting fill to remove when another animation is desired.
  3. perform the animation
  4. set fill to freeze 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.

jonathanverner commented 6 years ago

Thanks for reporting and working on this!

aroberge commented 6 years ago

Due to differences between SVG and Tkinter, there will definitely be differences between the Brython turtle module and the CPython one. For instance, whereas one can have an undo mode with CPython, used to remove objects added in an interactive session, the same cannot be done with Brython.

However, the counterpart can also be true: there are some features easily implemented in SVG that could not be done with the Tkinter-based turtle module. I will keep track here of some possible features to implement.

Edit

I have decided to postpone these additions for the near future.

aroberge commented 6 years ago

@PierreQuentel Do you have any objections to me adding features to Brython's turtle module which are not available in CPython's version? The converse is going to be true: there are features of CPython's version that cannot be reproduced using Brython's version ( -- at least, I cannot see how to implement all the features, like the undo() method). Having some additional features that can produce nicer looking graphics might counteract any negative impression caused by the missing features.

PierreQuentel commented 6 years ago

@aroberge Yes that's fine for me, can you write a documentation page with the major differences between the CPython version and this one ? There was a post on Reddit a few days ago about a program made with turtle, do you think it could work with the Brython version ?

aroberge commented 6 years ago

@PierreQuentel That post on Reddit was my motivation to have a closer look at the turtle module. It is my goal to be able to reproduce it if possible - I still cannot even though I've re-implemented most of the missing functions.

As for the documentation: I'm adding a selection of demos (chosen from a drop down menu) including some that will document all the differences between the Brython version and the CPython version, listing all the methods from https://docs.python.org/3/library/turtle.html#turtle-methods

aroberge commented 6 years ago

image

The reddit post code using my current version. I had forgotten to call the done() method (which I previously named show()) in my recent attempts - thus producing no output.

aroberge commented 6 years ago

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. image

Using the current version with Brython, I get the following: image

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.

aroberge commented 6 years ago

Can't resist posting an updated picture. :-) image

jonathanverner commented 6 years ago

Wow, nice!

aroberge commented 6 years ago

Once the pull request is accepted, this issue can be closed as far as I am concerned.

PierreQuentel commented 6 years ago

The Pull Request has been merged. Encore merci André :-)

aroberge commented 6 years ago

There seems to be a problem ... I've updated my local copy of the repository and the last example (Procedural version: comparison with CPython), it no longer works. I'll need to have a look at what went wrong.

EDIT: I know that this was working but, even after changing the branch to the one I used to work on, it still raises an exception ... and I do not understand why.

PierreQuentel commented 4 years ago

@aroberge I am reviewing old issues and wondering if keeping this one in the issue tracker still makes sense. What do you think ?

aroberge commented 4 years ago

@PierreQuentel On peut fermer le tout.