cocos2d-js 游戏源码

四款不同类型的游戏
这里写图片描述

完整的项目
https://github.com/QQ951127336/Game_byJavaScript

10秒内吃萝卜

/**
 * Created by 95112 on 2018/4/1.
 */
var rabbit = null;
var size = null;
var xStep = null;
var yStep = null;
var amountRadish = 5;
var cave = null;
var winBoard = null;
var EatRadishLayer = cc.Layer.extend({
    radishGroup:null,
    winBoard:null,
    key:0,
    lose:null,
    timeout:10,
    timeoutLabel:null,
    ctor:function(){
        this._super();
        this.radishGroup = [];
        amountRadish = 5;
        var Board = new cc.MenuItemFont("恭喜过关,进入下一关",this.nextGame,this);
        Board.fontSize = 33;
        winBoard =new cc.Menu(Board);
        size = cc.director.getWinSize();


        var bgSprite = new cc.Sprite(res.grassBackground);

        xStep = size.width/15;
        yStep = size.height/15;
        bgSprite.attr({
            x:size.width/2,
            y:size.height/2,
            scale:1.7
        });
        this.addChild(bgSprite);
        cc.audioEngine.playMusic("res/sounds/bgm1.wav",true);
        rabbit = new cc.Sprite(res.rabbitWalk)
        rabbit.attr({
            x:50,
            y:50,
            scale:0.5
        });
        for(var i =0; i< amountRadish;i++){
            this.addRandomRadish();
        }
        this.addCave();
        var backHome = new BackSprite(res.backHome);
        backHome.attr({
           x:size.width-backHome.width/5,
            y:size.height-backHome.height/5,
            scale:0.4
        });
        this.addChild(backHome);
        var falseBoard = new cc.MenuItemFont("重新开始",this.restartGame,this);
        falseBoard.fontSize = 33;
        this.lose = new cc.Menu(falseBoard);

        this.timeout = 10;
        this.timeoutLabel = cc.LabelTTF.create("剩余时间 : "+this.timeout,"Arial",30);
        this.timeoutLabel.attr({
            x:size.width/2,
            y:size.height-50,
        });
        this.addChild(this.timeoutLabel);

        this.schedule(this.timer, 1,this.timeout,1);


        this.scheduleUpdate();
        if('keyboard' in cc.sys.capabilities){
            cc.eventManager.addListener({
                event:cc.EventListener.KEYBOARD,
                onKeyReleased:function(keyCode,event){
                    if (keyCode == cc.KEY.right && (rabbit.x + xStep)<size.width){
                        rabbit.scaleX=0.5;
                        rabbit.x = rabbit.x + xStep;
                    }else if(keyCode == cc.KEY.left && (rabbit.x - xStep)>0){
                        rabbit.scaleX=-0.5;
                        rabbit.x = rabbit.x - xStep;
                    }else if(keyCode == cc.KEY.up && (rabbit.y + yStep) <size.height){
                        rabbit.y = rabbit.y + yStep;
                    }else if(keyCode == cc.KEY.down && (rabbit.y - yStep) > 0){
                        rabbit.y = rabbit.y - yStep;
                    }
                }
            },this);
        }
        return true;
    },
    timer:function(){
        if(this.timeout>0) {
            this.timeout -= 1;
            this.timeoutLabel.setString("剩余时间 : " + this.timeout);
        }else if(this.key==0){
            rabbit.removeFromParent();
            rabbit = new cc.Sprite();
            this.addChild(this.lose);
            this.unscheduleUpdate(this.update());
        }
    }
    ,
    restartGame:function(){
        cc.director.runScene(new EatRadishScene())
    }
    ,
    nextGame:function(){
        cc.director.runScene(new EatRadishScene1());
    }
    ,
    update:function(){
        this.removeRabbit();
        this.drawRabbit();
    }
    ,
    drawRabbit:function(){
        this.addChild(rabbit)
    },
    removeRabbit:function(){
        rabbit.removeFromParent();
        for(var i = 0; i<this.radishGroup.length; i++){
            if (Math.abs(rabbit.x - this.radishGroup[i].x)<xStep && Math.abs(rabbit.y - this.radishGroup[i].y)<yStep){
                this.radishGroup[i].removeFromParent();
                this.radishGroup[i] = undefined;
                this.radishGroup.splice(i,1);
                i = i-1;
                amountRadish -= 1;

                cc.audioEngine.playEffect("res/sounds/eatRadish.wav",false);
            }
        }
        if(amountRadish==0 && Math.abs(rabbit.x - cave.x )<xStep&&Math.abs(rabbit.y-cave.y)<yStep && this.key == 0){
            this.addChild(winBoard);
            rabbit = new cc.Sprite();
            this.key +=1;
            cc.audioEngine.stopMusic();
            cc.audioEngine.playEffect("res/sounds/win.wav",false);
        }
    },
    addRandomRadish:function(){
        var radish = new cc.Sprite(res.radish);
        radish.attr({
            x:radish.width/2 + size.width/1.5*cc.random0To1(),
            y:radish.height/2 + size.height/1.5*cc.random0To1(),
            scale:0.3
        });
        this.addChild(radish);
        this.radishGroup.push(radish);
    },
    addCave:function(){
        cave = new cc.Sprite(res.cave);
        cave.attr({
            x:cave.width/2 + size.width/1.5*cc.random0To1(),
            y:cave.height/2 + size.height/1.5*cc.random0To1(),
            scale:0.6
        });
        this.addChild(cave);
    }
}) ;
var EatRadishScene = cc.Scene.extend({
    onEnter:function(){
        this._super();
        var layer = new EatRadishLayer();
        this.addChild(layer);
    }
})

拼图游戏

/**
 * Created by 95112 on 2018/4/4.
 */
var puzzleGroup;
var click;
var size;
var which;
var number;
var over;
var winBoard = null;
var distance ;
var PuzzleLayer = cc.Layer.extend({
    ctor:function(){
        this._super();
        size = cc.winSize;
        over = false;
        puzzleGroup = new Array();
        distance = 100;
        which = null;
        click = false;
        number = 5;
        var Board = new cc.MenuItemFont("恭喜过关,进入下一关",this.nextGame,this);
        Board.fontSize = 33;
        winBoard =new cc.Menu(Board);

        var bgSprite = new cc.Sprite(res.puzzleBackground);
        bgSprite.attr({
            x:size.width/2,
            y:size.height/2,
        });
        this.addChild(bgSprite);
        cc.audioEngine.playMusic("res/sounds/bgm1.wav",true);
        puzzleGroup[0] = new cc.Sprite(res.fAnswer);
        puzzleGroup[0].attr({
            x:size.width/2,
            y:size.height/2,
            scale:0.5
        });
        for(var i = 1 ; i <= 5;i++){
            puzzleGroup[i] = new cc.Sprite("res/first/f"+i+".png");
            puzzleGroup[i].attr({
                x:i*size.width/6,
                y:70,
                scale:0.5,
            });
            this.addChild(puzzleGroup[i]);
        }
        puzzleGroup[1].scale=0.6
        puzzleGroup[2].scale=0.6;
        puzzleGroup[4].scale=0.45;
        puzzleGroup[5].scale=0.45;
        var backHome = new BackSprite(res.backHome);
        backHome.attr({
            x:size.width-backHome.width/5,
            y:size.height-backHome.height/5,
            scale:0.4
        });
        this.addChild(backHome);
        this.scheduleUpdate();
        if('mouse' in cc.sys.capabilities){
            cc.eventManager.addListener({
                event:cc.EventListener.MOUSE,
                onMouseDown:function(event){
                    click = true;
                    var pos = event.getLocation();
                    var minX = 1000;
                    var minY = 1000;
                    var distanceX ,distanceY;
                    for ( var i = 1 ; i<= number;i++){
                        distanceX = Math.abs(puzzleGroup[i].x - pos.x);
                        distanceY =  Math.abs(puzzleGroup[i].y - pos.y);
                        if(distanceX <puzzleGroup[i].width/4 &&distanceY <puzzleGroup[i].height/4 ){
                            if((Math.pow(distanceX,2) + Math.pow(distanceY,2)) < (Math.pow(minX,2)+Math.pow(minY,2))){
                                which = i;
                                minX = distanceX;
                                minY = distanceY;
                            }

                        }
                    }
                },
                onMouseMove:function(event){
                    var pos = event.getLocation();
                    if (click && which!=null){
                        puzzleGroup[which].attr({
                            x:pos.x,
                            y:pos.y,
                        })
                    }
                },
                onMouseUp:function(event){
                    click =false;
                    which = null;
                    cc.audioEngine.playEffect("res/sounds/puzzleMusic.wav",false);
                }
            },this);
        }
    },
    nextGame:function(){
        cc.director.runScene(new PuzzleScene2());
    },
    update:function(){
        this.removeItems();
        this.addItems();
    }
    ,
    addItems:function(){
      for  (var i =1 ; i <=number;i++){
          this.addChild(puzzleGroup[i]);
      }
    },
    removeItems:function(){
        for (var i =1; i<= number;i++){
            puzzleGroup[i].removeFromParent();
        }
        if((puzzleGroup[4].y > puzzleGroup[5].y) && puzzleGroup[4].x > puzzleGroup[5].x && (puzzleGroup[4].y - puzzleGroup[5].y)<distance && (puzzleGroup[4].x - puzzleGroup[5].x)< distance ){
            if(puzzleGroup[3].y < puzzleGroup[5].y && puzzleGroup[3].x > puzzleGroup[1].x  && (puzzleGroup[3].y - puzzleGroup[5].y)>( -distance) && (puzzleGroup[3].x - puzzleGroup[1].x )<distance){
                if(puzzleGroup[1].x<puzzleGroup[5].x && puzzleGroup[1].y < puzzleGroup[5].y && (puzzleGroup[5].x - puzzleGroup[1].x)<distance && (puzzleGroup[1].y - puzzleGroup[5].y)>(-distance)){
                    if (puzzleGroup[2].x<puzzleGroup[5].x && puzzleGroup[2].y > puzzleGroup[1].y && (puzzleGroup[2].x - puzzleGroup[5].x)>(-distance)){

                        if (!over){
                            console.log("win!!!!!!!!!!!!");
                            for(var i =1 ; i<=number;i++){
                                puzzleGroup[i] = new cc.Sprite();

                            }
                            cc.audioEngine.stopMusic();
                            cc.audioEngine.playEffect("res/sounds/win.wav",false);
                            this.addChild(puzzleGroup[0]);
                            this.addChild(winBoard);
                            over = true;
                        }
                    }
                }
            }
        }
    }
});

