返回

JAVA趣味经典《贪吃蛇大作战》

后端

在本文中,我们将一起使用Java语言创建一个经典的《贪吃蛇大作战》游戏。我们将利用Java的Swing图形用户界面组件来实现这款游戏,并深入探讨其技术细节和编程技巧。

在《贪吃蛇大作战》游戏中,玩家控制一条贪吃蛇在游戏区域内移动,并食用散落在其中的食物来增加蛇的长度。随着游戏的进行,蛇的长度会不断增长,而游戏难度也会随之增加。同时,游戏还会提供一些障碍物,如墙壁和障碍物,为游戏增加挑战性。

接下来,我们将详细介绍如何使用Java的Swing组件来实现《贪吃蛇大作战》游戏的各个部分。

游戏主类

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SnakeGame extends JFrame {

    private Snake snake;
    private Food food;
    private Timer timer;
    private int score;

    public SnakeGame() {
        init();
    }

    private void init() {
        setTitle("贪吃蛇大作战");
        setSize(800, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);

        JPanel panel = new JPanel();
        panel.setLayout(null);
        add(panel);

        snake = new Snake();
        food = new Food();

        timer = new Timer(100, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                snake.move();
                checkCollision();
                repaint();
            }
        });
        timer.start();

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_UP:
                        snake.setDirection(Snake.UP);
                        break;
                    case KeyEvent.VK_DOWN:
                        snake.setDirection(Snake.DOWN);
                        break;
                    case KeyEvent.VK_LEFT:
                        snake.setDirection(Snake.LEFT);
                        break;
                    case KeyEvent.VK_RIGHT:
                        snake.setDirection(Snake.RIGHT);
                        break;
                }
            }
        });
    }

    private void checkCollision() {
        if (snake.getHead().equals(food.getPosition())) {
            snake.eat();
            food.generate();
            score++;
        }

        if (snake.isCollidedWithSelf() || snake.isCollidedWithWall()) {
            timer.stop();
            JOptionPane.showMessageDialog(this, "游戏结束!您的得分是:" + score);
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        snake.draw(g);
        food.draw(g);

        g.drawString("得分:" + score, 10, 20);
    }

    public static void main(String[] args) {
        new SnakeGame().setVisible(true);
    }
}

蛇类

import java.awt.*;
import java.util.LinkedList;

public class Snake {

    private static final int UP = 1;
    private static final int DOWN = 2;
    private static final int LEFT = 3;
    private static final int RIGHT = 4;

    private LinkedList<Point> body;
    private int direction;

    public Snake() {
        body = new LinkedList<>();
        body.add(new Point(200, 200));
        body.add(new Point(190, 200));
        body.add(new Point(180, 200));
        direction = RIGHT;
    }

    public void move() {
        Point head = body.getFirst();
        switch (direction) {
            case UP:
                head = new Point(head.x, head.y - 10);
                break;
            case DOWN:
                head = new Point(head.x, head.y + 10);
                break;
            case LEFT:
                head = new Point(head.x - 10, head.y);
                break;
            case RIGHT:
                head = new Point(head.x + 10, head.y);
                break;
        }
        body.addFirst(head);

        if (body.size() > 3) {
            body.removeLast();
        }
    }

    public void eat() {
        Point head = body.getFirst();
        body.addFirst(head);
    }

    public Point getHead() {
        return body.getFirst();
    }

    public boolean isCollidedWithSelf() {
        Point head = body.getFirst();
        for (int i = 1; i < body.size(); i++) {
            if (head.equals(body.get(i))) {
                return true;
            }
        }
        return false;
    }

    public boolean isCollidedWithWall() {
        Point head = body.getFirst();
        return head.x < 0 || head.x > 790 || head.y < 0 || head.y > 590;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

    public void draw(Graphics g) {
        for (Point point : body) {
            g.fillRect(point.x, point.y, 10, 10);
        }
    }
}

食物类

import java.awt.*;
import java.util.Random;

public class Food {

    private Random random = new Random();
    private Point position;

    public Food() {
        generate();
    }

    public void generate() {
        position = new Point(random.nextInt(79) * 10, random.nextInt(59) * 10);
    }

    public Point getPosition() {
        return position;
    }

    public void draw(Graphics g) {
        g.setColor(Color.RED);
        g.fillRect(position.x, position.y, 10, 10);
    }
}

最后,如果您需要使用AI螺