Add Music + Sound Effects + Enemy
This commit is contained in:
parent
6a9a7b8556
commit
e0d53d391a
86
Script.js
86
Script.js
@ -26,8 +26,11 @@ var keys = {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Untuk background
|
||||||
var playerShipImg = new Image();
|
var playerShipImg = new Image();
|
||||||
playerShipImg.src = 'img/pixelShip.png'
|
playerShipImg.src = 'img/fighterShip.png'
|
||||||
|
|
||||||
var bg0 = new Image();
|
var bg0 = new Image();
|
||||||
bg0.src = 'img/bg_0.png';
|
bg0.src = 'img/bg_0.png';
|
||||||
@ -39,9 +42,38 @@ var bg2 = new Image();
|
|||||||
bg2.src = "img/bg_2.png";
|
bg2.src = "img/bg_2.png";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var enemyImgArray = [];
|
||||||
|
enemyImgArray.length = 7;
|
||||||
|
|
||||||
|
var enemy1 = new Image();
|
||||||
|
enemy1.src = "img/alien_0.png"
|
||||||
|
|
||||||
|
|
||||||
var missilesArray = [];
|
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 () {
|
window.onload = function () {
|
||||||
init()
|
init()
|
||||||
}
|
}
|
||||||
@ -57,6 +89,8 @@ function init() {
|
|||||||
function keyDownPressed(e) {
|
function keyDownPressed(e) {
|
||||||
if (e.keyCode == 87) {
|
if (e.keyCode == 87) {
|
||||||
keys.up = true;
|
keys.up = true;
|
||||||
|
backgroundMusic.play();
|
||||||
|
backgroundMusic.volume = 0.8;
|
||||||
} else if (e.keyCode == 83) {
|
} else if (e.keyCode == 83) {
|
||||||
keys.down = true
|
keys.down = true
|
||||||
}
|
}
|
||||||
@ -72,6 +106,11 @@ function keyDownPressed(e) {
|
|||||||
if (e.keyCode == 32) {
|
if (e.keyCode == 32) {
|
||||||
keys.fire = true;
|
keys.fire = true;
|
||||||
missilesArray.push(new Missile(player1.x + 120, player1.y + 50, 'white', 12));
|
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);
|
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function updateGame() {
|
function updateGame() {
|
||||||
|
|
||||||
addStarField();
|
addStarField();
|
||||||
@ -109,10 +151,16 @@ function updateGame() {
|
|||||||
game.frames++;
|
game.frames++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function drawGame() {
|
function drawGame() {
|
||||||
|
|
||||||
player1.draw();
|
player1.draw();
|
||||||
|
|
||||||
|
enemy.draw();
|
||||||
|
|
||||||
|
enemy.update();
|
||||||
|
|
||||||
for(var i = 0; i < missilesArray.length; i++){
|
for(var i = 0; i < missilesArray.length; i++){
|
||||||
var m = missilesArray[i];
|
var m = missilesArray[i];
|
||||||
m.draw();
|
m.draw();
|
||||||
@ -268,4 +316,38 @@ class Missile{
|
|||||||
update(){
|
update(){
|
||||||
this.x += this.speed
|
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
BIN
img/alien_0.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 529 KiB |
BIN
img/alien_1.png
Normal file
BIN
img/alien_1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 67 KiB |
BIN
img/alien_2.png
Normal file
BIN
img/alien_2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 122 KiB |
BIN
img/alien_3.png
Normal file
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
BIN
img/fighterShip.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 297 KiB |
BIN
music/Muriel-BobbyRichards.mp3
Normal file
BIN
music/Muriel-BobbyRichards.mp3
Normal file
Binary file not shown.
BIN
music/laser.mp3
Normal file
BIN
music/laser.mp3
Normal file
Binary file not shown.
BIN
music/laser2.mp3
Normal file
BIN
music/laser2.mp3
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user