您好,登錄后才能下訂單哦!
這篇文章主要介紹了微信小程序如何實(shí)現(xiàn)井字棋游戲的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇微信小程序如何實(shí)現(xiàn)井字棋游戲文章都會有所收獲,下面我們一起來看看吧。
效果圖
.wxml
<view class="title"> <view wx:if="{{currindex < 9 || defeat}}"> {{defeat?'勝出方:':'輪到了:'}}<span class="span">{{defeat?(outindex?'○':'x'):(outindex?'x':'○')}}</span> </view> <view wx:else>平局</view> </view> <view class="curr_toe"> <view wx:for="{{detail}}" wx:key="index" class="curr_item" bindtap="{{item.type > 0 || defeat?'':'tactoe'}}" data-index="{{index}}"> <view wx:if="{{item.type > 0}}">{{item.type == 1?'○':'×'}}</view> </view> </view> <button wx:if="{{defeat || currindex > 8}}" bindtap="reset">重新開始</button>
.wxss
page{background: #fff;} .title{width: 100%;display: flex;align-items: center;justify-content: center;margin-top: 20rpx;font-size: 34rpx;} .span{font-size: 60rpx;} .curr_toe{width: 510rpx;height: 510rpx;margin: 30rpx calc((100% - 500rpx) / 2);border-top:1px solid #ddd;border-right: 1px solid #ddd;} .curr_item{border-left: 1px solid #ddd;width: 33.33%;height: 170rpx;display: flex;align-items: center;justify-content: center;color:red; font-size:170rpx;float: left;border-bottom: 1px solid #ddd;}
.js
Page({ data: { lines:[ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6], ] }, onLoad: function (options) { this.reset() }, reset(e){ this.setData({ detail:[ {type:0},{type:0},{type:0},{type:0},{type:0},{type:0},{type:0},{type:0},{type:0} ], defeat:false, outindex:false, currindex:0 }) }, tactoe(e){ var index = e.currentTarget.dataset.index,currindex = this.data.currindex, detail = this.data.detail,outindex = this.data.outindex; currindex++ detail[index].type = outindex?2:1 this.setData({ detail:detail, currindex:currindex, outindex:!outindex }) if(currindex > 4){ this.validate() } }, validate(e){ var detail = this.data.detail,lines = this.data.lines; for(let i = 0;i < lines.length;i++){ const [a, b, c] = lines[i]; if(detail[a].type && detail[a].type == detail[b].type && detail[a].type == detail[c].type){ wx.showModal({ title: '提示', content: (detail[a].type == 1?'○':'×')+'獲得了勝利', showCancel:false, confirmText:'我知道了' }) this.setData({ defeat:true }) return false; } } }, })
關(guān)于“微信小程序如何實(shí)現(xiàn)井字棋游戲”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“微信小程序如何實(shí)現(xiàn)井字棋游戲”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。