var PuzzleScene = cc.Scene.extend({
    onEnter:function(){
        this._super();
        var layer = new PuzzleLayer();
        this.addChild(layer);
    }
});

连水管游戏

/**
 *
 * Created by 95112 on 2018/4/4.
 */
var size;
var tXStep;
var tYStep;
var pipeGroup;
var number;
var winBoard = null;
var over;
var tubeLayer = cc.Layer.extend({
    ctor:function(){
      this._super();
      pipeGroup = [];
      over =false;
      size = cc.director.getWinSize();
      var Board = new cc.MenuItemFont("恭喜过关,进入下一关",this.nextGame,this);
      Board.fontSize = 33;
      winBoard =new cc.Menu(Board);
      winBoard.y = size.height/2+200;
      var bgSprite = new cc.Sprite(res.waterBackground);
      bgSprite.attr({
          x:size.width/2,
          y:size.height/2
      });
      this.addChild(bgSprite);
      cc.audioEngine.playMusic("res/sounds/bgm2.wav",true);
      tXStep = size.width/6;
      tYStep = size.height/6;

        var backHome = new BackSprite(res.backHome);
        backHome.attr({
            x:size.width-backHome.width/5,
            y:size.height-backHome.height/5,
            scale:0.4
        });
        this.addChild(backHome);
      this.addStartAndEnd();
      this.addMustPipe();
      this.scheduleUpdate();
      },
    addStartAndEnd:function(){
        var pipeStart = new cc.Sprite(res.pipeNode);
        pipeStart.attr({
            x:pipeStart.width/2,
            y:size.height/2,
            rotation:90,
            scaleX:0.7

        });
        this.addChild(pipeStart);
        var pipeEnd = new cc.Sprite(res.pipeNode);
        pipeEnd.attr({
            x:size.width-pipeStart.width/2,
            y:size.height/2,
            rotation:90,
            scaleX:0.7

        });
        this.addChild(pipeEnd);
    },
    nextGame:function(){
        cc.director.runScene(new tubeScene2());
    },
    update:function(){
        this.removePipe();
        this.drawPipe();
    }
    ,
    drawPipe:function(){
      for(var i =0 ; i< pipeGroup.length;i++){
          this.addChild(pipeGroup[i]);
      }
    },
    removePipe:function(){
        for(var i =0 ; i< pipeGroup.length;i++){
            pipeGroup[i].removeFromParent();
        }
        if (pipeGroup[0].way%2==1 &&pipeGroup[1].way%2==1 &&pipeGroup[2].way%2==1&&pipeGroup[3].way%2==1&&pipeGroup[4].way%2==1 &&pipeGroup[5].way%2==1 &&!over){
            this.addChild(winBoard);
            console.log("win!!!");
            over =true;
            cc.audioEngine.stopMusic();
            cc.audioEngine.playEffect("res/sounds/win.wav",false);
        }
    },

    addMustPipe:function(){
        pipeGroup[0] = new Pipe(res.pipeLine);
        pipeGroup[0].attr({
            x:170,
            y:size.height/2,
            scale:0.7
        });
        this.addChild(pipeGroup[0]);
        for(var i =1 ;i < 6;i++){
            pipeGroup[i] = new Pipe(res.pipeLine);
            pipeGroup[i].attr({
                x: pipeGroup[i-1].x+pipeGroup[i-1].width+30,
                y:size.height/2,
                scale:0.7
            });
            this.addChild(pipeGroup[i]);
        }

    }
});

