From 6e7cf9166c8280425c7949da19973bab337a8422 Mon Sep 17 00:00:00 2001 From: Hijau-dev Date: Thu, 27 Nov 2025 21:55:32 +0700 Subject: [PATCH] Update JS --- Script.js | 63 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/Script.js b/Script.js index e01b245..dde9fa5 100644 --- a/Script.js +++ b/Script.js @@ -34,42 +34,45 @@ function init() { } function keyDownPressed(e) { - if (e.keycode == 81) { + if (e.keyCode == 81) { keys.up = true; - } else if (e.keycode == 65) { + } else if (e.keyCode == 65) { keys.down = true } - if (e.keycode == 79) { + if (e.keyCode == 79) { keys.left = true; } - if (e.keycode == 80) { + if (e.keyCode == 80) { keys.right = true; } - if (e.keycode == 32) { + if (e.keyCode == 32) { keys.fire = true; alert("fire"); } } + + + function keyUpPressed(e) { - if (e.keycode == 81) { + if (e.keyCode == 81) { keys.up = false; - } else if (e.keycode == 65) { + } else if (e.keyCode == 65) { keys.down = false; } - if (e.keycode == 79) { + if (e.keyCode == 79) { keys.left = false; } - if (e.keycode == 80) { + if (e.keyCode == 80) { keys.right = false; } - if (e.keycode == 32) { - keys.fire = false; + if (e.keyCode == 32) { + keys.fire = false; } } @@ -78,13 +81,13 @@ function clearGame() { } function updateGame() { - + player1.update(); game.frames++; } function drawGame() { - + player1.draw(); } function gameLoop(timestamp){ @@ -92,4 +95,36 @@ function gameLoop(timestamp){ updateGame(); drawGame(); window.requestAnimationFrame(gameLoop); -} \ No newline at end of file +} + +class PlayerObject{ + constructor(x,y){ + this.x = x; + this.y = y; + this.width = 200; + this.height = 150; + this.color = 'blue'; + this.speed = 10; + this.lives = 3; + this.score = 0; + this.health = 100; + } + draw(){ + ctx.save(); + ctx.fillStyle = this.color; + ctx.fillRect(this.x, this.y, this.width, this.height); + ctx.fill(); + ctx.restore(); + } + update(){ + if(keys.up){ + this.y -= this.speed; + } + + else if (keys.down){ + this.y += this.speed; + } + } +} + +let player1 = new PlayerObject(100.100); \ No newline at end of file