微信小程序实现简单的摇骰子游戏

2022-04-15 0 1,060

本文实例为大家分享了微信小程序实现摇骰子游戏的具体代码,供大家参考,具体内容如下

页面代码

<view class='top'>{{txt}}</view>
<view class='point1'>
 <image src='{{one_img}}'></image>
</view>
<view class='point2'>
 <image src='{{two_img}}'></image>
 <image src='{{three_img}}'></image>
</view>
<view class='btn' bindtap='enter'>{{msg}}</view>

样式代码

.top{
  width: 220px;
  height: 30px;
  font-size: 25px;
  margin: 20px auto;
}
.point1 image,.point2 image{
  width: 150px;
  height: 150px;
}
.point1 image{
  display: block;
  margin:45px auto;
}
.point2 image{
   margin-top: -20px;
   margin-left: 25px;
}
.btn{
  width:100%;
  height:60px;
  background:green;
  border-radius: 10px;
  line-height: 60px;
  text-align: center;
  font-size: 30px;
  margin-top: 60px;
}

js代码

//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    one_img:'../../image/6-point.png',
    two_img: '../../image/6-point.png',
    three_img: '../../image/6-point.png',
    flag:true,
    timer:null,
    msg:'摇一摇',
    total:0,
    txt:'恭喜你摇到了:0',
    //图片的素材和效果图会发在最下面
    arr:[
      '../../image/1-point.png',
      '../../image/2-point.png',
      '../../image/3-point.png',
      '../../image/4-point.png',
      '../../image/5-point.png',
      '../../image/6-point.png',
    ]
  },
  enter:function(event){
    let obj = this;
    if(obj.data.flag==true){
       obj.data.timer = setInterval(function () {
        let one = Math.floor(Math.random() * 6);
        let two = Math.floor(Math.random() * 6);
        let three = Math.floor(Math.random() * 6);
        obj.setData({
          one_img: obj.data.arr[one],
          two_img: obj.data.arr[two],
          three_img: obj.data.arr[three],
          flag:false,
          msg:'停止',
          total:one+two+three+3,
         // total:18,
          txt:'',
        })
      }, 50);
    }else{
      clearInterval(obj.data.timer);
      obj.setData({
         //one_img: obj.data.arr[5],
         //two_img: obj.data.arr[5],
         //three_img: obj.data.arr[5],
           msg:'摇一摇',
           flag:true,
           txt:'恭喜你摇到了:'+obj.data.total,
      })
    }
   
  },
  
})

图片素材和效果图

微信小程序实现简单的摇骰子游戏微信小程序实现简单的摇骰子游戏微信小程序实现简单的摇骰子游戏微信小程序实现简单的摇骰子游戏微信小程序实现简单的摇骰子游戏微信小程序实现简单的摇骰子游戏

微信小程序实现简单的摇骰子游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持NICE源码。

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 JavaScript 微信小程序实现简单的摇骰子游戏 https://www.niceym.com/27937.html