File size: 3,412 Bytes
1c9fd5a
 
 
a5192e0
1c9fd5a
 
 
 
 
 
 
 
a5192e0
1c9fd5a
 
 
 
 
 
 
 
 
a5192e0
1c9fd5a
 
 
 
 
 
 
 
a5192e0
1c9fd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5192e0
1c9fd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5192e0
1c9fd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5192e0
1c9fd5a
 
 
 
 
 
a5192e0
1c9fd5a
 
 
a5192e0
1c9fd5a
 
 
 
 
 
 
 
a5192e0
1c9fd5a
 
 
 
a5192e0
1c9fd5a
 
 
 
 
 
 
 
a5192e0
1c9fd5a
 
 
 
 
 
 
 
 
 
 
 
 
a5192e0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');

// Menetapkan ukuran canvas
canvas.width = 800;
canvas.height = 600;

let spaceship;
let bullets = [];
let enemies = [];
let gameOver = false;

// Fungsi untuk memulai ulang game
function startGame() {
    spaceship = new Spaceship();
    bullets = [];
    enemies = [];
    gameOver = false;
    document.getElementById('gameOver').style.display = 'none';
    gameLoop();
}

// Membuat objek pesawat
class Spaceship {
    constructor() {
        this.width = 50;
        this.height = 50;
        this.x = canvas.width / 2 - this.width / 2;
        this.y = canvas.height - this.height - 10;
        this.speed = 5;
        this.image = new Image();
        this.image.src = 'https://via.placeholder.com/50/00ff99/000000?text=%E2%9C%94';  // Gambar pesawat
    }

    move(direction) {
        if (direction === 'left' && this.x > 0) {
            this.x -= this.speed;
        } else if (direction === 'right' && this.x + this.width < canvas.width) {
            this.x += this.speed;
        }
    }

    draw() {
        ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
    }

    shoot() {
        const bullet = new Bullet(this.x + this.width / 2 - 5, this.y);
        bullets.push(bullet);
    }
}

// Membuat objek peluru
class Bullet {
    constructor(x, y) {
        this.x = x;
        this.y = y;
        this.width = 10;
        this.height = 20;
        this.speed = 5;
    }

    move() {
        this.y -= this.speed;
    }

    draw() {
        ctx.fillStyle = '#00ff99';
        ctx.fillRect(this.x, this.y, this.width, this.height);
    }
}

// Membuat objek musuh
class Enemy {
    constructor() {
        this.width = 50;
        this.height = 50;
        this.x = Math.random() * (canvas.width - this.width);
        this.y = -50;
        this.speed = 2;
    }

    move() {
        this.y += this.speed;
    }

    draw() {
        ctx.fillStyle = '#ff4444';
        ctx.fillRect(this.x, this.y, this.width, this.height);
    }
}

// Fungsi utama untuk menggambar dan mengupdate game setiap frame
function gameLoop() {
    if (gameOver) {
        document.getElementById('gameOver').style.display = 'block';
        return;
    }

    ctx.clearRect(0, 0, canvas.width, canvas.height);  // Bersihkan layar

    spaceship.draw();

    // Gerakkan dan gambar peluru
    bullets.forEach((bullet, index) => {
        bullet.move();
        bullet.draw();
        if (bullet.y < 0) {
            bullets.splice(index, 1);
        }
    });

    // Menambahkan musuh baru secara acak
    if (Math.random() < 0.02) {
        enemies.push(new Enemy());
    }

    // Gerakkan dan gambar musuh
    enemies.forEach((enemy, index) => {
        enemy.move();
        enemy.draw();

        if (enemy.y > canvas.height) {
            enemies.splice(index, 1);
        }

        // Cek tabrakan antara peluru dan musuh
        bullets.forEach((bullet, bulletIndex) => {
            if (
                bullet.x < enemy.x + enemy.width &&
                bullet.x + bullet.width > enemy.x &&
                bullet.y < enemy.y + enemy.height &&
                bullet.y + bullet.height > enemy.y
            ) {
                enemies.splice(index, 1);
                bullets.splice(bulletIndex, 1);
            }
        });
    });

    requestAnimationFrame(gameLoop);  // Loop permainan