diff --git a/README.md b/README.md index 3a0abf4..06633b2 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ The classic Snake game. It runs in your web browser from a single file and works - **Arrow keys** move the snake. - Eat the **red food** to grow and score a point. The snake speeds up a little each time it eats. -- The game ends if you hit a wall or your own tail. +- There are no deadly walls: go off one edge and the snake comes back in on the opposite side. +- The game ends if you run into your own tail. - On the Game Over screen, click **Play Again** or press **Space** or **Enter** to restart. - Press **P** to pause and resume. diff --git a/index.html b/index.html index 943eba4..80b77e2 100644 --- a/index.html +++ b/index.html @@ -83,7 +83,7 @@
@@ -184,14 +184,17 @@ // Use the next queued turn, if any (lets quick key presses register one per move). if (inputQueue.length) direction = inputQueue.shift(); - const head = { x: snake[0].x + direction.x, y: snake[0].y + direction.y }; + // Going off one edge brings the snake back in on the opposite side. + const head = { + x: (snake[0].x + direction.x + GRID) % GRID, + y: (snake[0].y + direction.y + GRID) % GRID, + }; const eating = food && head.x === food.x && head.y === food.y; // The tail moves away this turn unless we're eating, so it's safe to move into. const body = eating ? snake : snake.slice(0, -1); - const hitWall = head.x < 0 || head.y < 0 || head.x >= GRID || head.y >= GRID; const hitSelf = body.some(s => s.x === head.x && s.y === head.y); - if (hitWall || hitSelf) { + if (hitSelf) { gameOver(); return; }