Snake
Snake
Snake
In this part of the Java 2D games tutorial, we will create a Java Snake game clone.
Snake
Snake is an older classic video game. It was first created in late 70s. Later it was brought
to PCs. In this game the player controls a snake. The objective is to eat as many apples
as possible. Each time the snake eats an apple, its body grows. The snake must avoid the
walls and its own body. This game is sometimes called Nibbles.
Development
The size of each of the joints of a snake is 10px. The snake is controlled with the cursor
keys. Initially, the snake has three joints. If the game is finished, the "Game Over"
message is displayed in the middle of the board.
Board.java
package com.zetcode;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public Board() {
addKeyListener(new TAdapter());
setBackground(Color.black);
setFocusable(true);
dots = 3;
locateApple();
timer = new Timer(DELAY, this);
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
if (inGame) {
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT /
2);
}
dots++;
locateApple();
}
}
private void move() {
if (leftDirection) {
x[0] -= DOT_SIZE;
}
if (rightDirection) {
x[0] += DOT_SIZE;
}
if (upDirection) {
y[0] -= DOT_SIZE;
}
if (downDirection) {
y[0] += DOT_SIZE;
}
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
if(!inGame) {
timer.stop();
}
}
private void locateApple() {
@Override
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
@Override
public void keyPressed(KeyEvent e) {
These two arrays store the x and y coordinates of all joints of a snake.
dots = 3;
locateApple();
dots++;
locateApple();
}
}
If the apple collides with the head, we increase the number of joints of the snake. We call
thelocateApple() method which randomly positions a new apple object.
In the move() method we have the key algorithm of the game. To understand it, look at
how the snake is moving. We control the head of the snake. We can change its direction
with the cursor keys. The rest of the joints move one position up the chain. The second
joint moves where the first was, the third joint where the second was etc.
if (leftDirection) {
x[0] -= DOT_SIZE;
}
If the snake hits one of its joints with its head the game is over.
if (y[0] >= B_HEIGHT) {
inGame = false;
}
The game is finished if the snake hits the bottom of the board.
Snake.java
package com.zetcode;
import java.awt.EventQueue;
import javax.swing.JFrame;
public Snake() {
add(new Board());
setResizable(false);
pack();
setTitle("Snake");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame ex = new Snake();
ex.setVisible(true);
}
});
}
}
setResizable(false);
pack();