CIT 591 Assignment 11: Snake Game
Fall 2008, David Matuszek

Purposes of this assignment

General idea of the assignment

Implement a simple version of the game of "Snake". (You can find several versions of this game at; the one on the bottom left is a good example.) Provide a "High Scores" window with the game.

From Wikipedia:

Snake is a video game that came out in the late 1970s and has maintained popularity since then, becoming somewhat of a classic. The player controls a long, thin creature, resembling a snake, which roams around on a bordered plane, picking up food (or some other item), trying to avoid hitting its own tail or the "walls" that surround the playing area. Each time the snake eats a piece of food, its tail grows longer, making the game increasingly difficult. The user controls the direction of the snake's head (up, down, left, or right), and the snake's body follows. The player cannot stop the snake from moving while the game is in progress.

I will specify only a few basic things about how to code the game. The total program design is up to you and your partner. I strongly recommend that you use the lab period to design the program; that is, decide exactly what classes and methods you will have, what the signatures of those methods will be, and what each will do.

Rules of the game

The snake can be controlled by the arrow keys. (You can use additional keysets if you like, but use at least these.) There should also be a way to pause/resume the game (usually the "P" key).

High scores

The player's score should be based on the number of apples eaten (or equivalently, the length of the snake). The exact scoring algorithm is up to you; most games award more points for each apple as the snake gets longer.

Keep the ten highest scores, along with the names of the players who made those scores. The scores should be kept across all runs of the game. That is, they should not be lost when the program ends.

Keeping (and displaying) high scores isn't as "glamorous" as writing the game itself, but it is harder than you might expect (and more generally useful).


Name your project SnakeGame, your package snake, and your "main" class SnakeGame.

Write a "model" class for the game itself. Thoroughly JUnit test this class.

Provide a "hidden" extra-slow mode (but tell us how to find it) so that we can test your program without having to be really good at it.

Write a "model" class for keeping track of high scores. Thoroughly JUnit test this class.

Keeping high scores must be automatic; the user shouldn't have to explicitly say to "Save" them.

A High Scores window should be displayed whenever the player attains a score in the top ten. Additionally, provide a way to see the High Scores window any time that a game isn't actually in progress.

The model classes should do no input/output.

One partner should do the game model, its JUnit tests, and the game GUI. The other partner should do the high scores model, its JUnit tests, and the High Scores GUI. (If you have no partner, you can omit keeping track of the high scores.) Be sure to use @author tags in your classes.

Generate complete Javadoc documentation files for your program, review them, and hand them in as part of the complete package.

Programming notes

You can attach a KeyListener to your JPanel. Here's how you recognize arrow keys.

   public void keyPressed(KeyEvent evt) { 
      int key = evt.getKeyCode();  // Keyboard code for the pressed key.
      if (key == KeyEvent.VK_LEFT) {
      else if (key == KeyEvent.VK_RIGHT)  {
      else if (key == KeyEvent.VK_UP) {
      else if (key == KeyEvent.VK_DOWN) {

High scores must be kept across different runs of the program. You can do this is by keeping them on a file that you read in at the start of the game, and write out again whenever a high score is achieved.

Optional: If you would like an even better way of keeping high scores (and simpler, once you learn how to use it), see java.util.prefs.Preferences. The Java API documentation is hard to read, but you can find tutorials on the web. Also, I have a PowerPoint presentation you can refer to.


As usual, we require good programming style: Good names, good formatting, good documentation, short single-purpose methods, obedience to the DRY principle, etc. In previous assignments I have not required you to generate and turn in the Javadoc, but this time, that is a requirement.

As usual, you are responsible for reviewing your partner's work, and for getting your own work to your partner in time to be reviewed.

On this assignment, we might (or might not) assign different grades to the two partners, based on the classes you were responsible for; that depends primarily on the amount of time we have for grading. In any case, your grade will certainly be influenced by the total program quality.

Due date:

Thursday, December 4, before midnight. As usual, zip up the entire project and submit via Blackboard, with your partner's name in the Blackboard comments.