var tubeScene = cc.Scene.extend({
    onEnter:function(){
        this._super();
        var layer = new tubeLayer();
        this.addChild(layer);
    }
});
var Pipe = cc.Sprite.extend({
    onEnter:function(){
      this._super();
      this.addTouchListener();
    },
    way:0,
    ctor:function(imageURL){
        this._super(imageURL);
        this.way = 0;
    },
    onTouch:function(){
        cc.audioEngine.playEffect("res/sounds/puzzleMusic.wav",false);
        this.way = (this.way +1)%4;
        this.rotation = (this.rotation + 90)%360;
    },
    addTouchListener:function(){
        this.touchListener = cc.EventListener.create({
            event:cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches:true,
            onTouchBegan:function(touch,event){
                var pos = touch.getLocation();
                var target = event.getCurrentTarget();
                if(cc.rectContainsPoint(target.getBoundingBox(),pos)){
                    target.onTouch();
                    console.log(target.x+" "+target.y);
                    return true;
                }
                return false;
            }
        });
        cc.eventManager.addListener(this.touchListener,this);
    }
})

跳一跳游戏

/**
 * Created by 95112 on 2018/4/2.
 */
var ballX = 0;
var ballY = 0;
var xDel = 0;
var yDel = 5;
var board = null;
var rabbitBall = null;
var boardSpeed = 0;


