国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 熱點 > 微信 > 正文

微信小程序五子棋游戲的棋盤,重置,對弈實現方法【附demo源碼

2024-07-22 01:19:03
字體:
來源:轉載
供稿:網友

本文實例講述了微信小程序五子棋游戲的棋盤,重置,對弈實現方法。分享給大家供大家參考,具體如下:

DEMO下載

五子棋對弈、悔棋DEMO

效果圖

分析

1. 采用微信小程序的canvas制作五子棋;
2. 確定棋盤大小及格數;
3. 繪制棋盤—-通過棋盤寬高和格數計算間距,同時保存坐標點;
4. 黑方和白方下子—-定義一個布爾變量代表各自的身份;
5. 重置棋盤—-重新開始;
6. 通過判斷當前棋手,悔棋時進行改變。

繪制棋盤

drawLine(arr){ arr.forEach(current => {  this.ctx.setFillStyle(this.lineColor);  this.ctx.beginPath();  this.ctx.lineWidth = 1;  this.ctx.moveTo(current[0].x, current[0].y);  for (var i = 1; i < current.length; i++) {  this.ctx.lineTo(current[i].x, current[i].y);  }  this.ctx.stroke();  this.ctx.closePath();  this.ctx.draw(true); }); } drawChessboard(){ // 每個格子的寬高 var everyLen = this.everyLen; // 標記坐標的個數 var count = 0; // 從縱向保存坐標 var arrY = []; // 雙循環計算每個坐標的橫縱坐標 for(var i = 0;i <= this.type; i++){  var arr = [],arr0 = [];  for(var j = 0;j <= this.type; j++){  count++;  arr.push({   y: this.margin + i * everyLen,   x: this.margin + j * everyLen,   pointX: j,   pointY: i,   index: count  });  arr0.push({   x: this.margin + i * everyLen,   y: this.margin + j * everyLen  })  }  // 清空canvas  this.ctx.clearRect(0, 0, this.width, this.height);  // 保存橫線坐標和豎線坐標  this.everyPoint.push(arr);  arrY.push(arr0); } // 繪制橫向線 this.drawLine(this.everyPoint); // 繪制豎向線 this.drawLine(arrY); }

繪制當前點擊坐標的棋子

// 獲取當前點擊位置的坐標 getPosition(e){ return {  x: e.touches[0].x,  y: e.touches[0].y }; } // 將當前坐標和棋盤坐標數組對比,找到精確坐標 checkPoint(arr,po){ for (var i = 0; i < this.everyPoint.length; i++){  for (var j = 0; j < this.everyPoint[i].length; j++){  if (Math.abs(this.everyPoint[i][j].x - po.x) < this.everyLen/2 && Math.abs(this.everyPoint[i][j].y - po.y) < this.everyLen/2){   // 將棋盤精確坐標保存到當前持棋方數組   arr.push(this.everyPoint[i][j]);   // 同時刪除棋盤坐標數組的該值,表示當前位置已經存在棋子   this.everyPoint[i].splice(j,1);   break;  }  } } } // 繪制當前坐標棋子 drawCle(opts,color){ this.ctx.setFillStyle(color); this.ctx.beginPath(); this.ctx.arc(opts.x, opts.y, this.r, 0, Math.PI * 2, true); this.ctx.closePath(); this.ctx.fill(); this.ctx.draw(true); } drawLastPoint(type){ // 判斷是黑方持棋還是白方持棋,進行繪制棋子 if(type == 'AI'){  this.AIPoint.forEach((current, index) => {  this.drawCle(current, '#000000');  }); }else{  this.myPoint.forEach((current, index) => {  this.drawCle(current, '#ffffff');  }); } } this.page.changeTouchStart = function (e) {  // 判斷游戲是否開始  if (self.START_GAME){  // 獲取當前坐標  var newPo = self.getPosition(e);  // 獲取棋盤精確坐標  if (!self.boolAI && self.boolMy) {   self.checkPoint(self.myPoint, newPo);  } else if (self.boolAI && !self.boolMy) {   self.checkPoint(self.AIPoint, newPo);  }  } } this.page.changeTouchEnd = function (e) {  if (self.START_GAME) {  // 繪制棋子  if (!self.boolAI && self.boolMy) {   self.boolAI = !self.boolAI;   self.boolMy = !self.boolMy;   self.drawLastPoint('PO');   // 判斷白棋是否五子勝利   if (self.myPoint.length >= 5 && self.checkWinner(self.myPoint)){   wx.showToast({title: '白棋勝利!'});   self.START_GAME = false;   }  } else if (self.boolAI && !self.boolMy) {   self.boolAI = !self.boolAI;   self.boolMy = !self.boolMy;   self.drawLastPoint('AI');   // 判斷黑棋是否五子勝利   if(self.AIPoint.length >= 5 && self.checkWinner(self.AIPoint)){   wx.showToast({ title: '黑棋勝利!' });   self.START_GAME = false;   }  }  } }            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绵阳市| 靖安县| 科技| 益阳市| 苍南县| 临高县| 越西县| 河曲县| 肥西县| 永定县| 乐至县| 马尔康县| 老河口市| 涡阳县| 临西县| 眉山市| 霸州市| 西峡县| 肥乡县| 江陵县| 达州市| 尚义县| 奉贤区| 亳州市| 安达市| 永兴县| 丰宁| 赤峰市| 安吉县| 永和县| 彩票| 道孚县| 莒南县| 肇东市| 澄江县| 陵水| 宕昌县| 行唐县| 台北市| 塔城市| 彰化市|