微信小程序之猜数字和猜拳小游戏

目录

效果图

app.json

一、首页(index3)的代码

wxml代码

wxss代码

二、猜数字页面(index)代码

wxml代码

wxss代码

js代码

三.游戏规则页面(logs)代码

wxml代码

wxss代码

四.猜拳页面(q1)代码

wxml代码

wxss代码

js代码


效果图

app.json

一、首页(index3)的代码

wxml代码

html 复制代码
<view class="box2">
    <navigator url="../index/index">
        <button type="default">猜数字</button>
    </navigator>
</view>
<view class="box2">
    <navigator url="../logs/logs">
        <button type="primary">游戏规则</button>
    </navigator>
</view>
<view class="box3">
    <navigator url="../q1/q1">
        <button type="default">猜拳</button>
    </navigator>
</view>

wxss代码

html 复制代码
.box2{
  margin-top: 200rpx;
  width: 100%;
  height: 100rpx;
}
.box3{
  margin-top: 240rpx;
  width: 100%;
  height: 100rpx;
}

二、猜数字页面(index)代码

wxml代码

html 复制代码
<view class="container">
  <input type="number" placeholder="输入1到100的数字" bindinput="onInputChange"/>
  <button bindtap="makeGuess">猜数字</button>
  <text>{{message}}</text>
  <view>
    <text>猜数字历史:\n</text>
    <block wx:for="{{guesses}}" wx:key="*this">
      <text>第{{index + 1}}次: {{item.num}} - {{item.hint}}\n</text>
    </block>
  </view>
</view>

wxss代码

html 复制代码
.container {
  padding: 20px;
}

input {
  width: 100%;
  border: 1px solid #ccc;
  padding: 10px;
  margin-bottom: 10px;
}

button {
  width: 100%;
  background-color: #1aad19;
  color: white;
  padding: 10px;
  border: none;
}

.history {
  margin-top: 20px;
}

.history text {
  display: block; 
}

js代码

javascript 复制代码
Page({
  data: {
    numberToGuess: null,
    guesses: [],
    guessCount: 0,
    inputNumber: '',
    message: '',
    hint: ''
  },

  onLoad: function() {
    this.startNewGame();
  },

  startNewGame: function() {
    const randomNum = Math.round(Math.random() * 100) + 1;
    this.setData({
      numberToGuess: randomNum,
      guesses: [],
      guessCount: 0,
      message: '开始猜数字吧!',
      hint: ''
    });
  },

  onInputChange: function(e) {
    this.setData({
      inputNumber: e.detail.value
    });
  },

  makeGuess: function() {
    const guess = parseInt(this.data.inputNumber, 10);
    if (isNaN(guess) || guess < 1 || guess > 100) {
      this.setData({ message: '请输入1到100之间的数字。' });
      return;
    }

    let newHint = '';
    if (guess === this.data.numberToGuess) {
      newHint = '猜对了!';
      this.setData({
        message: '恭喜你猜对了!游戏即将重新开始。',
        hint: newHint
      });
      setTimeout(() => {
        this.startNewGame();
      }, 2000);
    } else {
      newHint = guess < this.data.numberToGuess ? '猜小了!' : '猜大了!';
      this.setData({
        message: '继续猜...',
        hint: newHint
      });
    }

    const count = this.data.guessCount + 1;
    const guesses = this.data.guesses.concat({ num: guess, hint: newHint });

    this.setData({
      guessCount: count,
      guesses: guesses
    });

    if (count >= 5) {
      this.setData({
        message: '游戏结束,即将跳转...'
      });
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/index3/index3' 
        });
      }, 2000);
    }
  }
});

三.游戏规则页面(logs)代码

wxml代码

html 复制代码
<view class="demo-box">
<text>
 1.游戏仅供娱乐
 2.此游戏有很多不足
 3.玩家可以提供您宝贵意见
</text>
</view>

wxss代码

html 复制代码
.demo-box{
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
  height: 100vh;
}
text{
  margin: 0 50rpx;
  line-height: 100rpx;
}

四.猜拳页面(q1)代码

wxml代码

html 复制代码
<view class="container">
  <button bindtap="makeChoice" data-choice="scissors">剪刀</button>
  <button bindtap="makeChoice" data-choice="rock">石头</button>
  <button bindtap="makeChoice" data-choice="paper">布</button>
  <text>玩家胜利次数:{{playerWins}}</text>
  <text>电脑胜利次数:{{computerWins}}</text>
  <text>平局次数:{{draws}}</text>
  <text>{{message}}</text>
</view>

wxss代码

html 复制代码
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

button {
  margin: 10px;
}

js代码

javascript 复制代码
Page({
  data: {
    playerWins: 0,
    computerWins: 0,
    draws: 0,
    message: ''
  },

  makeChoice: function(event) {
    const playerChoice = event.currentTarget.dataset.choice;
    const choices = ['scissors', 'rock', 'paper'];
    const computerChoice = choices[Math.round(Math.random() * choices.length)];
    const result = this.judge(playerChoice, computerChoice);

    if (result === 'win') {
      this.setData({
        playerWins: this.data.playerWins + 1,
        message: '你赢了这一轮!'
      });
    } else if (result === 'lose') {
      this.setData({
        computerWins: this.data.computerWins + 1,
        message: '电脑赢了这一轮!'
      });
    } else {
      this.setData({
        draws: this.data.draws + 1,
        message: '这一轮是平局!'
      });
    }

    this.checkGameEnd();
  },

  judge: function(player, computer) {
    if (player === computer) {
      return 'draw';
    }
    if (
      (player === 'scissors' && computer === 'paper') ||
      (player === 'rock' && computer === 'scissors') ||
      (player === 'paper' && computer === 'rock')
    ) {
      return 'win';
    }
    return 'lose';
  },

  checkGameEnd: function() {
    if (this.data.playerWins === 2 || this.data.computerWins === 2) {
      wx.navigateTo({
        url: '/pages/index3/index3' 
      });
    }
  }
});
相关推荐
彤银浦15 分钟前
python学习记录7
python·学习
少女忧16 分钟前
51单片机学习第六课---B站UP主江协科技
科技·学习·51单片机
邓校长的编程课堂1 小时前
助力信息学奥赛-VisuAlgo:提升编程与算法学习的可视化工具
学习·算法
missmisslulu2 小时前
电容笔值得买吗?2024精选盘点推荐五大惊艳平替电容笔!
学习·ios·电脑·平板
yunhuibin2 小时前
ffmpeg面向对象——拉流协议匹配机制探索
学习·ffmpeg
hengzhepa2 小时前
ElasticSearch备考 -- Search across cluster
学习·elasticsearch·搜索引擎·全文检索·es
蜡笔小新星3 小时前
Python Kivy库学习路线
开发语言·网络·经验分享·python·学习
说私域3 小时前
社群团购中的用户黏性价值:以开源小程序多商户AI智能名片商城源码为例
人工智能·小程序
攸攸太上3 小时前
JMeter学习
java·后端·学习·jmeter·微服务
Ljubim.te4 小时前
Linux基于CentOS学习【进程状态】【进程优先级】【调度与切换】【进程挂起】【进程饥饿】
linux·学习·centos