Advertisement
lemansky

Untitled

Nov 30th, 2020
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.83 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.             let ball = new Ball(50, 50, 20, 10, 13);
  13.             let player = new Player(canvas.width/2 - 100/2, canvas.height - 20, 100, 20);
  14.             document.addEventListener("mousemove", function(evt){
  15.                 const mouse = mouse_player1(evt, canvas);
  16.                 player.x = mouse.x - player.width/2;
  17.                 // do some tasks
  18.  
  19.             });
  20.  
  21.             const executeTasks = () => {
  22.                 moveTask();
  23.                 drawTask();
  24.             }
  25.  
  26.             const moveTask = () => {
  27.                 ball.x += ball.speedX;
  28.                 ball.y += ball.speedY;
  29.  
  30.                 if(ball.x + ball.r >= canvas.width || ball.x - ball.r <= 0){
  31.                    ball.speedX = -ball.speedX;
  32.                }
  33.  
  34.                if(ball.y + ball.r >= canvas.height || ball.y - ball.r <= 0){
  35.                    ball.speedY = -ball.speedY;
  36.                }
  37.  
  38.             }
  39.  
  40.             const drawTask = () => {
  41.                 canvasId.fillStyle = "black";
  42.                 canvasId.fillRect(0, 0, canvas.width, canvas.height);
  43.  
  44.                 canvasId.fillStyle = "green";
  45.                 canvasId.beginPath();
  46.                 canvasId.arc(ball.x, ball.y, 20, 0, Math.PI*2);
  47.                 canvasId.fill();
  48.                 canvasId.closePath();
  49.  
  50.                 canvasId.fillStyle = "white";
  51.                 canvasId.fillRect(player.x, player.y, player.width, player.height);
  52.             }
  53.  
  54.             setInterval(executeTasks, 33);
  55.  
  56.         });
  57.  
  58.         class Ball{
  59.             constructor(x, y, radius, speedX, speedY){
  60.                 this.x = x;
  61.                 this.y = y;
  62.                 this.r = radius;
  63.                 this.speedX = speedX;
  64.                 this.speedY = speedY;
  65.             }
  66.         }
  67.  
  68.         class Player{
  69.             constructor(x, y, width, height){
  70.                 this.x = x;
  71.                 this.y = y;
  72.                 this.width = width;
  73.                 this.height = height;
  74.             }
  75.         }
  76.  
  77.         const mouse_player1 = (evt, canvas) => {
  78.             let rect = canvas.getBoundingClientRect(); // взима позицията на платното в документа, в случай, че то не започва от горния ляв ъгъл
  79.             let mouseX = evt.clientX - rect.left; // позицията на курсора по хоризонтала
  80.             let mouseY = evt.clientY - rect.top; // позиията на курсора по вертикала
  81.             return {
  82.                 x: mouseX,
  83.                 y: mouseY,
  84.             } // фунцкията връша два параметъра, x и y
  85.         }
  86.  
  87.            
  88.     </script>
  89.     <style>
  90.         #canvasId{
  91.             background:black;
  92.         }
  93.     </style>
  94. </head>
  95. <body>
  96.     <canvas id="canvasId" width="1000" height="600"></canvas>
  97. </body>
  98. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement