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

Coin Quest: Make Him Jump

What this step looks like when it works

🪙 A hero who can't jump isn't much of a hero.

Write update(state, keys). It runs once per frame, changes the state, and returns it.

Here's the trick real games use. The hero has a speed, state["vy"]:

  1. If space is held and the hero is on the ground (y == 0), set vy to 3
  2. Every frame, add vy to y — that's the moving
  3. Every frame, subtract 1 from vy — that's gravity
  4. If y ever goes below 0, set both y and vy back to 0 — that's the landing

Gravity is doing all the work. Speed starts at 3, drops to 2, 1, 0, then goes negative and pulls him back down. You never say "now come down" — it just happens.

Press Run, then press space.

Expected output
0 0 3 5 6 6 5 3 0 0 0 0
Need a hint?
4 available · costs 5 XP each
python
def update(state, keys):
# 1. If space is held and state["y"] is 0, set state["vy"] to 3

# 2. Add state["vy"] to state["y"]

# 3. Subtract 1 from state["vy"]

# 4. If state["y"] is below 0, set state["y"] and state["vy"] to 0

return state


def draw(state):
grid = blank_grid()
row = GROUND - state["y"]
grid[row][5] = "gold"
grid[row][6] = "gold"
grid[row - 1][5] = "gold"
grid[row - 1][6] = "gold"
return grid