修改D类的构造函数,添加边界墙和积分功能:

D() {
    v.add(new Point(20, 10));
    v.add(new Point(20, 10));
    v.add(new Point(20, 10));
    v.add(new Point(20, 10));

    int t = r.nextInt(20);
    food.x = t * 10;
    food.y = t * 10;
    addKeyListener(this);
}

public void paint(Graphics g) {
    super.paint(g);
    // 画边界墙
    g.setColor(Color.gray);
    g.fillRect(0, 0, 600, 10);
    g.fillRect(0, 0, 10, 400);
    g.fillRect(590, 0, 10, 400);
    g.fillRect(0, 390, 600, 10);

    for (int i = 0; i < v.size(); i++) {
        Point p = v.get(i);
        int x = p.x;
        int y = p.y;
        if (i == v.size() - 1) {
            g.setColor(Color.blue);
            g.fillOval(x, y, 9, 9);
        } else {
            g.setColor(Color.black);
            g.fillOval(x, y, 9, 9);
        }
    }

    int x = food.x;
    int y = food.y;
    g.setColor(Color.red);
    g.fillRect(x, y, 9, 9);
}

public void run() {
    try {
        while (true) {
            for (int i = 0; i < v.size() - 1; i++) {
                Point p = v.get(i);
                Point p2 = v.get(i + 1);
                p.x = p2.x;
                p.y = p2.y;
            }
            if (direction == 0) {
                Point p = v.get(v.size() - 1);
                p.x = p.x + 10;
                // 撞墙检测
                if (p.x > 580) {
                    JOptionPane.showMessageDialog(null, "Game Over! Your score is " + (v.size() - 4));
                    System.exit(0);
                }
            }
            if (direction == 1) {
                Point p = v.get(v.size() - 1);
                p.x = p.x - 10;
                // 撞墙检测
                if (p.x < 10) {
                    JOptionPane.showMessageDialog(null, "Game Over! Your score is " + (v.size() - 4));
                    System.exit(0);
                }
            }
            if (direction == 2) {
                Point p = v.get(v.size() - 1);
                p.y = p.y - 10;
                // 撞墙检测
                if (p.y < 10) {
                    JOptionPane.showMessageDialog(null, "Game Over! Your score is " + (v.size() - 4));
                    System.exit(0);
                }
            }
            if (direction == 3) {
                Point p = v.get(v.size() - 1);
                p.y = p.y + 10;
                // 撞墙检测
                if (p.y > 370) {
                    JOptionPane.showMessageDialog(null, "Game Over! Your score is " + (v.size() - 4));
                    System.exit(0);
                }
            }

            for (int i = 0; i < v.size(); i++) {
                Point p = v.get(i);
                if (p.x == food.x && p.y == food.y) {
                    int t = r.nextInt(20);
                    food.x = t * 10;
                    food.y = t * 10;

                    v.add(new Point(p.x, p.y));
                    break;
                }
            }
            repaint();
            Thread.sleep(100);
        }
    } catch (Exception e) {

    }
}

在此基础上,我们可以在游戏结束时弹出对话框显示最终得分

Press Shift twice to open the Search Everywhere dialog and type show whitespaces then press Enter You can now see whitespace characters in your codeimport javaawt;import javaxswing;import javaawtevent

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

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