In this game are you using all the things you have learned in the previous chapters. If you are a beginning programmer, it is easier to use an already existing game interface. Most of the time when you get an assignment like this the school will provide a game interface. If you don’t have a game interface, go to http://zetcode.com/tutorials/javagamestutorial/snake/. You can find a nice tutorial over there, to start from nothing and build snake.
Alright, the first thing you have to do is to split the assignment into 3 parts. You have of course the main game, but you also have 2 other classes. To sum it over here:
- main
- snake
- coordinates
Why three classes? As you can see on the page about classes. You have to save the snake object in the game in an array. Because the snake will consist more than one piece,

coordinates will differ from each other and therefore it will be the same situation as in the example of the sheep.
1. Main Structure
Oke, what are we going to write in main:
- check methods
- changing directions
- a loop method (when you don’t change the direction)
- set methods (for example: set apple, set snake start position, etc…)
- a clear method
- game over method
A further breakdown is more precise:
- check methods
- check boundaries/walls
- check apple
- changing directions
- a loop method (when you don’t change the direction)
- set methods
- set apple
- set snake start position
- clear method ==> clear snake
- game over method ==> when the snake hits itself
Now we know what we have to write in the main method. We will now first create the class coordinate and snake.
3. Coordinate
The class coordinate will be a very short class. We only have to save the x and y coordinate. You don’t have to create a method because you will not create anything, but just save the value of two variables.
package Snake;
class Coordinate {
int x, y;
Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
}
if you look at this code you see this.x = x and this.y = y. You have to see it like this; when you make a new Coordinate in another class, you write:
Coordinate headSnake = new Coordinate(0,0)
You call the class Coordinate and say, oke could you save, an x value of 0 and a y value of 0. You call this point (0,0); the headSnake of the snake. Pretend it in maths:

If we fill in the variables as eclipse should do. We see something like this:
Coordinate(0,0) {
this.x = x; //then this.x = 0
this.y = y; // and this.y = 0
}
This is what you have to imagine. This is of course not what you see, but it is what the program executes.
So, now is this.x and this.y 0. Or in other words, the class variable x and class variable y are 0. For more information about class variables, click here.
2. Snake
The class snake will contain a method to add a coordinate to the array. The class has the same structure because this class will also nothing do. So we start with this: add coordinate at the back.
package Snake;
class Snake {
//create constant for the maximum size of the array
//create array
//create numberOfElements
Snake() {
//initialize array
//initialize numberOfElements
}
void moveSnake(Coordinate coordinate) {
//move Snake
}
void addCoordinateAtTheBack(Coordinate coordinate) {
//add coordinate at the back
}
}
To make a snake we have to create an array of coordinates. But why numberOfElements? Because we don’t know how many elements our array contains. Therefore we have to do this manually, this is the most common way to know the size of the array.
First, we create the first 12 lines. These lines are just made by following the rules. See following code:
package Snake;
class Snake {
static final int MAX_INPUT = 100000000;
Coordinate [] coordinateArray;
int numberOfElements;
Snake() {
coordinateArray = new Coordinate[MAX_INPUT];
numberOfElements = 0;
}
void moveSnake(Coordinate coordinate) {
//move snake
}
void addCoordinateAtTheBack(Coordinate coordinate) {
//add coordinate at the back
}
}
Explanation:
line 4: make a constant, because this is not a very big game, we can’t reach infinity many places in our array. Therefore is it enough to just write a big finite number.
Now we create the rest. In this part do you have to think how to add things to:
- the back
- move snake
We are going to use these methods for two things. If our snake has to grow, because we eat an apple, for example, we will add in the back a new element. The moveSnake method will be used for moving the snake around, so the snake will not become longer, but only move his body to a new coordinate.
VISUALIZATION
imagine you got an array of 3 elements:

1. add an element to the back
create a new element place at the back and add the new element to the box

Or in java code language:
void addCoordinateAtTheBack(Coordinate coordinate) {
coordinateArray[numberOfElements] = coordinate;
numberOfElements ++;
}
line 2: because the last element in the array is always: numberOfElements – 1. You can say, set my new element on last one + 1. Or in other words numberOfElements – 1 + 1 equals numberOfElements.
line 3: every element that you add has to be registered, otherwise you don’t know how many elements are in your array.
2. method Move Snake
Visualize it first:
create a new “element place” at the back and move every element in the array one to the right:

in java code:
for(int i = numberOfElements; i >= 1; i--) {
coordinateArray[i] = coordinateArray[i-1];
}
result:

