当前位置:

首页 > 编程开发 > Java编写小游戏教程:实现“不贪吃蛇”游戏

Java编写小游戏教程:实现“不贪吃蛇”游戏

代码蛇、药丸的抽象坐标Point.java记录横纵坐标值。packagecn.xeblog.snake.model;importjava.util.Objects;/***坐标**@authoranlingyi*@date2022/8/23:35PM*/publicclassPoint{publicintx;publicinty;publicPoint(intx,inty){this.x=x;this.y=y;}@Overridepublicbooleanequals(Objecto){if(this==

    Java编写小游戏教程:实现“不贪吃蛇”游戏

    Java编写小游戏教程:实现“不贪吃蛇”游戏

    代码

    蛇、药丸的抽象

    坐标 Point.java

    记录横纵坐标值。

    package cn.xeblog.snake.model;
    
    import java.util.Objects;
    
    /**
     * 坐标
     *
     * @author anlingyi
     * @date 2022/8/2 3:35 PM
     */
    public class Point {
    
        public int x;
    
        public int y;
    
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Point point = (Point) o;
            return x == point.x && y == point.y;
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }
    
        @Override
        public String toString() {
            return "Point{" +
                    "x=" + x +
                    ", y=" + y +
                    '}';
        }
    
    }

    移动方向 Direction.java

    提供上、下、左、右四个移动方向的枚举。

    package cn.xeblog.snake.model;
    
    /**
     * @author anlingyi
     * @date 2022/8/2 5:25 PM
     */
    public enum Direction {
        UP,
        DOWN,
        LEFT,
        RIGHT
    }

    蛇 Snake.java

    存储蛇身坐标信息,提供蛇身移动、移除蛇尾坐标、获取蛇头、蛇尾坐标、蛇身长度等方法。

    这里讲一下蛇移动的实现原理:游戏开始时,会固定一个移动方向,蛇会一直朝着这个方向移动,我们可以通过方向键改变蛇的移动方向,蛇的移动其实就是将蛇身的坐标移动一下位置,比如蛇身长度(不包含蛇头)为6,移动时,只需将蛇身位置为5的坐标移到位置为6的坐标去,位置为4的坐标移动到位置为5的坐标去,简单来说就是将前一个的坐标换到它后面一个去,这是蛇身的移动,蛇头的移动需要单独计算,如果是上下方向移动,那就是对y坐标的加减操作,向上运动需要减一个蛇身的高度,向下则与之相反,需要加一个蛇身的高度,左右运动同理。

    package cn.xeblog.snake.model;
    
    import java.util.List;
    
    /**
     * 蛇
     *
     * @author anlingyi
     * @date 2022/8/2 3:32 PM
     */
    public class Snake {
    
        public static int width = 10;
    
        public static int height = 10;
    
        /**
         * 蛇身坐标列表
         */
        public List body;
    
        public Snake(List body) {
            this.body = body;
        }
    
        /**
         * 添加蛇身坐标
         *
         * @param x
         * @param y
         */
        public void add(int x, int y) {
            this.body.add(new Point(x * width, y * height));
        }
    
        /**
         * 移除蛇尾坐标
         */
        public void removeLast() {
            int size = size();
            if (size == 0) {
                return;
            }
    
            this.body.remove(size - 1);
        }
    
        /**
         * 获取蛇头坐标
         *
         * @return
         */
        public Point getHead() {
            if (size() > 0) {
                return this.body.get(0);
            }
    
            return null;
        }
    
        /**
         * 获取蛇尾坐标
         *
         * @return
         */
        public Point getTail() {
            int size = size();
            if (size > 0) {
                return this.body.get(size - 1);
            }
    
            return null;
        }
    
        /**
         * 蛇身长度
         *
         * @return
         */
        public int size() {
            return this.body.size();
        }
    
        /**
         * 蛇移动
         *
         * @param direction 移动方向
         */
        public void move(Direction direction) {
            if (size() == 0) {
                return;
            }
    
            for (int i = this.size() - 1; i > 0; i--) {
                // 从蛇尾开始向前移动
                Point point = this.body.get(i);
                Point nextPoint = this.body.get(i - 1);
                point.x = nextPoint.x;
                point.y = nextPoint.y;
            }
    
            // 蛇头移动
            Point head = getHead();
            switch (direction) {
                case UP:
                    head.y -= height;
                    break;
                case DOWN:
                    head.y += height;
                    break;
                case LEFT:
                    head.x -= width;
                    break;
                case RIGHT:
                    head.x += width;
                    break;
            }
        }
    
    }

    药丸 Pill.java

    存储“药丸“的坐标、类型信息。

    package cn.xeblog.snake.model;
    
    /**
     * 药丸
     *
     * @author anlingyi
     * @date 2022/8/2 4:49 PM
     */
    public class Pill {
    
        public static int width = 10;
    
        public static int height = 10;
    
        /**
         * 坐标
         */
        public Point point;
    
        /**
         * 药丸类型
         */
        public PillType pillType;
    
        public enum PillType {
            /**
             * 红色药丸
             */
            RED(5),
            /**
             * 蓝色药丸
             */
            BLUE(2),
            /**
             * 绿色药丸
             */
            GREEN(1),
            ;
    
            /**
             * 分数
             */
            public int score;
    
            PillType(int score) {
                this.score = score;
            }
        }
    
        public Pill(int x, int y, PillType pillType) {
            this.point = new Point(x * width, y * height);
            this.pillType = pillType;
        }
    
    }

    游戏界面

    初始化一些信息,比如游戏界面的宽高、定时器、一些状态标识(是否停止游戏、游戏是否胜利)、监听一些按键事件(空格键开始/暂停游戏、四个方向键控制蛇移动的方向),绘制游戏画面。

    当检测到游戏开始时,初始化蛇、“药丸”的位置信息,然后启动定时器每隔一段时间重新绘制游戏画面,让蛇可以动起来。

    当检测到蛇咬到自己或者是蛇撞墙时,游戏状态将会被标记为“游戏失败”,绘制游戏结束画面,并且定时器停止,如果蛇身为0,则游戏结束,游戏状态标记为“游戏胜利”。

    package cn.xeblog.snake.ui;
    
    import cn.xeblog.snake.model.Direction;
    import cn.xeblog.snake.model.Pill;
    import cn.xeblog.snake.model.Point;
    import cn.xeblog.snake.model.Snake;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Random;
    
    /**
     * @author anlingyi
     * @date 2022/8/2 3:51 PM
     */
    public class SnakeGameUI extends JPanel implements ActionListener {
    
        /**
         * 宽度
         */
        private int width;
    
        /**
         * 高度
         */
        private int height;
    
    
        /**
         * 蛇
         */
        private Snake snake;
    
        /**
         * 药丸
         */
        private Pill pill;
    
        /**
         * 移动方向
         */
        private Direction direction;
    
        /**
         * 停止游戏标记
         */
        private boolean stop;
    
        /**
         * 游戏状态 0.初始化 1.游戏胜利 2.游戏失败
         */
        private int state = -1;
    
        /**
         * 定时器
         */
        private Timer timer;
    
        /**
         * 移动速度
         */
        private int speed = 100;
    
        /**
         * 分数
         */
        private int score;
    
        /**
         * 特殊药丸列表
         */
        private ArrayList specialPill;
    
        public SnakeGameUI(int width, int height) {
            this.width = width;
            this.height = height;
            this.timer = new Timer(speed, this);
            this.stop = true;
    
            initPanel();
        }
    
        /**
         * 初始化
         */
        private void init() {
            this.score = 0;
            this.state = 0;
            this.stop = true;
            this.timer.setDelay(speed);
    
            initSnake();
            initPill();
            generatePill();
            repaint();
        }
    
        /**
         * 初始化游戏面板
         */
        private void initPanel() {
            this.setPreferredSize(new Dimension(this.width, this.height));
            this.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    if (stop && e.getKeyCode() != KeyEvent.VK_SPACE) {
                        return;
                    }
    
                    switch (e.getKeyCode()) {
                        case KeyEvent.VK_UP:
                            if (direction == Direction.DOWN) {
                                break;
                            }
    
                            direction = Direction.UP;
                            break;
                        case KeyEvent.VK_DOWN:
                            if (direction == Direction.UP) {
                                break;
                            }
    
                            direction = Direction.DOWN;
                            break;
                        case KeyEvent.VK_LEFT:
                            if (direction == Direction.RIGHT) {
                                break;
                            }
    
                            direction = Direction.LEFT;
                            break;
                        case KeyEvent.VK_RIGHT:
                            if (direction == Direction.LEFT) {
                                break;
                            }
    
                            direction = Direction.RIGHT;
                            break;
                        case KeyEvent.VK_SPACE:
                            if (state != 0) {
                                init();
                            }
    
                            stop = !stop;
                            if (!stop) {
                                timer.start();
                            }
                            break;
                    }
                }
            });
        }
    
        /**
         * 初始化蛇
         */
        private void initSnake() {
            this.direction = Direction.LEFT;
            int maxX = this.width / Snake.width;
            int maxY = this.height / Snake.height;
    
            this.snake = new Snake(new ArrayList<>());
            this.snake.add(maxX - 2, 3);
            this.snake.add(maxX - 1, 3);
            this.snake.add(maxX - 1, 2);
            this.snake.add(maxX - 1, 1);
            for (int i = maxX - 1; i > 0; i--) {
                this.snake.add(i, 1);
            }
            for (int i = 1; i < maxY - 1; i++) {
                this.snake.add(1, i);
            }
            for (int i = 1; i < maxX - 1; i++) {
                this.snake.add(i, maxY - 2);
            }
        }
    
        /**
         * 初始化药丸
         */
        private void initPill() {
            this.specialPill = new ArrayList<>();
            for (int i = 0; i < 5; i++) {
                this.specialPill.add(Pill.PillType.RED);
            }
            for (int i = 0; i < 10; i++) {
                this.specialPill.add(Pill.PillType.BLUE);
            }
    
            Collections.shuffle(specialPill);
        }
    
        /**
         * 生成药丸
         */
        private void generatePill() {
            // 是否获取特殊药丸
            boolean getSpecialPill = new Random().nextInt(6) == 3;
            Pill.PillType pillType;
            if (getSpecialPill && this.specialPill.size() > 0) {
                // 生成特殊药丸
                int index = new Random().nextInt(this.specialPill.size());
                pillType = this.specialPill.get(index);
                this.specialPill.remove(index);
            } else {
                // 生成绿色药丸
                pillType = Pill.PillType.GREEN;
            }
    
            // 随机坐标
            int x = new Random().nextInt(this.width / Pill.width - 1);
            int y = new Random().nextInt(this.height / Pill.height - 1);
            this.pill = new Pill(x, y, pillType);
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
            g2.setColor(new Color(66, 66, 66));
            g2.fillRect(0, 0, this.width, this.height);
    
            if (this.snake != null) {
                // 画蛇
                g2.setColor(new Color(255, 255, 255));
                for (int i = this.snake.size() - 1; i >= 0; i--) {
                    Point point = this.snake.body.get(i);
                    if (i == 0) {
                        // 蛇头
                        g2.setColor(new Color(255, 92, 92));
                    } else {
                        g2.setColor(new Color(215, 173, 173));
                    }
    
                    g2.fillRect(point.x, point.y, Snake.width, Snake.height);
                }
            }
    
            if (this.pill != null) {
                // 画药丸
                Color pillColor;
                switch (this.pill.pillType) {
                    case RED:
                        pillColor = new Color(255, 41, 41);
                        break;
                    case BLUE:
                        pillColor = new Color(20, 250, 243);
                        break;
                    default:
                        pillColor = new Color(97, 255, 113);
                        break;
                }
    
                g2.setColor(pillColor);
                g2.fillOval(pill.point.x, pill.point.y, Pill.width, Pill.height);
            }
    
            if (state > 0) {
                // 显示游戏结果
                String tips = "游戏失败!";
                if (state == 1) {
                    tips = "游戏胜利!";
                }
                g2.setFont(new Font("", Font.BOLD, 20));
                g2.setColor(new Color(208, 74, 74));
                g2.drawString(tips, this.width / 3, this.height / 3);
    
                g2.setFont(new Font("", Font.PLAIN, 18));
                g2.setColor(Color.WHITE);
                g2.drawString("得分:" + this.score, this.width / 2, this.height / 3 + 50);
            }
    
            if (stop) {
                g2.setFont(new Font("", Font.PLAIN, 18));
                g2.setColor(Color.WHITE);
                g2.drawString("按空格键开始/暂停游戏!", this.width / 4, this.height - 50);
            }
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // 是否吃药
            boolean isAte = false;
            if (!this.stop) {
                // 移动蛇
                this.snake.move(this.direction);
                Point head = this.snake.getHead();
                if (head.equals(this.pill.point)) {
                    // 吃药了
                    isAte = true;
                    // 药丸分数
                    int getScore = this.pill.pillType.score;
                    // 累计分数
                    this.score += getScore;
                    for (int i = 0; i < getScore; i++) {
                        // 移除蛇尾
                        this.snake.removeLast();
                        if (this.snake.size() == 0) {
                            // 游戏胜利
                            this.state = 1;
                            this.stop = true;
                            break;
                        }
                    }
    
                    pill = null;
                    if (this.score % 10 == 0) {
                        int curSpeed = this.timer.getDelay();
                        if (curSpeed > 30) {
                            // 加速
                            this.timer.setDelay(curSpeed - 10);
                        }
                    }
                }
    
                if (state == 0) {
                    // 判断蛇有没有咬到自己或是撞墙
                    int maxWidth = this.width - this.snake.width;
                    int maxHeight = this.height - this.snake.height;
                    boolean isHitWall = head.x > maxWidth || head.x < 0 || head.y > maxHeight || head.y < 0;
                    boolean isBiting = false;
                    for (int i = this.snake.size() - 1; i > 0; i--) {
                        if (head.equals(this.snake.body.get(i))) {
                            isBiting = true;
                            break;
                        }
                    }
    
                    if (isHitWall || isBiting) {
                        // 游戏失败
                        this.state = 2;
                        this.stop = true;
                    }
                }
            }
    
            if (this.stop) {
                this.timer.stop();
            } else if (isAte) {
                // 重新生成药丸
                generatePill();
            }
    
            repaint();
        }
    
    }

    启动类

    游戏启动入口。

    package cn.xeblog.snake;
    
    import cn.xeblog.snake.ui.SnakeGameUI;
    
    import javax.swing.*;
    
    /**
     * 启动游戏
     *
     * @author anlingyi
     * @date 2022/8/2 3:41 PM
     */
    public class StartGame {
    
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setVisible(true);
            frame.setResizable(false);
            frame.setTitle("不贪吃蛇");
            frame.setSize(400, 320);
            frame.setLocationRelativeTo(null);
            JPanel gamePanel = new SnakeGameUI(400, 300);
            frame.add(gamePanel);
            gamePanel.requestFocus();
        }
    
    }
    本文内容来源于互联网,如有侵权请联系删除。
    作者最新文章
    编程开发
    相关文章 更多
    C++动态数组初始化怎么写?常用语句与代码示例
    C++动态数组初始化怎么写?常用语句与代码示例

    深入解析C++中动态数组的初始化机制,涵盖new操作符的不同用法、基本类型与类对象的初始化差异,以及为何在现代C++开发中应优先使用std::vector。

    谷歌浏览器Mac版入口
    谷歌浏览器Mac版入口

    谷歌浏览器Mac版官方安装指南 谷歌浏览器Mac版官方安装入口是https://www.google.com/chrome/,需macOS 12+系统、500MB空间,下载.dmg后拖入应用程序安装,支持多设备同步、性能优化与隐私保护功能。 苹果电脑Chrome的安装入口究竟在哪里?这个问题最近可是

    Chrome浏览器JS脚本不运行怎么办
    Chrome浏览器JS脚本不运行怎么办

    Chrome中JavaScript未执行需依次检查:一、移除站点级禁用并添加允许域名;二、开启全局JavaScript开关;三、禁用干扰扩展;四、在开发者工具中启用JavaScript;五、重置内容设置为默认。 有时在Chrome里打开网页,会发现交互按钮点了没反应,数据加载不出来,页面仿佛“静止”

    IE浏览器怀旧版在线网址
    IE浏览器怀旧版在线网址

    IE浏览器怀旧版在线网址:一次精准的技术时光回溯 最近,不少老用户和怀旧爱好者在反复搜索一个问题:那个经典的Internet Explorer,如今还能在哪里原汁原味地体验到?答案指向一个特定的地址:https://ie.microsoft.com/legacy/。 这个网站远不止是一个简单的“皮肤

    火狐浏览器有哪些设置功能
    火狐浏览器有哪些设置功能

    火狐浏览器五大核心设置功能:解锁高效、安全与个性化体验 火狐浏览器功能强大,但如果不仔细挖掘,很多能大幅提升效率和安全性的设置可能就“藏着掖着”了。这就好比拥有一台高性能设备,却只用了基础模式。那么,如何把它调整到最顺手、最安全的状态?接下来,我们就聚焦于当前版本(截至2025年末)最关键的五大设置

    chrome搜索免验证入口
    chrome搜索免验证入口

    Chrome官方免验证入口为https://www.google.cn/chrome/,提供全平台安装包、免登录即用、本地化安全机制及引擎级性能优化。 到底该去哪里找正版、免费且无需繁琐验证的Chrome浏览器入口?这个问题困扰了不少网友。今天,我们就来直通核心,为大家详细拆解Chrome引擎的官方

    java heap space 选型思路:使用场景与区别整理
    java heap space 选型思路:使用场景与区别整理

    Java堆是JVM存储对象的核心内存区域,配置需结合场景:单体应用适中设置;大数据处理需大堆并关注GC停顿;微服务强调快速启动;高并发需精细划分堆区域。关键参数-Xms和-Xmx建议等值以稳定性能。垃圾回收器选择影响效率,如G1适用于大堆,ZGC可实现低停顿。内存错误时需监控堆状态。

    java heap space 使用中遇到的问题怎么解决
    java heap space 使用中遇到的问题怎么解决

    Java堆内存溢出错误通常因内存泄漏、数据处理需求过大或JVM参数配置不当引起。排查时可借助jmap、堆转储及MAT等工具定位问题。解决方案包括调整JVM内存参数(如-Xmx)、修复代码中的内存泄漏、优化大数据处理逻辑,并建立持续监控与预防机制,以保障应用稳定运行。

    java xml 选型思路:使用场景与区别整理
    java xml 选型思路:使用场景与区别整理

    XML在Java开发中用于配置、数据交换等场景。解析方式主要有DOM、SAX、StAX及第三方库。DOM适合操作小文件,SAX/StAX适合处理大文件流,JAXB用于对象与XML映射。选型需结合数据大小、内存、性能及团队熟悉度,现代框架常封装底层解析。

    using namespace 使用中遇到的问题怎么解决
    using namespace 使用中遇到的问题怎么解决

    命名空间的基本概念与常见引入问题在C++等编程语言中,命名空间(namespace)是一种将代码标识符(如变量、函数、类名)封装在特定名称下的机制,其主要目的是避免命名冲突,尤其是在大型项目或使用多个第三方库时。使用“using namespace”指令可以将指定命名空间中的所有名称引入当前作用域,

    查看更多
    精品专题 更多
    装机必备
    装机必备

    正软商城装机必备专区,精选办公、浏览器、安全防护、影音播放、压缩解压、设计创作和系统工具等电脑常用正版软件,帮助用户快速完成新电脑软件配置。

    Windows
    Windows

    正软商城Windows软件专区,汇集适用于Windows电脑的办公、设计、安全防护、影音播放、开发工具和系统优化软件,提供软件介绍、系统要求、正版授权及购买下载服务。

    macOS软件
    macOS软件

    正软商城macOS软件专区,精选适用于Mac电脑的办公、设计、影音、效率、开发和系统工具,提供软件功能介绍、macOS兼容版本、正版授权及购买下载服务。

    Mac软件 更多
    灵活计算器
    灵活计算器
    macOS/iOS/Android

    灵活计算器是一款笔记式算数应用,支持实时计算、动态关联和云端同步功能。记录、整理和输出之间的过渡会更自然,适合长期写作、做笔记或持续沉淀个人内容。

    赤友清理大师
    赤友清理大师
    macOS

    赤友清理大师是一款为 Mac 设计的智能清理优化工具,可精准扫描垃圾、大文件、重复文件等,释放磁盘空间。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。

    极度公式
    极度公式
    Windows/macOS/Linux

    极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。

    WINDOWS 更多
    Windows 10
    Windows 10
    Windows

    Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。

    极度公式
    极度公式
    Windows/macOS/Linux

    极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。

    密码键盘
    密码键盘
    Windows/macOS/iOS/Android

    密码键盘是一款兼具安全性与便捷性的高效密码管理器。日常使用里的持续防护和信息管理会更突出,适合把安全控制放进长期使用流程中的场景。