Coders Packet

Snake game using Java AWT and Swing

By Shreya Shukla

Snake game is an arcade game played by many people in their childhood. This project has implemented this game using Java AWT and Swing.

Snake game output screen

Snake Game is an arcade game. This project is a 1 Player snake game implemented by using Java AWT and Swing.

This project is divided into 2 classes.

            1) Game class: This class creates and manages the GUI components required for this game, performs event handling, and implements the rules of this game.

            2) SnakeGame class: This class contains the main function i.e. the entry point function. This function creates the JFrame object and Game class object and adds the Game class object to the JFrame object.

 

About the Game class:

           Features of Game class:

                        1) Instance variables to check the direction of the snake's movement and game-over status have been declared inside the Game class.

                        2) Data structures are maintained to store and manage the position of the snake's body parts inside this class.

                        3) Instance variables are declared in this class to store the length of the snake, score, and number of moves.

                       4) Instance variables are also declared to store the location of the snake's food.

                       5) Timer class is used to control the speed of the game and repaints the GUI components of this game after a specific delay.

                       6) paint(): This method paints the strings and shapes required for this game.

                       7) actionPerformed(): This method is called after a delay, according to the Timer class object. It takes care of the movement of the snake's body parts, the direction in which the snake will move when any of the arrow keys is pressed. It ensures that the snake re-enters the screen from the side opposite to the side it went out of. This method also calls the eatsFood() and collidesWithBody() methods.

                       8) keyPressed(): This method manipulates the value of the instance variables that check the direction when one of the arrow keys is pressed. This method also calls the restart() method when the spacebar key is pressed after the game is over.

                       9) newFood(): This method generates the location of the food for the snake. It also ensures that the food is not painted over the snake's body.

                       10) eatsFood(): This method generates food for the snake when it eats its food and increments the length of the snake and the score.

                       11) collidesWithBody(): This method checks if the snake has collided with itself after every move.

                       12) restart(): This method restarts the necessary variables and calls the necessary methods to start a new game.

 

About SnakeGame Class:

           Features of SnakeGame class:

                     1) main(): This method creates the JFrame object, sets its size, title, resizeability, visibility, default close operation, and location on the screen, and creates and adds the Game object to the JFrame object. 

 

The flow of this game:

           1) Press any of the arrow keys to start the game.

           2) Press the arrow keys so that the snake can eat its food.

           3) When the snake eats, the score and its length is incremented.

           4) Avoid letting the snake collide with its own body otherwise the game ends.

           5) Press the SPACEBAR key to restart the game.

 

Algorithm of the snake game:

           1) Initialize the game board with a snake of a particular length and food.

           2) a) Take user input for the snake to move in a particular direction. 

               b) Move the snake's body in the direction specified by the user.

               c) If (snake has eaten the food on the game board)

                   {

                                     (length of the snake)++;

                                     score++;

                                     generate new food;

                    }

               d) If (snake has collided with itself)

                   {

                                     declare ("game is over");

                                     end the game;

                   }

           3) Perform steps 2 and 3 until the game ends.

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Shreya Shukla (Shreya35)

Download packets of source code on Coders Packet