Delete game.js
Browse files
game.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
| 1 |
-
const canvas = document.getElementById('gameCanvas');
|
| 2 |
-
const ctx = canvas.getContext('2d');
|
| 3 |
-
|
| 4 |
-
// Menetapkan ukuran canvas
|
| 5 |
-
canvas.width = 800;
|
| 6 |
-
canvas.height = 600;
|
| 7 |
-
|
| 8 |
-
let spaceship;
|
| 9 |
-
let bullets = [];
|
| 10 |
-
let enemies = [];
|
| 11 |
-
let gameOver = false;
|
| 12 |
-
|
| 13 |
-
// Fungsi untuk memulai ulang game
|
| 14 |
-
function startGame() {
|
| 15 |
-
spaceship = new Spaceship();
|
| 16 |
-
bullets = [];
|
| 17 |
-
enemies = [];
|
| 18 |
-
gameOver = false;
|
| 19 |
-
document.getElementById('gameOver').style.display = 'none';
|
| 20 |
-
gameLoop();
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
// Membuat objek pesawat
|
| 24 |
-
class Spaceship {
|
| 25 |
-
constructor() {
|
| 26 |
-
this.width = 50;
|
| 27 |
-
this.height = 50;
|
| 28 |
-
this.x = canvas.width / 2 - this.width / 2;
|
| 29 |
-
this.y = canvas.height - this.height - 10;
|
| 30 |
-
this.speed = 5;
|
| 31 |
-
this.image = new Image();
|
| 32 |
-
this.image.src = 'https://via.placeholder.com/50/00ff99/000000?text=%E2%9C%94'; // Gambar pesawat
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
move(direction) {
|
| 36 |
-
if (direction === 'left' && this.x > 0) {
|
| 37 |
-
this.x -= this.speed;
|
| 38 |
-
} else if (direction === 'right' && this.x + this.width < canvas.width) {
|
| 39 |
-
this.x += this.speed;
|
| 40 |
-
}
|
| 41 |
-
}
|
| 42 |
-
|
| 43 |
-
draw() {
|
| 44 |
-
ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
shoot() {
|
| 48 |
-
const bullet = new Bullet(this.x + this.width / 2 - 5, this.y);
|
| 49 |
-
bullets.push(bullet);
|
| 50 |
-
}
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
// Membuat objek peluru
|
| 54 |
-
class Bullet {
|
| 55 |
-
constructor(x, y) {
|
| 56 |
-
this.x = x;
|
| 57 |
-
this.y = y;
|
| 58 |
-
this.width = 10;
|
| 59 |
-
this.height = 20;
|
| 60 |
-
this.speed = 5;
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
move() {
|
| 64 |
-
this.y -= this.speed;
|
| 65 |
-
}
|
| 66 |
-
|
| 67 |
-
draw() {
|
| 68 |
-
ctx.fillStyle = '#00ff99';
|
| 69 |
-
ctx.fillRect(this.x, this.y, this.width, this.height);
|
| 70 |
-
}
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
// Membuat objek musuh
|
| 74 |
-
class Enemy {
|
| 75 |
-
constructor() {
|
| 76 |
-
this.width = 50;
|
| 77 |
-
this.height = 50;
|
| 78 |
-
this.x = Math.random() * (canvas.width - this.width);
|
| 79 |
-
this.y = -50;
|
| 80 |
-
this.speed = 2;
|
| 81 |
-
}
|
| 82 |
-
|
| 83 |
-
move() {
|
| 84 |
-
this.y += this.speed;
|
| 85 |
-
}
|
| 86 |
-
|
| 87 |
-
draw() {
|
| 88 |
-
ctx.fillStyle = '#ff4444';
|
| 89 |
-
ctx.fillRect(this.x, this.y, this.width, this.height);
|
| 90 |
-
}
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
// Fungsi utama untuk menggambar dan mengupdate game setiap frame
|
| 94 |
-
function gameLoop() {
|
| 95 |
-
if (gameOver) {
|
| 96 |
-
document.getElementById('gameOver').style.display = 'block';
|
| 97 |
-
return;
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
-
ctx.clearRect(0, 0, canvas.width, canvas.height); // Bersihkan layar
|
| 101 |
-
|
| 102 |
-
spaceship.draw();
|
| 103 |
-
|
| 104 |
-
// Gerakkan dan gambar peluru
|
| 105 |
-
bullets.forEach((bullet, index) => {
|
| 106 |
-
bullet.move();
|
| 107 |
-
bullet.draw();
|
| 108 |
-
if (bullet.y < 0) {
|
| 109 |
-
bullets.splice(index, 1);
|
| 110 |
-
}
|
| 111 |
-
});
|
| 112 |
-
|
| 113 |
-
// Menambahkan musuh baru secara acak
|
| 114 |
-
if (Math.random() < 0.02) {
|
| 115 |
-
enemies.push(new Enemy());
|
| 116 |
-
}
|
| 117 |
-
|
| 118 |
-
// Gerakkan dan gambar musuh
|
| 119 |
-
enemies.forEach((enemy, index) => {
|
| 120 |
-
enemy.move();
|
| 121 |
-
enemy.draw();
|
| 122 |
-
|
| 123 |
-
if (enemy.y > canvas.height) {
|
| 124 |
-
enemies.splice(index, 1);
|
| 125 |
-
}
|
| 126 |
-
|
| 127 |
-
// Cek tabrakan antara peluru dan musuh
|
| 128 |
-
bullets.forEach((bullet, bulletIndex) => {
|
| 129 |
-
if (
|
| 130 |
-
bullet.x < enemy.x + enemy.width &&
|
| 131 |
-
bullet.x + bullet.width > enemy.x &&
|
| 132 |
-
bullet.y < enemy.y + enemy.height &&
|
| 133 |
-
bullet.y + bullet.height > enemy.y
|
| 134 |
-
) {
|
| 135 |
-
enemies.splice(index, 1);
|
| 136 |
-
bullets.splice(bulletIndex, 1);
|
| 137 |
-
}
|
| 138 |
-
});
|
| 139 |
-
});
|
| 140 |
-
|
| 141 |
-
requestAnimationFrame(gameLoop); // Loop permainan
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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