37 ms·
These programs rely on Logo's turtle interface, which is a convenient and intuitive way to describe graphics imperatively. It's great for kids. These days, Pyth
by hackyhacky 7y ago
These programs rely on Logo's turtle interface, which is a convenient and intuitive way to describe graphics imperatively. It's great for kids. These days, Python also has a turtle library (edit: built-in! part of the standard library, for some reason), heavily inspired by Logo, which means that the code on this page can be easily translated into Python. Here are some example that you can run at home (since I assume more people have access to Python these days than likely have a Logo interpreter installed). Edit: these are Python translations (by me) of the top three entries on the linked page.
import turtle
import math
turtle.tracer(0,0) # remove this line to watch the drawing
def spirals():
# repeat 1800 [fd 10 rt repcount + .1]
for repcount in range(1800):
turtle.fd(10)
turtle.rt(repcount+.1)
turtle.update()
def snowflakes(variant=6):
# repeat 8 [rt 45 repeat 6 [repeat 90 [fd 2 rt 2] rt 90]]
for _ in range(8):
turtle.right(45)
for _ in range(variant):
for _ in range(90):
turtle.forward(2)
turtle.right(2)
turtle.right(90)
turtle.update()
def orb():
# for [i 0 420] [seth :i repeat :i [fd 2 rt 1] pu home pd]
for i in range(0,420):
turtle.seth(i)
for _ in range(i):
turtle.forward(2)
turtle.right(1)
turtle.penup()
turtle.home()
turtle.pendown()
turtle.update()
- misterdoubt 7y ago> built-in! part of the standard library, for some reason Well, as far as I can tell, the reason is that > Logo's turtle interface [...] is a convenient and intuitive way to describe graphics imperatively. It's great for kids. :)
- vkaku 7y agoWho'd have thought this was a built in.
- SamBam 7y agoI'm a Python newbie, running on macOS 10.13 with Python 2.7.10. When I save the above as turtle.py, and run with `python turtle.py` in the Terminal, I see an almost instantaneous flash of a tiny window, which then disappears. When I remove the tracer line to "watch the drawing," I don't even get that. The Terminal just returns instantly. Looking at tracer documentation, it's supposed to be `(which-frames-to-render, delay)`. If I set it to `turtle.tracer(1, 10000)` I get exactly the same flashing window as (0,0). How can I see the result?
- hackyhacky 7y agoDon't save the file to turtle.py, because then the "import turtle" line might try to import itself. Call it turtlestuff.py. Then do this in terminal: python -i turtlestuff.py Then you'll get an interactive Python prompt and you can call the functions directoy, e.g. spirals() Hope this helps you!
- SamBam 7y agoAh, thank you.
- DonHopkins 7y ago>because then the "import turtle" line might try to import itself Then you get turtles all the way down!
- hackyhacky 7y agoAlternatively, if you save the file as turtlestuff.py, you can run this: python -c 'from turtlestuff import *; snowflakes(); turtle.mainloop()' or python -c 'from turtlestuff import *; snowflakes(3); turtle.mainloop()' or python -c 'from turtlestuff import *; orb(); turtle.mainloop()'