0% found this document useful (0 votes)
69 views4 pages

Synopsis

Snake is a classic video game genre where the player controls a snake that grows in length as it eats food pieces on the screen. The game is over if the snake collides with itself or the edge of the screen. This document discusses implementing a Snake game, including using a queue to track the snake's body, a set to check for collisions, and updating the snake and food positions each turn. It also covers initializing the game, handling movement and eating food, and ending the game if the snake collides with itself or the wall.

Uploaded by

mannu.maniram8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views4 pages

Synopsis

Snake is a classic video game genre where the player controls a snake that grows in length as it eats food pieces on the screen. The game is over if the snake collides with itself or the edge of the screen. This document discusses implementing a Snake game, including using a queue to track the snake's body, a set to check for collisions, and updating the snake and food positions each turn. It also covers initializing the game, handling movement and eating food, and ending the game if the snake collides with itself or the wall.

Uploaded by

mannu.maniram8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Snake (video game genre)

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).

Implement the SnakeGame class:

 SnakeGame(int width, int height, int[][] food) Initializes the object


with a screen of size height x widthand the positions of the food.

 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

1. We will be using a Queue (Deque) data structure to keep track of the


snake’s body. The reason being the snake moves in a way that the head
advances and the tail retracts, which naturally resembles the operations of
a queue.

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.

2. Move: Depending on the direction, we will determine the new head’s


position. Check if the snake is still alive after the move. If yes, check if the
snake eats the apple.
Concepts used in the GAME

Setting the Stage

Our canvas is a Java Swing application, providing a graphical user


interface for the Snake game. The game unfolds within a grid, where a
snake maneuvers to consume apples and grow longer. Let’s delve into the
key components that bring this digital serpent to life.

The Game Class

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

The paintComponent method serves as our artistic canvas. It draws the


snake’s body, apples, and the ever-increasing score. A carefully crafted
algorithm ensures that each movement of the snake is mirrored visually
on the screen. The game doesn’t just run; it dances.
A Dance of Logic

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.

The Grand Finale: Game Over

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

In crafting this digital symphony, we’ve stitched together the fabric of a


classic Snake game using Java and Swing. The intricacies of user input,
game state management, and graphical representation have converged
into a nostalgic yet timeless experience. This journey through the code
has not just unveiled the mechanics; it has celebrated the artistry of game
development.

As we bid farewell to our digital serpent, we acknowledge the enduring


allure of these classic games, forever etched in the annals of gaming
history. The journey continues, and the dance of code persists — always
evolving, always engaging.

You might also like