The snake game operates by rendering a canvas, the snake segments, and the apple with each new frame.
-
Segment Size: The snake has a
size
variable that defines the pixel dimensions of each side of its segments. This determines how large each block of the snake appears on the canvas. -
Movement Logic:
- The snake's movement is controlled by an object containing functions for each direction (up, down, left, right).
- These functions modify a
movement
variable, which serves as an offset to calculate the snake's next position on the canvas. - With every frame, the snake’s position is updated based on this offset.
-
Random Placement:
- The apple is placed at a random location on the canvas when the game begins and every time it collides with the snake's head.
-
Static Behavior:
- Once placed, the apple remains stationary until the snake collects it by overlapping its head with the apple's position.
-
Increment:
- Each time the snake collects an apple, the score increases by
1
. showing the player’s progress in the game.
- Each time the snake collects an apple, the score increases by
-
Reset on Death:
- If the snake dies, the score resets to
0
, prompting the player to reset.
- If the snake dies, the score resets to