Java Snake – Open source project

Note from the author

At the very begin I’d like to point that this project has been made by a person who is still learning Java and programming in general. The solutions I’ve used are proper for me and it works perfectly fine. If you think different or you found some mistakes, please write a comment, e-mail me at or tweet me. Feedback is much appreciated.

What is ‘Java Snake’?

Java Snake

Java Snake is an Java application which allows you to play the famous ‘Snake’ game. Beside simply catching apples using our snake I’ve took this game to a new level. Application counts and saves the score, so you can browse the best scores on your computer. You can also change simple settings like music and game volume or change the snake colour. To keep your high scores safe I’ve used simple database. So the tools I’ve used are:

also the project has been written in Eclipse. In order to create .exe file I’ve used Launch4j.


Download section

Runnable .exe file for Windows

Runnable .jar file

Documentation through the program’s classes and methods

Source Code

Classes and methods

I will guide you through the methods and classes. Each method has been commented in the code.


The very first and probably the most important class is Snake. It creates the JFrame and sets up the whole user interface. It also registers new font to the graphic environment. The main method is stored in this class.


The Menu class is responsible for… menu. This class extends JComponent. It creates buttons and their listeners so user can easily use the ui.


This class is responsible for drawing the whole game. Starting from the playable area frame, finishing at the score submit form. It also counts the score and plays the eating sound each time when snake hits the apple. It contains the KeyListener so application knows when user uses arrow keys. It is strongly related to Apples, SnakeBody and ScoreDB classes which are explained bellow.


Scoreboard is respansible for drawing the score table so you can browse scores easily. Table is printed with simple lines and drawLine method. Since we don’t  need sorting or copying the data from this table I’ve decided that this solution is better than drawing unnecessary component (JTable). Notice that this class only draws the scores. ScoreDB is class that reads and writes into the database.


This class manages the settings ui. It draws the ui and uses it as a connector betten SnakePrefs class.


Info class draws basic ui with informations about the project.


This class helps the Game class in drawing the apples.


This class plays music in background. Notice that I’ve used the Singleton pattern here. It allows me to use the same class’ instance in different objects. In the Singleton pattern, the constructor needs to be private instead of public.


ScoreDB changes the database file. It writes and reads from database.


This class helps drawing the Snake parts in Game class.


I’ve created this because I don’t want to set up the JButton every time I need a button.  This class extends JButton and has additional instructions, so we can use the game style button easily.


This class helps with saving settings on user’s computer. Preferences class is good to this task because we don’t have to worry about OS. It works perfectly fine on both Unix (Linux) and Windows.

 Additional files and folders

Java Snake, beside from .java files, contains some additional files.


In Java Snake’s resources we can find:


Smoothing accompany music: THE ANT HILL GANG GOES WEST –

Crunch sound

Crunch III sound by David Young –


Little Bird font by Benjamin Wyler –


Java Snake logo drown using nail polish 🙂

Java Snake

SQLite .jar considered as “Referenced Library”

This file is called sqlite-jdbc-3.8.7.jar . It’s just SQLite external library which allows us to use database in the game.


This file stored in META-INF folder is, in this case, used to point the file with main method for Launch4j.


If you have any problems with this application feel free to write me! Preferable use the comment section bellow. Although, you can e-mail or tweet me.

Also if you like this page and you think you learnt something, please share this page with your friends, so more people could discover this game and look through the code.