Complete the wrap-around movement. Fill each blank so the snake teleports from one edge to the opposite.
# direction -> (dx, dy) lookup
dx, dy = ▢1[direction]
hx, hy = snake[0]
hx = (hx + dx) % ▢2 # wraps left ↔ right
hy = (hy + dy) % ▢3 # wraps top ↔ bottom
new_head = (hx, hy)
# Trace examples (GRID_COLS=20, GRID_ROWS=15):
# hx=19, moving RIGHT: (19+1) % 20 = ▢4 (wraps to left edge)
# hx=0, moving LEFT: ( 0-1) % 20 = ▢5 (wraps to right edge)
# hy=0, moving UP: ( 0-1) % 15 = ▢6 (wraps to bottom)
"___________________________________________________G________G_________1_1_The boundary code below has 3 bugs. Type the corrected line for each.
dx, dy = {"RIGHT":(1,0),"LEFT":(-1,0),"UP":(0,-1),"DOWN":(0,1)}[direction]
hx, hy = snake[0]
hx = (hx + dx) % GRID_ROWS # Bug 1 – wrong constant
hy = (hy + dy) % GRID_ROWS
# Wall-death mode:
WALL_DEATH = True
if WALL_DEATH:
alive = (0 <= hx < GRID_COLS) or (0 <= hy < GRID_ROWS) # Bug 2 – wrong operator
if not alive:
running == False # Bug 3 – comparison not assignment
Bug 1
Buggy line:
Your fix:
Hint: The x-axis (columns) wraps at GRID_COLS, not GRID_ROWS.
Bug 2
Buggy line:
Your fix:
Hint: Both coordinates must be in bounds simultaneously — use `and`, not `or`.
Bug 3
Buggy line:
Your fix:
Hint: `==` is a comparison that produces True/False but does nothing. `=` is the assignment that actually changes the variable.
Implement a `move_head(hx, hy, direction, wall_death)` function that returns `(new_hx, new_hy, alive)`. When `wall_death=True`, use bounds checking; when `False`, use modulo wrap.
def move_head(hx, hy, direction, wall_death=False):
dx, dy = {"RIGHT":(1,0),"LEFT":(-1,0),"UP":(0,-1),"DOWN":(0,1)}[direction]
# YOUR CODE:
# If wall_death: compute new position, set alive = in-bounds check
# Else: use modulo wrap, alive = True
pass # replace with your implementation
**Challenge:** Add a **WALL_DEATH toggle** controlled by pressing the W key during gameplay. Display the current mode ("Wrap" or "Wall") in the corner. Paste your updated code.
Answer all 5 questions, then the Reflective Journal will unlock.
1. def draw_snake(body): — what is body in this line?
2. What does a function return when it has no return statement?
3. Why call draw_snake(body) instead of pasting the drawing code everywhere?
4. What is variable scope?
5. def is_collision(head, body): return head in body — what type of value does this return?
Answer each prompt thoughtfully. There are no right or wrong answers — this is your own reflection.