From f21f6c82fffccf74ed006214cbd41d022d863a2e Mon Sep 17 00:00:00 2001 From: Mik1-1 Date: Wed, 18 Dec 2024 18:18:17 +0100 Subject: [PATCH 1/4] Add files via upload --- Space Alien Invaders.js | 152 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 Space Alien Invaders.js diff --git a/Space Alien Invaders.js b/Space Alien Invaders.js new file mode 100644 index 0000000000..f057fb35d5 --- /dev/null +++ b/Space Alien Invaders.js @@ -0,0 +1,152 @@ +// Space Alien Invaders Game +const canvas = document.createElement('canvas'); +const ctx = canvas.getContext('2d'); +document.body.appendChild(canvas); + +// Set canvas dimensions +canvas.width = 800; +canvas.height = 600; + +// Player settings +const player = { + x: canvas.width / 2 - 25, + y: canvas.height - 60, + width: 50, + height: 50, + color: 'grey', + speed: 5, + bullets: [] +}; + +// Alien settings +let aliens = []; +const alienRowsBase = 3; +const alienColsBase = 8; +const alienSize = 40; +const alienPadding = 10; +let alienSpeed = 1; +let alienDirection = 1; +let level = 1; + +// Initialize aliens +function createAliens() { + aliens = []; + for (let row = 0; row < alienRowsBase + level - 1; row++) { + for (let col = 0; col < alienColsBase; col++) { + aliens.push({ + x: col * (alienSize + alienPadding) + 50, + y: row * (alienSize + alienPadding) + 30, + width: alienSize, + height: alienSize, + color: 'green' + }); + } + } +} +createAliens(); + +// Key controls +const keys = {}; +window.addEventListener('keydown', (e) => { + keys[e.key] = true; + if (e.key === ' ' && player.bullets.length < 5) { + player.bullets.push({ + x: player.x + player.width / 2 - 5, + y: player.y, + width: 10, + height: 20, + color: 'red', + speed: 7 + }); + } +}); +window.addEventListener('keyup', (e) => keys[e.key] = false); + +// Game loop +function update() { + // Move player + if (keys['ArrowLeft'] && player.x > 0) player.x -= player.speed; + if (keys['ArrowRight'] && player.x + player.width < canvas.width) player.x += player.speed; + + // Update bullets + player.bullets.forEach((bullet, index) => { + bullet.y -= bullet.speed; + if (bullet.y < 0) player.bullets.splice(index, 1); + }); + + // Move aliens + let edgeReached = false; + aliens.forEach(alien => { + alien.x += alienSpeed * alienDirection; + if (alien.x + alien.width > canvas.width || alien.x < 0) edgeReached = true; + }); + + if (edgeReached) { + alienDirection *= -1; + aliens.forEach(alien => alien.y += alienSize / 2); + } + + // Check collisions + player.bullets.forEach((bullet, bIndex) => { + aliens.forEach((alien, aIndex) => { + if ( + bullet.x < alien.x + alien.width && + bullet.x + bullet.width > alien.x && + bullet.y < alien.y + alien.height && + bullet.y + bullet.height > alien.y + ) { + player.bullets.splice(bIndex, 1); + aliens.splice(aIndex, 1); + } + }); + }); + + // Check for next level + if (aliens.length === 0) { + level++; + alienSpeed += 0.5; // Increase alien speed + createAliens(); + } + + // Game over if aliens reach player + aliens.forEach(alien => { + if (alien.y + alien.height > player.y) { + alert(`Game Over! You reached level ${level}.`); + document.location.reload(); + } + }); +} + +function draw() { + // Clear canvas + ctx.clearRect(0, 0, canvas.width, canvas.height); + + // Draw player + ctx.fillStyle = player.color; + ctx.fillRect(player.x, player.y, player.width, player.height); + + // Draw bullets + player.bullets.forEach(bullet => { + ctx.fillStyle = bullet.color; + ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height); + }); + + // Draw aliens + aliens.forEach(alien => { + ctx.fillStyle = alien.color; + ctx.fillRect(alien.x, alien.y, alien.width, alien.height); + }); + + // Draw level info + ctx.fillStyle = 'black'; + ctx.font = '20px Arial'; + ctx.fillText(`Level: ${level}`, 10, 20); +} + +function loop() { + update(); + draw(); + requestAnimationFrame(loop); +} + +loop(); From 7b09a404f068a0b66b29c170d42de156c7d15c3f Mon Sep 17 00:00:00 2001 From: Gus Ruben <95830851+gusruben@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:18:30 -0500 Subject: [PATCH 2/4] Add metadata --- Space Alien Invaders.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Space Alien Invaders.js b/Space Alien Invaders.js index f057fb35d5..d664bc334f 100644 --- a/Space Alien Invaders.js +++ b/Space Alien Invaders.js @@ -1,3 +1,9 @@ +/* +@title: Space Alien Invaders +@author: Mik1 +@tags: [] +@addedOn: 2024-12-18 +*/ // Space Alien Invaders Game const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); From 87cd535fcb0a1c66d3e62467335fdf01759ea658 Mon Sep 17 00:00:00 2001 From: Gus Ruben <95830851+gusruben@users.noreply.github.com> Date: Fri, 20 Dec 2024 16:28:02 -0500 Subject: [PATCH 3/4] Fix filename --- Space Alien Invaders.js => Space_Alien_Invaders.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Space Alien Invaders.js => Space_Alien_Invaders.js (100%) diff --git a/Space Alien Invaders.js b/Space_Alien_Invaders.js similarity index 100% rename from Space Alien Invaders.js rename to Space_Alien_Invaders.js From 2848fbeb67b246c6dc80a79778aa1c4747de10a4 Mon Sep 17 00:00:00 2001 From: graham Date: Thu, 2 Jan 2025 14:25:53 -0500 Subject: [PATCH 4/4] Rename Space_Alien_Invaders.js to games/Space_Alien_Invaders.js --- Space_Alien_Invaders.js => games/Space_Alien_Invaders.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Space_Alien_Invaders.js => games/Space_Alien_Invaders.js (100%) diff --git a/Space_Alien_Invaders.js b/games/Space_Alien_Invaders.js similarity index 100% rename from Space_Alien_Invaders.js rename to games/Space_Alien_Invaders.js