Getting Started with Trinket.io

Creating a Trinket Account (Optional but Recommended)

  1. Go to Trinket.io
  2. Click “Sign Up” in the top right corner
  3. Fill in your information or sign up with Google
  4. Verify your email if required

Creating a New Python Trinket

  1. Go to Trinket.io
  2. Click the “+ New Trinket” button
  3. Select “Python” from the options (make sure it’s the Python with Turtle option)
  4. You should see a code editor on the left and output area on the right
  5. Delete any starter code that appears in the editor

Important Trinket Tips

  • Saving work: If you created an account, click “Save” often. If not, you’ll need to keep the tab open to preserve your work.
  • Running code: Click the “Run” button (▶️) at the top of the editor to execute your code.
  • Sharing: Use the “Share” button to get a link you can submit to your teacher.
  • Display issues: If the Turtle graphics don’t appear, try clicking on the “Result” tab.

Part 1: Setting up the Screen and Turtles

Start by creating a new Python Trinket. Use this minimal starter code:

import turtle

# Set up the screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.setup(width=600, height=450)

# Main loop
wn.mainloop()

TASK 1.1: Create your display turtles

We need several turtles to display different elements. Follow this pattern to create them:

# This is an example for the timer turtle
timer = turtle.Turtle()
timer.hideturtle()
timer.penup()
timer.goto(0, 50)
timer.color("white")

Now create similar turtles for:

  • A message turtle (positioned at y=-50)
  • A title turtle (positioned at y=150)
  • A clue turtle (positioned at y=100)
  • A hint turtle (positioned at y=-150)

HINT: For each turtle, follow the same pattern: create it, hide it, pick up the pen, position it, and set a color.

TASK 1.2: Set up global variables

We need variables to track the state of our game. Here’s one example:

# Example variable:
time_left = 120  # Time in seconds (2 minutes)

# Now add your variables for:
# - Whether the timer is running (a boolean)
# - What the secret code is (a string)
# - What code the user has entered so far (an empty string)

HINT: You need 4 variables total. For the timer running state, use a boolean (True/False). For the secret code, use a string like “1234”. For the user’s input, start with an empty string “”.

TASK 1.3: Display the title

NOTE: The following code is from a different program (a game scoreboard). Use it as inspiration for displaying your escape room title, but don’t copy it directly.

# Example from a game scoreboard:
score_turtle = turtle.Turtle()
score_turtle.hideturtle()
score_turtle.penup()
score_turtle.goto(0, 200)
score_turtle.color("green")
score_turtle.write("SCORE: 100", align="center", font=("Arial", 18, "bold"))

Now use your title turtle to write the title of your escape room. The title should be centered, bold, and in red color.

HINT: Follow the same pattern as the example, but change the text to “ESCAPE ROOM COUNTDOWN” and use a larger font size.

Part 2: Creating the Timer Functions (15 minutes)

TASK 2.1: Create display functions

We need functions to display the timer, messages, clue, and hint. Let’s start with the timer:

# Function to display the timer
def show_timer():
    # 1. Clear the timer turtle
    timer.clear()
    
    # 2. Change color based on time remaining
    # If less than 20 seconds, use red
    if time_left <= 20:
        timer.color("red")
    # Otherwise (for now) use white
    else:
        timer.color("white")
    
    # 3. Convert time_left to minutes and seconds
    # How many complete minutes in time_left?
    mins = time_left // 60
    # How many remaining seconds?
    secs = time_left % 60
    
    # 4. Create a time string in format "MM:SS"
    # The zfill() method adds leading zeros, e.g., 
    # "1".zfill(2) becomes "01"
    minutes_str = str(mins).zfill(2)
    seconds_str = str(secs).zfill(2)
    time_text = minutes_str + ":" + seconds_str
    
    # 5. Write the time using timer.write()
    timer.write(time_text, align="center", font=("Arial", 36, "bold"))

Now create these functions:

  • show_message(text, color="white") – displays messages to the user
  • show_clue() – displays the puzzle clue
  • show_hint(show=True) – displays a hint after some time passes

HINT: All these functions should follow a similar pattern: clear the turtle, set a color, and write some text.

TASK 2.2: Create timer control functions

NOTE: The following code is from a different program (a cooking timer). Use it as inspiration for your escape room timer, but don’t copy it directly.

# Example from a cooking timer:
def update_cooking_timer():
    global cooking_time
    
    if cooking_in_progress and cooking_time > 0:
        cooking_time -= 1
        display_cooking_time()
        
        # Check if food needs stirring
        if cooking_time == 30:
            show_stir_message()
            
        # Schedule next update in 1 second
        window.ontimer(update_cooking_timer, 1000)
    elif cooking_in_progress and cooking_time <= 0:
        cooking_in_progress = False
        show_finished_message()

Now create your update_timer() function for the escape room. It should:

  • Check if the timer should continue
  • Decrease the time by 1 second
  • Update the display
  • Show a hint after 30 seconds
  • Schedule itself to run again in 1 second
  • Handle what happens when time runs out

Also create:

  • start() – starts the timer and displays the clue
  • reset() – resets everything back to initial state

HINT: Remember to use the global keyword for variables you’ll be changing inside the function.

TASK 2.3: Add keyboard bindings for start and reset

First, we need to make the window listen for key presses:

# Make the window listen for key presses
wn.listen()

# Example: Bind the 's' key to the start function
wn.onkey(start, "s")

# Now add bindings for:
# - The 'S' key (uppercase) to the start function
# - The 'r' key to the reset function
# - The 'R' key to the reset function

HINT: Use wn.onkey(function_name, "key") for each binding.

Part 3: Implementing the Code Verification (15 minutes)

TASK 3.1: Create code verification functions

NOTE: The following code is from a different program (a login system). Use it as inspiration for your escape room code verification, but don’t copy it directly.

# Example from a login system:
def check_password():
    global access_granted
    
    if entered_password == correct_password:
        access_granted = True
        display_message("Access Granted!", "green")
        open_door()
    else:
        entered_password = ""  # Clear the input
        display_message("Access Denied! Try again.", "red")
        # Show error message briefly, then revert
        screen.ontimer(lambda: display_message("Enter Password:"), 1000)

Now create your check_code() function that verifies if the entered code matches the secret code. It should:

  • Check if the current code matches the secret code
  • If correct, stop the timer and display a success message
  • If incorrect, clear the input and show an error message

Also create a function to display the input as it’s typed:

# Function to display the current input
def show_input():
    # 1. Create a string of asterisks matching the input length
    stars = "*" * len(current_code)
    
    # 2. Display the masked input using show_message
    show_message("Code: " + stars)
    
    # 3. Check if code is complete (length matches secret code)
    if len(current_code) == len(code):
        # Call your code verification function
        check_code()

TASK 3.2: Handle number key presses

We need functions to handle each number key. Here’s one example:

# Function for key '1'
def press_1():
    global current_code
    if running:
        # Add '1' to the current code
        current_code += "1"
        # Update the display
        show_input()

Now create similar functions for the remaining digits (0, 2-9).

CHALLENGE: Instead of creating 10 separate functions, can you think of a way to create a single function that handles all number keys? (Hint: you’d need parameters)

TASK 3.3: Bind number keys

Connect the number keys to your functions:

# Bind number keys 0-9
wn.onkey(press_0, "0")
wn.onkey(press_1, "1")
# Continue binding keys 2-9

Part 4: Adding the Clue and Finishing Touches (10 minutes)

TASK 4.1: Customize your escape room clue

Choose a historical date or significant number as your secret code, and create a clue for it:

# Update your code variable with your chosen number
code = "????"  # Four-digit number

# Update your show_clue function to display a related question
def show_clue():
    clue.clear()
    clue.write("Your clue question here", 
              align="center", font=("Arial", 16, "italic"))

Some ideas:

  • Year of a historical event (moon landing, declaration of independence)
  • Famous math constant (first few digits of pi: 3141)
  • A pattern sequence (like 2468 for even numbers)

TASK 4.2: Add instructions and initialize display

Create instructions and set up the initial display:

# 1. Create a turtle for instructions
inst = turtle.Turtle()
inst.hideturtle()
inst.penup()
inst.goto(0, -190)
inst.color("gray")

# 2. Write instructions
inst.write("Instructions: Press 'S' to start, solve the clue for the code, 'R' to reset", 
          align="center", font=("Arial", 12, "normal"))

# 3. Initialize timer display
show_timer()

# 4. Show initial message
show_message("Press 'S' to start the timer")

TASK 4.3: Test and debug

Check your program for any errors or unexpected behavior:

  1. Does the timer start when you press ‘S’?
  2. Does it display the clue?
  3. Can you type numbers and see asterisks appear?
  4. Does the program recognize when you enter the correct code?
  5. Can you reset everything by pressing ‘R’?

Troubleshooting and Hints

Your program should:

  • Display a countdown timer that updates every second
  • Show a clue that leads to a 4-digit code
  • Accept number key inputs and display them as asterisks
  • Stop the timer when the correct code is entered
  • Allow resetting the game

Common issues:

  • Timer not updating? Make sure your update_timer function calls itself with wn.ontimer()
  • Code not registering? Check that your window is listening for keys with wn.listen()
  • Variables not changing? Remember to use the global keyword in functions
  • Text not appearing? Make sure your turtle positions don’t overlap
  • Trinket not responding? Try refreshing the page and pasting your code back in

Extension Challenges

If you finish early, try these enhancements:

  1. Visual countdown: Add a progress bar or circle that shrinks as time decreases
  2. Multiple difficulty levels: Create easy, medium, and hard modes with different times
  3. Multiple clues: Provide a sequence of clues that build toward the solution
  4. Animated success/failure: Create a special animation for winning or losing