Snake Game in Java

Below is a simple implementation of the classic Snake Game in Java. Copy and paste this code and run it!

Snake game image

GameBoard:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
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.JPanel;
import javax.swing.Timer;



public class GameBoard extends JPanel implements ActionListener {

    private final int BOARD_WIDTH = 300;
    private final int BOARD_HEIGHT = 300;
    private final int DOT_SIZE = 10;
    private final int MAX_DOTS = 900;
    private final int RANDOM_POS = 29;
    private final int DELAY = 140;

    private final int x[] = new int[MAX_DOTS];
    private final int y[] = new int[MAX_DOTS];

    private int dots;
    private int appleX;
    private int appleY;

    private boolean movingLeft = false;
    private boolean movingRight = true;
    private boolean movingUp = false;
    private boolean movingDown = false;
    private boolean inGame = true;

    private Timer timer;

    public GameBoard() {
        initializeBoard();
    }

    private void initializeBoard() {
        addKeyListener(new GameAdapter());
        setBackground(Color.black);
        setFocusable(true);
        setPreferredSize(new Dimension(BOARD_WIDTH, BOARD_HEIGHT));
        initializeGame();
    }

    private void initializeGame() {
        dots = 3;
        for (int z = 0; z < dots; z++) {
            x[z] = 50 - z * DOT_SIZE;
            y[z] = 50;
        }
        locateApple();
        timer = new Timer(DELAY, this);
        timer.start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        doDrawing(g);
    }

    private void doDrawing(Graphics g) {
        if (inGame) {
            drawDot(g, appleX, appleY, Color.RED);
            for (int z = 0; z < dots; z++) {
                if (z == 0) {
                    drawDot(g, x[z], y[z], Color.GREEN);
                } else {
                    drawDot(g, x[z], y[z], Color.YELLOW);
                }
            }
            Toolkit.getDefaultToolkit().sync();
        } else {
            displayGameOver(g);
        }
    }

    private void drawDot(Graphics g, int x, int y, Color color) {
        g.setColor(color);
        g.fillRect(x, y, DOT_SIZE, DOT_SIZE);
    }

    private void displayGameOver(Graphics g) {
        String message = "Game Over";
        Font smallFont = new Font("Helvetica", Font.BOLD, 14);
        FontMetrics fontMetrics = getFontMetrics(smallFont);
        g.setColor(Color.white);
        g.setFont(smallFont);
        g.drawString(message, (BOARD_WIDTH - fontMetrics.stringWidth(message)) / 2, BOARD_HEIGHT / 2);
    }

    private void checkApple() {
        if ((x[0] == appleX) && (y[0] == appleY)) {
            dots++;
            locateApple();
        }
    }

    private void move() {
        for (int z = dots; z > 0; z--) {
            x[z] = x[(z - 1)];
            y[z] = y[(z - 1)];
        }
        if (movingLeft) {
            x[0] -= DOT_SIZE;
        }
        if (movingRight) {
            x[0] += DOT_SIZE;
        }
        if (movingUp) {
            y[0] -= DOT_SIZE;
        }
        if (movingDown) {
            y[0] += DOT_SIZE;
        }
    }

    private void checkCollision() {
        for (int z = dots; z > 0; z--) {
            if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
                inGame = false;
            }
        }
        if (y[0] >= BOARD_HEIGHT) {
            inGame = false;
        }
        if (y[0] < 0) {
            inGame = false;
        }
        if (x[0] >= BOARD_WIDTH) {
            inGame = false;
        }
        if (x[0] < 0) {
            inGame = false;
        }
        if (!inGame) {
            timer.stop();
        }
    }

    private void locateApple() {
        int randomPos = (int) (Math.random() * RANDOM_POS);
        appleX = (randomPos * DOT_SIZE);
        randomPos = (int) (Math.random() * RANDOM_POS);
        appleY = (randomPos * DOT_SIZE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (inGame) {
            checkApple();
            checkCollision();
            move();
        }
        repaint();
    }

    private class GameAdapter extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            if ((key == KeyEvent.VK_LEFT) && (!movingRight)) {
                movingLeft = true;
                movingUp = false;
                movingDown = false;
            }
            if ((key == KeyEvent.VK_RIGHT) && (!movingLeft)) {
                movingRight = true;
                movingUp = false;
                movingDown = false;
            }
            if ((key == KeyEvent.VK_UP) && (!movingDown)) {
                movingUp = true;
                movingRight = false;
                movingLeft = false;
            }
            if ((key == KeyEvent.VK_DOWN) && (!movingUp)) {
                movingDown = true;
                movingRight = false;
                movingLeft = false;
            }
        }
    }
}
                    
                        

Snake:

import java.awt.EventQueue;
import javax.swing.JFrame;

public class Snake extends JFrame {

    public Snake() {
        
        initUI();
    }
    
    private void initUI() {
        
        add(new GameBoard());
                
        setResizable(false);
        pack();
        
        setTitle("Snake");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    

    public static void main(String[] args) {
        
        EventQueue.invokeLater(() -> {
            JFrame ex = new Snake();
            ex.setVisible(true);
        });
    }
}