Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not-so_space_invaders #2394

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 224 additions & 0 deletions games/Not-so_space_invaders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/*
Dodge the falling obstacles by moving left or right.
Survive as long as you can!
*/

// Define the player and obstacle sprites
const player = "p";
const obstacle = "o";
const outline = "b";
const blacktile = "d";

// Sprites setup
setLegend(
[player, bitmap`
2000000000000002
0000000LL0000000
0000000LL0000000
0000000LL0000000
2000000LL0000002
000000L77L000000
000000L77L000000
00000L1771L00000
2000L117711L0002
0000L111111L0000
000L11111111L000
000L11111111L000
200LLLLLLLLLL002
00000LL00LL00000
0000066006600000
0000006006000000`],
[obstacle, bitmap`
................
................
................
.......000000...
....0001111110..
...011L111LL10..
...01111111L10..
...01111111110..
....01L111L110..
....01111L1110..
.....01111000...
.....001100.....
.......00.......
................
................
................`],
[outline, bitmap`
0..............0
................
................
................
0..............0
................
................
................
0..............0
................
................
................
0..............0
................
................
................`],
[blacktile, bitmap`
2000000000000002
0000000000000000
0000000000000000
0000000000000000
2000000000000002
0000000000000000
0000000000000000
0000000000000000
2000000000000002
0000000000000000
0000000000000000
0000000000000000
2000000000000002
0000000000000000
0000000000000000
0000000000000000`]
);

// Create game map with an empty space at the bottom for player movement
setMap(map`
ddddd
ddddd
ddddd
ddddd
ddddd
ddddd
ddddd
ddddd
ddddd
ddddd
ddddd
ddddd
pdddd`);

// Initialize the score
let score = 0;
let obstacleSpeed = 200; // Initial speed of obstacles (in milliseconds)
let spawnInterval = 1000; // Initial spawn interval for obstacles

// Display the score on the screen
function displayScore() {
// Clear previous score text
clearText();

// Add score text in two lines
addText(`Score:`, { x: 0, y: 0, color: color`3` });
addText(`${score}`, { x: 0, y: 1, color: color`3` });
}

// Function to reset the game state
function resetGame() {
score = 0;
obstacleSpeed = 200;
spawnInterval = 1000;

// Clear the map and display player
setMap(map`
ddddd
ddddd
ddddd
ddddd
ddddd
ddddd
ddddd
ddddd
ddddd
ddddd
ddddd
ddddd
pdddd`);

displayScore();

// Restart intervals
obstacleInterval = setInterval(spawnObstacle, spawnInterval);
gameInterval = setInterval(moveObstacles, obstacleSpeed);
scoreInterval = setInterval(updateScore, 1000);
}

// Create game loop to update score
function updateScore() {
score++;
displayScore();

// Increase speed and spawn rate every 10 points
if (score % 10 === 0) {
obstacleSpeed = Math.max(50, obstacleSpeed - 100); // Decrease fall speed significantly
spawnInterval = Math.max(200, spawnInterval - 200); // Decrease spawn interval significantly

// Clear and reset intervals with new values
clearInterval(gameInterval);
gameInterval = setInterval(moveObstacles, obstacleSpeed); // Update movement speed

clearInterval(obstacleInterval);
obstacleInterval = setInterval(spawnObstacle, spawnInterval); // Update spawn rate
}
}

// Define controls for player movement
onInput("a", () => {
// Move left
const playerSprite = getFirst(player);
if (playerSprite.x > 0) {
// Leave a black tile behind
addSprite(playerSprite.x, playerSprite.y, blacktile);
playerSprite.x -= 1;
}
});

onInput("d", () => {
// Move right
const playerSprite = getFirst(player);
if (playerSprite.x < 4) {
// Leave a black tile behind
addSprite(playerSprite.x, playerSprite.y, blacktile);
playerSprite.x += 1;
}
});

// Function to randomly spawn obstacles
function spawnObstacle() {
let xPosition = Math.floor(Math.random() * 5);
addSprite(xPosition, 0, obstacle);
}

// Function to move obstacles down
function moveObstacles() {
getAll(obstacle).forEach(obs => {
obs.y += 1;
// If an obstacle reaches the bottom, remove it
if (obs.y > 11) {
obs.remove();
}
// Check if the player gets hit
if (obs.x === getFirst(player).x && obs.y === getFirst(player).y) {
endGame();
}
});
}

// End the game if the player is hit
function endGame() {
clearInterval(gameInterval);
clearInterval(obstacleInterval);
clearInterval(scoreInterval);
addText("Game Over", { y: 6, color: color`3` });
addText(`Final Score: ${score}`, { y: 8, color: color`3` });

// Add retry button
addText(`Retry? Press W`, { x: 3, y: 10, color: color`3` });
onInput("w", () => {
resetGame(); // Reset game on input
});
}

// Set initial intervals to keep spawning obstacles and moving them
let obstacleInterval = setInterval(spawnObstacle, spawnInterval); // Spawn every second
let gameInterval = setInterval(moveObstacles, obstacleSpeed); // Move obstacles initially
let scoreInterval = setInterval(updateScore, 1000); // Update score every second
Binary file added games/img/Not-so_space_invaders.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.