Escape Room Timer – Python Programming Activity
Introduction
Welcome, you’re going to build an interactive escape room countdown timer that displays a riddle, counts down time, and checks if players enter the correct code to “escape” before time runs out.
Don’t worry if you’ve never programmed before – we’ll build this step by step, explaining every concept along the way. By the end, you’ll have created a fully functional game!
Section 1: Understanding the Basics – What is Programming?
What You’ll Learn
- What variables are and how to use them
- How to display text on screen
- Basic Python syntax
Key Concepts
What is a Variable? Think of a variable like a labeled box where you can store information. Just like you might have a box labeled “birthday cards” that contains your birthday cards, in programming you can have a variable labeled “player_name” that contains someone’s name.
What is a String? A string is a type of data that contains text. In Python, we put strings inside quotation marks like this: “Hello World” or ‘My name is Sarah’
What is an Integer? An integer is a whole number like 5, 42, or 100. Unlike strings, integers don’t need quotation marks.
Example Code – Your First Program
Let’s start with a simple example that shows text on screen:
import turtle
# This line imports the turtle library - think of it as getting a toolbox
# The turtle library lets us draw graphics and display text on screen
# Create a screen to draw on
screen = turtle.Screen()
screen.bgcolor("black") # Set background color to black
screen.setup(width=400, height=300) # Set screen size
# Create a turtle to write text with
text_writer = turtle.Turtle()
text_writer.hideturtle() # Hide the turtle arrow - we just want text
text_writer.penup() # Lift the pen so we don't draw lines
text_writer.goto(0, 0) # Move to center of screen
text_writer.color("white") # Set text color to white
# Write some text on screen
text_writer.write("Hello, Future Programmer!", align="center", font=("Arial", 16, "normal"))
# Keep the window open
screen.mainloop()
Breaking Down the Code:
import turtle– This gets the turtle graphics libraryscreen = turtle.Screen()– This creates a variable called “screen” that holds our graphics windowscreen.bgcolor("black")– This sets the background colortext_writer = turtle.Turtle()– This creates a variable that holds our text-writing tooltext_writer.write(...)– This displays text on screen
Your Turn – Section 1 Challenge
Create a program that displays your name and favorite color on the screen. Use the example above as a guide, but change:
- The text to show your name
- The background color to your favorite color
- The text color to something that looks good with your background
Hint: Colors in turtle can be “red”, “blue”, “green”, “yellow”, “purple”, “orange”, “white”, “black”
Section 2: Working with Numbers and Time
What You’ll Learn
- How to work with numbers in programming
- How to format time display
- How to convert between different units
Key Concepts
What is an Integer? Integers are whole numbers (no decimal points): 1, 2, 100, 3600 We use integers to count things, like seconds or minutes.
What are Mathematical Operations? Just like in math class, Python can do calculations:
+for addition:5 + 3 = 8-for subtraction:10 - 4 = 6*for multiplication:6 * 7 = 42//for division (whole numbers only):10 // 3 = 3%for remainder:10 % 3 = 1(10 divided by 3 is 3 remainder 1)
Example Code – Time Converter
import turtle
# Set up screen
screen = turtle.Screen()
screen.bgcolor("navy")
screen.setup(width=500, height=400)
# Create turtle for displaying time
time_display = turtle.Turtle()
time_display.hideturtle()
time_display.penup()
time_display.goto(0, 50)
time_display.color("cyan")
# Create turtle for showing calculations
calc_display = turtle.Turtle()
calc_display.hideturtle()
calc_display.penup()
calc_display.goto(0, -50)
calc_display.color("yellow")
# Here's our time in seconds - this is a variable holding a number
total_seconds = 125
# Convert seconds to minutes and seconds
# // means "divide and give me only the whole number"
minutes = total_seconds // 60 # 125 // 60 = 2 minutes
# % means "give me the remainder after division"
seconds = total_seconds % 60 # 125 % 60 = 5 seconds
# Create a nicely formatted time string
# We need to add leading zeros for single digits (like 05 instead of 5)
if minutes < 10:
minutes_text = "0" + str(minutes) # str() converts number to text
else:
minutes_text = str(minutes)
if seconds < 10:
seconds_text = "0" + str(seconds)
else:
seconds_text = str(seconds)
# Combine minutes and seconds with a colon
time_string = minutes_text + ":" + seconds_text
# Display the formatted time
time_display.write("Time: " + time_string, align="center", font=("Arial", 24, "bold"))
# Show the calculation
calc_display.write("125 seconds = 2 minutes and 5 seconds", align="center", font=("Arial", 14, "normal"))
screen.mainloop()
Key Learning Points:
total_seconds = 125creates a variable storing the number 125minutes = total_seconds // 60calculates whole minutesseconds = total_seconds % 60calculates remaining secondsstr(minutes)converts the number to text so we can display it- We add “0” to single digits to make times look like “02:05” instead of “2:5”
Your Turn – Section 2 Challenge
Create a program that takes a number of seconds and displays it as a properly formatted time. Try these values:
- 90 seconds (should show 01:30)
- 200 seconds (should show 03:20)
- 45 seconds (should show 00:45)
Extension Challenge: Also display how many hours, minutes, and seconds for larger numbers like 3665 seconds.
Section 3: Making Things Change – Your First Animation
What You’ll Learn
- How to make programs change over time
- What loops and timing are
- How to update displays
Key Concepts
What is a Timer? A timer is like setting an alarm clock. We tell the computer “wait 1 second, then do this task again.” This creates animation and change over time.
What is a Function? A function is like a recipe. It’s a set of instructions that we can run whenever we want. Instead of writing the same code over and over, we write it once in a function and then “call” (use) it whenever needed.
Example Code – Bouncing Ball Counter
import turtle
# Set up screen
screen = turtle.Screen()
screen.bgcolor("black")
screen.setup(width=600, height=400)
# Create a counter display
counter_display = turtle.Turtle()
counter_display.hideturtle()
counter_display.penup()
counter_display.goto(0, 0)
counter_display.color("white")
# Create a bouncing ball
ball = turtle.Turtle()
ball.shape("circle")
ball.color("red")
ball.penup()
ball.goto(-200, 0)
# Variables to track our animation
count = 0 # This will count how many times we've updated
ball_x = -200 # Ball's horizontal position
ball_direction = 5 # How fast the ball moves (5 pixels per update)
def update_animation():
"""
This is a function - a set of instructions we can repeat.
The three quotes create a comment explaining what the function does.
"""
# We need to tell Python these variables belong to the whole program
global count, ball_x, ball_direction
# Update the counter
count = count + 1
# Clear the old number and show the new one
counter_display.clear()
counter_display.write("Update #" + str(count), align="center", font=("Arial", 20, "normal"))
# Move the ball
ball_x = ball_x + ball_direction
ball.goto(ball_x, 0)
# If ball hits the edges, reverse direction
if ball_x > 200 or ball_x < -200:
ball_direction = ball_direction * -1 # Multiply by -1 to reverse
# Schedule this function to run again in 100 milliseconds (0.1 seconds)
screen.ontimer(update_animation, 100)
# Start the animation
update_animation()
# Keep window open
screen.mainloop()
Key Learning Points:
def update_animation():creates a function (a reusable set of instructions)globaltells Python we want to use variables from outside the functionscreen.ontimer(update_animation, 100)means “wait 100 milliseconds, then run this function again”- This creates a loop that repeats forever, creating animation
Your Turn – Section 3 Challenge
Modify the bouncing ball program to:
- Change the ball color every 10 updates
- Make the ball move faster or slower
- Add a second ball that moves up and down instead of left and right
Hint: For the color change, use an if statement: if count % 10 == 0: (this checks if count is divisible by 10)
Section 4: Responding to User Input
What You’ll Learn
- How to detect when users press keys
- How to respond to different types of input
- How to build interactive programs
Key Concepts
What is User Input? User input is any way a person interacts with your program – pressing keys, clicking buttons, etc. We need to “listen” for these inputs and decide what to do when they happen.
What is an Event? An event is something that happens – like a key press or mouse click. We write code that responds to these events.
Example Code – Interactive Color Changer
import turtle
# Set up screen
screen = turtle.Screen()
screen.bgcolor("black")
screen.setup(width=600, height=400)
# Create display
message = turtle.Turtle()
message.hideturtle()
message.penup()
message.goto(0, 50)
message.color("white")
instructions = turtle.Turtle()
instructions.hideturtle()
instructions.penup()
instructions.goto(0, -50)
instructions.color("gray")
# Variable to track current color
current_color = "white"
def show_message():
"""Function to display the current color"""
message.clear()
message.color(current_color)
message.write("Current color: " + current_color, align="center", font=("Arial", 24, "bold"))
def change_to_red():
"""Function that runs when 'r' key is pressed"""
global current_color # We want to change the main variable
current_color = "red"
show_message()
def change_to_blue():
"""Function that runs when 'b' key is pressed"""
global current_color
current_color = "blue"
show_message()
def change_to_green():
"""Function that runs when 'g' key is pressed"""
global current_color
current_color = "green"
show_message()
def change_to_yellow():
"""Function that runs when 'y' key is pressed"""
global current_color
current_color = "yellow"
show_message()
# Set up key listening
screen.listen() # Tell screen to listen for key presses
screen.onkey(change_to_red, "r") # When 'r' is pressed, run change_to_red function
screen.onkey(change_to_blue, "b") # When 'b' is pressed, run change_to_blue function
screen.onkey(change_to_green, "g") # When 'g' is pressed, run change_to_green function
screen.onkey(change_to_yellow, "y") # When 'y' is pressed, run change_to_yellow function
# Show initial message and instructions
show_message()
instructions.write("Press: R=Red, B=Blue, G=Green, Y=Yellow", align="center", font=("Arial", 14, "normal"))
screen.mainloop()
Key Learning Points:
screen.listen()tells the program to pay attention to key pressesscreen.onkey(function_name, "key")connects a key to a function- When the key is pressed, the function automatically runs
- Each function changes the global variable and updates the display
Your Turn – Section 4 Challenge
Create an interactive counter that:
- Shows a number starting at 0
- Increases by 1 when you press the “+” key
- Decreases by 1 when you press the “-” key
- Resets to 0 when you press the “r” key
Extension: Make the number change color based on its value (positive = green, negative = red, zero = white)
Section 5: Building Your Timer – Part 1 (Display and Countdown)
What You’ll Learn
- How to combine everything you’ve learned
- How to create a countdown timer
- How to format and display time properly
Key Concepts
Bringing It All Together Now we’ll use variables (to store time), functions (to update display), timers (to count down), and formatting (to make it look good) all in one program.
Example Code – Simple Countdown Timer
import turtle
# Set up screen
screen = turtle.Screen()
screen.bgcolor("black")
screen.setup(width=600, height=400)
# Create timer display
timer_display = turtle.Turtle()
timer_display.hideturtle()
timer_display.penup()
timer_display.goto(0, 0)
timer_display.color("white")
# Create instruction display
instructions = turtle.Turtle()
instructions.hideturtle()
instructions.penup()
instructions.goto(0, -100)
instructions.color("cyan")
# Timer variables
time_left = 30 # Start with 30 seconds
timer_running = False # Is the timer currently counting down?
def format_time(total_seconds):
"""
Function to convert seconds into MM:SS format
This function takes a number and returns formatted text
"""
minutes = total_seconds // 60
seconds = total_seconds % 60
# Add leading zeros if needed
if minutes < 10:
min_text = "0" + str(minutes)
else:
min_text = str(minutes)
if seconds < 10:
sec_text = "0" + str(seconds)
else:
sec_text = str(seconds)
return min_text + ":" + sec_text # Return the formatted string
def update_display():
"""Function to show the current time on screen"""
timer_display.clear()
# Change color based on time remaining
if time_left <= 5:
timer_display.color("red") # Red for danger!
elif time_left <= 10:
timer_display.color("orange") # Orange for warning
else:
timer_display.color("white") # White for normal
# Show the formatted time
formatted_time = format_time(time_left)
timer_display.write(formatted_time, align="center", font=("Arial", 48, "bold"))
def countdown():
"""Function that runs every second to count down"""
global time_left, timer_running
if timer_running and time_left > 0:
time_left = time_left - 1 # Subtract 1 second
update_display()
# Schedule next countdown in 1000 milliseconds (1 second)
screen.ontimer(countdown, 1000)
elif timer_running and time_left <= 0:
# Time's up!
timer_running = False
timer_display.color("red")
timer_display.clear()
timer_display.write("TIME'S UP!", align="center", font=("Arial", 36, "bold"))
def start_timer():
"""Function to start the countdown"""
global timer_running
if not timer_running: # Only start if not already running
timer_running = True
countdown()
def reset_timer():
"""Function to reset the timer"""
global time_left, timer_running
timer_running = False
time_left = 30
update_display()
# Set up key controls
screen.listen()
screen.onkey(start_timer, "s") # Press 's' to start
screen.onkey(reset_timer, "r") # Press 'r' to reset
# Show initial display and instructions
update_display()
instructions.write("Press 'S' to Start, 'R' to Reset", align="center", font=("Arial", 16, "normal"))
screen.mainloop()
Your Turn – Section 5 Challenge
Using the example above as a guide, create your own timer that:
- Starts with 2 minutes (120 seconds)
- Shows different colors at different time intervals:
- Green when time > 60 seconds
- Yellow when time is 31-60 seconds
- Orange when time is 11-30 seconds
- Red when time ≤ 10 seconds
- Shows a custom message when time runs out
Hint: Use elif statements to check different time ranges:
if time_left > 60:
timer_display.color("green")
elif time_left > 30:
timer_display.color("yellow")
# ... and so on
Section 6: Building Your Timer – Part 2 (Adding the Puzzle)
What You’ll Learn
- How to check user input against correct answers
- How to create interactive puzzles
- How to combine timers with problem-solving
Key Concepts
String Comparison We can check if two pieces of text are the same using ==:
"hello" == "hello"is True"hello" == "Hello"is False (capital letters matter!)"123" == "123"is True
Building Strings We can combine strings using +:
"Hello" + " " + "World"becomes"Hello World""Code: " + "1234"becomes"Code: 1234"
Example Code – Number Guessing Game
import turtle
# Set up screen
screen = turtle.Screen()
screen.bgcolor("dark blue")
screen.setup(width=700, height=500)
# Create displays
title = turtle.Turtle()
title.hideturtle()
title.penup()
title.goto(0, 150)
title.color("yellow")
title.write("Number Guessing Game", align="center", font=("Arial", 24, "bold"))
question = turtle.Turtle()
question.hideturtle()
question.penup()
question.goto(0, 100)
question.color("white")
question.write("What is 7 x 8?", align="center", font=("Arial", 18, "normal"))
guess_display = turtle.Turtle()
guess_display.hideturtle()
guess_display.penup()
guess_display.goto(0, 0)
guess_display.color("cyan")
message = turtle.Turtle()
message.hideturtle()
message.penup()
message.goto(0, -50)
message.color("white")
instructions = turtle.Turtle()
instructions.hideturtle()
instructions.penup()
instructions.goto(0, -150)
instructions.color("gray")
instructions.write("Type numbers 0-9, then press ENTER to check", align="center", font=("Arial", 14, "normal"))
# Game variables
correct_answer = "56" # 7 x 8 = 56
current_guess = "" # What the player has typed so far
game_over = False # Is the game finished?
def update_guess_display():
"""Show the current guess on screen"""
guess_display.clear()
guess_display.write("Your guess: " + current_guess, align="center", font=("Arial", 20, "normal"))
def check_answer():
"""Check if the guess is correct"""
global game_over
if current_guess == correct_answer:
message.color("green")
message.write("Correct! Well done!", align="center", font=("Arial", 18, "bold"))
game_over = True
else:
message.color("red")
message.write("Wrong! Try again.", align="center", font=("Arial", 18, "normal"))
# Clear the message after 2 seconds
screen.ontimer(clear_message, 2000)
def clear_message():
"""Clear the message display"""
message.clear()
def add_digit(digit):
"""Add a digit to the current guess"""
global current_guess
if not game_over and len(current_guess) < 3: # Limit to 3 digits
current_guess = current_guess + digit
update_guess_display()
def clear_guess():
"""Clear the current guess"""
global current_guess
if not game_over:
current_guess = ""
update_guess_display()
message.clear()
def submit_guess():
"""Submit the current guess for checking"""
if not game_over and current_guess != "":
check_answer()
# Functions for each number key
def press_0(): add_digit("0")
def press_1(): add_digit("1")
def press_2(): add_digit("2")
def press_3(): add_digit("3")
def press_4(): add_digit("4")
def press_5(): add_digit("5")
def press_6(): add_digit("6")
def press_7(): add_digit("7")
def press_8(): add_digit("8")
def press_9(): add_digit("9")
# Set up key controls
screen.listen()
screen.onkey(press_0, "0")
screen.onkey(press_1, "1")
screen.onkey(press_2, "2")
screen.onkey(press_3, "3")
screen.onkey(press_4, "4")
screen.onkey(press_5, "5")
screen.onkey(press_6, "6")
screen.onkey(press_7, "7")
screen.onkey(press_8, "8")
screen.onkey(press_9, "9")
screen.onkey(submit_guess, "Return") # Enter key
screen.onkey(clear_guess, "BackSpace") # Backspace key
# Initialize display
update_guess_display()
screen.mainloop()
Your Turn – Section 6 Challenge
Create a riddle-solving game that:
- Shows a riddle: “I have cities, but no houses. I have mountains, but no trees. I have water, but no fish. What am I?”
- Accepts the answer “map” (hint: you might want to convert to lowercase)
- Shows “Correct!” in green if right, “Try again!” in red if wrong
- Allows the player to keep guessing until they get it right
Hint: To convert text to lowercase: current_guess.lower()
Section 7: Your Final Challenge – Complete Escape Room Timer
What You’ll Learn
- How to combine all previous concepts
- How to structure a complete program
- How to add finishing touches
Your Mission
Now it’s time to put everything together! Using all the concepts you’ve learned, create a complete escape room timer that includes:
Required Features:
- Timer Display: Shows countdown in MM:SS format
- Color Changes: Timer changes color as time runs low
- Riddle Display: Shows a challenging riddle for players to solve
- Code Entry: Players can type in their answer
- Success/Failure: Different outcomes for solving vs. running out of time
- Controls: Start, reset, and code entry functionality
Suggested Structure:
# 1. Import turtle and set up screen
# 2. Create all your turtle objects (timer, riddle, message displays)
# 3. Set up your variables (time_left, correct_code, current_guess, etc.)
# 4. Create your functions:
# - format_time() - converts seconds to MM:SS
# - update_timer_display() - shows current time with colors
# - countdown() - reduces time and schedules next update
# - start_timer() - begins the countdown
# - reset_timer() - resets everything
# - show_riddle() - displays your puzzle
# - check_code() - verifies the answer
# - Functions for each number key (0-9)
# 5. Set up key bindings
# 6. Initialize your displays
# 7. Start the main loop
Creative Additions (Optional):
- Add a hint that appears after some time has passed
- Include sound effects (research
winsoundmodule) - Add multiple riddles that players must solve in sequence
- Create different difficulty levels with different time limits
- Add visual effects like flashing text when time is almost up
Sample Riddles You Could Use:
- “What has keys but no locks, space but no room, and you can enter but not go inside?” (Answer: keyboard)
- “I am not alive, but I grow; I don’t have lungs, but I need air; I don’t have a mouth, but water kills me. What am I?” (Answer: fire)
- “The more you take, the more you leave behind. What am I?” (Answer: footsteps)
Testing Your Program
Make sure to test:
- Does the timer count down correctly?
- Do the colors change at the right times?
- Can you enter the code using number keys?
- Does it detect the correct answer?
- Can you start and reset properly?
- What happens when time runs out?
Debugging Tips
If your program isn’t working:
- Check your spelling –
timer_runingvstimer_running - Check your indentation – Python is very picky about spaces
- Use print statements – Add
print("Timer started!")to see what’s happening - Test one piece at a time – Don’t try to build everything at once
- Read error messages carefully – They often tell you exactly what’s wrong
Conclusion
Congratulations! You’ve just built a complete interactive program using fundamental programming concepts. You’ve learned about:
- Variables – storing information
- Functions – organizing code into reusable pieces
- User Input – responding to key presses
- Timers – making things happen over time
- Conditionals – making decisions in code
- String manipulation – working with text
These are the building blocks of all programming. Every app, game, and website uses these same concepts, just on a larger scale.