Exercise 4 · Chapter: GravityRequired+50 XP
Losing Height
A real ball doesn't bounce back to the same height — it loses energy every time and eventually settles.
Keep only three quarters of the speed on each bounce:
state["vy"] = -state["vy"] * 3 // 4
// is whole-number division from the Math chapter. The grid only has whole
rows, so keeping whole numbers here saves you from a dot at row 7.3.
Watch the bounces in the numbers: each one starts with a smaller vy than the
last, and the ball climbs less far each time before gravity wins.
Expected output
1/1 3/2 6/3 10/4 13/-4 10/-3 8/-2 7/-1 7/0 8/1 10/2 13/-3 11/-2 10/-1 10/0 11/1 13/-2 12/-1 12/0 13/-1
Need a hint?
3 available · costs 5 XP each
python
def new_game():
return {"y": 0, "vy": 0}
def update(state, keys):
state["vy"] = state["vy"] + 1
state["y"] = state["y"] + state["vy"]
if state["y"] >= HEIGHT - 1:
state["y"] = HEIGHT - 1
state["vy"] = -state["vy"] # keeps ALL the speed — take some away
return state
def draw(state):
grid = blank_grid()
grid[state["y"]][20] = "white"
return grid