


"jsList" : [    "src/resource.js",    "src/biscuit.js",    "src/game.js" ]  var Biscuit = cc.Sprite.extend({   winSize: 0,   ctor: function(snakeParts) {       /*    */              this._super(asset.SnakeBiscuit_png);                /*  winSize */       this.winSize = cc.view.getDesignResolutionSize();       /*    */       this.randPosition(snakeParts);   },   });  randPosition: function(snakeParts) {                  var step = 20;       var randNum = function(range) {           /*       range */           return Math.floor(Math.random() * range);              };       /*   ,     */       var range = {           x: (this.winSize.width / step) - 1,           y: (this.winSize.height / step) - 1                   }                                /*   */       var possible = {            x: randNum(range.x) * step,            y: randNum(range.y) * step       }                              var flag = true;       var hit = false;             /*     */       while (flag) {                      /*     */           for (var part = 0; part < snakeParts.length; part++) {    /*          */ if (snakeParts[part].x == possible.x &&   snakeParts[part].y == possible.y)   {                   /*   ,   hit */                   hit = true;               }           }           /*     */           if (hit == true) {                              /*   */               possible.x = randNum(range.x) * step;               possible.y = randNum(range.y) * step;                              hit = false;           } else { /*    */               /*    */               flag = false;               this.x = possible.x;               this.y = possible.y;           }                  }          },  biscuit: null, //     updateBiscuit: function() { /*        */ if (this.biscuit) { /*   */ this.biscuit.randPosition(this.snakeParts);       /*   */ } else { /*    */ this.biscuit = new Biscuit(this.snakeParts); /*       - */ this.addChild(this.biscuit); } },  ctor: function () { ...               /*      */ this.updateBiscuit(); ... }, 
 checkCollision: function() { var winSize = cc.view.getDesignResolutionSize(); var head = this.snakeParts[0]; var body = this.snakeParts; /*      */ if (head.x < 0) { head.x = winSize.width; } else if (head.x > winSize.width) { head.x = 0; } if (head.y < 0) { head.y = winSize.height; } else if (head.y > winSize.height) { head.y = 0; } /*     */ for (var part = 1; part < body.length; part++) { if (head.x == body[part].x && head.y == body[part].y) { /*   GameOver */ } }   /*     */ if (head.x == this.biscuit.x && head.y == this.biscuit.y) { /*    */ this.updateBiscuit(); /*    */ this.addPart(); } },  update: function(dt) { /*  ,    */ var up = 1; /*       */ if (this.counter < this.interval) { this.counter += dt;  } else { this.counter = 0; /*   */ this.moveSnake(this.curDir);                       /* ,       ,      */ this.checkCollision();           } },  ctor: function () { ... for (var parts = 0; parts < 10; parts++) { this.addPart(); } },  var SnakeLayer = cc.Layer.extend({   ...   curDir: 0,   nextDir: 0,   ... });  var SnakeLayer = cc.Layer.extend({   ...   moveSnake: function() {       ...                      },   update: function(dt) {       ...       this.moveSnake(this.nextDir);       this.moveSnake();                             ...   }  });  var SnakeLayer = cc.Layer.extend({   ...   ctor: function () {                  /*     */       cc.eventManager.addListener({ ...               /*     */               if (keyMap[keyCode] !== undefined) {                   //targ.curDir = keyMap[keyCode]; //   targ.nextDir = keyMap[keyCode];  //                 }                                     }                  }, this);             /*     */       cc.eventManager.addListener({           onTouchMoved: function(touch, event) { ... /*    */ if (delta.x !== 0 && delta.y !== 0) { if (Math.abs(delta.x) > Math.abs(delta.y)) {                   /*     */ //targ.curDir = Math.sign(delta.x) * right;  //   targ.nextDir = Math.sign(delta.x) * right;   //   } else if (Math.abs(delta.x) < Math.abs(delta.y)) { /*     */ //targ.curDir = Math.sign(delta.y) * up; //                         targ.nextDir = Math.sign(delta.y) * up;  //                       }                           }           }                        }, this);   },      moveSnake: function() {       ...                           /*    */ /*       */          //if (dirMap[dir] !== undefined) {       //   dirMap[dir]();          //} /*    */       /*       */          if (dirMap[this.curDir] !== undefined) {           dirMap[this.curDir]();          } ...   },   ... });  var SnakeLayer = cc.Layer.extend({   ...   moveSnake: function() {             /*   ,              */       if ((this.nextDir * -1) != this.curDir || this.snakeParts.length == 1)  {                      this.curDir = this.nextDir;       }             /*      */          if (dirMap[this.curDir] !== undefined) {           dirMap[this.curDir]();          }        ...   },   ... }); 

Source: https://habr.com/ru/post/281873/
All Articles