Coders Packet

Snake Game in Java using Java Swing and AWT

By Pallav Goswami

This is a simple implementation of the classic game "Snake" using the Java Swing library and AWT (Abstract Window Toolkit) API.

INTRODUCTION

The Java-based Snake Game is a GUI game that challenges players to navigate a snake and eat apples for its growth. Players must avoid obstacles such as walls and the snake's body while collecting as many apples as possible. The player's score is continuously recorded and displayed on the screen during the game.

TOOLS USED

1) VS CODE

2) JDK

3) GUI: SWING

4) API: AWT

PROJECT PRE-REQUISITES

1) Must have Java installed on the system.

2) Must have an IDE to run the project.

3) Must have SWING installed on the IDE.

IMPLEMENTATION

This game is implemented using Java Swing Library and AWT API for GUI components and graphics. The game loop is created using the Java Timer class to update the screen and detect user input. The HUD class sets the game's dimensions and background color, initializes the snake's length and apple variables, and adds a key listener to handle user input.

FUNCTIONS

The class has methods such as play(), paintComponent(), move(), checkApple(), draw(), addApple(), checkHit(), gameOver(), actionPerformed(), and MyKeyAdapter(). These methods handle the drawing of the snake, the apple, and the score, the movement of the snake, checking if the snake has eaten the apple or collided with a wall, and ending the game when necessary.

INTERFACE CLASS

To create the interface, a class named Interface is extending the JFrame class from the javax.swing package. This class creates a window for the game. A new instance of HUD class is created and added to the frame using the add() method. This panel holds the graphical components and handles the game logic. The setDefaultCloseOperation() method is called to set the operation that will be performed when the user closes the window. In this, the program will exit when the user closes the window. The setResizable() method is used to prevent the user from resizing the window, which might affect the game's graphics. The pack() method is used to resize the window to fit its contents, and the setVisible() method is called to display the window. Finally, the setLocationRelativeTo(null) method is used to center the window on the screen.

HUD CLASS

The HUD class sets up the game board, which is 500x500 pixels, and the snake moves in increments of 20 pixels. The game board is divided into a grid of 25x25 units. The snake is represented as a series of squares, with the head of the snake being a white square and the rest of the snake being a yellow color. The apple is represented as a red oval. Variables such as width, height, size of unit, total units, x, y, length, apple eaten, apple X, apple Y, direction, running, random, and timer. These variables hold the x-coordinates, y-coordinates, and other values for the snake and apple.

GAME CLASS

The Game class contains the main method that creates an instance of the Interface class and calls its constructor using the new keyword. The Interface class is responsible for displaying and running the Snake game. This main method creates an instance of Interface and starts the game.

GAME IMAGES

HIGH SCORE

GAME OVER

 

 

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Pallav Goswami (pallavgoswami)

Download packets of source code on Coders Packet