Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- body{
- /*margin:0px;*/
- }
- </style>
- <script>
- document.addEventListener('DOMContentLoaded', function(){
- const canvas = document.getElementById("canvasId");
- const context = canvas.getContext("2d");
- let moveX = 0;
- let moveY = 0;
- let radius = 20;
- let playerX = 0;
- let playerWidth = 100;
- let x = canvas.width/2;
- let y = canvas.height - 20 - radius; // minus visochinata na palkata
- canvas.addEventListener('mousemove', (e) => {
- let mouse = mouse_player1(e);
- context.beginPath();
- context.fillStyle = "red";
- context.arc(mouse.x, mouse.y, 20, 0, 2*Math.PI);
- context.fill();
- context.closePath();
- playerX = mouse.x - playerWidth/2;
- });
- canvas.addEventListener('click', (e) => {
- // moveX = moveX == 0 ? (Math.floor(Math.random()*2) == 1 ? 10 : -10) : moveX;
- if(moveX == 0){
- if(Math.floor(Math.random()*2) == 1) {
- moveX = 10;
- } else {
- moveX = -10;
- }
- }
- // moveY = moveY == 0 ? moveY = -14 : moveY;
- if(moveY == 0) {
- moveY = -14;
- }
- });
- const mouse_player1 = (e) => {
- return {
- x: e.layerX,
- y: e.layerY,
- }
- }
- const executeTasks = () =>{
- moveTask();
- drawTask();
- }
- const moveTask = () => {
- x += moveX;
- y += moveY;
- if( x >= canvas.width - radius) {
- moveX = -moveX;
- }
- if( x <= 0 + radius ) {
- moveX = -moveX;
- }
- if(y >= canvas.height - radius){
- moveY = -moveY;
- }
- if(y <= 0 + radius ){
- moveY = -moveY;
- }
- }
- const drawTask = () => {
- context.clearRect(0, 0, canvas.width, canvas.height);
- context.fillStyle = "green";
- context.beginPath();
- context.arc(x, y, radius, 0, Math.PI*2);
- context.fill();
- context.closePath();
- context.fillStyle = "white";
- context.fillRect(playerX, 580, playerWidth, 20);
- }
- setInterval(executeTasks, 24);
- });
- </script>
- <style>
- #canvasId{
- background:black;
- }
- </style>
- </head>
- <body>
- <canvas id="canvasId" width="800" height="600"></canvas>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement