The actual process of implementing the guessing number game in WeChat applet

The actual process of implementing the guessing number game in WeChat applet

Function Introduction

The user enters a number between 1-100, and the user is prompted to guess the size based on the result. If the user guesses correctly, they can start over (the number of guesses can also be set)

Rendering

Click to start the game interface

Game rules interface

About other interfaces

My homepage (effects picture) uses index03, starting the game uses index01, game rules uses index02, and everything else uses index02

(Create the file in app.json, this is the file created by my app.json)

(Since my homepage rendering uses index03, it should be placed in front)

1. Code for the home page rendering (index03)

WXML Code

<!--pages/index03/index03.wxml-->
<view class="box2">
//url is the page I want to jump to <navigator url="../index/index">
        <button type="primary">Start the game</button>
    </navigator>
</view>
<view class="box2">
    <navigator url="../index01/index01">
        <button type="warn">Game Rules</button>
    </navigator>
</view>
<view class="box3">
    <navigator url="../index02/index02">
        <button type="default">About Others</button>
    </navigator>
</view>

WXSS Code

/* pages/index03/index03.wxss */
.box2{
  margin-top: 200rpx;
  width: 100%;
  height: 100rpx;
}
.box3{
  margin-top: 240rpx;
  width: 100%;
  height: 100rpx;
}

2. Start game page (index) code

WXML Code

<!-- index.wxml -->
<view class="demo-box">
    <form>
        <block wx:if="{{isGameStart}}">
            <input type="number" placeholder="Please enter a number between 1-100" bindinput="getNumber"></input>
            <button type='primary' form-type="reset" bindtap='guess' class="btn">Submit</button>
        </block>
        <block wx:else>
            <button type="primary" bindtap='restartGame'>Restart</button>
        </block>
    </form>
    <text id="tip">{{tip}}</text>
</view>

WXSS Code

/**index.wxss**/
input{
  border: 2rpx solid green;
  margin: 30rpx 0;
  height: 90rpx;
  /* Rounded border */
  border-radius: 20rpx; 
}
#tip{
  /* Fixed height */
  height: 800rpx;
}
.demo-box{
  height: 400rpx;
}
navigator{
  text-align: center;
}

index.js code

// index.js
 
Page({
  data: {
  
  },
  initial:function(){
    this.setData({
      // Math.round to integer //Math.random() takes a random number that is just a decimal between 0 and 1, so here we * 100 to get a random number between 0 and 100 answer:Math.round(Math.random()*100),
      // Round count: 0,
      // Tip statement tip:'',
      // User guessed number x: -1,
      // The game has started isGameStart:true
    });
    //The console prints out the system random number answer console.log("The answer is "+this.data.answer);
  },
  // Get the number entered by the user getNumber:function(e){
    this.setData({
      x : e.detail.value
    });
  },
  // Start guessing numbers in this round guess:function(){
    // Get the number filled in by the user this round let x = this.data.x;
    // Reset x to the state of not getting a new number this.setData({x:-1});
    if(x<0){
      // prompt wx.showToast({
        title: 'Cannot be less than 0',
      });
    }else if(x>100){
      wx.showToast({
        title:'cannot be greater than 100',
      });
    }else{
      // The number of rounds increases let count = this.data.count + 1;
      // Get the current tip information let tip = this.data.tip;
      // Get the correct answer let answer = this.data.answer;
 
      if(x == answer){
        tip += '\n' + count + 'round:' + x + ', guessed right!';
        // Game ends this.setData({isGameStart:false});
      }else if(x > answer){
        tip += '\n' + count + 'round:' + x + ', big!';
      }else{
        tip += '\n' + count + 'round:' + x + ', too small!';
      }
        //Count the number of rounds. Here I set the user to only guess 5 times if (count == 5) {
        tip += '\nGame over';
        this.setData({isGameStart:false});
      }
      // Update the prompt statement and round number this.setData({
        tip:tip,
        count:count
      });
    }
  },
  // Restart the game restartGame:function(){
    this.initial();
  },
  //options(Object)
  onLoad: function(options) {
    this.initial();
  }

3. Game rules page (index01) code

WXML Code

<!--pages/index01/index01.wxml-->
<view class="demo-box">
<text>
 1. The system randomly generates numbers from 1-100 for players to guess 2. Players have 5 chances 3. Players guess successfully within 5 times 4. Click Start Game to enter the interface 5. Players can start over if they guess correctly or not</text>
</view>

WXSS Code

/* pages/index01/index01.wxss */
.demo-box{
  display: flex;
   //Vertical layout flex-direction: column;
  align-items: center;
  justify-content: space-around;
  /* width: 400rpx; */
  height: 100vh;
}
text{
  margin: 0 50rpx;
   //Line height line-height: 100rpx;
}

4. About other pages (index02) code

WXML Code

<!--pages/index02/index02.wxml-->
<view class="demo-box">
<text>
 1. The game is for entertainment only. 2. This game has many shortcomings. 3. Players can provide you with valuable suggestions. 4. Players can guess according to the hints, which will be of great help.
</view>

WXSS Code

/* pages/index02/index02.wxss */
.demo-box{
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
  /* width: 400rpx; */
  height: 100vh;
}
text{
  margin: 0 50rpx;
  line-height: 100rpx;
}

(The rules of the game are the same as the codes on the other two pages. This is for reference only. There are still many shortcomings.)

Summarize

This is the end of this article about how to implement the guessing number game in WeChat Mini Program. For more relevant content about the guessing number game in WeChat Mini Program, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • WeChat Mini Program Version of Flip Card Game
  • WeChat applet implements puzzle game

<<:  Html+css to achieve pure text and buttons with icons

>>:  Summary of experience in using div box model

Recommend

A brief discussion on the problem of forgotten mysql password and login error

If you forget your MySQL login password, the solu...

HTML+VUE paging to achieve cool IoT large screen function

Effect demo.html <html> <head> <me...

Example of how to implement value transfer between WeChat mini program pages

Passing values ​​between mini program pages Good ...

MySQL fuzzy query usage (regular, wildcard, built-in function)

Table of contents 1. MySQL wildcard fuzzy query (...

Vue integrates Tencent TIM instant messaging

This article mainly introduces how to integrate T...

CentOS7 firewall and port related commands introduction

Table of contents 1. Check the current status of ...

WeChat applet realizes linkage menu

Recently, in order to realize the course design, ...

MySQL learning tutorial clustered index

Clustering is actually relative to the InnoDB dat...

Summary of several principles that should be followed in HTML page output

1. DOCTYPE is indispensable. The browser determin...

Web design dimensions and rules for advertising design on web pages

1. Under 800*600, if the width of the web page is...

Vue achieves the top effect through v-show

html <div class="totop" v-show="...

Element-ui directly clicks on the cell in the table to edit

Table of contents Achieve results Implementation ...

Detailed example code of mysql batch insert loop

background A few days ago, when I was doing pagin...