久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

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

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

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

      效果

      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;         };         // 邊緣檢測(cè),碰撞檢測(cè)         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è)。。
      碰撞檢測(cè)時(shí)實(shí)的坐標(biāo)判斷,碰撞完成以后兩個(gè)速度分量為取反即可。
      事件是左右事件。。移動(dòng)即可。
      需要時(shí)實(shí)刷新,即,幀的概念

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)