Skip to Main Skip to Footer Navigation

Java Snake

About

This was my second project. I replicated the Google Snake Game. It is created in Java using the AWT and Swing libraries.

About Snake

Snake is a classic game where the goal is to eat as many apples as you can. The Snake grows as it eats. You are confined to a grid, in my version it is 17x17, and the apples will appear randomly on the grid. The player loses if the snake hits the wall or its own tail.

A snake running into its tail and losing.
You lose if the snake hits its own tail.

My Version

The Snake.java file contains the main method to run the program.

The board is made of a 2d array of Cell objects. The Cell class has methods to get and set the row and collumn as well as check for equality with other cells. The method positionOf(int, Cell, LinkedList<Cell>) is used to test if the snake has collided with itself. Finally, the Cell class uses the CellType to determine whether it is empty, part of the snake, or a piece of food.

The SnakePlayer uses a linked list to hold each cell it is occupying. When the Snake moves, a Cell is added to the head of the linked list and the end is popped off. If a piece of food is eaten, the tail of the list is not popped.

An active snake game.
My version of the Google Snake game.

To play, first run the Snake.java file. Then press the play button. The controls are as follows:

  • Up Arrow: Turn the snake up
  • Down Arrow: Turn the snake down
  • Left Arrow: Turn the snake left
  • Right Arrow: Turn the snake right

Issues

This being my second project, there are some known issues:

  • In some cases, when the game is opened, the images do not load until the play button is pressed.
  • Some computers display a stretched version of the game making it unplayable.
View on GitHub