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

文章目录

一、项目介绍

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

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

二、核心代码

启动窗口

java 复制代码
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());

    }
}

存放数据

java 复制代码
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);
    
}

游戏初始化

java 复制代码
 //游戏初始化
    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;
    }

绘制面板

java 复制代码
 //绘制面板
    @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);
        }

    }

三、项目展示

初始面板

开始游戏

游戏失败

四、源码获取

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

点我获取源码

相关推荐
mouseliu3 分钟前
python之二:docker部署项目
前端·python
阿蒙Amon4 分钟前
06. C#入门系列【自定义类型】:从青铜到王者的进阶之路
开发语言·c#
虾球xz8 分钟前
CppCon 2015 学习:CLANG/C2 for Windows
开发语言·c++·windows·学习
狂小虎21 分钟前
亲测解决self.transform is not exist
python·深度学习
Python智慧行囊22 分钟前
Python 中 Django 中间件:原理、方法与实战应用
python·中间件·架构·django·开发
HelloWord~30 分钟前
SpringSecurity+vue通用权限系统2
java·vue.js
让我上个超影吧30 分钟前
黑马点评【基于redis实现共享session登录】
java·redis
深科文库31 分钟前
构建 MCP 服务器:第 3 部分 — 添加提示
服务器·python·chatgpt·langchain·prompt·aigc·agi
CodeWithMe1 小时前
【C/C++】namespace + macro混用场景
c语言·开发语言·c++