王者骰子源码分享

阅读文本大概需要3分钟。

如果关注我的公众号的都会发现,公众号有一个如下图的菜单:

估计也没多少人点击这个菜单,这个小程序终于达到1000 UV(独立访客)。这个小程序只有一个页面,而且是纯java script写的。

今天把这个小程序的源码分享给大家,主要逻辑控制在index.js

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

Page({
  data: {
    diceConst: ["/pages/images/01.png", "/pages/images/02.png", "/pages/images/03.png", "/pages/images/04.png", "/pages/images/05.png", "/pages/images/06.png"], //骰子点数
    dicePng: ["/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png"],
    initPng: ["/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png", "/pages/images/00.png"], //未知点数
    historyCount: 0, //历史点数
    currentCount: 0, //当前点数
    actionSheetHidden: true, //列表隐藏标志
    diceCount: 6, //骰子数
    displayFlag: ["", "", "", "", "", ""], //不显示
    genCount: [] //生成的骰子点数
  },
  //事件处理函数
  bindViewTap: function() {

  },
  onLoad: function() {
    var displayFlag = [];
    for (var i = 0; i < 5; i++) {
      displayFlag[i] = "none";
    }
    this.setData({
      displayFlag: displayFlag,
      genCount: []
    });
  },
  //分享
  onShareAppMessage: function() {
    return {
      title: '王者摇骰子',
      desc: '王者摇骰子',
      path: '/pages/index/index'
    }
  },
  //骰子个数事件
  diceCountOpr: function() {
    console.log("diceCountOpr");
    this.setData({
      actionSheetHidden: false
    })
  },
  //点击取消
  actionSheetChange: function() {
    console.log("actionSheetChange")
    this.setData({
      actionSheetHidden: true
    })
  },
  //条目点击事件
  actionSheetItem: function(event) {
    console.log(event)
    this.setData({
      actionSheetHidden: true
    })
    var diceCount = event.currentTarget.dataset.value;
    if (diceCount != 0) {
      this.setData({
        diceCount: diceCount
      });
      var displayFlag = [];
      for (var i = 0; i < 6; i++) {
        if (diceCount - 1 == i) {
          displayFlag[i] = "";
          console.log(displayFlag[i])
        } else {
          displayFlag[i] = "none";
          console.log(displayFlag[i])
        }
        this.setData({
          displayFlag: displayFlag,
          historyCount: 0, //历史点数
          currentCount: 0, //当前点数
          genCount: [], //生成的骰子点数
          dicePng: this.data.initPng
        });
      }
    }
    console.log(this.data.diceCount);
  },
  //摇骰子事件
  diceWaveOpr: function() {
    console.log("diceWaveOpr");
    wx.playBackgroundAudio({
      dataUrl: "http://dx.sc.chinaz.com/Files/DownLoad/sound1/201510/6522.mp3",
    });
    var genCount = [];
    for (var i = 0; i < this.data.diceCount; i++) {
      //点子数
      var num = Math.floor(Math.random() * 6 + 1);
      genCount[i] = num;
    }
    console.log(genCount);
    this.setData({
      genCount: genCount, //生成的骰子点数
      dicePng: this.data.initPng
    });
    wx.vibrateLong({

    });
  },
  //开事件
  diceOpen: function() {
    console.log("diceOpen");
    var diceCount = this.data.diceCount;
    var currentCount = 0;
    var dicePng = this.data.dicePng;
    //生成的点子数
    var genCount = this.data.genCount;
    var len = 6;
    console.log("diceCount = " + diceCount)
    var dicePointPng = [];
    console.log("---genCount---" + genCount)
    console.log(this.data.diceConst)
    if (genCount.length > 0) {
      for (var i = 0; i < len; i++) {
        if (i < diceCount) {
          //生成的点数
          var dicePoint = genCount[i];
          console.log(dicePoint)
          console.log(this.data.diceConst[dicePoint])
          //改变png图片  dicePoint = 图片下标-1  数组从零开始
          dicePointPng[i] = this.data.diceConst[dicePoint-1];
          //点数=数组下标+1
          currentCount = currentCount + dicePoint;
        }
      }
      //变成历史
      var historyCount = this.data.currentCount;
      this.setData({
        historyCount: historyCount, //历史点数
        currentCount: currentCount, //当前点数
        dicePng: dicePointPng,
      });
      console.log(this.data.dicePng);
    } else {
      //提示
      wx.showToast({
        title: '先摇一摇',
        icon: 'success',
        duration: 1000,
        mask: true
      })
    }
  }
})

源码下载地址:

https://gitee.com/hjj520/xml/tree/master/mini

下载完代码后只要把project.config.json文件的如下标志换成自己申请的小程序appid即可。

推荐阅读

Spring Boot 最流行的 16 条实践

SSM框架的面试常见问题

【分布式】缓存穿透、缓存雪崩,缓存击穿解决方案

阿里P7给出的一份超详细 Spring Boot 知识清单

关注我每天进步一点点

你点的每个在看,我都认真当成了喜欢

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BUG弄潮儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值