help to learn how to implement the shooting, as on this gif

enter image description here

could not find in google something worthwhile about this

var canvas = document.getElementById("d1"); var ctx = canvas.getContext("2d"); // ΠΊΠ½ΠΎΠΏΠΊΠΈ var pressedLeft = false; // Π»Π΅Π²ΠΎ var pressedRight = false; // ΠΏΡ€Π°Π²ΠΎ var pressedSpace = false; // ΠΏΡ€ΠΎΠ±Π΅Π» // ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ ΠΈΠ³Ρ€ΠΎΠΊΠ° var playerWidth = 20; var playerHeight = 20; var playerX = (canvas.width - playerWidth)/2; var playerY = canvas.height - 25; //ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ ΠΏΡƒΠ»ΠΈ var bulletWidth = 5; var bulletHeight = 5; var bulletX = playerX; var bulletY = playerY; document.addEventListener("keydown", KeyDown, false); document.addEventListener("keyup", KeyUp, false); // ΠΊΠ½ΠΎΠΊΠ° Π½Π°ΠΆΠ°Ρ‚Π° function KeyDown(e) { if(e.keyCode == 37) { pressedLeft = true; } else if(e.keyCode == 39) { pressedRight = true; } else if(e.keyCode == 32) { pressedSpace = true; } } // ΠΊΠ½ΠΎΠΏΠΊΠ° Π½Π΅ Π½Π°ΠΆΠ°Ρ‚Π° function KeyUp(e) { if(e.keyCode == 37) { pressedLeft = false; } else if(e.keyCode == 39) { pressedRight = false; } else if(e.keyCode == 32) { pressedSpace = false; } } // рисуСм ΠΈΠ³Ρ€ΠΎΠΊΠ° function drawPlayer() { ctx.beginPath(); ctx.fillStyle = "black"; ctx.fillRect(playerX, playerY, playerWidth, playerHeight); ctx.fill(); ctx.closePath(); } // рисуСм ΠΏΡƒΠ»ΡŽ function drawBullet() { ctx.beginPath(); ctx.fillStyle = "blue"; ctx.fillRect(playerX, playerY, bulletWidth, bulletHeight); // Π½Π΅ ΡƒΠ²Π΅Ρ€Π΅Π½ Ρ‡Ρ‚ΠΎ это ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½Π°Ρ строчка ctx.fill(); ctx.closePath(); } function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); drawPlayer(); drawBullet(); if(pressedLeft && 0 < playerX) { playerX -= 5; } if(pressedRight && playerX < canvas.width - playerWidth) { playerX += 5; } } setInterval(draw, 10); 
 #d1 { border: 1px solid black; } 
 <canvas id="d1" width="250" height="250" ></canvas> 

Shooting should occur by pressing the spacebar.

