当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js实现翻牌小游戏

js实现翻牌小游戏

2020年08月16日  | 移动技术网IT编程  | 我要评论
本文实例为大家分享了js实现翻牌小游戏的具体代码,供大家参考,具体内容如下效果图需求分析1、生成两组顺序随机的1-8数据2、卡片需要有翻转效果3、两次翻转数据不相等,回复原状4、两次翻转数据相等,卡片

本文实例为大家分享了js实现翻牌小游戏的具体代码,供大家参考,具体内容如下

效果图

需求分析

1、生成两组顺序随机的1-8数据
2、卡片需要有翻转效果
3、两次翻转数据不相等,回复原状
4、两次翻转数据相等,卡片相等,不能再被点击
5、当所有卡片不能被点击游戏结束
6、限制最大点击次数50次

html结构

<div class="wrap">
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
 <div>
  <p class="top"></p>
  <p class="bottom"></p>
 </div>
</div>

css布局

实现卡片翻转效果需要3个关键属性

1.perspective: 1000px; 透视深度,形成3d视角
2.transform: rotatey(180deg);旋转
3.backface-visibility: hidden; 元素背面不可见

* {
  margin: 0;
  padding: 0;
 }

 body,
 html {
  height: 100%;
 }

 .wrap {
  perspective: 1000px;
  width: 320px;
  height: 320px;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  display: flex;
  justify-content: center;
  text-align: center;
  flex-wrap: wrap;
 }

 .wrap div {
  /* box-shadow: 5px 5px 10px #000; */
  transition: 1s;
  position: relative;
  text-align: center;
  line-height: 60px;
  height: 60px;
  width: 60px;
  border-radius: 10px;

  margin: 10px 10px;
 }

 .wrap div .active1 {
  transform: rotatey(180deg);
 }

 .wrap div .active2 {
  transform: rotatey(360deg);
 }

 .wrap p {
  border-radius: 10px;
  background-color: blueviolet;
  transition: 1s;
  backface-visibility: hidden;
  display: block;
  height: 60px;
  width: 60px;
  position: absolute;
  /* transform: rotatey(180deg); */
 }

 .wrap .bottom {
  transform: rotatey(180deg);
 }
 h3{
  text-align: center;
  width: 200px;
  height: 30px;
  margin: 150px auto;
 }

js

class game {
  constructor(selector) {
  this.init(selector);
  }
  init(selector) {
  let arr = this.randomarr();
  this.nodes = [...document.queryselectorall(selector)];//获取每一个卡片
  this.step = 50;//定义可以被点击的次数
  this.tit = document.queryselector("h3");
  this.nodes.foreach((item,index) => {//初始化卡片的值,并保存在num属性中
   item.children[1].innerhtml = arr[index];
   item.num = arr[index];
   var that = this
   item.onclick = function () {//给每一个卡片绑定事件
   that.handle(this);
   }
  })
  }
  handle(ele) {
  if(this.step === 0){
   alert('游戏结束')
   return
  }
  if(ele.active !== true){
   this.step--;
   this.tit.innertext = `剩余步数:${this.step}`
  }
  ele.children[0].classlist.add('active1');//卡片翻转
  ele.children[1].classlist.add('active2'); 
  let res = this.nodes.filter(item => item.children[0].classlist.contains('active1')&& item.active !== true); //获取翻过来的且没有配对成功的卡片
  if(res.length === 2 && res[0].num !== res[1].num){//如果有两张并且数值不相等,恢复原状 
   settimeout(()=>{
   res[0].children[0].classlist.remove('active1');
   res[0].children[1].classlist.remove('active2');
   res[1].children[0].classlist.remove('active1');
   res[1].children[1].classlist.remove('active2'); 
   },1000)//延迟一秒形成动画 
  }else if(res.length === 2 && res[0].num === res[1].num){//如果数值相等则配对成功
   res[0].active = true;//锁定卡片
   res[1].active = true;
  }
  }
  randomarr() {
  let arr = [];
  for (let i = 0, n = 8; i < n; i++) {
   do {
   var item = randomint(1, 8);
   } while (arr.indexof(item) !== -1)
   arr.push(item);
  }
  arr.push(...arr);
  return arr;
  }
 }
 new game('.wrap div')
 function randomint(min, max) {//产生[min,max]范围内的整数
 return math.round(math.random() * (max - min)) + min
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网