Complete the direction-control system. Fill each blank so the snake responds to arrow keys and moves correctly.
direction = ▢1 # starting direction
for event in pygame.event.get():
if event.type == pygame.▢2:
if event.key == pygame.K_UP and direction != ▢3:
direction = "UP"
elif event.key == pygame.K_DOWN and direction != ▢4:
direction = "DOWN"
elif event.key == pygame.K_LEFT and direction != ▢5:
direction = "LEFT"
elif event.key == pygame.K_RIGHT and direction != ▢6:
direction = "RIGHT"
hx, hy = head_x, head_y
if direction == "UP": hy ▢7= 1
elif direction == "DOWN": hy ▢8= 1
elif direction == "LEFT": hx ▢9= 1
elif direction == "RIGHT": hx ▢10= 1
"______K______"_____"___"______"_________The direction-control code below has 3 bugs. Type the corrected line for each.
direction = "RIGHT"
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and direction != "UP": # Bug 1
direction = "UP"
elif event.key == pygame.K_DOWN and direction != "DOWN": # Bug 1 (same guard issue)
direction = "DOWN"
hx, hy = head_x, head_y
if direction == "UP": hx -= 1 # Bug 2 – wrong axis
elif direction == "DOWN": hx += 1 # Bug 2
elif direction == "LEFT": hx -= 1
elif direction == "RIGHT": hx += 1
# Bug 3: elif chains missing – only first matching if fires
if direction == "LEFT": hx -= 1
Bug 1
Buggy line:
Your fix:
Hint: What direction would reverse the snake going UP? The guard should block that opposite.
Bug 2
Buggy line:
Your fix:
Hint: Moving UP changes the row (y), not the column (x).
Bug 3
Buggy line:
Your fix:
Hint: Without elif, every matching branch executes — the snake double-moves. Use elif.
Add **WASD controls** alongside the arrow keys so either set works. Both should respect the opposite-direction guard.
direction = "RIGHT"
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
# Arrow keys already handled:
if event.key == pygame.K_UP and direction != "DOWN": direction = "UP"
elif event.key == pygame.K_DOWN and direction != "UP": direction = "DOWN"
elif event.key == pygame.K_LEFT and direction != "RIGHT": direction = "LEFT"
elif event.key == pygame.K_RIGHT and direction != "LEFT": direction = "RIGHT"
# YOUR CODE: add W/A/S/D handling here
**Challenge:** Replace the `if/elif` chain with a **dictionary lookup**. Define a dict that maps each key constant to a `(new_direction, forbidden_opposite)` pair, then use it to update direction in two lines of code. Paste your solution.
Answer all 5 questions, then the Reflective Journal will unlock.
1. How is the snake's current direction typically stored in the game?
2. What condition prevents the snake from reversing directly?
3. if event.type == pygame.KEYDOWN: — what does this test?
4. Which direction vector moves the snake one cell to the RIGHT?
5. What should happen if the player presses the LEFT key while the snake moves RIGHT?
Answer each prompt thoughtfully. There are no right or wrong answers — this is your own reflection.