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

    }

三、项目展示

初始面板

开始游戏

游戏失败

四、源码获取

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

点我获取源码

相关推荐
科大饭桶18 分钟前
C++入门自学Day11-- String, Vector, List 复习
c语言·开发语言·数据结构·c++·容器
范范之交22 分钟前
JavaScript基础语法two
开发语言·前端·javascript
Felven23 分钟前
C. Game of Mathletes
c语言·开发语言
long31640 分钟前
构建者设计模式 Builder
java·后端·学习·设计模式
吐个泡泡v44 分钟前
Maven 核心命令详解:compile、exec:java、package 与 IDE Reload 机制深度解析
java·ide·maven·mvn compile
点云SLAM1 小时前
C++中内存池(Memory Pool)详解和完整示例
开发语言·c++·内存管理·内存池·new/delete·malloc/free
天上掉下来个程小白1 小时前
微服务-01.导入黑马商城
java·微服务·架构
R-G-B1 小时前
OpenCV Python——Numpy基本操作(Numpy 矩阵操作、Numpy 矩阵的检索与赋值、Numpy 操作ROI)
python·opencv·numpy·numpy基本操作·numpy 矩阵操作·numpy 矩阵的检索与赋值·numpy 操作roi
Noii.1 小时前
Spring Boot初级概念及自动配置原理
java·spring boot·后端
细节处有神明1 小时前
Jupyter 中实现交互式图表:ipywidgets 从入门到部署
ide·python·jupyter