WeChatGame-----------实现圆球穿越三重门小游戏

    WechatGame是一种以微信的小程序打开的游戏,类似与跳一跳一样,从小程序进入,由小程序支持游戏的进行。

    小程序的发布官方文档:https://mp.weixin.qq.com/debug/wxadoc/dev/quickstart/basic/getting-started.html。

    游戏的编程一般我们是使用Cocos、Egret、Laya等游戏引擎来实现,小程序中的游戏也不例外,Cocos、Egret、Laya等游戏引擎开发团队已经完成了自身引擎及其工具对小游戏的适配和支持。也就是说我们可以用游戏引擎进行编写游戏代码,在利用引擎去生成微信小程序。

    穿越三重门是一种简单的小游戏,玩家点击画面,实现圆球的发射,圆球避过三重滑动的门即可得分,是一种非常简单的小游戏,适合用来熟悉Cocos。

    下面是画面实现效果:

    

    

     

      

    首先是游戏入口场景 

        statr  Scene 

       


首先 建立一个用来记录得分的js文件

module.exports = {
    score: 0,//分数
};
        Start .js
// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

cc.Class({
    extends: cc.Component,

    properties: {
        //游戏名节点
            gameName: {
                default :null,
                type:cc.Node
            },
        //背景节点    
            bg:{
                default:null,
                type:cc.Node
            },
        //开始按钮节点    
             startBtn:{
                default:null,
                type:cc.Node      
             }  , 

        //按钮点击音效
         btnEffect: cc.AudioClip,
    },

//开始游戏按钮点击事件 
startGame: function () {
   cc.audioEngine.playEffect(this.btnEffect)   //点击音效
   cc.director.loadScene("game"); //转换场景
},

onLoad: function () {
    // 设置组件位置
    this.bg.width = cc.winSize.width;
    this.bg.height = cc.winSize.height;
    this.bg.setPosition(this.bg.width/2,this.bg.height/2);
    this.gameName.setPosition(cc.winSize.width/2,(cc.winSize.height/2)+100);
    this.startBtn.setPosition(this.gameName.getPositionX(),this.gameName.getPositionY()-400);

    cc.director.preloadScene("game"); //预加载游戏场景
    // 设置文字循环缩放
    var action = cc.repeatForever(cc.sequence(cc.scaleTo(1, 1.2),cc.scaleTo(1,1))); 
    this.gameName.runAction(action);
},

    start () {

    },

    // update (dt) {},
});

  游戏结束场景 overScene

   

Over.js

var Global=require("Global"); 

