当前位置: 首页 > news >正文

微信小程序【五】摇骰子

摇骰子

  • 一、dice.js
  • 二、dice.json
  • 三、dice.wxml
  • 四、dice.wxss

效果简述:点击设置“骰子个数”,喝一杯前,先摇一摇。
骰子图片命名示例: 1.png、2.png
在这里插入图片描述

一、dice.js

Page({data: {numDice: 1, // 初始化骰子数diceImages: [],dicePositions: [],rolling: false,intervalId: null},onInputNumDice(e) {this.setData({numDice: parseInt(e.detail.value) + 1,diceImages: [],dicePositions: []});},rollDice() {if (this.data.rolling) return;const { numDice } = this.data;this.setData({ rolling: true });const intervalId = setInterval(() => {const rollingImages = [];for (let i = 0; i < numDice; i++) {rollingImages.push('http://xxx/dice/' + Math.floor(Math.random() * 6 + 1) + '.png');}this.setData({diceImages: rollingImages,dicePositions: this.generateRandomPositions(numDice)});}, 100);this.setData({intervalId: intervalId});},revealDice() {if (!this.data.rolling) return;clearInterval(this.data.intervalId);const { numDice } = this.data;const finalDiceImages = [];for (let i = 0; i < numDice; i++) {finalDiceImages.push('http://xxx/dice/' + Math.floor(Math.random() * 6 + 1) + '.png');}this.setData({diceImages: finalDiceImages,rolling: false,intervalId: null,dicePositions: this.generateRandomPositions(numDice)});},generateRandomPositions(numDice) {const positions = [];const size = 50; const containerWidth = 300; const containerHeight = 200;const margin = 1; // 骰子间距for (let i = 0; i < numDice; i++) {let position;let overlaps;do {overlaps = false;position = {left: Math.floor(Math.random() * (containerWidth - size)),top: Math.floor(Math.random() * (containerHeight - size))};for (const other of positions) {if (Math.abs(position.left - other.left) < size + margin && Math.abs(position.top - other.top) < size + margin) {overlaps = true;break;}}} while (overlaps);positions.push(position);}return positions;},goBack() {wx.navigateBack();}
});

二、dice.json

这里不需要填,用默认的

{"usingComponents": {}
}

三、dice.wxml

<view class="container"><image class="background" src="http://xxx/bg/login_bg2.png" mode="aspectFill" /><view class="header"><image src="http://xxx/dice/return.png" class="back-button" bindtap="goBack" /></view><view class="content"><view class="dice-container"><block wx:for="{{diceImages}}" wx:key="index"><image src="{{item}}" class="dice" style="left:{{dicePositions[index].left}}px; top:{{dicePositions[index].top}}px;" /></block></view><view class="settings"><label for="numDice">骰子个数:</label><picker mode="selector" range="{{[1, 2, 3, 4, 5, 6, 7]}}" value="{{numDice-1}}" bindchange="onInputNumDice"><view>{{numDice}}</view></picker></view><view class="buttons"><view class="button-container"><image src="http://xxx/dice/roll.png" class="button" bindtap="rollDice" /><text>投掷</text></view><view class="button-container"><image src="http://xxx/dice/open.png" class="button" bindtap="revealDice" /><text>揭开</text></view></view></view>
</view>

四、dice.wxss

.container {display: flex;flex-direction: column;align-items: center;justify-content: center;width: 100%;height: 100vh;position: relative;overflow: hidden;
}.background {position: absolute;width: 100%;height: 100%;z-index: -1;
}.header {position: absolute;top: 10px;left: 10px;
}.back-button {width: 30px;height: 30px;margin-top: 20px;margin-left: 10px;
}.content {display: flex;flex-direction: column;align-items: center;justify-content: center;height: 100%;
}.dice-container {position: relative;width: 300px; height: 300px; margin-bottom: 20px;
}.dice {width: 50px;height: 50px;position: absolute;
}.settings {display: flex;align-items: center;margin-bottom: 20px;
}.settings label {margin-right: 10px;
}.buttons {display: flex;justify-content: center;
}.button-container {display: flex;flex-direction: column;align-items: center;margin: 0 40px;
}.button {width: 50px;height: 50px;margin-bottom: 5px;
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • android compose 设置 padding 和 margin
  • 【C++ 面试 - 基础题】每日 3 题(五)
  • 在vue中实现函数防抖
  • 2-17、18 HC06蓝牙模块(meArm机械臂)
  • TCP 通信全流程分析:从连接建立到数据传输的深度探索
  • Spring Boot实用小技巧 - - 第523篇
  • openEuler软件管理
  • MyBatis 源码学习 | Day 2 | MyBatis 初始化
  • 【前端】记录各种控制台警告/bug
  • yolo中的iou是什么意思
  • 力扣高频SQL 50题(基础版)第四十题之1164. 指定日期的产品价格
  • mysql事务与索引
  • 浅谈 Spring AOP框架 (2)——Spring统一功能处理
  • 24.8.5数据结构|栈
  • vscode ssh-remote 疑似内存泄漏问题
  • 分享一款快速APP功能测试工具
  • Apache的基本使用
  • CentOS7 安装JDK
  • create-react-app做的留言板
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • JavaScript 是如何工作的:WebRTC 和对等网络的机制!
  • Java到底能干嘛?
  • Linux Process Manage
  • Linux各目录及每个目录的详细介绍
  • log4j2输出到kafka
  • Spark VS Hadoop:两大大数据分析系统深度解读
  • 闭包--闭包之tab栏切换(四)
  • 构建工具 - 收藏集 - 掘金
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 使用docker-compose进行多节点部署
  • 写给高年级小学生看的《Bash 指南》
  • 优秀架构师必须掌握的架构思维
  • 大数据全解:定义、价值及挑战
  • 带你开发类似Pokemon Go的AR游戏
  • ​LeetCode解法汇总518. 零钱兑换 II
  • ​学习笔记——动态路由——IS-IS中间系统到中间系统(报文/TLV)​
  • # 手柄编程_北通阿修罗3动手评:一款兼具功能、操控性的电竞手柄
  • (02)Cartographer源码无死角解析-(03) 新数据运行与地图保存、加载地图启动仅定位模式
  • (02)Hive SQL编译成MapReduce任务的过程
  • (33)STM32——485实验笔记
  • (pojstep1.1.1)poj 1298(直叙式模拟)
  • (差分)胡桃爱原石
  • (附源码)php投票系统 毕业设计 121500
  • (九)信息融合方式简介
  • (免费分享)基于springboot,vue疗养中心管理系统
  • (四)activit5.23.0修复跟踪高亮显示BUG
  • (循环依赖问题)学习spring的第九天
  • (已解决)什么是vue导航守卫
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • ./configure,make,make install的作用(转)
  • .NET CF命令行调试器MDbg入门(二) 设备模拟器
  • .Net FrameWork总结
  • .NET Reactor简单使用教程
  • .Net Web项目创建比较不错的参考文章
  • .Net 代码性能 - (1)