贪吃蛇小游戏

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 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 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 库来创建游戏界面,并提供了基本的贪吃蛇游戏功能。按下上、下、左或右箭头键来控制贪吃蛇的移动方向。

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

你可能感兴趣的:(前端)