美文网首页前端社团
两人扑克游戏对象的建立

两人扑克游戏对象的建立

作者: wuyumumu | 来源:发表于2016-11-12 03:00 被阅读19次

    实现发给服务器发牌给两个人
    对方收到用户发的牌

    poker.js 建立poker对象

    function Poker() {
        this.pokers=[];
    }
    Poker.prototype.num = 27;
    Poker.prototype.drawPokers=function () {
      var x,y;
      this.sort(this.pokers);
      for(var i=0;i<this.num;i++) {
          x=(this.pokers[i]%100-1) * 90;
          y=(parseInt(this.pokers[i]/100)-1)*120;
          $('#usrPoker').append('<div id="poker'+i+'l"></div>');
          $('#usrPoker #poker'+i+'l').css({
              left: i*22+'px',
              backgroundPosition: (0-x)+ 'px ' + (0-y) + 'px'
          });
      }
    }
    
    Poker.prototype.reDrawPoker=function (poker) {
      var x,y;
      $('#usrPoker').html("");
      this.sort(poker);
      for(var i=0;i<poker.length;i++){
        x=(poker[i]%100-1)*90;
        y=(parseInt(poker[i]/100)-1)*120;
          $('#usrPoker').append('<div id="poker'+i+'l"></div>');
          $('#usrPoker #poker'+i+'l').css({
              left: i*22+'px',
              backgroundPosition: (0-x)+ 'px ' + (0-y) + 'px'
          });
      }
      this.pokers = poker;
    }
    
    Poker.prototype.sort = function (poker) {
        poker.sort(function (a,b) {
            a=a%100;
            b=b%100;
            return a-b;
        });
    
    }
    

    服务器进行发牌

    var users=[];
    var online=[];
    var sockets=[];
    var allpokers=[
      101,102,103,104,105,106,107,108,109,110,111,112,113,
      201,202,203,204,205,206,207,208,209,210,211,212,213,
      301,302,303,304,305,306,307,308,309,310,311,312,313,
      401,402,403,404,405,406,407,408,409,410,411,412,413,
      501,502
    ];
    
    io.on('connection', function(socket) {
    
        socket.on('login', function (usrName) {
            if (online.indexOf(usrName) != -1) {
                console.log("existed");
            } else {
                users[usrName] = socket.id;
                sockets[socket.id] = socket;
                online.push(usrName);
            }
        });
    
        socket.on('start game', function (uid,obj) {
            var index;
            if(online.length==2){
                for(var i = 0; i < 27; i++) {
                  index=parseInt(Math.random()*allpokers.length);
                  obj.pokers.push(allpokers[index]);
                  allpokers.splice(index,1);
                }
                socket.broadcast.emit('draw',obj.pokers);
                socket.emit('draw',allpokers);
    
    //存在问题,不知道为什么没办法直接发给某个固定用户了
                // sockets[users[online[0]]].emit('draw',obj.pokers);
                // sockets[users[online[1]]].emit('draw',allpokers);
            } else{
               socket.emit('wait');
            }
        });
        socket.on("play card",function(uid,pokers){
            var index=online.indexOf(uid);
            if(index!=-1){
              socket.broadcast.emit("show card",uid,pokers);
            }
        });
    
    });
    

    js端代码

     socket.on('draw',function (obj) {
        $("#startBox").hide();
        initOther();
        console.log(obj);
        pokerArray.pokers=obj;
        pokerArray.drawPokers();
      });
      
      socket.on("wait",function () {
        $("#startBtn").hide();
        $("#info").show();
      });
      socket.on("show card",function (other,pokers) {
        $('#otherShow .showPoker').html("");
        for(var i=0;i<pokers.length;i++){
          x=(pokers[i]%100-1)*90;
          y=(parseInt(pokers[i]/100)-1)*120;
          $('#otherShow .showPoker').append('<div id="ospoker'+i+'"></div>');
          $('#otherShow .showPoker #ospoker'+i).css({
            left: i*32+'px',
            backgroundPosition: (0-x)+ 'px ' +(0-y) + 'px'
          });
        }
      });
    

    相关文章

      网友评论

        本文标题:两人扑克游戏对象的建立

        本文链接:https://www.haomeiwen.com/subject/sdxqpttx.html