Stage 8: Win and Lose States
Course progressStage 8 of 10
One game, one Trinket
Keep building in the workspace on the right.
This stage is part of the same Crewmate Task Dash project you started in Setup. Type each new code block into the Trinket rail and keep building on the last stage.
Build
capture checks and all-tasks-complete victory
Learn
how conditions end a game loop
Ship
a round that can be won or lost
The big idea
A game needs rules for ending. Capture ends the round one way; finishing every task ends it another way.
- collision
- when two game objects touch or get close enough
- win state
- the condition that means the player won
- lose state
- the condition that means the player lost
- return
- leave a function immediately
Tasks 2/4Score 80
The player moves through the ship, collects tasks, and avoids the chaser. All playable shapes are drawn with Python Turtle code.
Build it
Type, run, test
Read the code aloud before you run it. The goal is to understand what changed in the game.
Need a hint?
Add the new code to the same Trinket project. Keep previous stage code unless the stage says to replace a function.
Write this part
main.py
Add this to your current Trinket file.
def end_game(message):
global game_on
game_on = False
hud.setposition(0, 0)
hud.write(message, align="center", font=("Arial", 24, "bold"))
def game_loop():
check_tasks()
move_chaser()
if chaser.distance(player) < 28:
end_game("Captured! Try again.")
return
if tasks_done == 4:
end_game("All tasks complete! You win!")
return
screen.ontimer(game_loop, 80)Trace the idea
- The capture check compares two Turtle positions.
- The task check compares progress to the total.
- `return` stops the loop from scheduling another frame.
Try this
Try this
Three short experiments. Predict before you run, then test your guess.
Predict first
Before running, predict which line will visibly change the screen first.
Compare
Change one number, run, then change it back. Which version feels more playable?
Connect
How does this stage make the final game more like a real project?
Test your stage
- Touching the chaser ends the game.
- Completing all tasks wins the game.
- The chaser stops after the game ends.