Francis89074 commited on
Commit
785b50d
·
verified ·
1 Parent(s): 8f79887

Update spaceinvaders.html

Browse files
Files changed (1) hide show
  1. spaceinvaders.html +193 -0
spaceinvaders.html CHANGED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Space Invaders</title>
5
+ <style>
6
+ canvas {
7
+ border: 1px solid black;
8
+ display: block;
9
+ margin: 0 auto;
10
+ background: black;
11
+ }
12
+ </style>
13
+ </head>
14
+ <body>
15
+ <canvas id="gameCanvas" width="800" height="600"></canvas>
16
+
17
+ <script>
18
+ const canvas = document.getElementById('gameCanvas');
19
+ const ctx = canvas.getContext('2d');
20
+
21
+ // Player
22
+ const player = {
23
+ x: canvas.width/2 - 25,
24
+ y: canvas.height - 50,
25
+ width: 50,
26
+ height: 30,
27
+ speed: 5,
28
+ dx: 0
29
+ };
30
+
31
+ // Bullets
32
+ const bullets = [];
33
+ const bulletSpeed = -10;
34
+ let canShoot = true;
35
+
36
+ // Aliens
37
+ const aliens = [];
38
+ const alienRows = 5;
39
+ const alienCols = 10;
40
+ const alienWidth = 40;
41
+ const alienHeight = 30;
42
+ const alienSpeed = 1;
43
+ let alienDirection = 1;
44
+ let alienMoveDown = false;
45
+
46
+ // Score
47
+ let score = 0;
48
+
49
+ // Initialize aliens
50
+ function createAliens() {
51
+ for (let row = 0; row < alienRows; row++) {
52
+ for (let col = 0; col < alienCols; col++) {
53
+ aliens.push({
54
+ x: col * (alienWidth + 10) + 50,
55
+ y: row * (alienHeight + 10) + 50,
56
+ width: alienWidth,
57
+ height: alienHeight,
58
+ alive: true
59
+ });
60
+ }
61
+ }
62
+ }
63
+
64
+ // Controls
65
+ document.addEventListener('keydown', (e) => {
66
+ if (e.key === 'ArrowLeft') player.dx = -player.speed;
67
+ if (e.key === 'ArrowRight') player.dx = player.speed;
68
+ if (e.key === ' ' && canShoot) {
69
+ bullets.push({
70
+ x: player.x + player.width/2 - 2,
71
+ y: player.y,
72
+ width: 4,
73
+ height: 10
74
+ });
75
+ canShoot = false;
76
+ setTimeout(() => canShoot = true, 300);
77
+ }
78
+ });
79
+
80
+ document.addEventListener('keyup', (e) => {
81
+ if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') player.dx = 0;
82
+ });
83
+
84
+ function update() {
85
+ // Move player
86
+ player.x += player.dx;
87
+ if (player.x < 0) player.x = 0;
88
+ if (player.x + player.width > canvas.width) player.x = canvas.width - player.width;
89
+
90
+ // Move bullets
91
+ bullets.forEach((bullet, index) => {
92
+ bullet.y += bulletSpeed;
93
+ if (bullet.y < 0) bullets.splice(index, 1);
94
+ });
95
+
96
+ // Move aliens
97
+ let reachedEdge = false;
98
+ aliens.forEach(alien => {
99
+ if (alien.alive) {
100
+ alien.x += alienSpeed * alienDirection;
101
+ if (alien.x <= 0 || alien.x + alien.width >= canvas.width) reachedEdge = true;
102
+ }
103
+ });
104
+
105
+ if (reachedEdge) {
106
+ alienDirection = -alienDirection;
107
+ alienMoveDown = true;
108
+ }
109
+
110
+ if (alienMoveDown) {
111
+ aliens.forEach(alien => alien.y += 10);
112
+ alienMoveDown = false;
113
+ }
114
+
115
+ // Collision detection
116
+ bullets.forEach((bullet, bIndex) => {
117
+ aliens.forEach((alien, aIndex) => {
118
+ if (alien.alive &&
119
+ bullet.x < alien.x + alien.width &&
120
+ bullet.x + bullet.width > alien.x &&
121
+ bullet.y < alien.y + alien.height &&
122
+ bullet.y + bullet.height > alien.y) {
123
+ alien.alive = false;
124
+ bullets.splice(bIndex, 1);
125
+ score += 10;
126
+ }
127
+ });
128
+ });
129
+
130
+ // Check game over
131
+ aliens.forEach(alien => {
132
+ if (alien.alive && alien.y + alien.height >= player.y) {
133
+ alert(`Game Over! Score: ${score}`);
134
+ resetGame();
135
+ }
136
+ });
137
+
138
+ if (aliens.every(alien => !alien.alive)) {
139
+ alert(`You Win! Score: ${score}`);
140
+ resetGame();
141
+ }
142
+ }
143
+
144
+ function draw() {
145
+ // Clear canvas
146
+ ctx.fillStyle = 'black';
147
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
148
+
149
+ // Draw player
150
+ ctx.fillStyle = 'white';
151
+ ctx.fillRect(player.x, player.y, player.width, player.height);
152
+
153
+ // Draw bullets
154
+ ctx.fillStyle = 'red';
155
+ bullets.forEach(bullet => {
156
+ ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
157
+ });
158
+
159
+ // Draw aliens
160
+ ctx.fillStyle = 'green';
161
+ aliens.forEach(alien => {
162
+ if (alien.alive) {
163
+ ctx.fillRect(alien.x, alien.y, alien.width, alien.height);
164
+ }
165
+ });
166
+
167
+ // Draw score
168
+ ctx.fillStyle = 'white';
169
+ ctx.font = '20px Arial';
170
+ ctx.fillText(`Score: ${score}`, 10, 30);
171
+ }
172
+
173
+ function resetGame() {
174
+ aliens.length = 0;
175
+ bullets.length = 0;
176
+ player.x = canvas.width/2 - 25;
177
+ score = 0;
178
+ createAliens();
179
+ }
180
+
181
+ // Game loop
182
+ function gameLoop() {
183
+ update();
184
+ draw();
185
+ requestAnimationFrame(gameLoop);
186
+ }
187
+
188
+ // Start game
189
+ createAliens();
190
+ gameLoop();
191
+ </script>
192
+ </body>
193
+ </html>