vue3開發(fā)打磚塊游戲代碼怎么寫

小億
109
2023-06-30 11:16:34
欄目: 編程語言

完整代碼


    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 = "恭喜你,挑戰(zhàn)成功!!"

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

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

    
    //移動(dòng)小球的方法  
    // 1.這里同過變量map 對(duì)象來記錄坐標(biāo)信息, 確定小球碰到 左右上 及移動(dòng)方塊是否回彈
    // 2.循環(huán)磚塊檢測(cè)小球碰撞到磚塊消失
    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)
    }
    // 開始游戲 
    const start = () => {
        timer = setInterval(moveBall, 20)
    }
    
    // 綁定移動(dòng)方塊事件
    onMounted(() => {
        document.addEventListener("keyup", bottomMove)
    })
    // 移動(dòng)出移動(dòng)方塊事件
    onUnmounted(() => {
        clearInterval(timer)
    })

0