贪吃蛇小游戏

2023-12-13 05:01:43
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SnakeGame extends JFrame {
    private static final int TILE_SIZE = 25;
    private static final int GRID_WIDTH = 20;
    private static final int GRID_HEIGHT = 20;

    private Snake snake;
    private Timer gameTimer;

    public SnakeGame() {
        snake = new Snake();
        gameTimer = new Timer(200, new GameLoopListener());

        setTitle("贪吃蛇");
        setSize(GRID_WIDTH * TILE_SIZE, GRID_HEIGHT * TILE_SIZE);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);

        addKeyListener(new SnakeKeyListener());
        startGame();
    }

    private void startGame() {
        snake.initialize(GRID_WIDTH / 2, GRID_HEIGHT / 2);
        gameTimer.start();
    }

    private void gameOver() {
        gameTimer.stop();
        JOptionPane.showMessageDialog(this, "游戏结束", "游戏结束", JOptionPane.INFORMATION_MESSAGE);
        startGame();
    }

    private void update() {
        if (!snake.move()) {
            gameOver();
        }
    }

    private void draw(Graphics g) {
        g.clearRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.GREEN);
        for (Point point : snake.getBody()) {
            g.fillRect(point.x * TILE_SIZE, point.y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
        }
    }

    private class GameLoopListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            update();
            repaint();
        }
    }

    private class SnakeKeyListener extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_UP && snake.getDirection() != Snake.Direction.DOWN) {
                snake.setDirection(Snake.Direction.UP);
            } else if (keyCode == KeyEvent.VK_DOWN && snake.getDirection() != Snake.Direction.UP) {
                snake.setDirection(Snake.Direction.DOWN);
            } else if (keyCode == KeyEvent.VK_LEFT && snake.getDirection() != Snake.Direction.RIGHT) {
                snake.setDirection(Snake.Direction.LEFT);
            } else if (keyCode == KeyEvent.VK_RIGHT && snake.getDirection() != Snake.Direction.LEFT) {
                snake.setDirection(Snake.Direction.RIGHT);
            }
        }
    }

    private class Snake {
        private LinkedList<Point> body;
        private Direction direction;

        public Snake() {
            body = new LinkedList<>();
            direction = Direction.RIGHT;
        }

        public void initialize(int x, int y) {
            body.clear();
            body.add(new Point(x, y));
        }

        public LinkedList<Point> getBody() {
            return body;
        }

        public Direction getDirection() {
            return direction;
        }

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

        public boolean move() {
            Point head = body.getFirst();
            Point newHead;
            switch (direction) {
                case UP:
                    newHead = new Point(head.x, head.y - 1);
                    break;
                case DOWN:
                    newHead = new Point(head.x, head.y + 1);
                    break;
                case LEFT:
                    newHead = new Point(head.x - 1, head.y);
                    break;
                case RIGHT:
                    newHead = new Point(head.x + 1, head.y);
                    break;
                default:
                    return false;
            }

            if (newHead.x < 0 || newHead.x >= GRID_WIDTH || newHead.y < 0 || newHead.y >= GRID_HEIGHT
                    || body.contains(newHead)) {
                return false;
            }

            body.addFirst(newHead);
            if (body.size() > 1 && !newHead.equals(food)) {
                body.removeLast();
            }

            return true;
        }

        public enum Direction {
            UP, DOWN, LEFT, RIGHT
        }
    }

    public void paint(Graphics g) {
        super.paint(g);
        draw(g);
    }

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

这个示例使用了 Java 的 Swing 库来创建游戏界面,并提供了基本的贪吃蛇游戏功能。按下上、下、左或右箭头键来控制贪吃蛇的移动方向。

请注意,这只是一个简单的示例,可能还有很多可以改进和优化的地方。

文章来源:https://blog.csdn.net/jjwlikegirl/article/details/134956555
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。