Skip to content

Instantly share code, notes, and snippets.

@stephengruppetta
Created August 15, 2023 07:34
Show Gist options
  • Save stephengruppetta/82a32e71975ce44487ea02ebd640884c to your computer and use it in GitHub Desktop.
Save stephengruppetta/82a32e71975ce44487ea02ebd640884c to your computer and use it in GitHub Desktop.
import functools
import turtle
import string
character_width = 30
character_height = 40
window = turtle.Screen()
window.tracer(0)
typewriter = turtle.Turtle()
typewriter.penup()
typewriter.setposition(
-window.window_width() / 2 + character_width,
window.window_height() / 2 - character_height,
)
# Select keys that are on a typewriter
typewriter_keys = {
# unpack string.ascii_letters, string.digits,
# and string.punctuation into a set
*(
string.ascii_letters
+ string.digits
+ string.punctuation
)
# remove the minus sign and add "minus"
} - {"-"} | {"minus"}
# Define a function that takes a character as an argument
# and writes it to the screen
def type_letter(character):
if character == "minus":
character = "-"
typewriter.write(
character, font=("Courier", character_width, "normal")
)
typewriter.forward(character_width)
window.update()
for character in typewriter_keys:
window.onkeypress(
functools.partial(type_letter, character),
character,
)
window.listen()
window.update()
turtle.done()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment