Exerpad
🔥 0
0 XP
Exercise 5 · Chapter: Colour and TimeOptional+50 XP

A Pulse

A square that grows and shrinks, over and over.

% alone can't do this: t % 8 counts 0 1 2 3 4 5 6 7 and then snaps back to 0. That's a sawtooth — it grows, then jumps. A pulse has to come back down smoothly.

abs() folds it in half:

size = abs(4 - state["t"] % 8)

That gives 4 3 2 1 0 1 2 3 4 3 2 1 0… — up and down forever. Subtracting from the middle and taking the absolute value is the standard way to turn a repeating count into a repeating bounce, and it shows up everywhere once you know it.

Draw a gold square of that size, centred at row 6, column 20: paint every cell within size rows and columns of the centre.

Expected output
........................................
........................................
........................................
.................AAAAAAA................
.................AAAAAAA................
.................AAAAAAA................
.................AAAAAAA................
.................AAAAAAA................
.................AAAAAAA................
.................AAAAAAA................
........................................
........................................
........................................
########################################
........................................
........................................
........................................
........................................
..................AAAAA.................
..................AAAAA.................
..................AAAAA.................
..................AAAAA.................
..................AAAAA.................
........................................
........................................
........................................
........................................
########################################
........................................
........................................
........................................
........................................
........................................
...................AAA..................
...................AAA..................
...................AAA..................
........................................
........................................
........................................
........................................
........................................
########################################
........................................
........................................
........................................
........................................
........................................
........................................
....................A...................
........................................
........................................
........................................
........................................
........................................
........................................
########################################
........................................
........................................
........................................
........................................
........................................
...................AAA..................
...................AAA..................
...................AAA..................
........................................
........................................
........................................
........................................
........................................
########################################
Need a hint?
3 available · costs 5 XP each
python
def new_game():
return {"t": 0}


def update(state, keys):
state["t"] = state["t"] + 1
return state


def draw(state):
grid = blank_grid()
size = 4 # always the same — make it pulse with abs(4 - t % 8)
for row in range(6 - size, 6 + size + 1):
for col in range(20 - size, 20 + size + 1):
grid[row][col] = "gold"
return grid