GarGerry commited on
Commit
a5192e0
·
verified ·
1 Parent(s): 1c9fd5a

Update game.js

Browse files
Files changed (1) hide show
  1. game.js +13 -16
game.js CHANGED
@@ -1,6 +1,7 @@
1
  const canvas = document.getElementById('gameCanvas');
2
  const ctx = canvas.getContext('2d');
3
 
 
4
  canvas.width = 800;
5
  canvas.height = 600;
6
 
@@ -9,6 +10,7 @@ let bullets = [];
9
  let enemies = [];
10
  let gameOver = false;
11
 
 
12
  function startGame() {
13
  spaceship = new Spaceship();
14
  bullets = [];
@@ -18,6 +20,7 @@ function startGame() {
18
  gameLoop();
19
  }
20
 
 
21
  class Spaceship {
22
  constructor() {
23
  this.width = 50;
@@ -26,7 +29,7 @@ class Spaceship {
26
  this.y = canvas.height - this.height - 10;
27
  this.speed = 5;
28
  this.image = new Image();
29
- this.image.src = 'https://via.placeholder.com/50'; // Add spaceship image URL here
30
  }
31
 
32
  move(direction) {
@@ -47,6 +50,7 @@ class Spaceship {
47
  }
48
  }
49
 
 
50
  class Bullet {
51
  constructor(x, y) {
52
  this.x = x;
@@ -66,6 +70,7 @@ class Bullet {
66
  }
67
  }
68
 
 
69
  class Enemy {
70
  constructor() {
71
  this.width = 50;
@@ -85,16 +90,18 @@ class Enemy {
85
  }
86
  }
87
 
 
88
  function gameLoop() {
89
  if (gameOver) {
90
  document.getElementById('gameOver').style.display = 'block';
91
  return;
92
  }
93
 
94
- ctx.clearRect(0, 0, canvas.width, canvas.height);
95
 
96
  spaceship.draw();
97
 
 
98
  bullets.forEach((bullet, index) => {
99
  bullet.move();
100
  bullet.draw();
@@ -103,10 +110,12 @@ function gameLoop() {
103
  }
104
  });
105
 
 
106
  if (Math.random() < 0.02) {
107
  enemies.push(new Enemy());
108
  }
109
 
 
110
  enemies.forEach((enemy, index) => {
111
  enemy.move();
112
  enemy.draw();
@@ -115,6 +124,7 @@ function gameLoop() {
115
  enemies.splice(index, 1);
116
  }
117
 
 
118
  bullets.forEach((bullet, bulletIndex) => {
119
  if (
120
  bullet.x < enemy.x + enemy.width &&
@@ -128,17 +138,4 @@ function gameLoop() {
128
  });
129
  });
130
 
131
- requestAnimationFrame(gameLoop);
132
- }
133
-
134
- document.addEventListener('keydown', (e) => {
135
- if (e.key === 'ArrowLeft') {
136
- spaceship.move('left');
137
- } else if (e.key === 'ArrowRight') {
138
- spaceship.move('right');
139
- } else if (e.key === ' ') {
140
- spaceship.shoot();
141
- }
142
- });
143
-
144
- startGame();
 
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
 
 
10
  let enemies = [];
11
  let gameOver = false;
12
 
13
+ // Fungsi untuk memulai ulang game
14
  function startGame() {
15
  spaceship = new Spaceship();
16
  bullets = [];
 
20
  gameLoop();
21
  }
22
 
23
+ // Membuat objek pesawat
24
  class Spaceship {
25
  constructor() {
26
  this.width = 50;
 
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) {
 
50
  }
51
  }
52
 
53
+ // Membuat objek peluru
54
  class Bullet {
55
  constructor(x, y) {
56
  this.x = x;
 
70
  }
71
  }
72
 
73
+ // Membuat objek musuh
74
  class Enemy {
75
  constructor() {
76
  this.width = 50;
 
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();
 
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();
 
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 &&
 
138
  });
139
  });
140
 
141
+ requestAnimationFrame(gameLoop); // Loop permainan