Exerpad
🔥 0
0 XP
Exercise 6 · Chapter: Coin QuestOptional+50 XP

Coin Quest: Game Over

What this step looks like when it works

🪙 The last piece. Right now, crashing just freezes everything — no message, no second chance.

Remember the very first Coin Quest exercise, back in the Variables chapter? You made three variables: player, score, and is_playing. You've been carrying score and is_playing ever since. Here's where is_playing finally earns its keep.

1. Write hud(state). It returns the line of text shown under the game.

  • Still playing → Coins: 3
  • Game over → GAME OVER - Coins: 3 - press R

Remember str() — you can't join a number onto text without it.

2. Add restart. Make the very first line of update:

if keys["r"]:
    return new_game()

Returning a brand-new state wipes everything — position, rocks, coins, score — in one line. That's why new_game() was worth writing as a function.

Where you'll use this
Every game you've ever lost showed you a screen like this. It's not decoration: without a way to see your score and start again, a game is just a thing that stops.

Crash on purpose. Read the message. Press R. Go again.

🎉 That's Coin Quest — a real game, built from print statements up.

Expected output
1/0/Coins: 0
1/0/Coins: 0
1/0/Coins: 0
1/0/Coins: 0
1/0/Coins: 0
1/0/Coins: 0
1/0/Coins: 0
0/0/GAME OVER - Coins: 0 - press R
0/0/GAME OVER - Coins: 0 - press R
1/0/Coins: 0
1/0/Coins: 0
1/0/Coins: 0
Need a hint?
3 available · costs 5 XP each
python
def add_coins(coins, amount):
return coins + amount


def hud(state):
# Return "Coins: 3" while playing
# Return "GAME OVER - Coins: 3 - press R" once is_playing is False
return ""


def new_game():
return {
"y": 0,
"vy": 0,
"x": 0,
"score": 0,
"is_playing": True,
"rocks": [14, 26],
"coins": [14, 26],
}


def update(state, keys):
# Press R to start over

if not state["is_playing"]:
return state

if keys["space"] and state["y"] == 0:
state["vy"] = 3
state["y"] = state["y"] + state["vy"]
state["vy"] = state["vy"] - 1
if state["y"] < 0:
state["y"] = 0
state["vy"] = 0
state["x"] = state["x"] + 1