[已解决] 如何使用 Java 制作蛇形游戏?

  

5
主题启动器

我想用 Java 和 Intelli J Idea (不是日蚀);

就像图片上的老式诺基亚手机一样 🙂:

诺基亚蛇形游戏

如果您能提供代码,那就太好了!谢谢

2 答案
4

首先,您需要创建 2 个软件包: 蛇棋 - 你可以把主类放在那里:SnakeGame;

另一个软件包是 物件 - 您可以在这里设置 2 个类别:蛇和苹果(蛇吃的东西)

下面是截图:

Java 游戏 - 使用 IntelliJ Idea 制作的蛇形游戏

以下是 蛇游戏 班级(主要班级):

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;
        }
    }
}

班级 :

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;
    }
}

班级 苹果:

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

Java 蛇形游戏的另一个代码,附带视频(尽管游戏是在 Eclipse 而不是 IntelliJ IDEA 上制作的):

酷游戏!谢谢!这正是我的 Java 项目所需要的! 🙂

分享: