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] GamblewithMinesweeper #2566

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
181 changes: 181 additions & 0 deletions games/GamblewithMinesweeper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

@title: MineSweeper
@author:
@tags: []
@addedOn: 2024-00-00
*/

const mine = "m";
const flag = "f";
const uncovered = "u";
const hidden = "h";

// Minesweeper sprites
setLegend(
[ mine, bitmap`
................
................
.....3333.......
....333333......
....333333......
....333333......
.....3333.......
....333333......
....333333......
....333333......
.....3333.......
................
................
................
................
................`],
[ flag, bitmap`
................
................
.....2222.......
.....2222.......
.....2222.......
.....2222.......
.....2222.......
.....2222.......
.....2222.......
.....2222.......
................
................
................
................
................
................`],
[ uncovered, bitmap`
................
................
................
....111111......
....111111......
....111111......
....111111......
................
................
................
................
................
................
................
................
................`],
[ hidden, bitmap`
................
................
................
....555555......
....555555......
....555555......
....555555......
................
................
................
................
................
................
................
................
................`]
);

// Generate a basic Minesweeper level
const levels = [
map`
hhhh
hmhh
hhhh
hhhh`,
];

// Set level dimensions
const levelWidth = 4; // 4 columns
const levelHeight = 4; // 4 rows

let level = 0;
let gameOver = false;
let flagPosition = { x: 0, y: 0 };

setMap(levels[level]);
setSolids([flag]);

// Function to reset the game
function resetGame() {
gameOver = false;
setMap(levels[level]);
addSprite(0, 0, flag); // Reset the flag position
flagPosition = { x: 0, y: 0 };
addText("New Game! Move the flag and start", { color: color`4`, y: 4 });
}

// Add the initial flag sprite and display new game text
resetGame();

// Movement logic with safety check for 'flag' within bounds
onInput("w", () => {
if (gameOver) return;
const f = getFirst(flag);
if (f) f.y = Math.max(0, f.y - 1); // Prevent out-of-bounds movement (min 0)
});

onInput("a", () => {
if (gameOver) return;
const f = getFirst(flag);
if (f) f.x = Math.max(0, f.x - 1); // Prevent out-of-bounds movement (min 0)
});

onInput("s", () => {
if (gameOver) return;
const f = getFirst(flag);
if (f) f.y = Math.min(levelHeight - 1, f.y + 1); // Prevent out-of-bounds movement (max levelHeight - 1)
});

onInput("d", () => {
if (gameOver) return;
const f = getFirst(flag);
if (f) f.x = Math.min(levelWidth - 1, f.x + 1); // Prevent out-of-bounds movement (max levelWidth - 1)
});

// Uncover tile logic
onInput("j", () => {
if (gameOver) return;
const f = getFirst(flag);
if (!f) return;

const tile = getTile(f.x, f.y);
if (tile.some(t => t.type === mine)) {
addText("Game Over!Press i", { color: color`3` });
gameOver = true;
} else if (tile.some(t => t.type === hidden)) {
clearTile(f.x, f.y);
addSprite(f.x, f.y, uncovered);
}
});

// Place or remove flag logic
onInput("k", () => {
if (gameOver) return;
const f = getFirst(flag);
if (!f) return;

const tile = getTile(f.x, f.y);
if (tile.some(t => t.type === hidden)) {
clearTile(f.x, f.y);
addSprite(f.x, f.y, flag);
} else if (tile.some(t => t.type === flag)) {
clearTile(f.x, f.y);
addSprite(f.x, f.y, hidden);
}
});

// Reset the game on 'i' press
onInput("i", () => {
if (!gameOver) return;
resetGame();
});
Loading