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

[Sprig App] game1 #2820

Closed
wants to merge 2 commits into from
Closed
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
171 changes: 171 additions & 0 deletions games/game1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
@title: Push Box Challenge
@author: 89Aman
@tags: []
@addedOn: 2025-01-27
*/

// Define the sprites used in the game
const playerSprite = "p";
const boxSprite = "b";
const goalSprite = "g";
const wallSprite = "w";

// Assign bitmap art to each sprite
setLegend(
[ playerSprite, bitmap`
................
................
................
.......0........
.....00.000.....
....0.....00....
....0.0.0..0....
....0......0....
....0......0....
....00....0.....
......00000.....
......0...0.....
....000...000...
................
................
................`],
[ boxSprite, bitmap`
................
................
................
...88888888888..
...8....8....8..
...8....8....8..
...8....8....8..
...8....8....8..
...88888888888..
...8....8....8..
...8....8....8..
...8....8....8..
...8....8....8..
...88888888888..
................
................`],
[ goalSprite, bitmap`
................
................
................
....444444......
...44....44.....
...4......4.....
...4.......4....
...4.......4....
...4.......4....
...44......4....
....4......4....
....44....44....
.....444444.....
................
................
................`],
[ wallSprite, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`]
);

// Define the levels for the game
let currentLevelIndex = 0;
const gameLevels = [
map`
..p.
.b.g
....`,
map`
p..
.b.
..g`,
map`
p.wg
.bw.
..w.
..w.`,
map`
p...
...b
...b
.bbg`,
map`
...
.p.
...`,
map`
p.w.
.bwg
....
..bg`
];

// Set the initial map to the first level
setMap(gameLevels[currentLevelIndex]);

// Define solid objects
setSolids([playerSprite, boxSprite, wallSprite]);

// Configure the player to not push other objects
setPushables({
[playerSprite]: []
});

// Player movement controls
onInput("w", () => {
getFirst(playerSprite).y -= 1;
});

onInput("a", () => {
getFirst(playerSprite).x -= 1;
});

onInput("d", () => {
getFirst(playerSprite).x += 1;
});

onInput("s", () => {
getFirst(playerSprite).y += 1;
});

// Reset level on "j" input
onInput("j", () => {
const levelMap = gameLevels[currentLevelIndex];
if (levelMap !== undefined) {
clearText("");
setMap(levelMap);
}
});

// After each input, check the game's progress
afterInput(() => {
const totalGoals = tilesWith(goalSprite).length;
const goalsCovered = tilesWith(goalSprite, boxSprite).length;

// If all goals are covered, move to the next level
if (goalsCovered === totalGoals) {
currentLevelIndex++;

const nextLevelMap = gameLevels[currentLevelIndex];
if (nextLevelMap !== undefined) {
setMap(nextLevelMap);
} else {
addText("You win!", { y: 4, color: color`3` });
}
}
});
Loading