微信小程序【五】摇骰子

news2024/9/21 0:43:49

摇骰子

  • 一、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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1990746.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【iOS多线程(二)】GCD其他方法详解

GCD其他方法 dispatch_semaphore &#xff08;信号量&#xff09;什么是dispatch_semaphore(信号量)?dispatch_semaphore主要的三个方法dispatch_semaphore主要作用线程安全线程同步 dispatch_afterdispatch_time_t 两种形式 GCD 一次性代码&#xff08;只执行一次&#xff09…

电脑维修店的主题源码 简洁wordpress企业主题模版下载

简洁wordpress企业主题&#xff0c;一个简洁的电脑维修店的主题 源码下载&#xff1a;https://download.csdn.net/download/m0_66047725/89612932 更多资源下载&#xff1a;关注我。

【深度学习】TTS,CosyVoice,推理部署的代码原理讲解分享

文章目录 demo代码加载配置文件speech_tokenizer_v1.onnx(只在zero_shot的时候使用)campplus.onnx(只为了提取说话人音色embedding)`campplus_model` 的作用代码解析具体过程解析总结示意图CosyVoiceFrontEndCosyVoiceModel推理过程总体推理过程推理速度很慢: https://git…

OpenNebula-6.9.80使用介绍

目录 准备&#xff1a;给宿主机添加一块网卡 1. 创建群组 2. 创建用户 3. 创建集群 4. 创建主机 5. 安全组 6. 网络模板 7. 虚拟网络 8. 导入镜像 9. 创建虚拟机模板 10. 实例化虚拟机 11. 卸载磁盘 12. 再次实例化 13. 添加新节点 14. 虚拟机迁移 准备&…

面壁的智能开源 MiniCPM-V 2.6 边缘人工智能多模态功能与 GPT-4V 不相上下

"MiniCPM-V2.6 "是一个边缘多模态人工智能模型&#xff0c;仅拥有 80 亿个参数&#xff0c;却在单图像、多图像和视频理解任务中取得了低于 200 亿个参数的三项 SOTA&#xff08;艺术境界&#xff09;成绩&#xff0c;显著增强了边缘多模态能力&#xff0c;并与 GPT-…

python.tkinter设计标记语言(转译2-html)

TOC 前言 本文只作为笔记记录。 前文我们已经通过TinText渲染器部分和TinML获得了test.tin解释后的标记内容列表。本文&#xff0c;我们将根据这个解释结果将Tin标记转为html文件。 转为html的好处 第一&#xff0c;Tin标记语言作为一个小小小小小项目&#xff0c;光把编写…

34-《球兰》

球兰 球兰&#xff08;学名&#xff1a;Hoya carnosa&#xff08;L.f.&#xff09;R. Br&#xff09;&#xff0c;又名&#xff1a;马骝解、狗舌藤、铁脚板等&#xff0c;马利筋亚科球兰属多年生植物 。攀援灌木&#xff0c;附生于树上或石上&#xff0c;茎节上生气根。分布于云…

单链表-数据结构

一、单链表 1.结构定义 typedef struct LNode {int data;struct LNode* next; }LNode, * LinkList; 2.功能实现 ①创造结点 //创造结点 LNode* NewNode(int x) {struct LNode* ret (LNode*)malloc(sizeof(LNode));ret->data x;ret->next NULL;return ret; } ②插…

TypeError: (0 , _xxx.default) is not a function

1.首先从控制台报错信息看很让人疑惑&#xff0c;好像并没有这个函数&#xff0c;我这里是引入了address.js这个这个文件里面的函数导致的 2. 直接说原因&#xff1a;导入的函数不是default&#xff0c;但使用的时候没有使用"{}" import xxx from yyy 3.直接加上&q…

Golang | Leetcode Golang题解之第330题按要求补齐数组

题目&#xff1a; 题解&#xff1a; func minPatches(nums []int, n int) (patches int) {for i, x : 0, 1; x < n; {if i < len(nums) && nums[i] < x {x nums[i]i} else {x * 2patches}}return }

