欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

cocos2d-js 游戏源码

程序员文章站 2022-04-07 17:29:10
...

四款不同类型的游戏
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);
    }
})
相关标签: 游戏