Thank you in advance.

    2 answers 2

    Like that:

     var canvas = document.getElementById("d1"); var ctx = canvas.getContext("2d"); // ΠΊΠ½ΠΎΠΏΠΊΠΈ var pressedLeft = false; // Π»Π΅Π²ΠΎ var pressedRight = false; // ΠΏΡ€Π°Π²ΠΎ var pressedSpace = false; // ΠΏΡ€ΠΎΠ±Π΅Π» // ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ ΠΈΠ³Ρ€ΠΎΠΊΠ° var playerWidth = 20; var playerHeight = 20; var playerX = (canvas.width - playerWidth)/2; var playerY = canvas.height - 25; //ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ ΠΏΡƒΠ»ΠΈ var bulletWidth = 5; var bulletHeight = 5; var bulletX = playerX; var bulletY = playerY; document.addEventListener("keydown", KeyDown, false); document.addEventListener("keyup", KeyUp, false); // ΠΊΠ½ΠΎΠΊΠ° Π½Π°ΠΆΠ°Ρ‚Π° function KeyDown(e) { if(e.keyCode == 32) { pressedSpace = true; } if(e.keyCode == 37) { pressedLeft = true; }else if(e.keyCode == 39) { pressedRight = true; }else if(e.keyCode == 32) { pressedSpace = true; } } // ΠΊΠ½ΠΎΠΏΠΊΠ° Π½Π΅ Π½Π°ΠΆΠ°Ρ‚Π° function KeyUp(e) { if(e.keyCode == 32) { pressedSpace = false; } if(e.keyCode == 37) { pressedLeft = false; } else if(e.keyCode == 39) { pressedRight = false; } else if(e.keyCode == 32) { pressedSpace = false; } } // рисуСм ΠΈΠ³Ρ€ΠΎΠΊΠ° function drawPlayer() { ctx.beginPath(); ctx.fillStyle = "black"; ctx.fillRect(playerX, playerY, playerWidth, playerHeight); ctx.fill(); ctx.closePath(); } const bullets = []; class Bullet { constructor(){ this.x = playerX; this.y = canvas.height - 25; bullets.push(this); } draw(){ this.y--; if(this.y < 0){ bullets.splice(bullets.indexOf(this)); } // fillRect Π½Π΅ Ρ‚Ρ€Π΅Π±ΡƒΠ΅Ρ‚ openPath! ctx.fillStyle = "blue"; ctx.fillRect(this.x, this.y, bulletWidth, bulletHeight); } } function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); drawPlayer(); // рисуСм ΠΏΡƒΠ»ΠΈ bullets.forEach(bullet => bullet.draw()); if(pressedLeft && 0 < playerX) { playerX -= 5; } if(pressedRight && playerX < canvas.width - playerWidth) { playerX += 5; } if(pressedSpace ) { new Bullet(); } } setInterval(draw, 10); 
     #d1 { border: 1px solid black; } 
     <canvas id="d1" width="250" height="250" ></canvas> 

    In order to be just like on the gif, you need to add to the player the reload time to igerit bullets only if the player is charged (charging takes place after a timeout after the shot)

    • biting: D keep a plus sign - Excess Suslik

     var canvas = document.getElementById("d1"); var ctx = canvas.getContext("2d"); var pressedSpace = false; var pressedRight = false; var pressedLeft = false; document.addEventListener("keydown", keyDown, false); document.addEventListener("keyup", keyUp, false); function keyDown(e) { if(e.keyCode == 32) { pressedSpace = true; } else if(e.keyCode == 37) { pressedRight = true; } else if(e.keyCode == 39) { pressedLeft = true; } } function keyUp(e) { if(e.keyCode == 32) { pressedSpace = false; } else if(e.keyCode == 37) { pressedRight = false; } else if(e.keyCode == 39) { pressedLeft = false; } } var bullet = []; var player = { x: (canvas.width-20)/2, y: canvas.height - 25, pW: 20, pH: 20, timer: 0, bullets: 0, draw: function() { ctx.beginPath(); ctx.rect(this.x, this.y, this.pW, this.pH); ctx.fillStyle = "black"; ctx.fill(); ctx.closePath(); } } var dBullet = { draw: function() { ctx.beginPath(); ctx.rect(bullet[i].x, bullet[i].y, 5, 5); ctx.fillStyle = "red"; ctx.fill(); ctx.closePath(); } } function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (i = 0; i < bullet.length; i++) { bullet[i].x += bullet[i].vx; bullet[i].y -= bullet[i].vy dBullet.draw(); } player.timer++; if(player.timer % 12 == 0) { player.bullets = 0; } if(pressedSpace) { if(player.bullets < 5) { bullet.push ( { x: player.x + player.pW/2 - 5/2, y: player.y, vx: 0, vy: 10, } ); player.bullets++; } } player.draw(); if(pressedRight && 0 < player.x) { player.x -= 3; } if(pressedLeft && player.x < canvas.width - player.pW) { player.x += 3; } } setInterval(draw, 1000/60); 
     #d1 { border: 1px solid black; } 
     <canvas id="d1" width="300" height="300"></canvas>