Advertisement
lemansky

Untitled

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