站長資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        canvas實(shí)現(xiàn)彈球的代碼示例

        本篇文章給大家?guī)淼膬?nèi)容是關(guān)于canvas實(shí)現(xiàn)彈球的代碼示例,有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對你有所幫助。

        效果

        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();         };         // 移動操作         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è)擋板對象     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)建出擋板對象     let panel = new Panel();     // 每10秒為一幀     window.setInterval(() => {         // 清空畫布         context.clearRect(0, 0, 400, 400);         // 畫出小球         ball.draw();         // 畫出擋板         panel.draw();         // 移動         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>

        思路

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

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號
        主站蜘蛛池模板: 精品水蜜桃久久久久久久| 亚洲精品欧美精品日韩精品| 国产乱人伦偷精品视频免观看| 无码aⅴ精品一区二区三区浪潮| 国产69精品久久久久9999| 精品无码人妻一区二区三区| 久久青青草原精品国产软件| 久久精品男人影院| 国产精品熟女一区二区| 亚洲午夜福利精品久久| 国产精品亚洲美女久久久| 热re99久久精品国产99热| jizz国产精品网站| 久久精品无码专区免费东京热| 欧美日韩精品一区二区视频 | 国产精品户外野外| 国产99精品久久| 久久久久99精品成人片直播 | 无码人妻精品一区二区蜜桃AV| 国产精品无码一区二区在线 | 国产精品无码无卡无需播放器| 四虎影视884a精品国产四虎| 69久久精品无码一区二区| 国产精品看高国产精品不卡| 精品一区二区三区波多野结衣| 一本一道精品欧美中文字幕| 欧美成人精品高清视频在线观看 | 国产亚洲精品成人a v小说| 亚洲自偷自偷精品| 中文字幕一区二区精品区| 亚洲国产精品第一区二区| 欧美一卡2卡3卡四卡海外精品| 精品免费视在线观看| 欧美久久精品一级c片片| 四虎国产精品免费入口| 欧洲精品视频在线观看| 欧美精品免费在线| 99热精品久久只有精品| 国产精品99爱免费视频| 国内精品国语自产拍在线观看 | 国产精品福利一区二区|