【vulnhub】Matrix:1靶机

靶机安装 下载地址&#xff1a;https://download.vulnhub.com/matrix/Machine_Matrix.zip 运行环境&#xff1a;VirtualBox 信息收集 靶机扫描 netdiscover -i eth0 -r 192.168.7.0/24 端口扫描&#xff0c;开放端口22、80、31337 nmap -A 192.168.7.203 -p- 目录扫描 d…

「C++系列」引用

文章目录 一、引用及定义引用的基本用法注意事项 二、引用与指针1. 定义和初始化2. 语法糖3. 空值4. 数组和函数5. 性能6. 用途 三、引用作为参数/返回值1. 把引用作为函数参数2. 把引用作为函数返回值 四、相关链接 一、引用及定义 在C中&#xff0c;引用&#xff08;Referen…

2025深圳国际户外用品暨跨境电商工厂选品展览会

2025深圳国际户外用品暨跨境电商工厂选品展览会 2025 Shenzhen International Outdoor Products and Cross border E-commerce Factory Selection Exhibition 时间&#xff1a;2025年02月27-3月01日 地点&#xff1a;深圳会展中心&#xff08;福田馆&#xff09; 详询主办方…

地接侠小程序(Taro)兼容IOS系统Bug解决(redux持久化不成功、整个页面会拖动)

在写地接侠小程序的时候就是有考虑过兼容问题的&#xff0c;但是在写的过程中并没有用苹果手机进行调式&#xff0c;一直都是用的自己的安卓手机&#xff0c;一直都是没有问题的&#xff0c;但是毕竟项目需要上线&#xff0c;于是在上线前用苹果手机测试果然出现了预想中的问题…

Leetcode JAVA刷刷站(3)无重复字符的最长子串

一、题目概述 二、思路方向 为了找出给定字符串中不含有重复字符的最长子串的长度&#xff0c;我们可以使用滑动窗口的方法。这种方法通过维护一个窗口&#xff08;或称为子串&#xff09;&#xff0c;并动态地调整窗口的左右边界来找到最长的无重复字符子串。 三、代码实现 …

利用自然语言处理(NLP)技术挖掘旅游评论数据

目录 简单了解 延伸 如何使用自然语言处理技术提高旅游评论情感倾向的准确性&#xff1f; 旅游评论数据中多模态信息融合的最佳实践是什么&#xff1f; 在旅游评论数据预处理和清洗过程中&#xff0c;哪些方法最有效&#xff1f; 使用Python网络爬虫技术进行旅游评论数据的…

便携式气象监测设备:精准掌握自然气象变化

在探索自然、指导农业生产、保障户外活动安全以及进行科学研究等多个领域&#xff0c;便携式气象监测设备正逐渐成为重要的工具。这些小巧而强大的设备&#xff0c;以其便携性、实时性和高精度&#xff0c;让我们能够轻松掌握周围环境的微妙变化。 便携式气象监测设备集成了多种…

35 搜索插入位置

解题思路&#xff1a; \qquad 时间复杂度要求为O(Log N)&#xff0c;可知此题用二分查找解决。 \qquad 二分基本思路是&#xff0c;通过锚定一个中间值mid&#xff0c;把搜索区间一分为二&#xff0c;根据mid与目标值的比较结果&#xff0c;更新下一次搜索区间&#xff08;左…

Java | Leetcode Java题解之第330题按要求补齐数组

题目&#xff1a; 题解&#xff1a; class Solution {public int minPatches(int[] nums, int n) {int patches 0;long x 1;int length nums.length, index 0;while (x < n) {if (index < length && nums[index] < x) {x nums[index];index;} else {x *…

华南理工大学-大学物理实验-不良导体热导率测量

不良导体热导率测量 引言 导热系数(又叫热导率)是反映材料热性能的重要物理量。热传导是热交换的三种(热传导、对流和辐射)基本形式之一,是工程热物理、材料科学、固体物理及能源、环保等各个研究领域的课题。材料的导热机理在很大程度上取决于它的微观结构,热量的传递依靠…