Skip to main content

Make turtle.py function calls with a mini-language of shortcuts, like 'f 100' for forward(100).

Project description

TurtleSC

TurtleSC provides a mini-language of shortcut instructions to carry out turtle.py function calls.

These shortcuts are quicker to type, making them ideal for experimenting from the interactive shell. Turtlesc takes the idea of the existing fd() and rt() aliases for forward() and right() to the next level. All shortcuts are run from a string passed to the turtlesc.sc() function.

TurtleSC was created by Al Sweigart, author of Automate the Boring Stuff with Python and other programming books. All of his books are available for free online under a Creative Commons license. If you'd like a tutorial specifically about Python's turtle module, check out The Simple Turtle Tutorial.

You can also write (less readable) turtle programs using shortcuts. For example, this program:

from turtle import *
from random import *

tracer(4, 0)

for i in range(50):
    fillcolor((random(), random(), random()))

    # Set a random heading and draw several short lines with changing direction:
    setheading(randint(0, 360))
    begin_fill()
    for j in range(randint(200, 600)):
        forward(1)
        left(randint(-4, 4))
    home()
    end_fill()

update()
done()

...could be written as:

from turtlesc import *
from random import *

sc('t 4 0')

for i in range(50):
    sc(f'fc {random()} {random()} {random()}, sh {randint(0, 360)}, bf')
    for j in range(randint(200, 600)):
        sc(f'''f 1
               l {randint(-4, 4)}''')
    sc('h,ef')

sc('u,done')

This code isn't very readable, but it's quick to type. This is useful if you are making rapid prototypes of ideas.

Shortcuts in the sc() string argument are separated by a comma, with any shortcut arguments separated by spaces. Whitespace is insignificant (you can have one more spaces, and it's the same as a single space). Shortcut names and arguments are case-insensitive: 'f' and 'F' work the same. Newlines in the string argument are treated as shortcut-separating commas.

Some less-common shortcuts (such as 'done', 'bye', and 'exitonclick') only have their full function name. All shortcuts have their full function name as a shortcut name: you can use either 'f' or 'forward'.

TurtleSC also adds cardinal movement shortcuts that move the turtle independent of it's current heading (and does not change the heading): 'n 100', 's 100', 'e 100', 'w 100' will move the turtle up, down, right, and left 100 steps, respectively. There are also full name shortcuts ('north', 'south', 'east', 'west') and diagnoal shortcuts ('nw', 'ne', 'sw', 'se', 'northwest', 'northeast', 'southwest', 'southeast').

By default, the sc() function operates on the single, global turtle. You can also pass the turtle_obj keyword argument to operate on different Turtle objects:

from turtlesc import *
from turtle import *

t1 = Turtle()
t2 = Turtle()

# Make turtle 1 red and move up-right:
sc('pc red, l 45, f 100', turtle_obj=t1)

# Make turtle 2 blue and move down-right:
sc('pc blue, r 45, f 100', turtle_obj=t2)

sc('done')

The turtlesc module also provides new in_radians_mode() or in_degrees_mode() functions that return a Boolean True or False value depending on which mode the turtle is in. These features are missing in the original turtle module.

If you want to get the original code for a shortcuts string, pass it to the get_turtle_code() function:

>>> from turtlesc import *
>>> get_turtle_code('f 100, r 45, f 100')
('forward(100)', 'right(45)', 'forward(100)')

Note that the return value of get_turtle_code() is a tuple of strings, all of which lack the turtle. prefix in case you want to add your own (either the turtle module or a variable containing a Turtle object.)

Reference

Here is a complete reference of supported shortcuts:

Shortcut Call Turtle.py Equivalent
sc('f 100') forward(100)
sc('b -100.5') backward(-100.5)
sc('l 45') left(45)
sc('r 90') right(90)
sc('h') home()
sc('c') clear()
sc('g 15 40') goto(15 40)
sc('x 10') setx(10)
sc('y -20') sety(-20)
sc('st') stamp()
sc('pd') pendown()
sc('pu') penup()
sc('ps 4') pensize(4)
sc('pc 1.0 0.0 0.5') pencolor(1.0, 0.0, 0.5)
sc('fc 255 0 128') fillcolor(255, 0, 128)
sc('bc FF00FF') bgcolor(255, 0, 255)
sc('sh 90') setheading(90)
sc('cir 10') circle(10)
sc('undo') undo()
sc('bf') begin_fill()
sc('ef') end_fill()
sc('reset') reset()
sc('sleep 5') time.sleep(5)
sc('n 10') setheading(90) ; forward(10)
sc('s 10') setheading(270) ; forward(10)
sc('e 10') setheading(180) ; forward(10)
sc('w 10') setheading(0) ; forward(10)
sc('nw 10') setheading(135) ; forward(10)
sc('ne 10') setheading(45) ; forward(10)
sc('sw 10') setheading(225) ; forward(10)
sc('se 10') setheading(315) ; forward(10)
sc('done') done()
sc('bye') bye()
sc('exitonelick') exitonclick()
sc('t 100 0') tracer(100, 0)
sc('u') update()
sc('hide') hide()
sc('show') show()
sc('dot 5') dot(5)
sc('cs 42') clearstamp(42)
sc('css') clearstamps()
sc('css 10') clearstamps(10)
sc('degrees') degrees()
sc('radians') radians()
sc('spd 5') speed(5)
sc('spd fastest') speed('fastest')

Notes

The sc('sleep 5') shortcut exists to call the time.sleep() function.

You can pass multiple strings to sc(). For example, sc('f 100', 'r 45', 'f 100') is equivalent to sc('f 100, r 45, f 100').

The 'pc', 'fc', and 'bc' shortcuts for pen color, fill color, and background color can take a color argument as:

  • A color name, such as 'red'
  • Three 0 to 255 integer values, such as '255 0 0
  • Three 0.0 to 1.0 float values, such as '1.0 0.0 0.0
  • A hexadecimal RGB code, with or without a leading # hashtag, such as '#FF0000' or 'FF0000'

All of these values work no matter if the color mode is 255 or 1.

The cardinal directions shortcuts change both the heading and position of the turtle.

Contribute

If you'd like to contribute, send emails to al@inventwithpython.com

If you find this project helpful and would like to support its development, consider donating to its creator on Patreon.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

turtlesc-0.0.1.tar.gz (66.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

turtlesc-0.0.1-py2.py3-none-any.whl (8.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file turtlesc-0.0.1.tar.gz.

File metadata

  • Download URL: turtlesc-0.0.1.tar.gz
  • Upload date:
  • Size: 66.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for turtlesc-0.0.1.tar.gz
Algorithm Hash digest
SHA256 458df86043fd6f8951d4afcd143f13187e287ab75ee3f59d0a025985d6ab001d
MD5 451c7fba05bd99f7d69c4762202c12cb
BLAKE2b-256 e308fd5716c80bf8a0b7d8db7b4f1fe1fc0662c59798968fad57d64360617967

See more details on using hashes here.

File details

Details for the file turtlesc-0.0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: turtlesc-0.0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for turtlesc-0.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5b40d9f08abf5c6ab4e0a18233d9812bb85e00200b9a55333521b58cf9c65b84
MD5 e1f1a636c36095f1066447217bddcb5f
BLAKE2b-256 d38d7dbadc4cb8ab9faee45b43e131cebf1726ad979b7546fbbfa2c3dae873d2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page