作者:湛蓝天空jk | 来源:互联网 | 2023-08-31 02:11
我的博客:泉源链接昨天看有个石头铰剪布的演习,就拿出来做了一下,规划的代码浪费了许多时候,果真CSS这块的还不是很闇练,下面直接上图上代码了。JS:varnumAi0vartime
我的博客:泉源链接
昨天看有个石头铰剪布的演习,就拿出来做了一下,规划的代码浪费了许多时候,果真CSS这块的还不是很闇练,下面直接上图上代码了。
JS:
var numAi = 0
var timer
Page({
data:{
//掌握按钮是不是可点击
btnState:false,
//纪录得胜次数
winNum:0,
//中心的话“Ho~ You Win”
gameOfPlay:'',
//用户挑选的图片
imageUserScr:'/pages/image/wenhao.png',
//电脑随机的图片
imageAiScr:'',
//石头铰剪布图片数组
srcs:[
'/pages/image/shitou.png',
'/pages/image/jiandao.png',
'/pages/image/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({
imageUserScr:this.data.srcs[e.currentTarget.id]
});
//消灭计时器
clearInterval(timer);
//猎取数据源
var user = this.data.imageUserScr;
var ai = this.data.imageAiScr;
var num = this.data.winNum;
var str = '0.0~\nYou Lost!';
//推断是不是得胜
if( user == "/pages/image/shitou.png" && ai == "/pages/image/jiandao.png"){
//得胜后增添次数、转变笔墨内容、重新缓存得胜次数
num++;
str = 'Ho~\nYou Win!';
wx.setStorageSync('winNum', num);
};
if(user == "/pages/image/jiandao.png" && ai == "/pages/image/bu.png"){
num++;
str = 'Ho~\nYou Win!';
wx.setStorageSync('winNum', num);
};
if(user== "/pages/image/bu.png" && ai == "/pages/image/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的,石头铰剪布响应的图片。
imageAiScr: this.data.srcs[numAi],
})
numAi++;
},
again(){
//掌握按钮
if(this.data.btnState == false){
return;
}
//重新最先计时器
this.timerGo();
//革新数据
this.setData({
btnState:false,
gameOfPlay:'',
imageUserScr:'/pages/image/wenhao.png'
});
}
})
.WXML
你已得胜了{{winNum}}次
{{gameOfPlay}}
出拳吧,少年~
.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-image{
height: 160rpx;
width: 160rpx;
border-radius:80rpx;
}
/*再来*/
.againBtn{
margin:80rpx;
background: #FAE738;
}
demo资本下载:小顺序-石头铰剪布