No U-turns
Pygame challenges · Snake, step 4 of 4 · 20 XP
Press the arrow pointing back and the snake bites itself at once.
Your task
Write turn(direction, wanted). It returns wanted, the arrow just pressed, unless that points straight back the way the snake is going: then it keeps direction.
The opposite of a direction (dx, dy) is (-dx, -dy).
The function is at the top of the program, marked fix this. Press Run to play the game as it is, then Check when your function is ready.
What Check tests
| Call | Should give |
|---|---|
a quarter turn is fineturn((1, 0), (0, 1)) | (0, 1) |
straight back is ignoredturn((1, 0), (-1, 0)) | (1, 0) |
up then down is ignoredturn((0, -1), (0, 1)) | (0, -1) |
the same way againturn((0, 1), (0, 1)) | (0, 1) |