Exerpad
🔥 0
0 XP
Exercise 5 · Chapter: Many at OnceOptional+50 XP

Real Rain

Eight drops, each with its own column, starting row and speed — and none of it typed out by hand.

Build the list in new_game with a loop:

drops = []
for i in range(8):
    drops.append({"y": i, "speed": 1 + i % 3, "x": i * 5})
return {"drops": drops}

1 + i % 3 cycles the speeds through 1, 2, 3, 1, 2, 3… — a cheap way to get variety without randomness. And i * 5 spreads them across the width.

Then draw each drop at its own "x" instead of a column worked out from the index.

This is a real rain effect, and it is the same four lines you have been writing since the start of the chapter. Adding the ninth drop is now a change to one number.

Expected output
........................................
A.......................................
........................................
.....A..................................
...............A........................
..........A.............................
....................A...................
..............................A.........
.........................A..............
...................................A....
........................................
........................................
........................................
########################################
........................................
........................................
A.......................................
........................................
........................................
.....A.........A........................
........................................
........................................
..........A.........A.........A.........
........................................
........................................
.........................A.........A....
........................................
########################################
Need a hint?
3 available · costs 5 XP each
python
def new_game():
drops = []
# Build 8 drops with a loop: y = i, speed = 1 + i % 3, x = i * 5
return {"drops": drops}


def update(state, keys):
for i in range(len(state["drops"])):
drop = state["drops"][i]
drop["y"] = drop["y"] + drop["speed"]
if drop["y"] > HEIGHT - 1:
drop["y"] = 0
return state


def draw(state):
grid = blank_grid()
for i in range(len(state["drops"])):
drop = state["drops"][i]
grid[drop["y"]][drop["x"]] = "aqua"
return grid