Skip to content

Instantly share code, notes, and snippets.

@stephengruppetta
Created August 15, 2023 07:37
Show Gist options
  • Save stephengruppetta/89c5b22a0eb389c5501c533d76dccecc to your computer and use it in GitHub Desktop.
Save stephengruppetta/89c5b22a0eb389c5501c533d76dccecc 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", "space", "Return"}
# Define a function that takes a character as an argument
# and writes it to the screen
def type_letter(character):
if character == "minus":
character = "-"
elif character == "space":
character = " "
elif character == "Return":
typewriter.setposition(
-window.window_width() / 2 + character_width,
typewriter.ycor() - character_height,
)
window.update()
return
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