PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

使用vue实现的扫雷游戏(附代码)

不言
不言 转载
2018-11-14 10:05:13 3375浏览

本篇文章给大家带来的内容是关于使用vue实现的扫雷游戏(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

上班闲来没事做,,心血来潮。想用刚学的vue,写一个扫雷游戏。。好了,直入正题.

第一步,先制作一个10x10的格子图。。这个pcss就不说了。。大家都会。

2432327086-5beb78a383f59_articlex.png

第二步,制造一个数组,用来生成随机雷区。

let arr = []
for (var i = 0; i <p>第三步,制造一个json数组,让他循环生成页面上的格子。</p><pre class="brush:php;toolbar:false">  let arrs = []
  for (var j = 0; j  -1) {
      obj.isLei = true // 是否是地雷
    } else {
      obj.isLei = false // 是否是地雷
    }
    obj.id = j
    obj.isTrue = false // 安全区样式
    obj.isFalse = false // 雷区样式
    arrs.push(obj)
  }

大概是这样子的数据

362171790-5beb79ac27e50_articlex.png

第四步,点击格子,触发事件。判断是否这个是雷区。如果安全就显示绿色,否则显示红色。

toclick (e) {
  console.log(e.isTrue)
  if (e.isLei === true) {
    e.isFalse = true
  } else {
    e.isTrue = true
    this.surPlus = this.surPlus - 1
  }
}

以下是所有代码:

<script>
export default{
  data () {
    return {
      lattice: null, // 100个格子
      surPlus: 90 // 安全区。。因为是10个雷。所以就是100-10 = 90
    }
  },
  methods: {
    toclick (e) {
      console.log(e.isTrue)
      if (e.isLei === true) {
        e.isFalse = true
      } else {
        e.isTrue = true
        this.surPlus = this.surPlus - 1
      }
    },
    random () {
      let arr = []
      for (var i = 0; i < 10; i++) {
        arr.push(Math.floor(Math.random() * 100))
      }
      let arrs = []
      for (var j = 0; j < 100; j++) {
        let obj = {}
        if (arr.indexOf(j) > -1) {
          obj.isLei = true // 是否是地雷
        } else {
          obj.isLei = false // 是否是地雷
        }
        obj.id = j
        obj.isTrue = false // 安全区样式
        obj.isFalse = false // 雷区样式
        arrs.push(obj)
      }
      this.lattice = arrs
      console.log(arrs)
    }
  },
  mounted () {
    this.random()
  }
}
</script><template>
  <div>
      <div>
        <div></div>
      </div>
      <div>剩余{{surPlus}}个安全格子</div>
  </div>
</template><style>
.page{
    overflow: hidden;
}
.bg{
    border:1px solid #000;
    width:600px;
    height:600px;
    margin:20px auto;
}
.lattice{
    border: 1px solid #ccc;
    box-sizing: border-box;
    float:left;
    width:60px;
    height:60px;
}
.surplus{
    line-height: 38px;
    height:38px;
    width:150px;
    margin:0 auto;
}
.security{
    background-color: green;
}
.danger{
    background-color: red;
}
</style>

最后样子大概就是这样:

1470458595-5beb7a7f58f02_articlex.png

声明:本文转载于:segmentfault,如有侵犯,请联系admin@php.cn删除