Now, do you have to add the new element to box 0.
in java code:
coordinateArray[0] = coordinate;
Your moveSnake method will now look like this:
void moveSnake(Coordinate coordinate) {
for(int i = numberOfElements; i >= 1; i--) {
coordinateArray[i] = coordinateArray[i-1];
}
coordinateArray[0] = coordinate;
}
We use NO numberOfElements + 1, because we don’t add any elements we just move the snake, so the number of elements stays the same.
The class snake will look like this:
package Snake;
class Snake {
static final int MAX_INPUT = 100000000;
Coordinate[] coordinateArray;
int numberOfElements;
CoordinateRow() {
coordinateArray = new Coordinate[MAX_INPUT];
numberOfElements = 0;
}
void addCoordinateAtTheBack(Coordinate coordinate) {
coordinateArray[numberOfElements] = coordinate;
numberOfElements ++;
}
void moveSnake(Coordinate coordinate) {
for(int i = numberOfElements; i >= 1; i--) {
coordinateArray[i] = coordinateArray[i-1];
}
coordinateArray[0] = coordinate;
}
}
We now finished 2 classes, snake and coordinate. We made the structure of main. Now we are going to program the full main class. Because your main program will highly depend on the game interface I will show you a sketch of how your program has to look:
Snake | Whole Program
package Snake;
class Main {
static final int MAX_WIDTH = .., MAX_HEIGHT = ..;
PrintStream out;
//create coordinates for head, tail, apple
//create array of the whole snake
//create variables with: score, speed, direction, apple
Main() {
out = new PrintStream(System.out);
//initialize speed
}
void start() {
setSpeed(speed);
out.printf("Press an arrow to start the game\n");
//set in start position
while (true) {
game();
}
}
void setStartPosition() {
score = 0;
direction = apple = null;
newSnake();
appleCheck();
}
void game() {
if (//arrow is pressed) {
processEvent(//arrow);
} else {
//method: check if there has to be a new apple
//method: delete the current snake
//method: you can go through the "walls"/sides
//method: determine direction
//method: place (new) snake
//method: check if you are game over
showChanges();
}
}
void processEvent(Event event) {
if (//arrow left) {
if (//direction is not right) {
direction = "left";
}
} else if (//arrow right) {
if (//direction is not left) {
direction = "right";
}
} else if (//arrow up) {
if (//directon is not down) {
direction = "up";
}
} else if (//arrow down) {
if (//direction is not up) {
direction = "down";
}
}
}
void deleteCurrentSnake() {
//loop through the whole snake array and delete every element
}
void appleCheck() {
if (appleResult() == "true") {
setApple();
//place on the obtained coordinates an apple
out.printf("score= %d\n", score);
}
}
String appleResult() {
if (//start position) {
apple = "true";
} else if (//you hit the apple) {
apple = "true";
score++;
snake.numberOfElements++;
} else {
apple = "false";
}
return apple;
}
void setApple() {
Random randomGenerator = new Random();
coordinateApple = new Coordinate(randomGenerator.nextInt(MAX_WIDTH), randomGenerator.nextInt(MAX_HEIGHT));
if (//apple is placed on the snake) {
setApple();
}
}
void makePermeableSides() {
if (coordinateHeadSnake != null) {
if (//direction is right and coordinateHeadSnake.x is bigger than the width) {
coordinateHeadSnake = new Coordinate(-1, coordinateHeadSnake.y);
} else if (//direction is left and coordinateHeadSnake.x <= 0) {
coordinateHeadSnake = new Coordinate(MAX_WIDTH, coordinateHeadSnake.y);
} else if (//direction is down and coordinateHeadSnake.x is bigger than the height) {
coordinateHeadSnake = new Coordinate(coordinateHeadSnake.x, -1);
} else if (//direction is up and coordinateHeadSnake.x <= 0) {
coordinateHeadSnake = new Coordinate(coordinateHeadSnake.x, MAX_HEIGHT);
}
}
}
void setDirection() {
if (//not direction not null) {
if (//direction left) {
coordinateHeadSnake = new Coordinate(coordinateHeadSnake.x - 1, coordinateHeadSnake.y);
} else if (//direction right) {
coordinateHeadSnake = new Coordinate(coordinateHeadSnake.x + 1, coordinateHeadSnake.y);
} else if (//direction up) {
coordinateHeadSnake = new Coordinate(coordinateHeadSnake.x, coordinateHeadSnake.y - 1);
} else if (//direction down) {
coordinateHeadSnake = new Coordinate(coordinateHeadSnake.x, coordinateHeadSnake.y + 1);
}
snake.moveSnake(coordinateHeadSnake);
}
}
void placeSnake() {
//place the whole snake array
}
void checkGameOver() {
if (// the snake hits itself) {
out.printf("Game Over -_-\n" + "Press an arrow to start another game\n");
//clear the whole field
//reset ==> go to start position
}
}
void newSnake() {
coordinateHeadSnake = new Coordinate(0,1);
coordinateTail = new Coordinate(0,0);
snake = new Snake();
snake.addCoordinateAtTheBack(coordinateHeadSnake);
snake.addCoordinateAtTheBack(coordinateTail);
}
void place(int x, int y, Object type) {
//method that places a new element in the game
}
void showChanges() {
//method that shows your array
}
void setSpeed(speed) {
//method that sets framesPerSecond to speed you want
}
public static void main(String[] args) {
new Main().start();
}
}