• Java课程设计:基于swing的贪吃蛇小游戏


    一、项目介绍

    贪吃蛇是一款经典的休闲益智游戏,自问世以来便深受广大用户的喜爱。这个游戏的基本玩法是控制一条不断增长的蛇,目标是吃掉屏幕上出现的食物,同时避免撞到边缘或自身。随着游戏的进行,蛇的身体会越长越大,操控难度也越来越高,为玩家带来了挑战性和乐趣。

    随着计算机和移动设备的普及,贪吃蛇游戏也逐渐从最初的黑白方块发展成为精美的图形游戏。但是无论视觉效果如何,游戏的核心玩法始终保持不变,这也是贪吃蛇游戏能持续吸引玩家的重要原因。

    二、核心代码

    启动窗口

    public class StartGame {
        public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
            //音乐.
            /*Thread t1 = new PlayMusic();
            t1.start();*/
    
            JFrame jf = new JFrame();
            jf.setTitle("贪吃蛇大作战");
            jf.setBounds(10,10,600,485);
            jf.setResizable(false);
            jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            jf.setVisible(true);
            //正常游戏界面放在面板上
            jf.add(new GamePanel());
    
        }
    }
    

    存放数据

    public class DataCenter {
        public static URL headerURL=DataCenter.class.getResource("/Static/Header.png");
        public static URL upURL=DataCenter.class.getResource("/Static/Up.png");
        public static URL rightURL=DataCenter.class.getResource("/Static/Right.png");
        public static URL downURL=DataCenter.class.getResource("/Static/Down.png");
        public static URL leftURL=DataCenter.class.getResource("/Static/Left.png");
        public static URL bodyURL=DataCenter.class.getResource("/Static/Body.png");
        public static URL foodURL=DataCenter.class.getResource("/Static/Food.png");
    
        public static ImageIcon header=new ImageIcon(headerURL);
        public static ImageIcon up=new ImageIcon(upURL);
        public static ImageIcon right=new ImageIcon(rightURL);
        public static ImageIcon down=new ImageIcon(downURL);
        public static ImageIcon left=new ImageIcon(leftURL);
        public static ImageIcon body=new ImageIcon(bodyURL);
        public static ImageIcon food=new ImageIcon(foodURL);
        
    }
    

    游戏初始化

     //游戏初始化
        public void init(){
            length = 3;//蛇的长度,初始为3
            String direction;//初始方向向右
            //脑袋的坐标
            snakeX[0] = 95;
            snakeY[0] = 110;
            //第一节身体
            snakeX[1] = 70;
            snakeY[1] = 110;
            //第二节身体
            snakeX[2] = 45;
            snakeY[2] = 110;
            direct = "R";//初始方向向右
            score=0;
            gameState = false;//默认还没开始游戏
            //游戏一开始定时器就启动
            timer.start();
            foodX = 20 + 25 * random.nextInt(22);//生成[0-21]的整数
            foodY = 85 + 25 * random.nextInt(14);//生成[0-13]的整数
            isFail = false;
        }
    

    绘制面板

     //绘制面板
        @Override
        protected void paintComponent(Graphics g) {
            this.setBackground(Color.WHITE);
            super.paintComponent(g);//清屏
            //绘制静态面板
            //头部图片
            DataCenter.header.paintIcon(this, g, 20, 8);
            //游戏面板
            g.fillRect(20, 85, 548, 355);
    
            //画积分
            g.setColor(Color.WHITE);
            g.setFont(new Font("微软雅黑",Font.BOLD,20));
            g.drawString("长度:"+length,450,32);
            g.drawString("分数:"+score,450,55);
    
    
            //画食物
            DataCenter.food.paintIcon(this, g, foodX, foodY);
    
            //画小蛇头
            if (direct.equals("U")) {
                DataCenter.up.paintIcon(this, g, snakeX[0], snakeY[0]);
            } else if (direct.equals("R")) {
                DataCenter.right.paintIcon(this, g, snakeX[0], snakeY[0]);
            } else if (direct.equals("D")) {
                DataCenter.down.paintIcon(this, g, snakeX[0], snakeY[0]);
            } else if (direct.equals("L")) {
                DataCenter.left.paintIcon(this, g, snakeX[0], snakeY[0]);
            }
            //画蛇身
            for (int i = 1; i < length; i++) {
                DataCenter.body.paintIcon(this, g, snakeX[i], snakeY[i]);
            }
    
            //游戏状态
            if (gameState == false) {
                g.setColor(new Color(231, 85, 18));
                g.setFont(new Font("微软雅黑", Font.BOLD, 40));
                g.drawString("按下空格开始游戏!", 126, 265);
            }
            if (isFail) {
                g.setColor(new Color(226, 9, 9));
                g.setFont(new Font("微软雅黑", Font.BOLD, 40));
                g.drawString("游戏失败!按下空格重新开始", 40, 265);
            }
    
        }
    

    三、项目展示

    初始面板
    在这里插入图片描述
    开始游戏
    在这里插入图片描述
    游戏失败
    在这里插入图片描述

    四、源码获取

    因为页面与源码太多了,所以页面与源码只展示了一部分,完整源码已经打包了,点击下面蓝色链接获取!

    点我获取源码

  • 相关阅读:
    以sqlilabs靶场为例,讲解SQL注入攻击原理【42-50关】
    HJ20 密码验证合格程序
    开启AI高效办公时代,成为AI时代的先行者
    【threejs教程9】threejs加载360全景图(VR)的两种方法
    使用Mondo Rescue对CentOS7进行封装
    GRU 关键词提取实例
    EventListener
    为什么建议将常量用const关键字来修饰
    论文翻译:Large Language Models for Education: A Survey
    关于ribbon的重试机器,望解答
  • 原文地址:https://blog.csdn.net/2401_84040513/article/details/139690179