From 6a9a7b8556bd883f826d799bf3d3f0a1a2a74955 Mon Sep 17 00:00:00 2001 From: Hijau-dev Date: Sat, 29 Nov 2025 20:46:13 +0700 Subject: [PATCH] JS Update --- Script.js | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/Script.js b/Script.js index 8b66c2d..6f8b485 100644 --- a/Script.js +++ b/Script.js @@ -39,7 +39,7 @@ var bg2 = new Image(); bg2.src = "img/bg_2.png"; - +var missilesArray = []; window.onload = function () { @@ -71,7 +71,7 @@ function keyDownPressed(e) { if (e.keyCode == 32) { keys.fire = true; - alert("fire"); + missilesArray.push(new Missile(player1.x + 120, player1.y + 50, 'white', 12)); } } @@ -113,6 +113,14 @@ function drawGame() { player1.draw(); + for(var i = 0; i < missilesArray.length; i++){ + var m = missilesArray[i]; + m.draw(); + m.update(); + } + + + drawNewText('Score: ' +player1.score, 30, 610, 'white'); drawNewText('Player Lives: ' +player1.lives, 1100, 610, 'white'); } @@ -237,4 +245,27 @@ function addStarField() { background1.update(); background1a.draw(); background1a.update(); +} + +class Missile{ + constructor(x, y, color, speed){ + this.x = x; + this.y = y; + this.width = 3; + this.height = 10; + this.color = color; + this.speed = speed; + } + + draw(){ + ctx.save(); + ctx.fillStyle = this.color; + ctx.fillRect(this.x, this.y, this.width, this.height); + ctx.fill(); + ctx.restore(); + } + + update(){ + this.x += this.speed + } } \ No newline at end of file