WeChat applet implements a simple dice game

WeChat applet implements a simple dice game

This article shares the specific code of the WeChat applet to implement the dice game for your reference. The specific content is as follows

Page Code

<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>

Style Code

.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 code

//index.js
//Get the application instance 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:'Shake it',
    total:0,
    txt:'Congratulations, you got: 0',
    //The image material and renderings will be posted at the bottom 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:'Stop',
          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:'Shake it',
           flag:true,
           txt:'Congratulations, you won:' + obj.data.total,
      })
    }
   
  },
  
})

Picture materials and renderings

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • WeChat applet realizes the effect of shaking the sieve

<<:  How to use VirtualBox to simulate a Linux cluster

>>:  Tutorial diagram of installing MySQL service through MySQL Installer under Windows

Recommend

In-depth understanding of the matching logic of Server and Location in Nginx

Server matching logic When Nginx decides which se...

Linux completely removes node.js and reinstalls it through the yum command

first step Delete it once with the built-in packa...

Vue implements adding watermark to uploaded pictures

This article shares the specific implementation c...

Nginx reverse proxy forwards port 80 requests to 8080

Let's first understand a wave of concepts, wh...

CSS3 realizes the animation of shuttle starry sky

Result: html <canvas id="starfield"&...

Implementation of IP address configuration in Centos7.5

1. Before configuring the IP address, first use i...

Docker private warehouse harbor construction process

1. Preparation 1.1 harbor download harbor downloa...

MySQL 8.0.22 winx64 installation and configuration graphic tutorial

mysql 8.0.22 winx64 installation and configuratio...

Vue implements image drag and drop function

This article example shares the specific code of ...

MySQL 8.0.23 Major Updates (New Features)

Author: Guan Changlong is a DBA in the Delivery S...

Markup Language - Image Replacement

Click here to return to the 123WORDPRESS.COM HTML ...

MySQL database introduction: detailed explanation of database backup operation

Table of contents 1. Single database backup 2. Co...

WeChat applet learning notes: page configuration and routing

I have been studying and reviewing the developmen...