返回

boredom is also a kind of productivity

前端

boredom is also a kind of productivity

Recently, I've been feeling not only bored but also irritated. When I'm in this state, there are only three things that can make me feel happy: going out with someone, playing games, or writing code. The first two are not feasible due to the current situation, so I can only resort to the third one. Moreover, at this time, I can always write something I want to do with code.

Mini programs. I wrote a very simple one a long time ago. At that time, I felt that the native SDK of WeChat mini programs could meet most of my requirements, so I made this one.

The function is very simple. When you feel bored, open the mini program and click the start button. Then the mini program will generate a random number between 1 and 100. If you guess the number within 10 times, you win.

The game is very simple, but it can indeed kill time and make me feel happy. When I'm bored and irritable, I often play this game for a while.

In addition to killing time, this mini program can also be used to practice your programming skills. If you are a beginner in programming, you can try to implement this mini program by yourself. The code is very simple, and you can learn a lot from it.

Code implementation

The code implementation of this mini program is very simple. You can refer to the following code:

Page({
  data: {
    number: 0,
    times: 10,
    result: '',
  },
  onLoad() {
    this.generateNumber();
  },
  generateNumber() {
    this.setData({
      number: Math.floor(Math.random() * 100) + 1,
    });
  },
  onInput(e) {
    const value = e.detail.value;
    if (value === this.data.number) {
      this.setData({
        result: '猜中了!',
      });
    } else if (this.data.times > 1) {
      this.setData({
        times: this.data.times - 1,
      });
    } else {
      this.setData({
        result: '猜错了!',
      });
    }
  },
});

Summary

This is a very simple mini program that can be used to kill time and practice your programming skills. The code implementation is very simple, and you can learn a lot from it. If you are interested, you can try to implement it yourself.