Synopsis
Synopsis
Introduction
Snake is a sub-genre of action video games where the player maneuvers the
end of a growing line, often themed as a snake. The player must keep the
snake from colliding with both other obstacles and itself, which gets harder as
the snake lengthens. It originated in the 1976 two-player arcade video
game Blockade from Gremlin Industries where the goal is to survive longer
than the other player. The concept evolved into a single-player variant where a
snake gets longer with each piece of food eaten—often apples or eggs. The
simplicity and low technical requirements of snake games have resulted in
hundreds of versions—some of which have the word snake or worm in the
title—for many platforms.
1982's Tron arcade game, based on the film, includes snake gameplay for the
single-player Light Cycle segment, and some later snake games borrow the
theme. After a version simply called Snake was pre-loaded on Nokia mobile
phones in 1998, there was a resurgence of interest in snake games.
Problem Statement
Design a Snake game that is played on a device with screen size height x width.
The snake is initially positioned at the top left corner (0, 0) with a length
of 1 unit.
You are given an array food where food[i] = (ri, ci) is the row and column position of
a piece of food that the snake can eat. When a snake eats a piece of food, its
length and the game's score both increase by 1.
Each piece of food appears one by one on the screen, meaning the second piece
of food will not appear until the snake eats the first piece of food.
When a piece of food appears on the screen, it is guaranteed that it will not
appear on a block occupied by the snake.
The game is over if the snake goes out of bounds (hits a wall) or if its head
occupies a space that its body occupies aftermoving (i.e. a snake of length 4
cannot run into itself).
int move(String direction) Returns the score of the game after applying
one direction move by the snake. If the game is over, return -1.
Intuition
2. We also need a Set to keep track of all the positions of the snake’s body
for quick access when checking if the snake hits itself.
3. To know if the snake has eaten an apple, we will keep track of the
current apple’s position and compare it with the snake’s head after each
move.
Design
1. Initialize: Set the starting position of the snake and the first apple. The
snake starts with a size of 1.
At the core of our creation is the Game class. It encapsulates the game
logic, handling everything from snake movement to collision detection.
Constants define the game’s dimensions, and arrays track the coordinates
of the snake’s body parts. A timer ensures regular updates, triggering the
actionPerformed method.
Initialization Rituals
Upon initiation, the game initializes its state, creates a randomizer for
apple placement, and sets up the graphical interface. The user’s keyboard
input is captured through a custom KeyAdapter — MyKeyAdapter —
which allows the snake to change direction based on arrow key presses.
Artistry in Motion
Our snake’s ballet involves intricate logic. The move method elegantly
updates the snake’s position, and checkApple verifies if an apple has been
consumed, signaling the snake to grow. Collisions with the snake’s own
body or the screen borders are meticulously monitored by the
checkCollisions method.
When the dance concludes, and the snake meets its demise, the
gameOver method takes center stage. It displays the final score with a
touch of flair, signaling the end of the performance.
Conclusion