微信小程序開(kāi)發(fā)之實(shí)現(xiàn)搖色子游戲
一、項(xiàng)目展示
搖色子是一款簡(jiǎn)易的游戲類(lèi)小程序
用戶可以投出1-9個(gè)色子
二、核心代碼
dice.wxml
<!--pages/dice/dice.wxml--> <import src="dice/dice-template.wxml" /> <view id="header"> <view class="btn" catchtap="reduceDice"> <image src="/images/btn-left.png"></image> </view> <text id="dice-count">{{diceCount}}</text> <view class="btn" catchtap="addDice"> <image src="/images/btn-right.png"></image> </view> </view> <view id="dice-zone"> <block wx:for="{{dicesData}}"> <template is="dice-template" data="{{...item}}" /> </block> </view> <view id="btn-roll-container" catchtap="onRollTap"> <text id="btn-roll" >搖</text> </view>
dice.js
// pages/dice/dices.js Page({ data: { diceCount: 1, dicesData:[] }, onLoad: function (options) { // 頁(yè)面初始化 options為頁(yè)面跳轉(zhuǎn)所帶來(lái)的參數(shù) this.dotsData = { 1: "5", 2: "28", 3: "357", 4: "1379", 5: "13579", 6: "134679" }; this.timer = null; this.animation = wx.createAnimation({ duration: 400, timingFunction: 'linear', }); }, onReady: function () { // 頁(yè)面渲染完成 }, onShow: function () { // 頁(yè)面顯示 }, // 產(chǎn)生色子點(diǎn)數(shù) createDotData: function () { var num = Math.ceil(Math.random() * 6); var diceData = this.dotsData[num]; var dotsHidden = {}; for (var i = 1; i <= 9; i++) { if (diceData.indexOf(i) > -1) { dotsHidden[i] = "black"; } else { dotsHidden[i] = "white"; } }; return dotsHidden; }, // 產(chǎn)生色子動(dòng)畫(huà) createAnim: function (left, top) { // 色子移入屏幕 this.animation.top(top + "rpx").left(left + "rpx").rotate(Math.random()*180).step({ duration: 1000, timingFunction: "ease-out" }); return this.animation.export(); }, // 產(chǎn)生色子移動(dòng)終點(diǎn)位置 createDicesPos: function () { var dicesPos = []; // 色子位置判斷 function judgePos(l, t) { for (var j = 0; j < dicesPos.length; j++) { // 判斷新產(chǎn)生的色子位置是否與之前產(chǎn)生的色子位置重疊 if ((dicesPos[j].left-146 < l && l < dicesPos[j].left + 146) && (dicesPos[j].top-146 < t && t < dicesPos[j].top + 146)) { return false; } } return true; } for (var i = 0; i < this.data.diceCount; i++) { var posData = {}, left = 0, top = 0; do { // 隨機(jī)產(chǎn)生色子的可能位置 left = Math.round(Math.random() * 600); // 0~600,根據(jù)色子區(qū)域和色子的大小計(jì)算得出 top = Math.round(Math.random() * 550); // 0~550,根據(jù)色子區(qū)域和色子的大小計(jì)算得出 } while (!judgePos(left,top)); posData.left = left; posData.top = top; dicesPos.push(posData); } return dicesPos; }, // 設(shè)置色子數(shù)據(jù) setDicesData: function (diceCount) { var dicesData = []; // 色子動(dòng)畫(huà)數(shù)據(jù) var dicesPos = this.createDicesPos(); // 所有色子的位置數(shù)據(jù) for (var i = 0; i < diceCount; i++) { var diceData = {}; diceData.anim = this.createAnim(dicesPos[i].left, dicesPos[i].top); diceData.dots = this.createDotData(); dicesData.push(diceData); } this.setData({ dicesData: dicesData }); }, // 搖色子 onRollTap: function () { // 設(shè)置色子移出動(dòng)畫(huà) var newData = this.data.dicesData; if(newData.length < this.data.diceCount){ for(var i = 0; i < this.data.diceCount;i++){ var data = {}; newData.push(data); } } for (var i = 0; i < newData.length; i++) { this.animation.left("-233rpx").top("123rpx").rotate(-180).step(); newData[i].anim = this.animation.export(); this.setData({ dicesData: newData }); } var that = this; this.timer = setTimeout(function(){ // 色子改變點(diǎn)數(shù)并移入屏幕 that.setDicesData(that.data.diceCount); },1400) }, // 減少色子數(shù)量 reduceDice: function () { if (this.data.diceCount > 1) { this.setData({ diceCount: this.data.diceCount - 1 }) } }, // 增加色子數(shù)量 addDice: function () { if (this.data.diceCount < 9) { this.setData({ diceCount: this.data.diceCount + 1 }) } } })
三、效果圖
具體的效果展示如下
以上就是微信小程序開(kāi)發(fā)之實(shí)現(xiàn)搖色子游戲的詳細(xì)內(nèi)容,更多關(guān)于小程序搖色子游戲的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用微信小程序制作核酸檢測(cè)點(diǎn)查詢(xún)工具
出門(mén)在外,沒(méi)有核酸證明寸步難行,此文將教你如何通過(guò)小程序制作一個(gè)工具幫你在人生地不熟的情況如何迅速找到核酸檢測(cè)點(diǎn),實(shí)現(xiàn)核酸點(diǎn)查詢(xún)、地圖導(dǎo)航、撥號(hào)等功能,需要的朋友可以參考下2022-10-10前端使用Compressor.js實(shí)現(xiàn)圖片壓縮上傳的詳細(xì)過(guò)程
Compressor.js一個(gè)JavaScript圖像壓縮器,使用瀏覽器的原生canvas.toBlob API來(lái)執(zhí)行壓縮工作,這篇文章主要給大家介紹了關(guān)于前端使用Compressor.js實(shí)現(xiàn)圖片壓縮上傳的詳細(xì)過(guò)程,需要的朋友可以參考下2024-07-07微信小程序表單驗(yàn)證form提交錯(cuò)誤提示效果
這篇文章主要為大家詳細(xì)介紹了微信小程序表單驗(yàn)證form提交錯(cuò)誤提示效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07JS實(shí)現(xiàn)的Object數(shù)組去重功能示例【數(shù)組成員為Object對(duì)象】
這篇文章主要介紹了JS實(shí)現(xiàn)的Object數(shù)組去重功能,可實(shí)現(xiàn)針對(duì)數(shù)組成員為Object對(duì)象的去重復(fù)功能,涉及javascript數(shù)組元素遍歷、屬性判斷等相關(guān)操作技巧,需要的朋友可以參考下2019-02-02webpack配置proxyTable時(shí)pathRewrite無(wú)效的解決方法
這篇文章主要介紹了webpack配置proxyTable時(shí)pathRewrite無(wú)效的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12JS網(wǎng)頁(yè)圖片按比例自適應(yīng)縮放實(shí)現(xiàn)方法
這篇文章主要介紹了JS網(wǎng)頁(yè)圖片按比例自適應(yīng)縮放實(shí)現(xiàn)方法,有需要的朋友可以參考一下2014-01-01