0

0

Java编写小游戏教程:不贪吃蛇游戏

王林

王林

发布时间:2023-04-27 12:22:07

|

1247人浏览过

|

来源于亿速云

转载

    如何利用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

    立即学习Java免费学习笔记(深入)”;

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

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

    蛇 Snake.java

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

    BiLin AI
    BiLin AI

    免费的多语言AI搜索引擎

    下载

    这里讲一下蛇移动的实现原理:游戏开始时,会固定一个移动方向,蛇会一直朝着这个方向移动,我们可以通过方向键改变蛇的移动方向,蛇的移动其实就是将蛇身的坐标移动一下位置,比如蛇身长度(不包含蛇头)为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<Point> body;
    
        public Snake(List<Point> 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<Pill.PillType> 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();
        }
    
    }

    相关文章

    在线游戏
    在线游戏

    海量精品小游戏合集,无需安装即点即玩,休闲益智、动作闯关应有尽有,秒开即玩,轻松解压,快乐停不下来

    下载

    相关标签:

    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    热门AI工具

    更多
    DeepSeek
    DeepSeek

    幻方量化公司旗下的开源大模型平台

    豆包大模型
    豆包大模型

    字节跳动自主研发的一系列大型语言模型

    WorkBuddy
    WorkBuddy

    腾讯云推出的AI原生桌面智能体工作台

    腾讯元宝
    腾讯元宝

    腾讯混元平台推出的AI助手

    文心一言
    文心一言

    文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

    讯飞写作
    讯飞写作

    基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

    即梦AI
    即梦AI

    一站式AI创作平台,免费AI图片和视频生成。

    ChatGPT
    ChatGPT

    最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

    相关专题

    更多
    TypeScript类型系统进阶与大型前端项目实践
    TypeScript类型系统进阶与大型前端项目实践

    本专题围绕 TypeScript 在大型前端项目中的应用展开,深入讲解类型系统设计与工程化开发方法。内容包括泛型与高级类型、类型推断机制、声明文件编写、模块化结构设计以及代码规范管理。通过真实项目案例分析,帮助开发者构建类型安全、结构清晰、易维护的前端工程体系,提高团队协作效率与代码质量。

    26

    2026.03.13

    Python异步编程与Asyncio高并发应用实践
    Python异步编程与Asyncio高并发应用实践

    本专题围绕 Python 异步编程模型展开,深入讲解 Asyncio 框架的核心原理与应用实践。内容包括事件循环机制、协程任务调度、异步 IO 处理以及并发任务管理策略。通过构建高并发网络请求与异步数据处理案例,帮助开发者掌握 Python 在高并发场景中的高效开发方法,并提升系统资源利用率与整体运行性能。

    46

    2026.03.12

    C# ASP.NET Core微服务架构与API网关实践
    C# ASP.NET Core微服务架构与API网关实践

    本专题围绕 C# 在现代后端架构中的微服务实践展开,系统讲解基于 ASP.NET Core 构建可扩展服务体系的核心方法。内容涵盖服务拆分策略、RESTful API 设计、服务间通信、API 网关统一入口管理以及服务治理机制。通过真实项目案例,帮助开发者掌握构建高可用微服务系统的关键技术,提高系统的可扩展性与维护效率。

    178

    2026.03.11

    Go高并发任务调度与Goroutine池化实践
    Go高并发任务调度与Goroutine池化实践

    本专题围绕 Go 语言在高并发任务处理场景中的实践展开,系统讲解 Goroutine 调度模型、Channel 通信机制以及并发控制策略。内容包括任务队列设计、Goroutine 池化管理、资源限制控制以及并发任务的性能优化方法。通过实际案例演示,帮助开发者构建稳定高效的 Go 并发任务处理系统,提高系统在高负载环境下的处理能力与稳定性。

    51

    2026.03.10

    Kotlin Android模块化架构与组件化开发实践
    Kotlin Android模块化架构与组件化开发实践

    本专题围绕 Kotlin 在 Android 应用开发中的架构实践展开,重点讲解模块化设计与组件化开发的实现思路。内容包括项目模块拆分策略、公共组件封装、依赖管理优化、路由通信机制以及大型项目的工程化管理方法。通过真实项目案例分析,帮助开发者构建结构清晰、易扩展且维护成本低的 Android 应用架构体系,提升团队协作效率与项目迭代速度。

    92

    2026.03.09

    JavaScript浏览器渲染机制与前端性能优化实践
    JavaScript浏览器渲染机制与前端性能优化实践

    本专题围绕 JavaScript 在浏览器中的执行与渲染机制展开,系统讲解 DOM 构建、CSSOM 解析、重排与重绘原理,以及关键渲染路径优化方法。内容涵盖事件循环机制、异步任务调度、资源加载优化、代码拆分与懒加载等性能优化策略。通过真实前端项目案例,帮助开发者理解浏览器底层工作原理,并掌握提升网页加载速度与交互体验的实用技巧。

    102

    2026.03.06

    Rust内存安全机制与所有权模型深度实践
    Rust内存安全机制与所有权模型深度实践

    本专题围绕 Rust 语言核心特性展开,深入讲解所有权机制、借用规则、生命周期管理以及智能指针等关键概念。通过系统级开发案例,分析内存安全保障原理与零成本抽象优势,并结合并发场景讲解 Send 与 Sync 特性实现机制。帮助开发者真正理解 Rust 的设计哲学,掌握在高性能与安全性并重场景中的工程实践能力。

    227

    2026.03.05

    PHP高性能API设计与Laravel服务架构实践
    PHP高性能API设计与Laravel服务架构实践

    本专题围绕 PHP 在现代 Web 后端开发中的高性能实践展开,重点讲解基于 Laravel 框架构建可扩展 API 服务的核心方法。内容涵盖路由与中间件机制、服务容器与依赖注入、接口版本管理、缓存策略设计以及队列异步处理方案。同时结合高并发场景,深入分析性能瓶颈定位与优化思路,帮助开发者构建稳定、高效、易维护的 PHP 后端服务体系。

    532

    2026.03.04

    AI安装教程大全
    AI安装教程大全

    2026最全AI工具安装教程专题:包含各版本AI绘图、AI视频、智能办公软件的本地化部署手册。全篇零基础友好,附带最新模型下载地址、一键安装脚本及常见报错修复方案。每日更新,收藏这一篇就够了,让AI安装不再报错!

    171

    2026.03.04

    热门下载

    更多
    网站特效
    /
    网站源码
    /
    网站素材
    /
    前端模板

    精品课程

    更多
    相关推荐
    /
    热门推荐
    /
    最新课程
    Kotlin 教程
    Kotlin 教程

    共23课时 | 4.4万人学习

    C# 教程
    C# 教程

    共94课时 | 11.3万人学习

    Java 教程
    Java 教程

    共578课时 | 82.1万人学习

    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送

    Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号