Cocos Creator 案例源码分享二(黑白迭代)

最近闲来无聊,想学习Cocos Creator游戏编程,并把自己在学习过程中的一些案例分享出来,后面会陆续推出一些小游戏的,由于本人第一次接触Cocos Creator 和JavaScript,代码多有疏漏欢迎留言指正。

游戏画面展示:

游戏源码共享:

https://download.csdn.net/download/hucailai/20166220

游戏引擎版本:2.3.3

// 学习交流:QQ群:624554876
cc.Class({
    extends: cc.Component,

    properties: {
        RootNode: {
            type: cc.Node,
            default:null,
        },

        smallNode: {
            type: cc.Node,
            default:null,
        },

        Success: {
            type: cc.Label,
            default:null,
        },
    },



    onLoad () {
        this.cellWidth = 50;
        this.cellHeight = this.cellWidth;
        this.lineWidth = 2;
        this.cellCnt = 9;  //9*9格子
        this.frameWidth = 20;
        this.baseX = -(this.cellCnt * this.cellWidth +  (this.cellCnt + 1) * this.lineWidth )/2;
        this.baseY = -(this.cellCnt * this.cellHeight +  (this.cellCnt + 1) * this.lineWidth )/2;

        this.sCellWidth = 25;
        this.sCellHeight = this.sCellWidth;
        this.sLineWidth = 1;
        this.sCellCnt = 9;  //9*9格子
        this.sFrameWidth = 10;
        this.sBaseX = -(this.sCellCnt * this.sCellWidth +  (this.sCellCnt + 1) * this.sLineWidth )/2;
        this.sBaseY = -(this.sCellCnt * this.sCellHeight +  (this.sCellCnt + 1) * this.sLineWidth )/2;

        this.node.on(cc.Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
        this.array= new Array()
        this.desArray = new Array()
        this.heap = new Array()

        this.level = 1  // 游戏难度
    },

    initArray(){
        for (i = 0; i < this.cellHeight; i++){
            this.array[i] = new Array()
            for (j = 0; j < this.cellWidth; j++){
                this.array[i][j] = false
            }
        }
    },

    initDestArray(){
        for (i = 0; i < this.cellHeight; i++){
            this.desArray[i] = new Array()
            for (j = 0; j < this.cellWidth; j++){
                this.desArray[i][j] = false
            }
        }

        for (i = 0; i < 2+this.level*4; i++){
            x = this.random(0, this.cellCnt)
            y = this.random(0, this.cellCnt)
            cc.log("i=" + x + " j=" + y)
            this.onClickArray(this.desArray, true, x, y)
        }

        cc.log("desArray:" + this.desArray)
    },

    onDestroy: function () {
        this.node.off(cc.Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
    },

    onMouseDown: function (event) {
        let mouseType = event.getButton();
        if (mouseType === cc.Event.EventMouse.BUTTON_LEFT) {
            // 鼠标左键按下
            let mousePoint = event.getLocation();
            let localPoint = this.RootNode.convertToNodeSpace(mousePoint);

            let j = Math.floor((localPoint.x - this.baseX - this.lineWidth) / (this.cellWidth + this.lineWidth))
            let i = Math.floor((localPoint.y - this.baseY - this.lineWidth) / (this.cellHeight + this.lineWidth))

            if (!(i < 0 || j < 0 || i >= this.cellCnt || j >= this.cellCnt)){
                this.onClick(i, j)
            }
        } 
    },

    onClickArray(array, small, i, j){
        array[i][j] = !array[i][j]
        this.fillCell(i, j, small, array[i][j])

        if ((j+1) < this.cellCnt){
            array[i][j+1] = !array[i][j+1]
            this.fillCell(i, j+1, small, array[i][j+1])
        }

        if ((j-1) >= 0){
            array[i][j-1] = !array[i][j-1]
            this.fillCell(i, j-1, small, array[i][j-1])
        }

        if ((i+1) < this.cellCnt){
            array[i+1][j] = !array[i+1][j]
            this.fillCell(i+1, j, small, array[i+1][j])
        }

        if ((i-1) >= 0){
            array[i-1][j] = !array[i-1][j]
            this.fillCell(i-1, j, small, array[i-1][j])
        }
    },

    onClick(i, j){ // 单击了i行,j列
        this.Success.node.active = false
        this.onClickArray(this.array, false, i, j)

        // 放到堆栈中以便回退
        pos=[i,j]
        this.heap.push(pos)

        // 检测是否完成,并重新开始
        if(this.checkFinished()){
            this.Success.node.active = true
            this.restart()
        }
    },

    onBack(){
        // 回退算法就是重复点击上次的位置
        if (this.heap.length > 0){
            var pos = this.heap.pop(pos)
            this.onClickArray(this.array, false, pos[0], pos[1])
        }
    },  
    
    onLevel(sender, level){
        cc.log("Level:" + level)
        if (level == this.level){
            return
        }
        this.level = level

        this.restart()
    },

    drawBackCells(graphics, cellWidth,cellHeight, cellCnt, 
                    lineWidth, frameWidth, baseX, baseY){
        graphics.fillColor = cc.color(255, 255, 255, 255);

        nodeWidth = cellCnt * cellWidth + 
                    (cellCnt + 1) * lineWidth + 
                    2 * frameWidth;
        graphics.fillRect(-nodeWidth/2,-nodeWidth/2,nodeWidth,nodeWidth)

        // 绘制9*9的格子
        graphics.fillColor = cc.color(0, 0, 0, 255);
        for (i =0; i <= cellCnt; i++){
            graphics.fillRect(baseX, baseY + (cellHeight+lineWidth) * i, 
                              cellCnt * cellWidth +  (cellCnt + 1) * lineWidth, 
                              lineWidth)
            graphics.fillRect(baseX + (cellWidth+lineWidth) * i, baseY , 
                              lineWidth,
                              cellCnt * cellHeight +  (cellCnt + 1) * lineWidth)                        
        }
     },

    fillCell(i, j, bSmall, bBlack) {
        graphics = this.graphics
        cellWidth = this.cellWidth
        cellHeight = this.cellHeight
        x = this.baseX + this.lineWidth + j * (this.cellWidth + this.lineWidth)
        y = this.baseY + this.lineWidth + i * (this.cellHeight + this.lineWidth)
        if (bSmall) {
            graphics = this.smallgraphics
            cellWidth = this.sCellWidth
            cellHeight = this.sCellHeight
            x = this.sBaseX + this.sLineWidth + j * (this.sCellWidth + this.sLineWidth)
            y = this.sBaseY + this.sLineWidth + i * (this.sCellHeight + this.sLineWidth)
        }

        graphics.fillColor = cc.color(255, 255, 255, 255);
        if (bBlack){
            graphics.fillColor = cc.color(0, 0, 0, 255);
        }
        
        graphics.fillRect(x, y, cellWidth, cellHeight) 
    },

    checkFinished(){
        for (i = 0; i < this.cellHeight; i++){
            for (j = 0; j < this.cellWidth; j++){
                if (this.array[i][j] != this.desArray[i][j])
                    return false
            }
        }
        cc.log("Finished")
        return true
    },

    restart(){
        this.drawBackCells(this.graphics, this.cellWidth, this.cellHeight, this.cellCnt, 
            this.lineWidth, this.frameWidth, this.baseX, this.baseY);
        this.drawBackCells(this.smallgraphics,  this.sCellWidth, this.sCellHeight, this.sCellCnt, 
                this.sLineWidth, this.sFrameWidth, this.sBaseX, this.sBaseY);
        this.initArray()
        this.initDestArray()
        this.heap.length = 0
    },

    start () {
        this.Success.node.active = false
        this.graphics = this.RootNode.addComponent(cc.Graphics);
        this.drawBackCells(this.graphics, this.cellWidth, this.cellHeight, this.cellCnt, 
                      this.lineWidth, this.frameWidth, this.baseX, this.baseY);
        
        this.smallgraphics = this.smallNode.addComponent(cc.Graphics);
        this.drawBackCells(this.smallgraphics,  this.sCellWidth, this.sCellHeight, this.sCellCnt, 
                        this.sLineWidth, this.sFrameWidth, this.sBaseX, this.sBaseY);

        this.initArray()
        this.initDestArray()
    },

    random : function (lower, upper) {
        return Math.floor(Math.random() * (upper - lower)) + lower; 
    },
});

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
### 回答1: Cocos Creator是一个流行的游戏开发引擎,提供了各种功能和工具,可以帮助开发者快速创建和设计小游戏。当我们提到Cocos Creator游戏源代码时,通常是指游戏的源代码文件和资源文件。 Cocos Creator的源代码文件由JavaScript编写,通常包括游戏的逻辑、场景组织和界面展示等方面的代码。开发者可以通过编辑源代码来修改游戏的行为,添加新的功能或调整游戏的操作方式。通过理解源代码,开发者可以深入了解游戏的工作原理和内部机制。 此外,Cocos Creator的源代码还包括资源文件,如图像、声音和动画等。这些资源文件是游戏中使用的各种元素,可以通过编辑软件进行设计和修改。通过修改资源文件,开发者可以改变游戏的外观和音效,增加游戏的吸引力和乐趣。 如果我们拥有Cocos Creator游戏的源代码,我们可以根据自己的需求进行定制和修改,使游戏更符合自己的创意和目标。我们可以通过学习源代码来了解游戏的设计理念和技巧,从而提高自己的游戏开发能力。 总而言之,Cocos Creator游戏源代码是游戏开发过程中的重要组成部分,通过对源代码的理解和修改,我们可以定制和优化游戏,使其更具个性和品质。 ### 回答2: Cocos Creator 是一款用于开发2D和3D游戏游戏引擎。它是基于 Cocos2d-x 引擎的基础上进行开发的,同时支持 JavaScript 和 TypeScript 编程语言。使用 Cocos Creator 可以方便地创建各种类型的小游戏Cocos Creator 的小游戏源代码是指使用 Cocos Creator 编写的游戏的源代码。这些源代码包含了游戏的各种各样的功能和逻辑,例如游戏场景的创建、角色控制和碰撞检测等。 在 Cocos Creator 的小游戏源代码中,你可以看到许多不同的文件。其中最重要的是场景文件,它包含了游戏中的场景布局和对象的放置。另外还有脚本文件,用于编写游戏的逻辑和功能。以及资源文件,包括游戏中使用的图片、音效和动画等。 小游戏源代码中的脚本文件是游戏的核心部分,通过编写脚本可以实现游戏中的各种功能。你可以在脚本中定义角色的移动方式、敌人的行为模式、背景音乐的播放等。同时,你还可以使用脚本文件进行碰撞检测、游戏得分的计算和关卡的切换等。 总而言之,Cocos Creator 的小游戏源代码提供了游戏的各种功能和逻辑的实现方式。通过学习和理解这些源代码,你可以自由地修改和扩展游戏的功能,以满足自己的需求。同时,你也可以参考源代码中的编程方式和设计思路,来进行自己的游戏开发。 ### 回答3: Cocos Creator是一款用于开发小型游戏游戏引擎和开发工具。它提供了适用于多平台的跨平台开发功能,以及强大的编辑器和场景编辑工具。 Cocos Creator使用JavaScript脚本语言编写游戏逻辑,并使用C++编写引擎核心功能。游戏内容可以在编辑器中创建和编辑,然后通过编译输出为可在多个平台上运行的游戏代码。 Cocos Creator游戏源代码包括引擎核心的C++部分以及游戏逻辑的JavaScript脚本代码。引擎核心代码的作用是提供游戏开发所需的基础功能,包括图形渲染、物理碰撞检测、动画控制和资源管理等。 在游戏源代码中,开发者可以编写游戏逻辑的JavaScript脚本,包括角色移动、碰撞检测、游戏进度控制等。开发者可以通过编辑器添加场景、角色和其他游戏资源,并使用JavaScript脚本将它们连接起来,实现游戏的逻辑和交互。 通过编写自定义的脚本代码,开发者可以为游戏增加各种功能和特效,比如实现角色的特殊能力、设计游戏关卡的难度、添加音效和动画效果等。 总的来说,Cocos Creator游戏源代码包含了游戏引擎的核心和游戏逻辑的脚本代码,开发者可以利用这些代码来创建和定制自己的小型游戏

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hucailai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值