var JumpRabbitLayer = cc.Layer.extend({
    size:null,
    boardWidth:0,
    win:null,
    lose:null,
    radishGroup:null,
    amountRadish:10,
    key:0,
    ctor:function(){
        this._super();
        this.radishGroup = [];
        var winBoard = new cc.MenuItemFont("恭喜全部过关,返回主菜单",this.nextGame,this);
        winBoard.fontSize = 33;
        var falseBoard = new cc.MenuItemFont("重新开始",this.restartGame,this);
        falseBoard.fontSize = 33;
        this.win = new cc.Menu(winBoard);
        this.lose = new cc.Menu(falseBoard);
        this.amountRadish =5;
        xDel =0;
        this.size = cc.director.getWinSize();
        var bgSprite =new cc.Sprite(res.jumpBackground);
        bgSprite.attr({
            x:this.size.width/2,
            y:this.size.height/2,
            scale:1.5
        });
        this.addChild(bgSprite);
        rabbitBall = new cc.Sprite(res.rabbitJump);
        rabbitBall.attr({
            x:this.size.width/2,
            y:this.size.height/2,
            scale:0.4
        });
        board = new cc.Sprite(res.board);
        board.scale = 0.5;
        board.x = this.size.width/2;
        board.y = this.size.height/15;
        this.boardWidth = board.width;
        for(var i =0; i< this.amountRadish;i++){
            this.addRandomRadish();
        }
        var backHome = new BackSprite(res.backHome);
        backHome.attr({
            x:size.width-backHome.width/5,
            y:size.height-backHome.height/5,
            scale:0.4
        });
        this.addChild(backHome);
        this.scheduleUpdate();
        if('keyboard' in cc.sys.capabilities){
            cc.eventManager.addListener({
                event:cc.EventListener.KEYBOARD,
                onKeyPressed:function(keyCode,event){
                    if(keyCode == cc.KEY.left){
                        boardSpeed = -10;
                    }else if(keyCode == cc.KEY.right){
                        boardSpeed = 10;
                    }else{
                        boardSpeed = 0;
                    }
                },
                onKeyReleased:function(keyCode,event){
                    boardSpeed = 0;
                }
            },this)
        }
    },
    restartGame:function(){
        cc.director.runScene(new JumpRabbitScene());
    },
    nextGame:function(){
        cc.director.runScene(new MenuScene());
    }
    ,
    update:function(){
        this.removeBall();
        this.addBall();
        this.removeBoard();
        this.addBoard();
    },
    addBoard:function(){
        if (board.x + boardSpeed > 0 && board.x + boardSpeed < this.size.width)
            board.x = board.x + boardSpeed;
        this.addChild(board);
    },
    removeBoard:function(){
        board.removeFromParent();
    },
    addBall:function(){
        if (rabbitBall.x <= 0 || rabbitBall.x >= this.size.width)
            xDel = -xDel;
        if (rabbitBall.y >= this.size.height)
            yDel = -yDel;
        if (Math.abs(board.x - rabbitBall.x)<this.boardWidth/2 && rabbitBall.y -board.y <30 && rabbitBall.y > board.y){
            yDel = -yDel;
            xDel = xDel + boardSpeed/2;
        }
        rabbitBall.x = rabbitBall.x + xDel;
        rabbitBall.y = rabbitBall.y + yDel;
        this.addChild(rabbitBall);
    },
    removeBall:function(){
        rabbitBall.removeFromParent();
        for (var i =0 ; i<this.radishGroup.length;i++){
            if(Math.abs(rabbitBall.x - this.radishGroup[i].x)< 20 && Math.abs(rabbitBall.y - this.radishGroup[i].y)<20){
                this.radishGroup[i].removeFromParent();
                this.radishGroup[i] = undefined;
                this.radishGroup.splice(i,1);
                i = i-1;
                this.amountRadish -= 1
                yDel = -yDel;
            }
        }
        if (this.amountRadish == 0 && this.key==0){
            this.addChild(this.win);
            rabbitBall = new cc.Sprite();
            this.key += 1;
            cc.audioEngine.stopMusic();
            cc.audioEngine.playEffect("res/sounds/win.wav",false);
        }
        if(rabbitBall.y < 0){
            rabbitBall = new cc.Sprite();
            this.addChild(this.lose);
        }
    },
    addRandomRadish:function(){
        var radish = new cc.Sprite(res.radish);
        radish.attr({
            x:radish.width/2 + this.size.width/1.5*cc.random0To1(),
            y:radish.height/2 + this.size.height/1.5*cc.random0To1(),
            scale:0.3
        });
        this.addChild(radish);
        this.radishGroup.push(radish);
    },
});

