- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > JavaScript >
- 手把手教你用vue3開(kāi)發(fā)一個(gè)打磚塊小游戲
用vue3寫(xiě)了幾個(gè)實(shí)例,感覺(jué)Vue3的composition Api設計得還是很不錯,改變了一下習慣,但寫(xiě)多兩個(gè)就好了。 這次寫(xiě)一個(gè)也是兒時(shí)很覺(jué)得很好玩的游戲-打磚塊, 無(wú)聊的時(shí)候玩一下也覺(jué)得挺好玩,游戲性也挺高。這次我直接用vite+vue3打包嘗試一下,vite也是開(kāi)箱即用,特點(diǎn)是也是可以清除死代碼,按需打包,所以打包速度也是非??斓?。沒(méi)用過(guò)的同學(xué)可以嘗試用用。
<template> <button @click="stop">停止</button> <button @click="start">游戲開(kāi)始</button> <div style="color: red; text-align: center;font-size: 25px">score:{{scroce}}</div> <div class="box" :style="{width :boxWidth +'px', height:boxHeight +'px'}"> <div class="str">{{str}}</div> <div class="kuaiBox"> <div class="kuai" v-for="(item,index) in arr" :key="index" :style="{opacity :item.active ? '0':'1'}"></div> </div> <div class="ball" :style="{left :x + 'px', top : y + 'px', width : ball +'px', height: ball+'px'}"></div> <div class="bottomMove" :style="{left :mx + 'px' , top : my + 'px',width :moveBottomW +'px',height : moveBottomH+'px' }"></div> </div> </template> <script setup> import {onMounted, onUnmounted, reactive, toRefs} from 'vue' const boxWidth = 500, // 場(chǎng)景寬度 boxHeight = 300, // 場(chǎng)景高度 ball = 10,//小球的寬高 moveBottomH = 5,//移動(dòng)方塊高度 moveBottomW = 100//移動(dòng)方塊快讀 const strArr = '恭喜你,挑戰成功!!' //用reactive 保存一些可觀(guān)察信息 const state = reactive({ x: boxWidth / 2 - ball / 2, // 小球x軸位置信息 計算默認位置在中間 y: boxHeight - ball - moveBottomH, // 小球Y軸的位置信息 計算默認位置在中間 mx: boxWidth / 2 - moveBottomW / 2, //移動(dòng)方塊的位置信息 計算默認位置在中間 my: boxHeight - moveBottomH, // 移動(dòng)方塊y軸的的位置信息 計算默認位置在中間 // 被打擊方塊的數組 arr: Array.from({length: 50}, (_, index) => { return { index, active: false } }), str: '', // 返回挑戰成功字眼 scroce: 0 // 分數 }) // 用toRefs將觀(guān)察對象的信息解構出來(lái)供模板使用 const {x, y, mx, my, arr, str, scroce} = toRefs(state) let timer = null, // 小球定時(shí)器 speed = 3,// 小球速度 map = {x: 10, y: 10}, timer2 = null, // 挑戰成功字眼顯示定時(shí)器 index = 0//挑戰成功字眼續個(gè)顯示的索引值 // 挑戰成功字眼續個(gè)顯示的方法 const strFun = () => { if (strArr.length === index) clearInterval(timer2) state.str += strArr.substr(index, 1) index++ } //移動(dòng)小球的方法 // 1.這里同過(guò)變量map 對象來(lái)記錄坐標信息, 確定小球碰到 左右上 及移動(dòng)方塊是否回彈 // 2.循環(huán)磚塊檢測小球碰撞到磚塊消失 const moveBall = () => { const {offsetTop, offsetHeight, offsetLeft, offsetWidth} = document.querySelector('.bottomMove') if (state.x <= 0) { map.x = speed } else if (state.x > boxWidth - ball) { map.x = -speed } if (state.y <= 0) { map.y = speed } if (state.y >= offsetTop - offsetHeight && state.y <= offsetTop + offsetHeight && state.x >= offsetLeft && state.x < offsetLeft + offsetWidth) { map.y = -speed } if (state.y > boxHeight) { clearInterval(timer) alert('game over') window.location.reload() } Array.from(state.arr).forEach((item, index) => { const { offsetLeft, offsetTop, offsetWidth, offsetHeight } = document.querySelectorAll('.kuai')[index] if (state.x > offsetLeft && state.x < offsetLeft + offsetWidth && state.y > offsetTop && state.y < offsetTop + offsetHeight) { if (!state.arr[index].active) { state.scroce += 100 } state.arr[index].active = true } }) if (Array.from(state.arr).every(item => item.active)) { clearInterval(timer) timer2 = setInterval(strFun, 1000) } state.x = state.x += map.x state.y = state.y += map.y } //移動(dòng)方塊左右移動(dòng)方法 ,接住小球 const bottomMove = ev => { if (ev.code === 'Space') clearInterval(timer) switch (ev.key) { case 'ArrowRight': state.mx += 100 break case 'ArrowLeft': state.mx -= 100 break } state.mx = state.mx < 0 ? 0 : state.mx state.mx = state.mx > boxWidth - moveBottomW ? boxWidth - moveBottomW : state.mx } // 暫停游戲 const stop = () => { clearInterval(timer) } // 開(kāi)始游戲 const start = () => { timer = setInterval(moveBall, 20) } // 綁定移動(dòng)方塊事件 onMounted(() => { document.addEventListener('keyup', bottomMove) }) // 移動(dòng)出移動(dòng)方塊事件 onUnmounted(() => { clearInterval(timer) }) </script> <style> .bottomMove { width: 100px; height: 10px; background: red; position: absolute; transition-duration: 100ms; transition-timing-function: ease-out; } .ball { width: 20px; height: 20px; background-color: red; border-radius: 50%; position: absolute; } .kuaiBox { display: flex; flex-wrap: wrap; } .kuai { width: 30px; height: 10px; background: red; margin: 10px; transition-duration: 100ms; transition-timing-function: ease-in; } .str { text-align: center; font-size: 50px; color: red; } .box { justify-content: center; width: 500px; height: 500px; margin: 0 auto; position: relative; border: 5px solid red; overflow: hidden; } .picker { width: 50px; height: 50px; } </style>
以后繼續用vue3,多寫(xiě)寫(xiě)實(shí)例。
附上打磚塊小游戲地址
…
到此這篇關(guān)于用vue3開(kāi)發(fā)一個(gè)打磚塊小游戲的文章就介紹到這了,更多相關(guān)vue3打磚塊小游戲內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站