Editor

Eat and grow

Pygame challenges · Snake, step 3 of 4 · 25 XP

The snake swims over the food and stays the same length.

Your task

Write step(snake, direction, food). snake is a list of cells with the head first. Moving adds the new head in front and drops the last cell. When the new head is on the food, the last cell stays, so the snake is one cell longer.

Return the new snake and True if it ate, or the moved snake and False.

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

CallShould give
moves without eatingstep([(2, 2), (1, 2)], (1, 0), (9, 9))([(3, 2), (2, 2)], False)
eats and growsstep([(2, 2), (1, 2)], (1, 0), (3, 2))([(3, 2), (2, 2), (1, 2)], True)
a snake of one cellstep([(5, 5)], (0, 1), (0, 0))([(5, 6)], False)
does not change the list it was givenbefore = [(2, 2), (1, 2)] step(before, (1, 0), (3, 2))before[(2, 2), (1, 2)]