溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

canvas實(shí)現(xiàn)彈球的案例

發(fā)布時(shí)間:2020-10-16 15:52:32 來源:億速云 閱讀:166 作者:小新 欄目:web開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)canvas實(shí)現(xiàn)彈球的案例,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

效果

canvas實(shí)現(xiàn)彈球的案例

代碼

<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <meta charset="UTF-8">
    <title>彈球</title>
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script>
    // 全局canvas
    let canvas = document.getElementById("canvas");
    let context = canvas.getContext("2d");
    // 彈球?qū)ο?
    class Ball{
        x = 100;
        y = 40;
        xSpeed = -2;
        ySpeed = -2;
        constructor(){};
        getX(){
            return this.x;
        }
        getY(){
            return this.y;
        }
        setX(x){
            this.x = x;
        }
        setY(y){
            this.y = y;
        }
        getXSpeed(){
            return this.xSpeed;
        }
        setXSpeed(xSpeed){
            this.xSpeed = xSpeed;
        }
        getYSpeed(){
            return this.ySpeed;
        }
        setYSpeed(ySpeed){
            this.ySpeed = ySpeed;
        }
        // 繪制小球的方法
        draw = () => {
            context.beginPath();
            context.arc(this.x, this.y, 10, 0, Math.PI * 2, false);
            context.strokeRect(0, 0, 400, 400);
            context.fill();
        };
        // 移動(dòng)操作
        move = () => {
            this.x = this.x + this.xSpeed;
            this.y = this.y + this.ySpeed;
        };
        // 邊緣檢測,碰撞檢測
        checkCanvas = (panel) => {
            // 左右
            if(this.x < 5 || this.x > 400 - 5){
                this.xSpeed = -this.xSpeed;
            }
            // 上方
            if(this.y < 0){
                this.ySpeed = -this.ySpeed;
            }
            // 下方
            // 碰到擋板

            if(this.y > 390 - 10){
                if(this.x > panel.x && this.x < panel.xSize + panel.x){
                    this.ySpeed = -this.ySpeed;
                }else{
                    alert("游戲結(jié)束");
                    this.x = 100;
                    this.y = 10;
                }
            }
        }
    }
    // 增加一個(gè)擋板對(duì)象
    class Panel{
        constructor(){};
        // 左x
        x = 200;
        // 左y
        y = 390;
        // 長度
        xSize = 50;
        // 寬度
        ySize = 5;
        draw(){
            context.fillRect(this.x, this.y, this.xSize, this.ySize);
        }
    }
    // 創(chuàng)建出一個(gè)小球?qū)ο?
    let ball = new Ball();
    // 創(chuàng)建出擋板對(duì)象
    let panel = new Panel();
    // 每10秒為一幀
    window.setInterval(() => {
        // 清空畫布
        context.clearRect(0, 0, 400, 400);
        // 畫出小球
        ball.draw();
        // 畫出擋板
        panel.draw();
        // 移動(dòng)
        ball.move();
        // 進(jìn)行邊界判斷
        ball.checkCanvas(panel);
    },10);
    // 控制擋板
    $("body").keydown((event) => {
        if(event.keyCode == 37){
            panel.x = panel.x - 5;
            // 移出邊界問題處理
            if(panel.x < 0){
                panel.x = 0;
            }
        }
        if(event.keyCode == 39){
            panel.x = panel.x + 5;
            // 移出邊界處理
            if(panel.x + panel.xSize > 400){
                panel.x = 400 - panel.xSize;
            }
        }
    })
</script>
</body>
</html>

思路

這就是倆對(duì)象,,一個(gè)依賴于另一個(gè)。。
碰撞檢測時(shí)實(shí)的坐標(biāo)判斷,碰撞完成以后兩個(gè)速度分量為取反即可。
事件是左右事件。。移動(dòng)即可。
需要時(shí)實(shí)刷新,即,幀的概念

關(guān)于canvas實(shí)現(xiàn)彈球的案例就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI