1

today I want draw a tree in browser with RapydScript. I have code in Python:

import random
import turtle

def tree(size, myTurtle):
    myTurtle.pensize(size / 20)

    if size < random.randint(1,2) * 20:
        myTurtle.color("green")
    else:
        myTurtle.color("brown")

    if size > 5:
        myTurtle.forward(size)
        myTurtle.left(25)
        tree(size - random.randint(10, 20), myTurtle)
        myTurtle.right(50)
        tree(size - random.randint(10, 20), myTurtle)
        myTurtle.left(25)
        myTurtle.penup()
        myTurtle.backward(size)
        myTurtle.pendown()

window = turtle.Screen()
window.setup(800,600)
window.bgcolor("white")

myTurtle = turtle.Turtle()
myTurtle.color("brown", "blue")
myTurtle.left(90)
myTurtle.speed(0)
myTurtle.penup()
myTurtle.setpos(0, -250)
myTurtle.pendown()

And I want to run it in browser to get this effect:

image ​Don't worry about text over the tree, is in polish ;) I run this in Skulpt, maybe you hear about it, effect you have above. Now I want to run this in RapydScript and compare it to Skulpt and Brython.

I am wonder if it possible in RapydScript beacuse this code use turtle module. I guess that I need to change this code somehow to run it on RapydScript. Did RapydScript allow to import turtle module?

As you can see here: http://www.transcrypt.org/live/turtle_site/turtle_site.html

Transcrypt(similiar tool to RapydScript) somehow can draw with turtle.

Can you help me with this?

Of course I want use Python, I know that RapydScript allows to use JavaScript, but I want Python :))

Monica
  • 111
  • 11

2 Answers2

2

See src/lib in RapydScript repo - there is no turtle module. And it can't import turtle module from Python because it doesn't draw on canvas in browser. So you can't draw tree if you doesn't create turtle module.

furas
  • 134,197
  • 12
  • 106
  • 148
2

As @furas mentioned, there is no turtle module in the base repo. First of all, I think you're misunderstanding what the turtle module is, it's nothing more than an abstraction around another graphics library. Even in Python, it's not the preferred way of handling graphics, it's just a subset of Logo toolkit aimed at making programming easier to kids.

With that said, Transcrypt exists in the exact same JavaScript world as RapydScript, the turtle it uses has nothing to do with Python's turtle, it's a wrapper around SVG. In fact, here it is: https://github.com/JdeH/Transcrypt/blob/master/transcrypt/modules/turtle/init.py

And looking at that code, I can tell you that you can copy-paste it almost verbatim into RapydScript to "gain" a turtle module there. Everything that code does is supported by RapydScript, even the import mechanism between RS and Transcrypt will work the same.

Moreover, a quick Google search revealed 2 JavaScript implementations of this turtle module (which you can just attach to the same page as RapydScript and use them as if they're Python):
http://berniepope.id.au/html/js-turtle/turtle.html
https://github.com/davebalmer/turtlewax

Finally, whether or not you use the turtle module doesn't make your code any more or less Python. You also seem to have a misconception that by using a JavaScript library from RapydScript, you need to write the rest of the code in JavaScript. That is not the case, The RapydScript examples directory already shows D3, canvas, and WebGL examples. The reason there is no turtle module is because it's obsolete compared to the graphics libraries that JavaScript (and RapydScript) has access to. You're welcome to do a pull request with a turtle module implementation, however.

Alexander Tsepkov
  • 3,946
  • 3
  • 35
  • 59