当前位置: 首页 > news >正文

微信小程序项目实例——扫雷

今日推荐💁‍♂️


2023许嵩演唱会即将到来🎤🎤🎤大家一起冲冲冲🏃‍♂️🏃‍♂️🏃‍♂️
在这里插入图片描述

🔮🔮🔮🔮🔮往期优质项目实例🔮🔮🔮🔮🔮
微信小程序项目实例——图片处理小工具
🔗 https://blog.csdn.net/ws15168689087/article/details/125356342
微信小程序项目实例——我有一支画笔(画画)
🔗 https://blog.csdn.net/ws15168689087/article/details/124967906
微信小程序项目实例——印记
🔗 https://blog.csdn.net/ws15168689087/article/details/124606436
微信小程序项目实例——飞机大战
🔗 https://blog.csdn.net/ws15168689087/article/details/123153607
微信小程序项目实例——狼人杀
🔗 https://blog.csdn.net/ws15168689087/article/details/123307880

文章目录

    • 今日推荐💁‍♂️
    • 1️⃣ 项目介绍 👨‍🏫
    • 2️⃣ 项目结构 👨‍💻
    • 3️⃣ 项目展示 👨‍🎨
    • 4️⃣ 结尾 👨‍🎓

🌻🌻🌻🌼🌼🌼🌺🌺🌺🌼🌼🌼🌻🌻🌻

1️⃣ 项目介绍 👨‍🏫


🎃游戏介绍:
👉《扫雷》是一款大众类的益智小游戏,于1992年发行。游戏目标是在最短的时间内根据点击格子出现的数字找出所有非雷格子,同时避免踩雷,踩到一个雷即全盘皆输。

🎯PC端扫雷:
在这里插入图片描述

📑雷诀八条:
1️⃣基本定式不要忘,现场推理真够呛。
2️⃣鼠标点击不要快,稳定节奏把空开。
3️⃣顺手标雷不要惯,积累下来记录悬。
4️⃣无从下手不要愣,就近猜雷把心横。
5️⃣遇到猜雷不要怕,爆了脸上不留疤。
6️⃣猜雷猜错不要悔,哭天抢地也白费。
7️⃣碰上好局不要慌,紧盯局部慢扩张。
8️⃣痛失好局不要恨,既然有缘定有份。

项目展示:
👉扫雷小程序借鉴经典的PC端扫雷
👉玩家可以自行设置格子数🟫和地雷数💣
👉单点是开启,长按为插旗🚩

在这里插入图片描述

🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡🤡

2️⃣ 项目结构 👨‍💻


🎃项目目录:
在这里插入图片描述

🧑‍🚀核心代码:

<!--pages/game/game.wxml-->
<view class="page">
    <view class="section btns">
        <button bindtap="newGame">重新开始</button>
        <button bindtap="handleSetting">游戏设置</button>
    </view>
    <view class="section">
        <view wx:for="{{grids}}" class="grid">
            <view wx:for="{{item}}" class="grid_cell" style="height:{{height}}rpx;">
                <view bindtouchstart="handleTouchStart" bindtouchend="handleTouchEnd" bindtap="handleClick" id="{{item}}" class="card {{cards[item].open?'open':''}}">
                    <view wx:if="{{cards[item].open}}">
                        <text wx:if="{{cards[item].boom}}" class="iconfont icon-zhadan-BOOM"></text>
                        <text wx:else>{{cards[item].num>0?cards[item].num:''}}</text>
                    </view>
                    <view wx:else>
                        <text wx:if="{{cards[item].flag}}" class="iconfont icon-qizhi" style="color:red"></text>
                    </view>
                </view>
            </view>
        </view>
    </view>
</view>

