微信小程序【五】摇骰子

效果简述:点击设置“骰子个数”,喝一杯前,先摇一摇。
骰子图片命名示例: 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;
}

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sinysama

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

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

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

打赏作者

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

抵扣说明:

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

余额充值