vue打地鼠的简单实现,效果差不多就这个样子:
目录:
|mouse |--|components //放分页的 |--|--|GamePage.vue |--|image //装图片的 |--|index.vue //入口
index.vue:
<template> <div v-if="isplay"> <ul> <li class="mytitle">打个地鼠 选择难度</li> <li> <span><img src="./image/v.png"/></span> <span>速度</span> <input placeholder="请输入预期速度" style="width: 150px;" v-model="mouseSpeed"/> </li> <li> <span><img src="./image/num.png"/></span> <span>数量</span> <input placeholder="请输入地鼠数量" style="width: 150px;" v-model="mouseNum"/> </li> <li> <span><img src="./image/map.png"/></span> <span>规模</span> <select v-model="selectid" style="width: 150px;"> <option value="0">未选择</option> <option value="1">3 x 3 格</option> <option value="2">6 x 6 格</option> <option value="3">9 x 9 格</option> </select> </li> <li> <button @click="play()">开始游戏</button> </li> </ul> </div> <div v-else> <game-page :selectid="selectid" :mouseNum="mouseNum" :mouseSpeed="mouseSpeed" @back-chose="backChose"></game-page> </div> </template> <script> import GamePage from './components/GamePage.vue' export default { name: 'mouse', data () { return { selectid: "0", mouseNum: 40, mouseSpeed: 1000, isplay: true } }, components: { GamePage }, methods: { play() { this.isplay=false }, backChose() { this.isplay=true } } } </script> <style scoped> .mytitle { padding: 0; color: green; font-size: 50px; text-align: center; line-height: 100px; } img { width: 26px; height: 26px; text-align: center; } ul { width: 70%; display: flex; flex-direction: column; justify-content: center; background-color: lightgreen; } li { padding: 0; line-height: 45px; text-align: center; font-size: 23px; } button { background-color: lightgreen; color: green; } </style>
GamePage.vue
<template> <div> <div class="mytitle" v-if="isrun">鼠鼠我啊要出来啦</div> <div class="mytitle" v-else>鼠鼠我啊在准备了</div> <div class="myframe"> <!-- words --> <div class="board"> <ul> <li><img src="../image/v.png"/>速度:{{ mouseSpeed }}</li> <li><img src="../image/map.png"/>地图:{{ selectid*3 }} x {{ selectid*3 }}</li> <li style="color: green;">返回 开始 终止</li> <li> <img src="../image/back.png" @click="backPage()" class="myImg"/> <img src="../image/start.png" @click="playgame()" class="myImg"/> <img src="../image/stop.png" @click="endgame()" class="myImg"/> </li> </ul> <ul> <li>统计分数面板</li> <li><img src="../image/num.png"/>鼠鼠数: {{mouseNum}}</li> <li><img src="../image/click.png"/>击中数: {{cnt}}</li> <li><img src="../image/clicktrue.png"/>击中率: {{level}}%</li> </ul> <ul> <li>{{ log }}</li> </ul> </div> <!-- map --> <div class="board"> <div v-for="(row, rowIndex) in board" :key="rowIndex" class="row"> <div v-for="(cell, cellIndex) in row" :key="cellIndex" @click="onclick(rowIndex, cellIndex)" class="cell"> <span v-if="cell.flagged"> <img src='../image/mouse.png'/> </span> <span v-else> <img src='../image/all.png'/> </span> </div> </div> </div> </div> </div> </template> <script> export default { props: ['mouseSpeed', 'mouseNum', 'selectid'], data () { return { cnt_all: 0, cnt: 0, level: 0.0, board: [], nowTime: null, isrun: false, // 测试日志 log: "", timer: "" } }, created () { this.initGame() }, methods: { backPage() { console.log("click backToPage buttom") this.$emit('back-chose') }, initGame () { for (let i = 0; i < this.selectid * 3; i++) { const row = [] for (let j = 0; j < this.selectid * 3; j++) { row.push({ismouse:false, flagged: false}) } this.board.push(row) } }, onclick(row, col) { if (this.board[row][col].flagged){ this.cnt++ this.level = (this.cnt * 100.0 / this.mouseNum).toFixed(2) if(this.cnt >= this.mouseNum) { clearInterval(this.timer) this.log = "you win!" this.cnt = 0 } } for (let i = 0; i < this.selectid * 3; i++) { for (let j = 0; j < this.selectid * 3; j++) { this.board[i][j].flagged=false } } }, getmouse () { const arr = [] //随即搞10个变成地鼠 for (let i = 0; i < 10; i++) { const itrow = Math.floor(Math.random() * this.selectid * 3) const itcol = Math.floor(Math.random() * this.selectid * 3) if (!this.board[itrow][itcol].flagged) { this.board[itrow][itcol].flagged = true arr.push({r: itrow, c: itcol}) // this.cnt++ // this.log = arr } else { i-- } } //让它们消失 setTimeout(()=>{ for (let i = 0; i < 10; i++) { const it = arr.pop() this.board[it.r][it.c].flagged = false // this.log = arr } },1000) }, playgame () { this.timer = setInterval(this.getmouse,1000) }, endgame () { clearInterval(this.timer) } }, beforeDestroy() { clearInterval(this.timer); } } </script> <style scoped> img { width: 30px; height: 30px; margin-left: 10px; margin-right: 10px; } .myImg { width: 30px; height: 30px; margin-left: 1px; margin-right: 1px; cursor: pointer; } ul { display: flex; flex-direction: column; justify-content: center; border: 3px solid green; } li { padding: 0; line-height: 45px; text-align: center; margin-right: 10px; } .myframe { display: flex; flex-direction: row; justify-content: center; } .board { background-color: lightgreen; display: inline-block; border: 10px solid lightgreen; margin: 10px; } .row { display: flex; } .cell { margin: 10px; cursor: pointer; } .mytitle { padding: 0; color: green; font-size: 50px; text-align: center; line-height: 100px; } </style>
定时器和布局什么的记在vue学习记录6
哎哟忘记改标题了,isrun没用上。算咯
标签:cnt,vue,center,记录,++,selectid,学习,board From: https://www.cnblogs.com/yyn20230517/p/17502674.html