cc.Class({
    extends: cc.Component,
    properties: {    

     scoreLabel:{
            default:null,
            type:cc.Label
     },
       //over text节点
       OverText:{
        default :null,
        type:cc.Node
    },
    OverNode:{
        default:null,
        type:cc.Node,
    },
    //背景节点    
        bg:{
            default:null,
            type:cc.Node
        },
    //按钮节点    
        OverBtn:{
            default:null,
            type:cc.Node      
        }, 

        overEffect:cc.AudioClip,
        btnEffect:cc.AudioClip,
    
        
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad :function() {
          // 组件位置
          this.bg.width = cc.winSize.width;
          this.bg.height = cc.winSize.height;
          this.bg.setPosition(this.bg.width/2,this.bg.height/2);
          this.OverText.setPosition(cc.winSize.width/2,cc.winSize.height/2);
          this.OverNode.setPosition(cc.winSize.width/2,(cc.winSize.height/2)+250);
          this.OverBtn.setPosition(cc.winSize.width/2,(cc.winSize.height/2)-250);
        
        //文字缩放
        var action = cc.repeatForever(cc.sequence(cc.scaleTo(1, 1.2),cc.scaleTo(1,1)));      
        this.OverText.runAction(action);   
        // 音效
        cc.audioEngine.playEffect(this.overEffect);
        // 分数
       this.scoreLabel.string="最终分数:"+Global.score;      
    },

    //按钮点击事件
    OverGame:function(){
        Global.score=0;
        cc.audioEngine.playEffect(this.btnEffect);
        cc.director.loadScene("start"); //转换场景
    },
});

在编写game的gameScene之前 需要生成一个Prefab资源来存储发射的圆球,在这里使用Mask对Sprite进行遮盖来形成圆点




效果图:


game.js

var Glod=require("Global");
cc.Class({
    extends: cc.Component,

    properties: {
        radio:{
            default:null,
            type:cc.Prefab
        },
        radios:{
            default:null,
            type:cc.Node 
        },
        blackBg:{
            default:null,
            type:cc.Prefab
        },
        bg:{
            default:null,
            type:cc.Node 
        },
        oneLeft:{
            default:null,
            type:cc.Node  
        },
        oneRight:{
            default:null,
            type:cc.Node  
        },
        twoLeft:{
            default:null,
            type:cc.Node, 
        },
        twoRight:{
            default:null,
            type:cc.Node  
        },
        thirdLeft:{
            default:null,
            type:cc.Node  
        },
        thirdRight:{
            default:null,
            type:cc.Node  
        },
        sroceNum:{
            default:null,
            type:cc.Node
        },
        sroceLabel:{
            default:null,
            type:cc.Label
        },
        isnew:false,
        is:false,
        bgMusic:cc.AudioClip,
        clickEffect:cc.AudioClip
    },
    onDestroy: function(){
        // 停止背景音乐
        cc.audioEngine.stopMusic(this.bgMusic);
    },
     onLoad :function() {
        cc.audioEngine.playMusic(this.bgMusic,true); //背景音乐
        //组件位置
        this.bg.width = cc.winSize.width;
        this.bg.height = cc.winSize.height;
        this.bg.setPosition( -cc.winSize.width/2,-cc.winSize.height/2);

        this.radios.width = 90;
        this.radios.height = 300;
        this.radios.setPosition(0,-cc.winSize.height/2);

        this.sroceNum.width = 40;
        this.sroceNum.height = 300;
        this.sroceNum.setPosition(-cc.winSize.width/2+100,-cc.winSize.height/2+100);

        //设置圆球
        for(var i=0;i<3;i++){
            var  ra = cc.instantiate(this.radio);
            ra.width =this.radios.width-10;
            ra.height = (this.radios.height-30)/3;
            ra.setPosition(0,i*100+50);
            this.radios.addChild(ra);
        };

        //门的动画设置
        var layouts=[
            [this.oneLeft,this.oneRight],
            [this.twoLeft,this.twoRight],
            [this.thirdLeft,this.thirdRight],
        ];
        var speed=2.5;
        var width=-100;
        for(var num=0;num<layouts.length;num++){
            for(var i=0;i<2;i++){
                layouts[num][i].width =cc.winSize.width/2+100;
                layouts[num][i].height = 40;
                layouts[num][i].setPosition(0,width);
                if(i==0){
                    layouts[num][i].runAction(cc.repeatForever(cc.sequence(cc.moveBy(-num+speed, -400, 0),cc.moveBy(-num+speed, 400, 0))));
                };
                if(i==1){
                    layouts[num][i].runAction(cc.repeatForever(cc.sequence(cc.moveBy(-num+speed, 400, 0),cc.moveBy(-num+speed, -400, 0))));
                };
            } 
            width+=200;
        };

     },
     //点击发射圆球
     send:function(){
        var newRadio=cc.instantiate(this.radio); //生成一个新的节点
        newRadio.getComponent("Radio").isnew = true;
        newRadio.getComponent("Radio").game = this;
        newRadio.width =this.radios.width-10;
        newRadio.height = (this.radios.height-30)/3;
        newRadio.setPosition(0,this.radios.getPositionY()+230);
        var action=cc.moveTo(1,0, cc.winSize.height/2+100);
        newRadio.runAction(action); 
        cc.audioEngine.playEffect(this.clickEffect);
        this.node.addChild(newRadio);
      },

     update :function(dt) {
        if(this.is){
            this.node.stopAllActions();
            cc.director.loadScene('over'); 
        }
        this.sroceLabel.string=Glod.score;
     },
});

    圆点的js文件:

  // Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
var Glod=require("Global");
cc.Class({
    extends: cc.Component,

    properties: {
        isnew:false,
        posdisplay:65,  //圆球与门的距离大小设置
    },

    //判断圆点是否触碰到门
    getOne:function(){
        var Left = this.game.oneLeft.getPosition();
        var Right = this.game.oneRight.getPosition();
        // 根据两点位置计算两点之间距离
        var dist = cc.pDistance(this.node.position, Left);
        var dist2 = cc.pDistance(this.node.position, Right);
        if (dist<=this.posdisplay&&dist2<=this.posdisplay) {
           this.game.is=true;
           return true;
        }else{
            this.game.is=false;
            return false;
        }
    },

    getTwo:function(){
        var Left = this.game.twoLeft.getPosition();
        var Right = this.game.twoRight.getPosition();
        // 根据两点位置计算两点之间距离
        var dist = cc.pDistance(this.node.position, Left);
        var dist2 = cc.pDistance(this.node.position, Right);
        if (dist<=this.posdisplay&&dist2<=this.posdisplay) {
           this.game.is=true;
           return true;
        }else{
            this.game.is=false;
            return false;
        }
    },

    getThird:function(){
        var Left = this.game.thirdLeft.getPosition();
        var Right = this.game.thirdRight.getPosition();
        // 根据两点位置计算两点之间距离
        var dist = cc.pDistance(this.node.position, Left);
        var dist2 = cc.pDistance(this.node.position, Right);
        if (dist<=this.posdisplay&&dist2<=this.posdisplay) {
           this.game.is=true;
           return true;
        }else{
            this.game.is=false;
            return false;
        }
    },

    onPicked:function(){
        //销毁当前节点
        Glod.score++;
        this.node.destroy();
    },

    update:function(dt){
        if(this.isnew){
            if(!this.getOne()){
                if(!this.getTwo()){
                    this.getThird();
                }
            };
            
         // if(this.node.getPositionY()>this.game.thirdLeft.getPositionY()){
               
                if(this.node.getPositionY()>=cc.winSize.height/2){
                    this.onPicked();
                }
          
        };
    },
 
});

最后点击Cocos的项目--->构建发布  选择WeChatGame   利用Cocos本身的构建生成WeChatGame 


   完成之后点击发布路径后面的打开即可找到 构建完成的WeChatGame文件

   然后再打开微信Web开发者工具  新建项目 在项目目录选择刚才点击打开之后的路径 选择该路径 

   AppID选择小游戏

    

最后即可在微信Web开发者工具预览 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值