欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

手把手教你用vue3開發(fā)一個打磚塊小游戲

 更新時間:2021年07月07日 10:38:59   作者:lizhenwen  
這篇文章主要給大家介紹了關(guān)于如何利用vue3開發(fā)一個打磚塊小游戲的相關(guān)資料,通過一個小游戲?qū)嵗梢钥焖倭私鈜ue3開發(fā)小游戲的流程,需要的朋友可以參考下

前言

用vue3寫了幾個實例,感覺Vue3的composition Api設(shè)計得還是很不錯,改變了一下習(xí)慣,但寫多兩個就好了。 這次寫一個也是兒時很覺得很好玩的游戲-打磚塊, 無聊的時候玩一下也覺得挺好玩,游戲性也挺高。這次我直接用vite+vue3打包嘗試一下,vite也是開箱即用,特點是也是可以清除死代碼,按需打包,所以打包速度也是非常快的。沒用過的同學(xué)可以嘗試用用。

游戲效果

游戲需求

  1. 創(chuàng)建一個場景
  2. 創(chuàng)建一個球,創(chuàng)建一堆被打擊方塊
  3. 創(chuàng)建一個可以移動方塊并可控制左右移動
  4. 當球碰撞左右上邊界及移動方塊回彈
  5. 擋球碰撞下邊界游戲結(jié)束

上完整代碼

<template>

    <button @click="stop">停止</button>
    <button @click="start">游戲開始</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, // 場景寬度
        boxHeight = 300, // 場景高度
        ball = 10,//小球的寬高
        moveBottomH = 5,//移動方塊高度
        moveBottomW = 100//移動方塊快讀

    const strArr = '恭喜你,挑戰(zhàn)成功!!'

    //用reactive 保存一些可觀察信息
    const state = reactive({
        x: boxWidth / 2 - ball / 2,  // 小球x軸位置信息 計算默認位置在中間
        y: boxHeight - ball - moveBottomH, // 小球Y軸的位置信息 計算默認位置在中間
        mx: boxWidth / 2 - moveBottomW / 2, //移動方塊的位置信息 計算默認位置在中間
        my: boxHeight - moveBottomH, // 移動方塊y軸的的位置信息  計算默認位置在中間
        // 被打擊方塊的數(shù)組
        arr: Array.from({length: 50}, (_, index) => {
            return {
                index,
                active: false
            }
        }),
        str: '', // 返回挑戰(zhàn)成功字眼
        scroce: 0 // 分數(shù)
    })
    // 用toRefs將觀察對象的信息解構(gòu)出來供模板使用 
    const {x, y, mx, my, arr, str, scroce} = toRefs(state)
    let timer = null, // 小球定時器
        speed = 3,// 小球速度
        map = {x: 10, y: 10},
        timer2 = null, // 挑戰(zhàn)成功字眼顯示定時器
        index = 0//挑戰(zhàn)成功字眼續(xù)個顯示的索引值

    // 挑戰(zhàn)成功字眼續(xù)個顯示的方法
    const strFun = () => {
        if (strArr.length === index) clearInterval(timer2)
        state.str += strArr.substr(index, 1)
        index++
    }

    
    //移動小球的方法  
    // 1.這里同過變量map 對象來記錄坐標信息, 確定小球碰到 左右上 及移動方塊是否回彈
    // 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
    }

    //移動方塊左右移動方法 ,接住小球
    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)
    }
    // 開始游戲 
    const start = () => {
        timer = setInterval(moveBall, 20)
    }
    
    // 綁定移動方塊事件
    onMounted(() => {
        document.addEventListener('keyup', bottomMove)
    })
    // 移動出移動方塊事件
    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>

最后

以后繼續(xù)用vue3,多寫寫實例。

附上打磚塊小游戲地址

shinewen189.github.io/nigo-ball-v

到此這篇關(guān)于用vue3開發(fā)一個打磚塊小游戲的文章就介紹到這了,更多相關(guān)vue3打磚塊小游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vant的picker組件設(shè)置文字超長滾動方式

    vant的picker組件設(shè)置文字超長滾動方式

    這篇文章主要介紹了vant的picker組件設(shè)置文字超長滾動方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Vue學(xué)習(xí)筆記之計算屬性與偵聽器用法

    Vue學(xué)習(xí)筆記之計算屬性與偵聽器用法

    這篇文章主要介紹了Vue學(xué)習(xí)筆記之計算屬性與偵聽器用法,結(jié)合實例形式詳細分析了vue.js計算屬性與偵聽器基本功能、原理、使用方法及操作注意事項,需要的朋友可以參考下
    2019-12-12
  • vue使用el-table動態(tài)合并列及行

    vue使用el-table動態(tài)合并列及行

    這篇文章主要為大家詳細介紹了vue使用el-table動態(tài)合并列及行,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • vite.config.ts如何加載.env環(huán)境變量

    vite.config.ts如何加載.env環(huán)境變量

    這篇文章主要介紹了vite.config.ts加載.env環(huán)境變量方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue style width a href動態(tài)拼接問題的解決

    vue style width a href動態(tài)拼接問題的解決

    這篇文章主要介紹了vue style width a href動態(tài)拼接問題的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 關(guān)于Vue中img動態(tài)拼接src圖片地址的問題

    關(guān)于Vue中img動態(tài)拼接src圖片地址的問題

    這篇文章主要介紹了關(guān)于Vue中img動態(tài)拼接src圖片地址的問題,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-10-10
  • Vue3中嵌套路由和編程式路由的實現(xiàn)

    Vue3中嵌套路由和編程式路由的實現(xiàn)

    Vue?Router在Vue.js的核心庫上提供了路由的功能,使得我們可以在單頁應(yīng)用中實現(xiàn)頁面的切換、跳轉(zhuǎn)和參數(shù)傳遞等功能,本文主要介紹了Vue3中嵌套路由和編程式路由的實現(xiàn),感興趣的可以了解一下
    2023-12-12
  • 在vue中獲取token,并將token寫進header的方法

    在vue中獲取token,并將token寫進header的方法

    今天小編就為大家分享一篇在vue中獲取token,并將token寫進header的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • vue實現(xiàn)輸入框自動跳轉(zhuǎn)功能

    vue實現(xiàn)輸入框自動跳轉(zhuǎn)功能

    這篇文章主要為大家詳細介紹了vue實現(xiàn)輸入框自動跳轉(zhuǎn)功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • vue實現(xiàn)將圖像文件轉(zhuǎn)換為base64

    vue實現(xiàn)將圖像文件轉(zhuǎn)換為base64

    這篇文章主要介紹了vue實現(xiàn)將圖像文件轉(zhuǎn)換為base64,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02

最新評論