var JumpRabbitScene = cc.Scene.extend({
    onEnter:function(){
        this._super();
        var layer = new JumpRabbitLayer();
        this.addChild(layer);
    }
})
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: cocos2d-x是一款流行的开源游戏引擎,它可以用作开发本地移动游戏和桌面游戏。利用cocos2d-x游戏源码,开发人员可以快速构建流畅、高效、具有吸引力的游戏。该引擎使用C ++语言和脚本语言Lua来实现游戏开发,用户可以根据自己的需要进行选择。 cocos2d-x游戏源码包含许多强大的功能和工具,例如精灵、动画、场景管理、碰撞检测和音频控制等。通过这些功能,开发人员可以方便地创建各种类型的游戏,例如动作游戏、射击游戏、冒险游戏和益智游戏等。 此外,cocos2d-x游戏源码还具有高度定制化的特性,允许开发人员根据他们的需求自定义游戏元素。这种定制化可以通过改进游戏画面,添加新的模式和关卡,甚至整个新的游戏玩法来完成。 总之,cocos2d-x游戏源码是一款功能强大、易于使用和高度定制的游戏引擎,它可以帮助开发人员快速开发各种类型的游戏。无论你是专业开发人员还是无经验的游戏制作者,cocos2d-x都是一个极好的选择。 ### 回答2: Cocos2d-x游戏源码是使用Cocos2d-x引擎进行开发的游戏程序代码。Cocos2d-x引擎是一个开源的跨平台游戏引擎,可以支持iOS、Android、Windows Phone、Windows、MacOS和Linux等多个平台,并且提供了丰富的游戏开发API和工具集。Cocos2d-x游戏源码包含了游戏的核心逻辑、UI设计、动画效果、音频效果等各方面的代码,可以作为开发者学习和借鉴的重要资料。 由于Cocos2d-x引擎支持多种编程语言(如C++、Lua等),因此Cocos2d-x游戏源码也会有对应的编程语言版本。开发者可以通过阅读Cocos2d-x游戏源码,了解游戏开发过程中的技术细节,学习如何使用Cocos2d-x引擎进行游戏开发。同时,开发者也可以通过对Cocos2d-x游戏源码进行修改和优化,来满足游戏的特定需求,提升游戏的性能和用户体验。 总之,Cocos2d-x游戏源码游戏开发者必备的重要资源,可以帮助开发者更好地了解游戏开发技术,提升游戏开发水平。 ### 回答3: cocos2d-x是一款优秀的游戏开发引擎,其源码包含了许多功能强大的游戏开发工具和API。使用cocos2d-x可以帮助开发者更快速更高效地开发出优秀的游戏作品。 cocos2d-x的游戏源码非常丰富,其包含了许多不同类型的游戏示例和模板,如平面射击、塔防、解谜等,这些示例可以帮助开发者更好地了解cocos2d-x的使用方法和原理。 cocos2d-x的源码还包含了许多强大的API和工具,例如场景管理、动画控制、音频引擎等,这些工具和API能够帮助开发者更好地完成游戏的开发和调试。 此外,cocos2d-x的源码还提供了完整的游戏开发框架,包括资源管理、内存管理、事件机制等,这些框架可以帮助开发者更好地组织代码和提高代码的可维护性。 总的来说,cocos2d-x游戏源码提供了丰富的工具和API,可以帮助开发者更高效地进行游戏开发,大大降低了开发难度和成本,是一款非常适合游戏开发者使用的引擎。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值