-
-
Save stephengruppetta/9386de71a22dc6ef32f9f42094c34761 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import turtle | |
import string | |
character_width = 30 | |
character_height = 40 | |
window = turtle.Screen() | |
window.bgcolor(0.1, 0.1, 0.1) | |
window.tracer(0) | |
window.title("Tiny Turtle Typewriter") | |
typewriter = turtle.Turtle() | |
typewriter.color(0.9, 0.9, 0.9) | |
typewriter.penup() | |
typewriter.setposition( | |
-window.window_width() / 2 + character_width, | |
window.window_height() / 2 - character_height, | |
) | |
# Make the turtle look like a cursor | |
typewriter.shape("square") | |
typewriter.shapesize(0.1, 1) | |
# 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" | |
# and add "space", "Return", and "BackSpace" | |
} - {"-"} | {"minus", "space", "Return", "BackSpace"} | |
# 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 | |
elif character == "BackSpace": | |
for _ in range(2): | |
typewriter.undo() | |
window.update() | |
return | |
typewriter.write( | |
character, font=("Courier", character_width, "normal") | |
) | |
typewriter.forward(character_width) | |
window.update() | |
for character in typewriter_keys: | |
window.onkeypress( | |
lambda character=character: 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