Koppelingen in nieuw tabblad openen
  1. The Snake game is a classic arcade game where the player controls a snake to eat food, growing in size while avoiding collisions with walls or itself. Below is a simple implementation of the Snake game in Java using Swing for the graphical interface.

    Code Implementation

    // Main Class
    import javax.swing.JFrame;

    public class Snake extends JFrame {

    public Snake() {
    initUI();
    }

    private void initUI() {
    add(new Board());
    setResizable(false);
    pack();

    setTitle("Snake");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

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

    // Game Board Class
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class Board extends JPanel implements ActionListener {

    private final int B_WIDTH = 300;
    private final int B_HEIGHT = 300;
    private final int DOT_SIZE = 10;
    private final int ALL_DOTS = 900;
    private final int RAND_POS = 29;
    private final int DELAY = 140;

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

    private int dots;
    private int apple_x;
    private int apple_y;

    private boolean leftDirection = false;
    private boolean rightDirection = true;
    private boolean upDirection = false;
    private boolean downDirection = false;
    private boolean inGame = true;

    private Timer timer;
    private Image ball;
    private Image apple;
    private Image head;

    public Board() {
    initBoard();
    }

    private void initBoard() {
    addKeyListener(new TAdapter());
    setBackground(Color.black);
    setFocusable(true);

    setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
    loadImages();
    initGame();
    }

    private void loadImages() {
    ball = new ImageIcon("src/resources/dot.png").getImage();
    apple = new ImageIcon("src/resources/apple.png").getImage();
    head = new ImageIcon("src/resources/head.png").getImage();
    }

    private void initGame() {
    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);

    if (inGame) {
    g.drawImage(apple, apple_x, apple_y, this);

    for (int z = 0; z < dots; z++) {
    if (z == 0) {
    g.drawImage(head, x[z], y[z], this);
    } else {
    g.drawImage(ball, x[z], y[z], this);
    }
    }
    Toolkit.getDefaultToolkit().sync();
    } else {
    gameOver(g);
    }
    }

    private void gameOver(Graphics g) {
    String msg = "Game Over";
    Font small = new Font("Helvetica", Font.BOLD, 14);
    FontMetrics metr = getFontMetrics(small);

    g.setColor(Color.white);
    g.setFont(small);
    g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
    }

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

    private void move() {
    for (int z = dots; z > 0; z--) {
    x[z] = x[(z - 1)];
    y[z] = y[(z - 1)];
    }

    if (leftDirection) x[0] -= DOT_SIZE;
    if (rightDirection) x[0] += DOT_SIZE;
    if (upDirection) y[0] -= DOT_SIZE;
    if (downDirection) 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] >= B_HEIGHT || y[0] < 0 || x[0] >= B_WIDTH || x[0] < 0) inGame = false;

    if (!inGame) timer.stop();
    }

    private void locateApple() {
    int r = (int) (Math.random() * RAND_POS);
    apple_x = ((r * DOT_SIZE));

    r = (int) (Math.random() * RAND_POS);
    apple_y = ((r * DOT_SIZE));
    }

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

    repaint();
    }

    private class TAdapter extends KeyAdapter {

    @Override
    public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) leftDirection = true;
    if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) rightDirection = true;
    if ((key == KeyEvent.VK_UP) && (!downDirection)) upDirection = true;
    if ((key == KeyEvent.VK_DOWN) && (!upDirection)) downDirection = true;
    }
    }
    }
    Gekopieerd.
    Feedback
  2. Java Snake game - learn how to create Snake game in …

    10 jan. 2023 · In this part of the Java 2D games tutorial, we create a Java Snake game clone. Source code and images can be found at the author's Github Java …

  3. Design Snake Game - GeeksforGeeks

    11 jul. 2025 · Code Explanation: The code in this class represents a snake game. The Snake object stores the information about the snake and the Board object …

  4. Snake Game In Java With Free Source Code

    • The Snake Game Java is built fully in Java and MySQLDatabase. It has a full-featured Graphical User Interface (GUI) with all the functionalities. This article is a way to enhance and develop our skills and logic ideas, which is important in practicing the Java programming language, which is the most well-known and most usable programming language i...
    Meer bekijken op itsourcecode.com
  5. Build a Classic Snake Game from Scratch with Pure JavaScript!

    • Meer weergeven

    Build a classic Snake game from scratch with pure JavaScript. This step-by-step guide covers setup, game logic, and customization tips for a fun coding project.

  6. Snake Game using Java Swing and Java AWT in Java - CodeSpeedy

    Hi, Today we are learning to build a simple snake game in java using Java-Swing and Java AWT. Java-Swing is a lightweight and cross-platform toolkit used to provide GUI to java programs.

  7. Mensen vragen ook naar
    Loading
    Unable to load answer
  8. How To Make Snake Game In Bluej | Mini Coding Projects - Blogger

    29 sep. 2020 · Source Code ========= import java.applet.*; import java.awt.*; import java.awt.event.*; public class Snake extends Applet implements KeyListener, Runnable { Color colors []= {Color.BLACK, …

  9. JavaScript Snake - patorjk.com

    JavaScript Snake Use the arrow keys on your keyboard to play the game. On Windows, press F11 to play in Full Screen mode.

  10. How to Create the Classic Snake Game in Java

    27 jan. 2025 · In this tutorial, we will guide you step-by-step to create a simple version of the Snake game using the Java programming language. The goal of this program is to help you understand how …

  11. Building a Snake Game in Java: A Comprehensive Guide

    Learn to create a classic Snake game in Java with this step-by-step tutorial. Perfect for beginners and advanced programmers alike!

  12. Verkrijg uitgebreide informatie over Snake Game Java code