好的,主人。以下是澪为您编写的 Java 贪吃蛇代码:

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

public class SnakeGame extends JFrame implements ActionListener {
    private Random rand = new Random();
    private Timer timer;
    private JPanel board;
    private JLabel scoreLabel;
    private int score;
    private int[][] grid;
    private ArrayList<Point> snake;
    private Point food;

    public SnakeGame() {
        setTitle('贪吃蛇');
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        board = new JPanel();
        board.setLayout(new GridLayout(20, 20));
        getContentPane().add(board, BorderLayout.CENTER);

        scoreLabel = new JLabel('得分: 0');
        getContentPane().add(scoreLabel, BorderLayout.NORTH);

        grid = new int[20][20];
        snake = new ArrayList<Point>();
        snake.add(new Point(10, 10));
        grid[10][10] = 1;
        food = new Point(rand.nextInt(20), rand.nextInt(20));
        grid[food.x][food.y] = 2;

        timer = new Timer(100, this);
        timer.start();

        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                int code = e.getKeyCode();
                if (code == KeyEvent.VK_UP && snake.get(0).y > 0 && grid[snake.get(0).x][snake.get(0).y-1] != 1) {
                    snake.add(0, new Point(snake.get(0).x, snake.get(0).y-1));
                } else if (code == KeyEvent.VK_DOWN && snake.get(0).y < 19 && grid[snake.get(0).x][snake.get(0).y+1] != 1) {
                    snake.add(0, new Point(snake.get(0).x, snake.get(0).y+1));
                } else if (code == KeyEvent.VK_LEFT && snake.get(0).x > 0 && grid[snake.get(0).x-1][snake.get(0).y] != 1) {
                    snake.add(0, new Point(snake.get(0).x-1, snake.get(0).y));
                } else if (code == KeyEvent.VK_RIGHT && snake.get(0).x < 19 && grid[snake.get(0).x+1][snake.get(0).y] != 1) {
                    snake.add(0, new Point(snake.get(0).x+1, snake.get(0).y));
                }
            }
        });

        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == timer) {
            int x = snake.get(0).x;
            int y = snake.get(0).y;
            if (x == food.x && y == food.y) {
                score += 10;
                scoreLabel.setText('得分: ' + score);
                food = new Point(rand.nextInt(20), rand.nextInt(20));
                grid[food.x][food.y] = 2;
            } else {
                Point tail = snake.remove(snake.size()-1);
                grid[tail.x][tail.y] = 0;
            }
            if (snake.get(0).x < 0 || snake.get(0).x >= 20 || snake.get(0).y < 0 || snake.get(0).y >= 20 || grid[snake.get(0).x][snake.get(0).y] == 1) {
                timer.stop();
                JOptionPane.showMessageDialog(this, '游戏结束,得分为: ' + score);
            } else {
                grid[snake.get(0).x][snake.get(0).y] = 1;
                board.repaint();
            }
        }
    }

    public void paint(Graphics g) {
        super.paint(g);
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                if (grid[i][j] == 1) {
                    g.setColor(Color.GREEN);
                    g.fillRect(i*20, j*20, 20, 20);
                } else if (grid[i][j] == 2) {
                    g.setColor(Color.RED);
                    g.fillOval(i*20, j*20, 20, 20);
                }
            }
        }
    }

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

主人,以上是澪为您编写的 Java 贪吃蛇代码。其中,使用了 Java 的 Swing 库来实现游戏界面和交互,使用了 Timer 类来实现游戏的计时和更新。代码中使用了二维数组来表示游戏区域,使用 ArrayList 来表示蛇的身体,使用 Point 类来表示食物的位置。在游戏中,通过监听键盘事件来控制蛇的移动,通过计时器来更新游戏状态和界面。希望这份代码能够满足您的需求,祝您玩得愉快!

Java 贪吃蛇游戏代码 - 轻松实现经典游戏

原文地址: https://www.cveoy.top/t/topic/kBYM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录