Exerpad
🔥 0
0 XP
Exercise 4 · Chapter: Many at OnceRequired+50 XP

Own Speeds

Rain where every drop falls at the same speed looks fake. Give each one its own.

A drop is now a dictionary instead of a bare number:

{"y": 0, "speed": 1}

new_game already builds three of them, at speeds 1, 2 and 3. Move each by its own "speed", and recycle it to y of 0 when it passes the bottom.

Here's something worth noticing: with dictionaries, this works —

drop = state["drops"][i]
drop["y"] = drop["y"] + drop["speed"]

— even though the same shape did nothing in the last exercise. A dictionary isn't copied when you pull it out of a list; drop and the thing in the list are the same dictionary. Numbers copy. Dictionaries don't.

Expected output
[{'y': 1, 'speed': 1}, {'y': 2, 'speed': 2}, {'y': 3, 'speed': 3}] [{'y': 2, 'speed': 1}, {'y': 4, 'speed': 2}, {'y': 6, 'speed': 3}] [{'y': 3, 'speed': 1}, {'y': 6, 'speed': 2}, {'y': 9, 'speed': 3}] [{'y': 4, 'speed': 1}, {'y': 8, 'speed': 2}, {'y': 12, 'speed': 3}] [{'y': 5, 'speed': 1}, {'y': 10, 'speed': 2}, {'y': 0, 'speed': 3}] [{'y': 6, 'speed': 1}, {'y': 12, 'speed': 2}, {'y': 3, 'speed': 3}] [{'y': 7, 'speed': 1}, {'y': 0, 'speed': 2}, {'y': 6, 'speed': 3}] [{'y': 8, 'speed': 1}, {'y': 2, 'speed': 2}, {'y': 9, 'speed': 3}]
Need a hint?
3 available · costs 5 XP each
python
def new_game():
return {"drops": [{"y": 0, "speed": 1}, {"y": 0, "speed": 2}, {"y": 0, "speed": 3}]}


def update(state, keys):
for i in range(len(state["drops"])):
drop = state["drops"][i]
# Move this drop by its own speed, then recycle it past the bottom.
return state


def draw(state):
grid = blank_grid()
for i in range(len(state["drops"])):
grid[state["drops"][i]["y"]][8 + i * 10] = "aqua"
return grid