翻牌闯关游戏

news2024/7/6 19:12:56

翻牌闯关游戏


3关:关卡由少至多12格、20格、30格
图案:12个
玩法:点击两张卡牌,图案一到即可消除掉

记忆时长(毫秒):memoryDurationTime:5000
可配置,默认5000

提示游戏玩法:showTipsFlag:1
可配置,1:判断localStorage值,仅一次提示游戏玩法 2:每次游戏第一关(12格关卡)都提示游戏玩法

提示游戏玩法 时长(毫秒):showTipsTime:4100
可配置,默认4100,注:设置的引导手势动画2s

是否需要计时:ifNeedTime:2
可配置,0:不需要计时; 1:正计时; 2:倒计时

倒计时开始时间(秒):baseTime:[20,60,90]
可配置,第N关卡(baseTime下标对应值)对应的秒数

开始游戏(根据闯关情况自动设置对应关卡):
this.gameRun()

当前关卡闯关游戏结束:
this.gameOver()

游戏组件页代码:

<template>
  <div v-show="showGame" class="page game">
    <div class="game_body">
      <div class="game_time">
        <span>倒计时:</span>
        <span>{{curTime}}s</span>
      </div>
      <div v-if="isStartGame" class="game_box">
        <ul class="game_list" :class="{'first_level':levelStartNum == 0,'second_level':levelStartNum == 1,'third_level':levelStartNum == 2}">
          <li @click="chooseBrand(index)" v-for="item,index in currLevelData" :key="index">
            <img :class="clearBrandArr.indexOf(index) == -1 && inGame && (currBrandIndex1 !== index && currBrandIndex2 !== index) ? 'show' : 'hide'" src="@/assets/img/game_3.png" />
            <img :class="!inGame || (currBrandIndex1 === index || currBrandIndex2 === index) ? 'show' : 'hide'" :src="item" />
          </li>
        </ul>
      </div>
    </div>
    <!-- 提示游戏玩法 -->
    <div v-show="showTips" class="game_tips">
      <div class="game_tips_body">
        <img src="@/assets/img/game_5.png" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return{
      // 翻牌对应的图片(12张)
      gameBrandList:[
        require('@/assets/img/game/1.png'),
        require('@/assets/img/game/2.png'),
        require('@/assets/img/game/3.png'),
        require('@/assets/img/game/4.png'),
        require('@/assets/img/game/5.png'),
        require('@/assets/img/game/6.png'),
        require('@/assets/img/game/7.png'),
        require('@/assets/img/game/8.png'),
        require('@/assets/img/game/9.png'),
        require('@/assets/img/game/10.png'),
        require('@/assets/img/game/11.png'),
        require('@/assets/img/game/12.png'),
      ],
      LevelNum:[12,20,30], // 第N关卡对应的格子数
      levelStartNum:0, // 第N关卡 从0开始
      currLevelData:[], //当前关卡 翻牌对应的图片
      showGame:false,
      inGame:false, //进入游戏页面后,是否用户可点击参与了
      isStartGame:false,
      currBrandIndex1:'', //每两次点击第1次选择的格子index
      currBrandIndex2:'', //每两次点击第2次选择的格子index
      clearBrandArr:[], //已清除的格子
      memoryDurationTime:5000, //记忆时长(毫秒)

      showTips:false, //是否提示游戏玩法
      showTipsEnd:false, //是否提示游戏玩法 展示结束
      showTipsFlag:1, //1:判断localStorage值,仅一次提示游戏玩法 2:每次游戏第一关都提示游戏玩法
      // showTipsFuncName:'setTimeRun', //在什么时候提示游戏玩法 setTimeRun:牌面已经全部翻转反面未开始计时 goInGame:牌面未全部翻转反面时
      showTipsFuncName:'goInGame', //在什么时候提示游戏玩法 setTimeRun:牌面已经全部翻转反面未开始计时 goInGame:牌面未全部翻转反面时
      showTipsTime:4100, //提示游戏玩法 时长(毫秒)


      ifNeedTime:2, //0:不需要计时; 1:正计时; 2:倒计时
      baseTime:[20,60,90], //倒计时开始时间,秒 第N关卡(baseTime下标对应值)对应的秒数
      timer:null, //定时器
      curTime:'-', //当前 用时 或 倒计时
      timing:0,
      rafId:'',
      isClearAll:false, //清除完毕当前关卡闯关游戏结束 用于结束计时
      gameEnd:false, //倒计时结束,当前关卡闯关游戏结束
    }
  },
  mounted() {
    // this.gameRun();
    this.$emit('flipBrandMounted');
  },
  watch:{
    showTipsEnd:function(newV,oldV){
      if(newV){
        if(this.showTipsFuncName == 'setTimeRun'){
          this.setTimeRun();
        }
        if(this.showTipsFuncName == 'goInGame'){
          this.goInGame();
        }
      }
    }
  },
  methods:{
    // 倒计时
    showTime(){
      this.curTime--;
      // 计时结束
      if(this.curTime == 0){
        clearInterval(this.timer);
        this.gameEnd = true;
        if(!this.isClearAll){
          this.gameOver();
        }
      }
      // 清除完毕当前关卡闯关游戏结束
      if(this.isClearAll == true){
        clearInterval(this.timer);
      }
    },
    // 计时
    changeTime(k){
      // console.log(k);
      if(!this.timing && k){
        this.timing = k
      }
      this.rafId = requestAnimationFrame(this.changeTime);
      this.curTime = ((k - this.timing) / 1000).toFixed(2);
      // console.log(this.curTime);
      this.$nextTick(()=>{
        // 清除完毕当前关卡闯关游戏结束
        if(this.isClearAll == true){
          cancelAnimationFrame(this.rafId);
        }
      })
    },
    // 开始计时
    setTimeRun(){
      this.showTipsDo('setTimeRun',(flag)=>{
        if(flag){
          if(this.ifNeedTime == 1){
            this.timing = 0;
            this.changeTime();
          }
          if(this.ifNeedTime == 2){
            this.curTime = this.baseTime[this.levelStartNum];
            this.timer ? clearInterval(this.timer) : '';
            this.timer = setInterval(this.showTime,1000);
          }
        }
      })
    },

    // 提示游戏玩法 逻辑处理
    showTipsDo(funcName,callback){
      console.log(funcName , this.showTipsFuncName)
      if(funcName != this.showTipsFuncName){
        callback && callback(true);
        return;
      }
      // 游戏开始前 提示游戏玩法
      if((this.showTipsFlag == 1 && !localStorage.getItem('isShowTips')) || (this.showTipsFlag == 2 && this.levelStartNum == 0 && !this.showTipsEnd)){
        this.showTips = true;
        // 一轮手势引导动画2s
        setTimeout(()=>{
          this.showTips = false;
          this.showTipsEnd = true;
          if(this.showTipsFlag == 1) localStorage.setItem('isShowTips',1)
        },this.showTipsTime)
        callback && callback(false);
      }else{
        callback && callback(true);
      }
    },

    // 开始游戏
    gameRun(){
      // 闯关结束后再继续下一关
      if(this.gameEnd || this.isClearAll){
        if(this.isClearAll){
          // 闯关成功
          if(this.levelStartNum < this.LevelNum.length - 1){
            this.levelStartNum++;
          }else{
            // 全部关卡闯关成功,重新第1关再开始
            this.levelStartNum = 0;
          }
        }else{
          // 闯关失败
        }
      }
      this.gameEnd = false;
      this.isClearAll = false;
      this.$nextTick(()=>{
        this.gameStart();
      })
    },

    // 当前关卡闯关游戏结束
    gameOver(){
      this.$emit('gameOver',this.isClearAll,this.gameEnd,this.levelStartNum);
    },

    // 初始化游戏 当前关卡 翻牌对应的图片
    gameStart(){
      this.isStartGame = false;
      this.inGame = false;
      this.currBrandIndex1 = '';
      this.currBrandIndex2 = '';
      this.clearBrandArr = [];
      this.currLevelData = [];

      let currBrandList = [].concat(this.gameBrandList).sort(function(a, b){
        return 0.5 - Math.random();
      });
      let currLevelLen = this.LevelNum[this.levelStartNum];
      let currLevelData = [];
      for(let i = 0; i < currLevelLen / 2; i++){
        if(i > this.gameBrandList.length - 1){
          // 12个牌子不够双倍匹配
          let randomKey = Math.floor(Math.random() * this.gameBrandList.length);
          currLevelData[i] = currBrandList[randomKey];
          currLevelData[currLevelLen - 1 - i] = currBrandList[randomKey];
        }else{
          currLevelData[i] = currBrandList[i];
          currLevelData[currLevelLen - 1 - i] = currBrandList[i];
        }
      }
      this.currLevelData = currLevelData.sort(function(a, b){
        return 0.5 - Math.random();
      });
      this.$nextTick(()=>{
        this.showGame = true;
        this.isStartGame = true;
        this.goInGame();
      })
    },
    // 用户可以去点击清除操作了
    goInGame(){
      this.showTipsDo('goInGame',(flag)=>{
        if(flag){
          setTimeout(()=>{
            this.inGame = true;
            this.setTimeRun();
          },this.memoryDurationTime)
        }
      })
    },
    // 点击格子
    chooseBrand(index){
      // 倒计时结束不可点击
      if(this.gameEnd){
        console.log('倒计时结束不可点击');
        return;
      }
      if(!this.inGame || this.currBrandIndex1 === index || (this.currBrandIndex1 && this.currBrandIndex2) || this.clearBrandArr.indexOf(index) !== -1){
        return;
      }
      if(this.currBrandIndex1 !== ''){
        this.currBrandIndex2 = index;
        if(this.currLevelData[index] == this.currLevelData[this.currBrandIndex1] && index != this.currBrandIndex1){
          // 两点击一样
          this.clearBrand(this.currBrandIndex1,index);
        }else{
          // 两点击不一样
          setTimeout(()=>{
            this.currBrandIndex1 = '';
            this.currBrandIndex2 = '';
          },300)
        }
      }else{
        this.currBrandIndex1 = index;
      }
    },
    // 两次点击相同清除格子
    clearBrand(index,index2){
      setTimeout(()=>{
        this.currBrandIndex1 = '';
        this.currBrandIndex2 = '';
        this.clearBrandArr.push(index,index2);
        this.$nextTick(()=>{
          if(this.clearBrandArr.length == this.LevelNum[this.levelStartNum]){
            // 清除完毕
            this.isClearAll = true;
            // this.gameRun();
            if(!this.gameEnd){
              this.gameOver();
            }
          }
        })
      },300)
    },

  }
}
</script>