// pages/game/game.js
const app = getApp();
Page({
  data:{
    config: {
      x: 0,
      y: 0,
      n: 0
    },
    cards: [],
    grids: [],
    // arr: [],
    start: false,
    open: 0,
    touchStart:0,
    touchEnd:0,
    height: 0
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    this.setData({
      config:{
        x:app.globalData.config.x,
        y:app.globalData.config.y,
        n:app.globalData.config.n
      },
      height: 750/app.globalData.config.x      
    });
    this.newGame();
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  newGame(){
    const x = this.data.config.x;
    const y = this.data.config.y;
    const n = this.data.config.n;
    const cards = [];
    const grids = [];
    // const arr = [];
    
    for(let i=0; i<y; i++){
      grids.push([]);
      for(let j=0; j<x; j++){
        const index = i*x+j;
        cards.push({
          index:index,
          boom:false,
          num:0,
          open:false,
          flag:false
        });
        grids[i].push(index);
        // arr.push(index);
      }
    }

    this.setData({
      cards: cards,
      grids: grids,
      // arr: arr,
      start: false,
      open: x*y-n
    });
  },
  handleClick(e){
    const i = parseInt(e.currentTarget.id);
    console.log(i);

    // console.log(this.data.touchEnd);
    // console.log(this.data.touchStart);
    const touchTime = this.data.touchEnd-this.data.touchStart;
    // console.log(touchTime);
    if(touchTime>350){
      const cards = this.data.cards;
      cards[i].flag = true;
      this.setData({cards:cards});
    }else{
      !this.data.start && this.setBoom(i);
      this.handleOpen(i);
    }


  },
  handleTouchStart(e){
    this.setData({
      touchStart:e.timeStamp
    });
  },
  handleTouchEnd(e){
    this.setData({
      touchEnd:e.timeStamp
    });
  },
  handleOpen:function(i){
    // const i = e.currentTarget.id;
    // console.log(i);

    // !this.data.start && this.setBoom(i);
    // this.getNeighbor(i);

    const cards = this.data.cards;

    if(cards[i].open){
      // console.log('OPENED!!!!!!!!!!!!!!!');
      return;
    }else{
      cards[i].open = true;
      this.setData({
        cards:cards,
        open:--this.data.open
      });
      console.log(this.data.open);
      if(this.data.open==0){
        wx.showModal({
          title: 'YOU WIN!!!!!',
          showCancel: false,
          success: function(res) {
            if (res.confirm) {
              // console.log('用户点击确定')
              this.newGame();
            }
          }.bind(this)
        });
      }
    }

    if(cards[i].boom){
      // console.log('boom!!!!!!!!!!!!!!!');
      wx.showModal({
        title: 'GAME OVER',
        showCancel: false,
        success: function(res) {
          if (res.confirm) {
            // console.log('用户点击确定')
            this.newGame();
          }
        }.bind(this)
      });
    }else if(cards[i].num==0){
      const ns = this.getNeighbor(i);
      ns.forEach(function(item){
        this.handleOpen(item);
      }.bind(this));
    }



  },
  setBoom:function(j){
    const cards = this.data.cards;
    // const arr = this.data.arr;
    const num = this.data.config.n;
    const newArr = this.getNeighbor(j).concat(j);
    // arr.splice(j,1);
    // const arr = [...Array(cards.length).keys()];
    const arr = [];
    for (let index of Array(cards.length).keys()) {
      newArr.indexOf(index)==-1 && arr.push(index);
    }
    // console.log(newArr);
    // console.log(arr);

    for(let i=0; i<num; i++){
      const index = Math.floor(Math.random()*arr.length);
      cards[arr[index]].boom = true;
      newArr.push(arr[index]);
      arr.splice(index,1);
    }

    const newCards = cards.map(function(card,i){
      let n = 0;
      this.getNeighbor(i).forEach(function(neighbor){
        cards[neighbor].boom && n++;
      });
      card.num = n;
      return card;
    }.bind(this));



    this.setData({
      arr:arr.concat(newArr),
      cards:newCards,
      start: true
    });
  },
  getNeighbor:function(i){
    const x = this.data.config.x;
    const y = this.data.config.y;
    // const arr = [i-x-1,i-x,i-x+1,i-1,i+1,i+x-1,i+x,i+x+1];
    const arr = [];

    !(i<x||i%x==0) && arr.push(i-x-1);
    !(i<x) && arr.push(i-x);
    !(i<x||i%x==(x-1)) && arr.push(i-x+1);
    !(i%x==0) && arr.push(i-1);
    // arr.push(i);
    !(i%x==(x-1)) && arr.push(i+1);
    !(i>=x*(y-1)||i%x==0) && arr.push(i+x-1);
    !(i>=x*(y-1)) && arr.push(i+x);
    !(i>=x*(y-1)||i%x==(x-1)) && arr.push(i+x+1);

    // console.log(arr);
    return arr
  },
  handleSetting:function(){
    wx.navigateTo({
      url: '../setting/setting'
    })
  }
})
🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈🙈

3️⃣ 项目展示 👨‍🎨


在这里插入图片描述

🧭🧭🧭🧭🧭🧭🧭🧭🧭🧭🧭

4️⃣ 结尾 👨‍🎓


具体的介绍就到这里了💭
扫雷小程序与PC经典的扫雷玩法一致🎰
有兴趣的同学可以继续研究🔎
代码放到下面链接里了👇
👉点击下载 扫雷小程序💣

在这里插入图片描述

相关文章:

  • 一个完整的渗透学习路线是怎样的?如何成为安全渗透工程师?
  • 云上办公系统项目
  • Python自动化抖音自动刷视频
  • 基于Vue+Vue-cli+webpack搭建渐进式高可维护性前端实战项目
  • C#等高级语言运行过程
  • 人脸活体检测系统(Python+YOLOv5深度学习模型+清新界面)
  • 超详细的堆排序,进来看看吧。
  • HTTP 缓存的工作原理
  • STM32开发(九)STM32F103 通信 —— I2C通信编程详解
  • Leetcode 6322. 检查骑士巡视方案 暴力模拟法 第337场周赛,第二题
  • 蚂蚁一面面试经历
  • Spring事务和事务传播机制
  • ChatGPT加强版GPT-4面世,打工人的方式将被颠覆
  • oracle和mysql的区别
  • 指针进阶(上)
  • 【css3】浏览器内核及其兼容性
  • 2019年如何成为全栈工程师?
  • 3.7、@ResponseBody 和 @RestController
  • Android Studio:GIT提交项目到远程仓库
  • CSS实用技巧干货
  •  D - 粉碎叛乱F - 其他起义
  • gops —— Go 程序诊断分析工具
  • JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
  • JS 面试题总结
  • JWT究竟是什么呢?
  • Laravel 实践之路: 数据库迁移与数据填充
  • Linux链接文件
  • mongo索引构建
  • Netty+SpringBoot+FastDFS+Html5实现聊天App(六)
  • php中curl和soap方式请求服务超时问题
  • PV统计优化设计
  • unity如何实现一个固定宽度的orthagraphic相机
  • Xmanager 远程桌面 CentOS 7
  • 关于springcloud Gateway中的限流
  • 看图轻松理解数据结构与算法系列(基于数组的栈)
  • 聊聊springcloud的EurekaClientAutoConfiguration
  • 前端_面试
  • 如何在GitHub上创建个人博客
  • 我是如何设计 Upload 上传组件的
  • 项目实战-Api的解决方案
  • 自制字幕遮挡器
  • 【运维趟坑回忆录】vpc迁移 - 吃螃蟹之路
  • NLPIR智能语义技术让大数据挖掘更简单
  • 如何通过报表单元格右键控制报表跳转到不同链接地址 ...
  • 树莓派用上kodexplorer也能玩成私有网盘
  • ​【C语言】长篇详解,字符系列篇3-----strstr,strtok,strerror字符串函数的使用【图文详解​】
  • ​queue --- 一个同步的队列类​
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • #ubuntu# #git# repository git config --global --add safe.directory
  • $$$$GB2312-80区位编码表$$$$
  • %3cscript放入php,跟bWAPP学WEB安全(PHP代码)--XSS跨站脚本攻击
  • (4)STL算法之比较
  • (42)STM32——LCD显示屏实验笔记
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (9)目标检测_SSD的原理