Editor

Tunnels at the edges

Pygame challenges · Maze Chase, step 2 of 4 · 20 XP

The tunnels at the sides of the maze are dead ends.

Your task

The middle row of the maze is open at both ends: leave on the left and you come back on the right. The % operator (the remainder after dividing) does the wrapping. With a width of 19, 19 % 19 is 0 and -1 % 19 is 18, while columns 0 to 18 stay as they are, so column % width is always a column inside the maze.

Write next_cell(grid, cell, direction). direction is how far to move: (1, 0) is right, (-1, 0) left, (0, -1) up and (0, 1) down. Add it to cell, wrap the column with % width (the width is len(grid[0])), and return the new cell as a tuple (column, row). Rows do not wrap. If the new cell is a wall, or above the top or below the bottom, the player stays where it is: return cell itself. Your can_move from the last step tells you which.

The game moves the player with player = next_cell(grid, player, direction). A new arrow key takes over as soon as next_cell in its direction is not the cell the player is on, so you can press a turn before you reach it.

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
right, into an empty pathgrid = ["#####", "#. .#", " .#. ", "#####"]next_cell(grid, (1, 1), (1, 0))(2, 1)
down onto a dotgrid = ["#####", "#. .#", " .#. ", "#####"]next_cell(grid, (1, 1), (0, 1))(1, 2)
into a wall: staygrid = ["#####", "#. .#", " .#. ", "#####"]next_cell(grid, (1, 1), (-1, 0))(1, 1)
out of the right end of the tunnelgrid = ["#####", "#. .#", " .#. ", "#####"]next_cell(grid, (4, 2), (1, 0))(0, 2)
out of the left endgrid = ["#####", "#. .#", " .#. ", "#####"]next_cell(grid, (0, 2), (-1, 0))(4, 2)
the top edge does not wrapgrid = ["#.#", "#.#"]next_cell(grid, (1, 0), (0, -1))(1, 0)