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);
        }

    }

三、项目展示

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

四、源码获取

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

点我获取源码

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值