Exercise 1 · Chapter: Press to FlapRequired+50 XP
Height, Not Row
Get used to the new way of measuring before anything moves.
The state has "y": 3 — the hero is three squares above the ground. Work out
the grid row with:
row = GROUND - state["y"]
then stamp the hero so its feet land on that row. The sprite is 3 rows
tall, so its top goes at row - 2.
Nothing moves in this exercise. The only job is turning a height into a row, and it's worth doing on its own, because getting this backwards is the bug you'll otherwise spend the next two chapters chasing.
Expected output
........................................ ........................................ ........................................ ........................................ ........................................ ........................................ ........................................ .......AA............................... ......AAAA.............................. .......A.A.............................. ........................................ ........................................ ........................................ ########################################
Need a hint?
3 available · costs 5 XP each
python
HERO = [
[0, 1, 1, 0],
[1, 1, 1, 1],
[0, 1, 0, 1],
]
def stamp(grid, sprite, top, left, colour):
for r in range(len(sprite)):
for c in range(len(sprite[r])):
if sprite[r][c] == 1:
if 0 <= top + r < HEIGHT and 0 <= left + c < WIDTH:
grid[top + r][left + c] = colour
def new_game():
return {"y": 3, "vy": 0}
def update(state, keys):
return state
def draw(state):
grid = blank_grid()
row = state["y"] # wrong way round — convert the height to a row
stamp(grid, HERO, row - 2, 6, "gold")
return grid