猜拳的游戏主函数 用组件的方式

//运行于cocoscreator 中,挂载在主canvas中
//必须创建一个按钮作为点击node

var Box = cc.Class({
    data: 0,
    pot: 0
});//方便扩展可能还有一些数据要储存呢

//这个组件只是负责挂载在主node下,主要的数据储存还是由全局数据保存
cc.Class({
    extends: cc.Component,

    //可通过拖动方便修改
    properties: {
        //显示分数的label
        scoreLabel: {
            default: null,
            type: cc.Label
        },
        //默认的分数节点
        exampleButtonNode: {
            default: null,
            type: cc.Node
        },
        //方块间的距离
        btnDistance: {
            default: 110,
            type: cc.Integer
        },
    },

    buttonNodeParent: null,//默认分数节点的父节点
    gameData: null,//目前的游戏数据
    stepGame: 0,//目前游戏的步数
    isGameWin: false,//是否胜利

    //判断步数是否达到最大
    isStepGameEnd: function () {
        this.stepGame += 1;
        if (this.stepGame >= 9) {
            this.scoreLabel.string = "game over";
            return true;
        }
        return false;
    },
    getOneGameData: function (tmpx, tmpy) {
        var middleArr = this.gameData[tmpx];
        return middleArr[tmpy].data;
    },
    changeGameData: function (tmpx, tmpy, value) {
        var middleArr = this.gameData[tmpx];
        middleArr[tmpy].data = value;
    },
    isThreeBox: function (tmpx, tmpy) { //1玩家赢,-1ai赢
        var sum = 0;
        //斜向
        if (tmpx == tmpy) {
            sum = 0;
            for (var j = 0; j < 3; j++) {
                sum += this.getOneGameData(j, j);;
            }
            if (sum == 3) { return 1; }
            if (sum == -3) { return -1; }
        }
        if (Math.abs(tmpy - tmpx) == 2) {
            sum = 0;
            for (var j = 0; j < 3; j++) {
                sum += this.getOneGameData(j, 2 - j);
            }
            if (sum == 3) { return 1; }
            if (sum == -3) { return -1; }
        }
        //横向
        sum = 0;
        for (var j = 0; j < 3; j++) {
            sum += this.getOneGameData(j, tmpx);
            cc.log("sum:" + sum)
        }
        if (sum == 3) { return 1; }
        if (sum == -3) { return -1; }
        //竖向
        sum = 0;
        for (var j = 0; j < 3; j++) {
            sum += this.getOneGameData(tmpy, j);
        }
        if (sum == 3) { return 1; }
        if (sum == -3) { return -1; }
        return 0;
    },
    judgeGameData: function (tmpx, tmpy) {
        var result = this.isThreeBox(tmpx, tmpy);
        cc.log("result:" + result);
        if (result == 1 || result == -1) {
            if (result == 1) {
                this.scoreLabel.string = "you win";
            } else {
                this.scoreLabel.string = "you lose";
            }
            var children = this.buttonNodeParent.children;
            for (var i = 0; i < children.length; ++i) {
                var onechild = children[i];
                onechild.targetOff(onechild);
            }
        }
        return false;
    },
    //判断是否游戏胜利
    judgeGameWin: function (event) {
        var newPot = event.getUserData();
        var tmpx = newPot.x;
        var tmpy = newPot.y;
        //cc.log(event.target);//这里的target是谁发送来的事件
        //cc.log(this);//这里的this是表示挂载这个监听事件本身的node
        //十分注意这里的this 即是canvas,热不是这个对象本身;
        var self = this.getComponent("MainScript");
        var tmpMSArray = self.gameData;
        cc.log(self.stepGame + ",touch:" + tmpx + "," + tmpy);

        //玩家移动
        self.changeGameData(tmpx, tmpy, 1);
        if (self.judgeGameData(tmpx, tmpy)) { return; }
        if (self.isStepGameEnd()) { return; }

        //扫描剩下数组,查看可以插入的位置
        var restData = new Array();
        for (var i = 0; i < self.gameData.length; i++) {
            var tmpMidArr = self.gameData[i];
            for (var j = 0; j < tmpMidArr.length; j++) {
                if (tmpMidArr[j].data == 0) {
                    restData.push(new cc.Vec2(i, j));
                }
            }
        }

        //随机选择一个数据
        var randomVec2 = restData[Math.floor(restData.length * Math.random())];
        var aitmpx = Math.floor(randomVec2.x);
        var aitmpy = Math.floor(randomVec2.y);

        //ai移动
        var aibtnName = "btn_" + aitmpx + "_" + aitmpy;
        var aibtn = self.buttonNodeParent.getChildByName(aibtnName);

        var aiLabel = aibtn.getChildByName("Label");//得到的是一个node
        var aiLabelfunc = aiLabel.getComponent(cc.Label);
        aiLabelfunc.string = "x";
        aibtn.targetOff(aibtn);//注意这里不是绑在label上而是button上

        self.changeGameData(aitmpx, aitmpy, -1);
        cc.log("ai move:" + aitmpx + "," + aitmpy);
        if (self.judgeGameData(aitmpx, aitmpy)) { return; }
        if (self.isStepGameEnd()) { return; }
    },
    gameInit: function () {
        //数据初始化
        this.gameData = new Array();
        this.stepGame = 0;
        this.isGameWin = false;
        //放入数据
        for (var i = 0; i < 3; i++) {
            var middleArr = new Array();
            for (var j = 0; j < 3; j++) {
                var onebox = new Box();
                onebox.data = 0;
                middleArr.push(onebox);
            }
            this.gameData.push(middleArr);
        }
    },
    // use this for initialization
    onLoad: function () {
        var self = this;
        self.gameInit();

        if (!self.scoreLabel) {
            self.scoreLabel = cc.find('Canvas/center/label').getComponent(cc.Label);
            self.scoreLabel.string = "";
        }
        if (!self.exampleButtonNode) {
            self.exampleButtonNode = cc.find('Canvas/center/btn_0_0');
        }
        self.buttonNodeParent = self.exampleButtonNode.parent;

        //控制按钮
        function onTouchDown(event) {
            var tmpLabel = event.target.getChildByName("Label");
            var tmpLabelfunc = tmpLabel.getComponent(cc.Label);
            tmpLabelfunc.string = "O";
        }
        //按钮事件
        function onTouchUp(event) {
            var userPot = {};
            //cc.log(event.target);
            userPot.x = event.target.nowPot.x;
            userPot.y = event.target.nowPot.y;
            var newEvent = new cc.Event.EventCustom('judgeGameWin', true);
            newEvent.setUserData(userPot);
            event.target.dispatchEvent(newEvent);

            event.target.targetOff(event.target);//停止按钮的事件
            //event.target.off(cc.Node.EventType.TOUCH_START,event.target);
            //event.target.off(cc.Node.EventType.TOUCH_END,event.target);
            //event.target.off(cc.Node.EventType.TOUCH_CANCEL,event.target);
        }

        for (var i = 0; i < 3; i++) {
            for (var j = 0; j < 3; j++) {
                //复制例子
                var copynode = cc.instantiate(self.exampleButtonNode);
                copynode.name = "btn_" + i + "_" + j;
                //cc.log("btn_"+j+"_"+i);
                copynode.parent = self.buttonNodeParent;
                var copynodePot = new cc.Vec2(self.exampleButtonNode.getPosition().x + self.btnDistance * j,
                    self.exampleButtonNode.getPosition().y - self.btnDistance * i);
                copynode.setPosition(copynodePot);
                //给每个node添加一个额外的数据
                copynode.nowPot = { x: i, y: j };

                //所有的按钮添加touch事件
                copynode.on(cc.Node.EventType.TOUCH_START, onTouchDown, copynode);
                copynode.on(cc.Node.EventType.TOUCH_END, onTouchUp, copynode);
                copynode.on(cc.Node.EventType.TOUCH_CANCEL, onTouchUp, copynode);
            }
        }

        self.exampleButtonNode.removeFromParent();
        //监听全局游戏逻辑事件
        this.node.on('judgeGameWin', this.judgeGameWin);
    }
});

你可能感兴趣的:(游戏)