Add Music + Sound Effects + Enemy

This commit is contained in:
Bluwww 2025-11-30 09:19:11 +07:00
parent 6a9a7b8556
commit e0d53d391a
10 changed files with 84 additions and 2 deletions

View File

@ -26,8 +26,11 @@ var keys = {
// Untuk background
var playerShipImg = new Image();
playerShipImg.src = 'img/pixelShip.png'
playerShipImg.src = 'img/fighterShip.png'
var bg0 = new Image();
bg0.src = 'img/bg_0.png';
@ -39,9 +42,38 @@ var bg2 = new Image();
bg2.src = "img/bg_2.png";
var enemyImgArray = [];
enemyImgArray.length = 7;
var enemy1 = new Image();
enemy1.src = "img/alien_0.png"
var missilesArray = [];
var backgroundMusic = document.createElement('audio');
backgroundMusic.src = "music/Muriel-BobbyRichards.mp3";
var laser = document.createElement("audio");
laser.src = "music/laser2.mp3";
window.onload = function () {
init()
}
@ -57,6 +89,8 @@ function init() {
function keyDownPressed(e) {
if (e.keyCode == 87) {
keys.up = true;
backgroundMusic.play();
backgroundMusic.volume = 0.8;
} else if (e.keyCode == 83) {
keys.down = true
}
@ -72,6 +106,11 @@ function keyDownPressed(e) {
if (e.keyCode == 32) {
keys.fire = true;
missilesArray.push(new Missile(player1.x + 120, player1.y + 50, 'white', 12));
laser.currentTime = 0;
laser.play();
laser.volume = 0.4;
}
}
@ -101,6 +140,9 @@ function clearGame() {
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
}
function updateGame() {
addStarField();
@ -109,10 +151,16 @@ function updateGame() {
game.frames++;
}
function drawGame() {
player1.draw();
enemy.draw();
enemy.update();
for(var i = 0; i < missilesArray.length; i++){
var m = missilesArray[i];
m.draw();
@ -268,4 +316,38 @@ class Missile{
update(){
this.x += this.speed
}
}
}
class EnemyObj {
constructor(x, y, speed) {
this.x = x;
this.y = y;
this.width = 170;
this.height = 105;
this.image = enemy1;
this.speed = speed;
this.health = 100;
this.damage = 10;
}
draw() {
ctx.save();
ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
ctx.restore();
}
update() {
this.x -= this.speed;
}
}
let enemy = new EnemyObj(800, 200, 12);

BIN
img/alien_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 529 KiB

BIN
img/alien_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 KiB

BIN
img/alien_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

BIN
img/alien_3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 209 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

BIN
img/fighterShip.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 297 KiB

Binary file not shown.

BIN
music/laser.mp3 Normal file

Binary file not shown.

BIN
music/laser2.mp3 Normal file

Binary file not shown.