App下載

在html5怎么實(shí)現(xiàn)吃蘋(píng)果小游戲?使用Canvas 幀動(dòng)畫(huà)吃蘋(píng)果小游戲!

喜歡熬夜的小孩 2021-08-16 11:35:33 瀏覽數(shù) (2388)
反饋

今天為大家?guī)?lái)有關(guān)于:“在html5怎么實(shí)現(xiàn)吃蘋(píng)果小游戲?”這個(gè)問(wèn)題的解決方法和相關(guān)內(nèi)容,相信大家在閱讀完這篇文章之后會(huì)更加的喜歡html5!

先看頁(yè)面效果。下面四個(gè)按鈕分別表示開(kāi)始、結(jié)束、暫停、繼續(xù)

效果實(shí)現(xiàn)主圖

下面是幀動(dòng)畫(huà)圖片素材:

實(shí)現(xiàn)素材動(dòng)畫(huà)

幀動(dòng)畫(huà)的實(shí)現(xiàn),關(guān)鍵是Canvas API ctx.drawImage() (9個(gè)參數(shù))和 setInterval 定時(shí)器。
設(shè)置圖片的視圖窗口,每次執(zhí)行定時(shí)任務(wù),位移展示下一幀圖片。

直接上代碼,Ctrl+C/V 即插即用

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>幀動(dòng)畫(huà)</title>
    </head>
    <body>
        <canvas id="canvas" width="400" height="300"></canvas>
        <div class="">
            <button class="start-btn" type="button">重新吃</button>
            <button class="end-btn" type="button">不吃了</button>
            <button class="pause-btn" type="button">歇一歇</button>
            <button class="continue-btn" type="button">繼續(xù)吃</button>
        </div>
        <script type="text/javascript">
            const canvas = document.getElementById("canvas")
            canvas.style.border = "1px solid black"
            const ctx = canvas.getContext("2d")

            const img = new Image() // 創(chuàng)建圖片對(duì)象
            let timer // 定時(shí)器標(biāo)識(shí)符
            let millisec = 300 // 執(zhí)行時(shí)間間隔
            let colIndex = 0 // 列數(shù)
            let rowIndex = 0 // 行數(shù)
            const timerFun = () => { // 聲明定時(shí)器執(zhí)行函數(shù)
                console.log("設(shè)置定時(shí)器");
                ctx.clearRect(0, 0, canvas.style.width, canvas.style.height) // 清除畫(huà)布

                if (rowIndex < 3) { // 如果是前5幀
                    ctx.drawImage(img, colIndex * 240, rowIndex * 240, 200, 200, 50, 50, 200, 200) // 圖片對(duì)象,x坐標(biāo),y坐標(biāo)(注:圖片上定位的坐標(biāo)),width,height(圖片上截取的大?。?,x坐標(biāo),y坐標(biāo)(注:圖片在畫(huà)布上的起點(diǎn),即左上角),width,height(縮放,不是裁剪)
                    colIndex++ // 下一幀

                    if (colIndex > 4) {
                        colIndex = 0
                        rowIndex++
                    }
                } else {
                    colIndex = 0
                    rowIndex = 0
                }
            }

            img.onload = () => {
                timer = setInterval(timerFun, millisec)
            }
            img.src = "image/apple.jpg"

            const startBtn = document.getElementsByClassName('start-btn')[0]
            const endBtn = document.getElementsByClassName('end-btn')[0]
            const pauseBtn = document.getElementsByClassName('pause-btn')[0]
            const continueBtn = document.getElementsByClassName('continue-btn')[0]

            startBtn.addEventListener('click', () => {
                console.log("點(diǎn)擊開(kāi)始", timer)
                clearInterval(timer)
                colIndex = 0 // 列數(shù)
                rowIndex = 0 // 行數(shù)
                timer = setInterval(timerFun, millisec)
            })
            endBtn.addEventListener('click', () => {
                console.log("點(diǎn)擊結(jié)束", timer)
                clearInterval(timer)
                colIndex = 0
                rowIndex = 0
                ctx.drawImage(img, colIndex * 240, rowIndex * 240, 200, 200, 50, 50, 200, 200)
                timer = 0
            })
            pauseBtn.addEventListener('click', () => {
                console.log("點(diǎn)擊暫停", timer)
                clearInterval(timer)
                timer = 0
            })
            continueBtn.addEventListener('click', () => {
                if (timer) {
                    alert('吃著呢,別催')
                    return
                }
                console.log("點(diǎn)擊繼續(xù)", timer)
                timer = setInterval(timerFun, millisec)
            })
        </script>
    </body>
</html>

那么到此這篇關(guān)于:"在html5怎么實(shí)現(xiàn)吃蘋(píng)果小游戲?"這個(gè)問(wèn)題我們就解決了,更多有關(guān)于html5 Canvas 的使用我們都可以在W3Cschool中進(jìn)行學(xué)習(xí)和 了解!


0 人點(diǎn)贊