Exerpad
🔥 0
0 XP
Exercise 4 · Chapter: BounceRequired+50 XP

Up and Down

Same trick, other axis.

Make the dot bounce between the top of the picture and the bottom, using state["y"] and state["dy"].

Start at y of 10 with dy of 1. The floor is HEIGHT - 1, the ceiling is 0.

If you find yourself copying the last exercise and swapping letters — good. That's the pattern working. Sideways and downwards were never different ideas.

Expected output
11/1 12/1 13/-1 12/-1 11/-1 10/-1 9/-1 8/-1 7/-1 6/-1 5/-1 4/-1 3/-1 2/-1 1/-1 0/1 1/1 2/1 3/1 4/1 5/1 6/1 7/1 8/1 9/1 10/1 11/1 12/1 13/-1 12/-1
Need a hint?
3 available · costs 5 XP each
python
def new_game():
return {"y": 10, "dy": 1}


def update(state, keys):
# Move by dy, then bounce off the top and the bottom.
return state


def draw(state):
grid = blank_grid()
grid[state["y"]][20] = "cyan"
return grid