Crash detection
Pygame challenges · Snake, step 2 of 4 · 20 XP
The snake goes through walls and through itself without a scratch.
Your task
Write crashed(head, body, cols, rows). It is True when the head is off the board (columns go from 0 to cols - 1, rows from 0 to rows - 1) or on a cell of body, a list of cells.
Your next_head from the last step moves the snake.
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 |
|---|---|
an open cell is safecrashed((3, 3), [(4, 3), (5, 3)], 10, 10) | False |
off the left edgecrashed((-1, 3), [], 10, 10) | True |
off the right edge (column 10 of 10)crashed((10, 0), [], 10, 10) | True |
off the bottomcrashed((4, 10), [], 10, 10) | True |
into its own bodycrashed((4, 3), [(4, 3), (5, 3)], 10, 10) | True |
the last cell is still on the boardcrashed((9, 9), [], 10, 10) | False |