您好,登錄后才能下訂單哦!
昨天看有個石頭剪刀布的練習,就拿出來做了一下,布局的代碼浪費了很多時間,果然CSS這塊的還不是很熟練,下面直接上圖上代碼了。
JS:
var numAi = 0
var timer
Page({
data:{
//控制按鈕是否可點擊
btnState:false,
//記錄獲勝次數
winNum:0,
//中間的話“Ho~ You Win”
gameOfPlay:'',
//用戶選擇的圖片
p_w_picpathUserScr:'/pages/p_w_picpath/wenhao.png',
//電腦隨機的圖片
p_w_picpathAiScr:'',
//石頭剪刀布圖片數組
srcs:[
'/pages/p_w_picpath/shitou.png',
'/pages/p_w_picpath/jiandao.png',
'/pages/p_w_picpath/bu.png'
]
},
//生命周期,剛進來
onLoad: function () {
//獲取本地緩存“已經獲勝的次數”
var oldWinNum = wx.getStorageSync('winNum');
//如果有緩存,那么賦值,否則為0
if(oldWinNum != null && oldWinNum !=''){
this.data.winNum = oldWinNum;
}
this.timerGo();
},
//點擊按鈕
changeForChoose(e){
console.log();
if(this.data.btnState == true){
return;
}
//獲取數組中用戶的,石頭剪刀布相應的圖片。
this.setData({
p_w_picpathUserScr:this.data.srcs[e.currentTarget.id]
});
//清除計時器
clearInterval(timer);
//獲取數據源
var user = this.data.p_w_picpathUserScr;
var ai = this.data.p_w_picpathAiScr;
var num = this.data.winNum;
var str = '0.0~\nYou Lost!';
//判斷是否獲勝
if( user == "/pages/p_w_picpath/shitou.png" && ai == "/pages/p_w_picpath/jiandao.png"){
//獲勝后增加次數、改變文字內容、從新緩存獲勝次數
num++;
str = 'Ho~\nYou Win!';
wx.setStorageSync('winNum', num);
};
if(user == "/pages/p_w_picpath/jiandao.png" && ai == "/pages/p_w_picpath/bu.png"){
num++;
str = 'Ho~\nYou Win!';
wx.setStorageSync('winNum', num);
};
if(user== "/pages/p_w_picpath/bu.png" && ai == "/pages/p_w_picpath/shitou.png"){
num++;
str = 'Ho~\nYou Win!';
wx.setStorageSync('winNum', num);
};
//如果平局
if(user == ai){
str = 'Game Draw!';
}
//刷新數據
this.setData({
winNum:num,
gameOfPlay:str,
btnState:true
});
},
//開啟計時器
timerGo(){
timer = setInterval(this.move,100);
},
//ai滾動方法
move(){
//如果大于等于3,重置
if(numAi>=3){
numAi=0;
}
this.setData({
//獲取數組中Ai的,石頭剪刀布相應的圖片。
p_w_picpathAiScr: this.data.srcs[numAi],
})
numAi++;
},
again(){
//控制按鈕
if(this.data.btnState == false){
return;
}
//從新開始計時器
this.timerGo();
//刷新數據
this.setData({
btnState:false,
gameOfPlay:'',
p_w_picpathUserScr:'/pages/p_w_picpath/wenhao.png'
});
}
})
.WXML
<view class="downView" >
<text class="winNum">你已經獲勝了<text >`winNum`</text>次</text>
<view class="showView">
<p_w_picpath src="`p_w_picpathAiScr`" class="gesturesImgL"></p_w_picpath>
<text class="winOrLost">`gameOfPlay`</text>
<p_w_picpath src="`p_w_picpathUserScr`" class="gesturesImgR"></p_w_picpath>
</view>
<view class="chooseForUserView">
<text class="winNum">出拳吧,少年~</text>
<view class="choose-V">
<block wx:for="`srcs`">
<view class="choose-view" bindtap="changeForChoose" id="`index`">
<p_w_picpath class="choose-p_w_picpath" src="`item`" ></p_w_picpath>
</view>
</block>
</view>
<button class="againBtn" bindtap="again">再來!</button>
</view>
</view>
.WXSS
/*底*/
.downView{
width: 100%;
height: 1250rpx;
background: #FAE738;
margin: 0rpx;
text-align: center;
}
/*獲勝次數*/
.winNum{
padding-top: 40rpx;
display: block;
font-size: 30rpx;
color: #363527;
font-weight:500;
}
/*展示出拳結果*/
.showView{
display: flex;
width: 100%;
margin-top:30rpx;
height: 200rpx;
}
.gesturesImgL{
height: 180rpx;
width: 180rpx;
margin-left:80rpx;
}
.gesturesImgR{
height: 180rpx;
width: 180rpx;
margin-right:80rpx;
}
.winOrLost{
color: orangered;
flex:1;
font-size: 30rpx;
margin-top:75rpx;
}
/*用戶出拳*/
.chooseForUserView{
margin:40rpx;
height: 800rpx;
background: white;
text-align: center;
}
.choose-V{
display: flex;
margin-top: 40rpx;
}
.choose-view{
flex: 1;
content:none !important;
height: 140rpx;
width: 140rpx;
border:1px solid white;
}
.choose-p_w_picpath{
height: 160rpx;
width: 160rpx;
border-radius:80rpx;
}
/*再來*/
.againBtn{
margin:80rpx;
background: #FAE738;
}
demo資源下載:小程序-石頭剪刀布
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。