Exercise 5 · Chapter: Press to FlapOptional+50 XP
Jump on a Treadmill
Put the jump into the scrolling world.
- ground marks on row
13, sliding with the clock - the hero at column
6, jumping when space is pressed - nothing to hit, nothing to score — just a runner who can jump
This is the first thing in the milestone that feels like a game. It isn't one yet: you cannot win and you cannot lose. But every mechanical piece is now on screen and under your control.
The next chapter adds the only missing ingredient — something that can go wrong.
Expected output
........................................ ........................................ ........................................ ........................................ ........................................ ........................................ .......AA............................... ......AAAA.............................. .......A.A.............................. ........................................ ........................................ ........................................ ........................................ ###B###B###B###B###B###B###B###B###B###B ........................................ ........................................ ........................................ .......AA............................... ......AAAA.............................. .......A.A.............................. ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ##B###B###B###B###B###B###B###B###B###B# ........................................ .......AA............................... ......AAAA.............................. .......A.A.............................. ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ #B###B###B###B###B###B###B###B###B###B## .......AA............................... ......AAAA.............................. .......A.A.............................. ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ B###B###B###B###B###B###B###B###B###B###
Need a hint?
3 available · costs 5 XP each
python
HERO = [
[0, 1, 1, 0],
[1, 1, 1, 1],
[0, 1, 0, 1],
]
def stamp(grid, sprite, top, left, colour):
for r in range(len(sprite)):
for c in range(len(sprite[r])):
if sprite[r][c] == 1:
if 0 <= top + r < HEIGHT and 0 <= left + c < WIDTH:
grid[top + r][left + c] = colour
def new_game():
return {"y": 0, "vy": 0, "t": 0}
def update(state, keys):
state["t"] = state["t"] + 1
if keys["space"] and state["y"] == 0:
state["vy"] = 4
state["y"] = state["y"] + state["vy"]
state["vy"] = state["vy"] - 1
if state["y"] < 0:
state["y"] = 0
state["vy"] = 0
return state
def draw(state):
grid = blank_grid()
# Scrolling ground on row 13, then the hero at column 6.
return grid