<style scoped>
.page{ width:100%; height:100%; position:absolute; left:0; top:0; overflow: hidden;}

/* 游戏页 */
.page.game{ background-color: #fff;}
.game_body{ height: 1104px; padding-top: 60px; background: url(../assets/img/game_1.png) no-repeat center top; background-size: 100%; position: relative;}
.game_body::after{ content: ""; width: 100%; min-height: calc(100vh - 1104px); height: 396px; background: url(../assets/img/game_2.png); background-size: 100% 100%; position: absolute; left: 0; top: 1102px;}
.game_time{ height: 62px; line-height: 53px; color: #FFFDF4; font-size: 36px; font-weight: bold;
  display: flex; justify-content: center; align-items: center;
  text-shadow: 2px 2px 0 #895F41 , 2px 2px 0 #895F41 ,  -2px -2px 0 #895F41 , -2px -2px 0 #895F41 ,  2px -2px 0 #895F41 , 2px -2px 0 #895F41 ,  -2px 2px 0 #895F41 , -2px 2px 0 #895F41;
}
.game_box{ height: 940px; margin-top: 42px;}
.game_list{
  display: flex; flex-direction: row; flex-wrap: wrap; justify-content: flex-start; align-items: flex-start;
}
.game_list li img{ width: 100%; height: 100%; position: absolute; left: 0; top: 0;
  transition: all .2s linear;
}
.game_list li img.show{
  transform: scaleX(1);
}
.game_list li img.hide{ display: block !important;
  transform: scaleX(0);
}
/* 12格 */
.game_list.first_level{ padding: 0 98px;}
.game_list.first_level li{ width: 168px; height: 222px; position: relative;
  animation: fadeToDown .5s linear both;
}
.game_list.first_level li:nth-child(3n + 2){ margin: 0 calc((100% - 168px * 3) / 2);}
.game_list.first_level li:nth-child(3) ~ li{ margin-top: 15px;}
/* 20格 */
.game_list.second_level{ padding: 0 82px;}
.game_list.second_level li{ width: 131px; height: 173px; position: relative;
  animation: fadeToDown .5s linear both;
}
.game_list.second_level li{ margin-left: calc((100% - 131px * 4) / 3);}
.game_list.second_level li:nth-child(4n + 1){ margin-left: 0;}
.game_list.second_level li:nth-child(4) ~ li{ margin-top: 10px;}
/* 30格 */
.game_list.third_level{ padding: 0 71px;}
.game_list.third_level li{ width: 108px; height: 143px; position: relative;
  animation: fadeToDown .5s linear both;
}
.game_list.third_level li{ margin-left: calc((100% - 108px * 5) / 4);}
.game_list.third_level li:nth-child(5n + 1){ margin-left: 0;}
.game_list.third_level li:nth-child(5) ~ li{ margin-top: 8px;}

/* 提示游戏玩法 */
.game_tips{ width: 100%; height: 100%; padding-top: 438px; background-color: rgba(0,0,0,.6); position: absolute; left: 0; top: 0;}
.game_tips_body{ width: 479px; height: 365px; margin: 0 auto; background: url(../assets/img/game_4.png); background-size: 100% 100%; position: relative;}
.game_tips_body img{ width: 128px; position: absolute; left: 117px; top: 96px;
  transform-origin: 35px 32px;
  animation: gameTips 2s linear both infinite;
}
@keyframes gameTips{
  0%{ transform: translate(0,0) scale(1.15);}
  15%,30%{ transform: translate(0,0) scale(1);}
  45%{ transform: translate(166px,0) scale(1.15);}
  60%,100%{ transform: translate(166px,0) scale(1);}
}
</style>

父组件中引用:


HTML:

<flipBrand ref="gameTemp" @flipBrandMounted="flipBrandLoaded" @gameOver="gameOverEnd"></flipBrand>

JS:

methods:{
  // 游戏组件加载完毕
  flipBrandLoaded(){
    // 开始闯关
    // this.$refs.gameTemp.gameRun();
  },
  // 当前关卡闯关游戏结束
  gameOverEnd(isClearAll,gameEnd,levelStartNum){
    // isClearAll:'', //清除完毕游戏结束 正计时时用于结束计时
    // gameEnd:'', //倒计时结束,游戏结束
    // levelStartNum 当前关卡数
    this.levelStartNum = levelStartNum;
    this.$nextTick(()=>{
      this.gameOver(isClearAll);
    })
  },
  // 开始游戏
  gameStartDo(){
    this.$refs.gameTemp.gameRun();
  },
  // 游戏结束接口
  gameOver(isClearAll){
    if(isClearAll){
      // 闯关成功
    }
  },
}

图片资源:

 game_1.png

game_2.png

 

game_3.png

game_4.png

game_5.png

 

game/1.png 至 game/12.png

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1015853.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

算法宝典2——Java版本(此系列持续更新,这篇文章目前3道)(有题目的跳转链接)(此份宝典包含了二叉树的算法题)

注&#xff1a;由于字数的限制&#xff0c;我打算把算法宝典做成一个系列&#xff0c;一篇文章就20题&#xff01;&#xff01;&#xff01; 目录 一、二叉树的算法题&#xff08;目前3道&#xff09; 1. 平衡二叉树&#xff08;力扣&#xff09; 2. 对称二叉树&#xff0…

合肥先进光源国家重大科技基础设施项目及配套工程启动会纪念

合肥先进光源国家重大科技基础设施项目及配套工程启动会纪念 卡西莫多 合肥长丰岗集里 肥鸭从此别泥塘 先平场地设围栏 进而工地筑基忙 光阴似箭指日争 源流汇智山水长 国器西北扩新地 家校又添新区园 重器托举有群力 大步穿梭两地间 科教兴邦大国策 技术盈身坦荡行…

Sourcetree 无法打开/闪退问题

Sourcetree在某次开机以后无法打开或者是闪退。 Sourcetree是一款Git的可视化图形管理界面,提供了Windows和Mac的免费Git客户端,很方便的管理项目的代码版本 出现问题的环境 win11&#xff0c;sourcTree版本&#xff1a;3.4.12.0 在开始菜单搜索sourcetree&#xff0c;打开…

Golang中的GMP调度模型

GMP调度模型 Golang调度器的由来 单进程时代不需要调度器 1.单一的执行流程&#xff0c;计算机只能一个任务一个任务处理。 2.进程阻塞所带来的CPU时间浪费。 后来操作系统就具有了最早的并发能力&#xff1a;多进程并发&#xff0c;当一个进程阻塞的时候&#xff0c;切换…

polygon yolo

[1] : github: https://github.com/HRan2004/Yolo-ArbPolygon [2] https://github.com/XinzeLee/PolygonObjectDetection [3] https://github.com/AlbinZhu/yolov7-polygon-detection 链接&#xff1a;https://pan.baidu.com/s/1Zpl1bIGfMli6p5LQdbET0w?pwddw2b 提取码&#…

C++之std::holds_alternative与std::get应用实例(二百一十九)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 人生格言&#xff1a; 人生…

Peppertype:AI写作工具

【产品介绍】 名称 Peppertype 具体描述 Peppertype是一款人工智能AI内容生成工具&#xff0c;它通过使用机器学习来理解用户的需求。适合在写作过程中寻求帮助的作家&#xff0c;而无需花时间自己学习如何使用。Peppertypeai支持快速制作电子邮件&#xff0c;30…

nginx 全相联结构的引申

修改 nginx 纯属巧合&#xff0c;任务是将 reuseport 的支持换一种方式。目前 nginx 的方式是 master 创建 worker 数量个 reuseport listening socket&#xff0c;由 worker 继承。在这种实现方式下&#xff0c;效果是 “所有 worker 可以处理所有 listening socket” 这不就是…

安卓内部存储不需要申请权限,外部文件需要申请权限

内部存储和外部存储的访问权限区别&#xff1a; 内部路径&#xff1a;/data/user/0/com.xxx.xxx/ getExternalFilesDir可以获取到属于 App 自身的文件路径&#xff0c;通常是~/Android/data/<package-name>/**/。在该目录中读写文件均不需要申请权限,随着APP卸载就会删…

华为云云耀云服务器L实例评测|Git 私服搭建指南

前言 本文为华为云云耀云服务器L实例测评文章&#xff0c;测评内容是 云耀云服务器L实例 Git 私有服务器搭建指南 系统配置&#xff1a;2核2G 3M Ubuntu 20.04 我们平时在使用代码托管服务的时候&#xff0c;可能某些代码托管平台对成员有限制&#xff0c;或是由于内容原因会对…

自动编码器

Autoencoder is designed in a way to perform task of data encoding plus data decoding to reconstruct input. -- Anushka Jain 前言 两兄弟 N.Coder 和 D.Coder 经营着一家艺术画廊。一周末&#xff0c;他们举办了一场特别奇怪的展览&#xff0c;因为它只有一面墙&#x…

nginx中sent_timeout属性使用注意事项

send_timeout使用注意事项 send_timeout:指客户端向服务器发送请求并且等待服务器返回数据的时间&#xff0c;超过这个时间链接就断开。如果咱们返回的数据复杂&#xff0c;很耗时&#xff0c;就将该值设置大些。注意该时间指准备过程&#xff0c;不是传输过程&#xff08;下载…

在线海报图片设计器、图片编辑器源码/仿照稿定设计源码

在线海报设计系统素材设计源码是一个漂亮且功能强大的在线海报图片设计器&#xff0c;仿照稿定设计而成。该系统适用于多种场景&#xff0c;包括海报图片生成、电商分享图、文章长图、视频/公众号封面等。用户无需下载软件&#xff0c;即可轻松实现创意&#xff0c;迅速完成排版…

7-38 掉入陷阱的数字

输入样例: 5 输出样例: 1:16 2:22 3:13 4:13 ACcode: #include <bits/stdc.h>using namespace std;int main(){int n;cin >> n;vector<int> ans;int limit 1;ans.push_back(n);for(int i0; i<limit; i){//各位数字的和int sum 0;int num ans[i];w…

独立开发了一款Material3风格的RSS阅读器 - Agr Reader

截图 背景&#x1f4d6; 在之前接触到RSS后&#xff0c;发现RSS真是一个十分不错的信息聚合的方式&#xff0c;虽然现在看来RSS的时代已经开始落幕&#xff0c;但是至少目前还是处于能用的阶段。 在我用了Android上好几个RSS阅读App后&#xff0c;发现很多在全文解析方面不是…

栈与队列经典题目——用栈实现队列

上篇文章对栈和队列的一个经典题目——Leetcode.225-用队列实现栈进行讲解。本篇文章将对另一个题目Leetcode.232-用栈实现队列进行讲解 1. Leetcode.232——用栈实现队列&#xff1a; 题目如下&#xff1a; 1.1 大体思路分析&#xff1a; 题目要求需要实现下列函数所表示的…

JAVA注解总结

总结一下java注解。 元注解 元注解也是一种注解。元注解的作用就是来限制和定义一个普通的注解。 注解的语法 public(可选) interface 注解名称{ 具体的参数 } 注解里面的参数 第一个表示参数的类型是什么&#xff0c;类型后面可以跟[],表示数组&#xff0c;在后面就是参数…

详解机器视觉性能指标相关概念——混淆矩阵、IoU、ROC曲线、mAP等

目录 0. 前言 1. 图像分类性能指标 1.1 混淆矩阵(Confusion Matrix) 1.2 准确率(Precision) 1.3 召回率(Recall) 1.4 F1值(F1 score) 1.5 ROC曲线(接收者工作特征曲线&#xff0c;Receiver Operating Characteristic curve) 1.6 mAP(mean Average Precision) 2. 图像分…

Apereo CAS反序列化漏洞中数据加解密研究

Apereo CAS反序列化漏洞中数据加解密研究 0x01、简介0x02、网上获取资料0x03、初步运行失败1、分析&#xff1a;2、Tips&#xff1a; 0x04、分析原因1、自己写解密算法 / 直接使用cas工程的相关jar包、java文件&#xff0c;调用解密函数2、为什么会解密失败&#xff1f; 0x05、…

企业级数据仓库-数仓实战

数仓实战 安装包大小 安装清单 环境搭建 一、环境搭建01&#xff08;机器准备&#xff09; 准备好三台虚拟机&#xff0c;并进行修改hostname、在hosts文件增加ip地址和主机名映射 。 1、设置每个虚拟机的hostname vi /etc/sysconfig/network 修改HOSTNAMEnode02修改hostna…