微信小程序开发一个简单的摇骰子游戏

页面代码

<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,
      })
    }
   
  },
  
})

图片素材和效果图
在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值