[Résolu] Comment créer un jeu de serpent avec Java ?

  

5
Début du sujet

Je veux créer un jeu de serpent en utilisant Java avec Intelli J Idea (pas Eclipse);

Comme sur les anciens téléphones Nokia 🙂 sur la photo :

Jeu Nokia Snake

Si vous pouvez fournir un code, ce serait génial ! Merci de votre compréhension.

2 Réponses
4

Vous devez d'abord créer deux paquets : jeu du serpent - vous pouvez y placer la classe principale : SnakeGame ;

L'autre paquet est objets - vous pouvez y mettre 2 classes : Serpent et Pomme (ce que le serpent mange)

Voici la capture d'écran :

Jeu Java - Snake créé avec IntelliJ Idea

Voici le Jeu du serpent Classe (la principale) :

package snakegame;
 
import objects.Apple;
import objects.Snake;
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
 
public class SnakeGame extends JPanel implements ActionListener {
 
    public static final int SCALE = 32;
    public static final int WIDTH = 20;
    public static final int HEIGHT = 20;
    public static final int SPEED = 4;
 
    Apple a = new Apple((int) (Math.random() * WIDTH), (int) (Math.random() * HEIGHT));
    Snake s = new Snake(10, 10, 9, 10);
    Timer t = new Timer(1000 / SPEED, this);
 
    public SnakeGame() {
        t.start();
        addKeyListener(new Keyboard());
        setFocusable(true);
    }
 
    public void paint(Graphics g) {
        g.setColor(color(5, 50, 10));
        g.fillRect(0, 0, WIDTH * SCALE, HEIGHT * SCALE);
        g.setColor(color(255, 216, 0));
 
        for (int xx = 0; xx <= WIDTH * SCALE; xx += SCALE) {
            g.drawLine(xx, 0, xx, HEIGHT * SCALE);
        }
 
        for (int yy = 0; yy <= HEIGHT * SCALE; yy += SCALE) {
            g.drawLine(0, yy, WIDTH * SCALE, yy);
        }
 
        for (int d = 0; d < s.length; d++) {
            g.setColor(color(0, 0, 255));
            g.fillRect(s.snakeX[d] * SCALE + 1, s.snakeY[d] * SCALE + 1, SCALE - 1, SCALE - 1);
        }
 
        g.setColor(color(255, 0, 0));
        g.fillRect(a.posX * SCALE + 1, a.posY * SCALE + 1, SCALE - 1, SCALE - 1);
    }
 
    public Color color(int red, int green, int blue) {
        return new Color(red, green, blue);
    }
 
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
        f.setSize(WIDTH * SCALE + 7, HEIGHT * SCALE + 29);
        f.setLocationRelativeTo(null);
        f.add(new SnakeGame());
        f.setVisible(true);
    }
 
    public void actionPerformed(ActionEvent e) {
        s.move();
 
        if ((s.snakeX[0] == a.posX) & (s.snakeY[0] == a.posY)) {
            a.setRandomPosition();
            s.length++;
        }
 
        for (int k = 1; k < s.length; k++) {
            if ((s.snakeX[k] == a.posX) & (s.snakeY[k] == a.posY)) {
                a.setRandomPosition();
            }
        }
 
        repaint();
    }
 
    private class Keyboard extends KeyAdapter {
        public void keyPressed(KeyEvent kEve) {
            int key = kEve.getKeyCode();
 
            if ((key == KeyEvent.VK_RIGHT) & s.direction != 2) s.direction = 0;
            if ((key == KeyEvent.VK_DOWN) & s.direction != 3) s.direction = 1;
            if ((key == KeyEvent.VK_LEFT) & s.direction != 0) s.direction = 2;
            if ((key == KeyEvent.VK_UP) & s.direction != 1) s.direction = 3;
        }
    }
}

Classe Serpent:

package objects;
 
import snakegame.SnakeGame;
 
public class Snake {
 
    SnakeGame main;
 
    public int direction = 0;
    public int length = 2;
 
    public int snakeX[] = new int[main.WIDTH * main.HEIGHT];
    public int snakeY[] = new int[main.WIDTH * main.HEIGHT];
 
    public Snake(int x0, int y0, int x1, int y1) {
        snakeX[0] = x0;
        snakeY[0] = y0;
        snakeX[1] = x1;
        snakeY[1] = y1;
    }
 
    @SuppressWarnings("static-access")
    public void move() {
 
        for (int d = length; d > 0; d--) {
            snakeX[d] = snakeX[d - 1];
            snakeY[d] = snakeY[d - 1];
        }
 
        if (direction == 0) snakeX[0]++;
        if (direction == 1) snakeY[0]++;
        if (direction == 2) snakeX[0]--;
        if (direction == 3) snakeY[0]--;
 
        for (int d = length - 1; d > 0; d--) {
            if ((snakeX[0] == snakeX[d]) & (snakeX[0] == snakeY[d])) length = d - 2;
        }
 
        if (snakeX[0] > main.WIDTH) snakeX[0] = 0;
        if (snakeX[0] < 0) snakeX[0] = main.WIDTH - 1;
        if (snakeY[0] > main.HEIGHT - 1) snakeY[0] = 0;
        if (snakeY[0] < 0) snakeY[0] = main.HEIGHT - 1;
 
        if (length < 2) length = 2;
    }
}

Classe Pomme:

package objects;
 
import snakegame.SnakeGame;
 
public class Apple {
 
    SnakeGame main;
 
    public int posX;
    public int posY;
 
    public Apple(int startX, int startY) {
        posX = startX;
        posY = startY;
 
    }
 
    @SuppressWarnings("static-access")
    public void setRandomPosition() {
        posX = (int) (Math.random() * main.WIDTH);
        posY = (int) (Math.random() * main.HEIGHT);
    }
}
3

Un autre code pour un jeu de serpent en Java avec vidéo (bien que le jeu soit fait sur Eclipse, et non IntelliJ IDEA) :

Super jeu ! Merci ! Juste ce qu'il me fallait pour mon projet Java 🙂 .

Partager :