Advertisement
lemansky

Untitled

Nov 30th, 2020
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.84 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Document</title>
  7.     <script>
  8.  
  9.         document.addEventListener('DOMContentLoaded', function(){
  10.             const canvas = document.getElementById("canvasId");
  11.             const canvasId = canvas.getContext("2d");
  12.            
  13.             let player = new Player(canvas.width/2 - 100/2, canvas.height - 20, 100, 20);
  14.             let ball = new Ball(canvas.width/2, canvas.height - player.height - 20, 20, 0, 0);
  15.             let brickArray = [];
  16.             for (let i = 0; i < 9; i++) {
  17.                let brick = new Brick(10 + i*110, 0, 100, 20);
  18.                brickArray.push(brick);
  19.            }          
  20.  
  21.             document.addEventListener("mousemove", function(evt){
  22.                 const mouse = mouse_player1(evt, canvas);
  23.                 player.x = mouse.x - player.width/2;
  24.                 // do some tasks
  25.             });
  26.  
  27.            canvas.addEventListener('click', function(){
  28.                reset();
  29.                ball.speedX = Math.floor(Math.random()*2) == 0 ? -10 : 10;
  30.                // if(Math.floor(Math.random()*2) == 0){
  31.                //     ball.speedX = -10;
  32.                // } else {
  33.                //     ball.speedX = 10;
  34.                // }
  35.                ball.speedY = -13;
  36.            })
  37.  
  38.            const reset = () => {
  39.                 ball.x = canvas.width/2;
  40.                 ball.y = canvas.height - player.height - ball.r;
  41.                 ball.speedX = 0;
  42.                 ball.speedY = 0;
  43.                 console.log('game over');
  44.             }
  45.  
  46.             const executeTasks = () => {
  47.                 moveTask();
  48.                 drawTask();
  49.             }
  50.  
  51.             const moveTask = () => {
  52.                 ball.x += ball.speedX;
  53.                 ball.y += ball.speedY;
  54.  
  55.                 if(ball.x + ball.r >= canvas.width || ball.x - ball.r <= 0){
  56.                    ball.speedX = -ball.speedX;
  57.                }
  58.  
  59.                if(ball.y - ball.r - 20 <= 0){
  60.                    for (let i = 0; i < brickArray.length; i++) {
  61.                        if(ball.x + ball.r >=  brickArray[i].x && ball.x - ball.r <=  brickArray[i].x +  brickArray[i].width){
  62.                            brickArray[i].height = 0;
  63.                         }
  64.                     }
  65.                    
  66.                     ball.speedY = -ball.speedY;
  67.  
  68.                 }
  69.  
  70.                 if(ball.y + ball.r + player.height >= canvas.height){
  71.                     if(ball.x + ball.r >= player.x && ball.x - ball.r <= player.x + player.width){
  72.                        ball.speedY = -ball.speedY;
  73.                     } else {
  74.                         reset();
  75.                     }
  76.                 }
  77.             }
  78.  
  79.             const drawTask = () => {
  80.                 canvasId.fillStyle = "black";
  81.                 canvasId.fillRect(0, 0, canvas.width, canvas.height);
  82.  
  83.                 canvasId.fillStyle = "green";
  84.                 canvasId.beginPath();
  85.                 canvasId.arc(ball.x, ball.y, 20, 0, Math.PI*2);
  86.                 canvasId.fill();
  87.                 canvasId.closePath();
  88.  
  89.                 canvasId.fillStyle = "white";
  90.                 canvasId.fillRect(player.x, player.y, player.width, player.height);
  91.  
  92.                 canvasId.fillStyle = "blue";
  93.                 for (let i = 0; i < brickArray.length; i++) {
  94.                    canvasId.fillRect(brickArray[i].x, brickArray[i].y, brickArray[i].width, brickArray[i].height);
  95.                }
  96.              
  97.  
  98.             }
  99.  
  100.             setInterval(executeTasks, 33);
  101.  
  102.         });
  103.  
  104.        class Ball{
  105.            constructor(x, y, radius, speedX, speedY){
  106.                this.x = x;
  107.                this.y = y;
  108.                this.r = radius;
  109.                this.speedX = speedX;
  110.                this.speedY = speedY;
  111.            }
  112.        }
  113.  
  114.        class Player{
  115.            constructor(x, y, width, height){
  116.                this.x = x;
  117.                this.y = y;
  118.                this.width = width;
  119.                this.height = height;
  120.            }
  121.        }
  122.  
  123.        class Brick{
  124.            constructor(x, y, width, height){
  125.                this.x = x;
  126.                this.y = y;
  127.                this.width = width;
  128.                this.height = height;
  129.            }
  130.        }
  131.  
  132.         const mouse_player1 = (evt, canvas) => {
  133.             let rect = canvas.getBoundingClientRect(); // взима позицията на платното в документа, в случай, че то не започва от горния ляв ъгъл
  134.             let mouseX = evt.clientX - rect.left; // позицията на курсора по хоризонтала
  135.             let mouseY = evt.clientY - rect.top; // позиията на курсора по вертикала
  136.             return {
  137.                 x: mouseX,
  138.                 y: mouseY,
  139.             } // фунцкията връша два параметъра, x и y
  140.         }
  141.  
  142.            
  143.     </script>
  144.     <style>
  145.         #canvasId{
  146.             background:black;
  147.         }
  148.     </style>
  149. </head>
  150. <body>
  151.     <canvas id="canvasId" width="1000" height="600"></canvas>
  152. </body>
  153. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement