mirror of
https://github.com/csd4ni3l/loginween.git
synced 2026-01-01 12:33:49 +01:00
Add whack a pumpkin and best score, move games to game_info and setup_game, make a modular basegame template and build upon that, fix game settings not using the current game specific settings
This commit is contained in:
114
static/js/whack_a_pumpkin.js
Normal file
114
static/js/whack_a_pumpkin.js
Normal file
@@ -0,0 +1,114 @@
|
||||
function game_info() {
|
||||
const SETTINGS = {
|
||||
"Graphics": {
|
||||
"Anti-Aliasing": {"type": "bool", "default": "true"},
|
||||
"Texture Filtering": {"type": "option", "options": ["Nearest", "Linear"], "default": "Linear"},
|
||||
"FPS Limit": {"type": "slider", "min": 0, "max": 480, "default": 60},
|
||||
},
|
||||
"Sound": {
|
||||
"Music": {"type": "bool", "default": "true"},
|
||||
"SFX": {"type": "bool", "default": "true"},
|
||||
"Music Volume": {"type": "slider", "min": 0, "max": 100, "default": 50},
|
||||
"SFX Volume": {"type": "slider", "min": 0, "max": 100, "default": 50},
|
||||
}
|
||||
};
|
||||
|
||||
return ["Whack a Pumpkin", SETTINGS];
|
||||
}
|
||||
|
||||
function spawn_pumpkin(pumpkin_spaces, used_slots) {
|
||||
const free_slots = pumpkin_spaces.filter((_, i) => !used_slots.has(i));
|
||||
const random_index = Math.floor(Math.random() * free_slots.length);
|
||||
const [x, y] = free_slots[random_index];
|
||||
|
||||
const pumpkin_sprite = add([
|
||||
sprite("pumpkin"),
|
||||
pos(x, y),
|
||||
area(),
|
||||
"pumpkin"
|
||||
])
|
||||
|
||||
setInterval(() => {
|
||||
destroy(pumpkin_sprite);
|
||||
}, 600);
|
||||
|
||||
return pumpkin_sprite;
|
||||
}
|
||||
|
||||
function setup_game() {
|
||||
loadSprite("bg", "/static/graphics/whackapumpkin.png");
|
||||
loadSprite("pumpkin", "/static/graphics/pumpkin.png");
|
||||
|
||||
const pumpkin_spaces = [
|
||||
[480, 12000],
|
||||
[615, 12000],
|
||||
[750, 12000],
|
||||
|
||||
[480, 420],
|
||||
[615, 420],
|
||||
[750, 420],
|
||||
|
||||
[480, 540],
|
||||
[615, 540],
|
||||
[750, 540],
|
||||
];
|
||||
|
||||
scene("play", () => {
|
||||
const pumpkins = [];
|
||||
const used_slots = new Set();
|
||||
const start = performance.now();
|
||||
|
||||
let game_over = false;
|
||||
let score = 0;
|
||||
let high_score = Number(localStorage.getItem("whackapumpkin_high_score"));
|
||||
|
||||
const bg = add([
|
||||
sprite("bg"),
|
||||
pos(420, 15),
|
||||
scale(0.85)
|
||||
]);
|
||||
|
||||
create_button(5, 5, 150, 75, "Back", color(127, 127, 127), color(0, 0, 0, 0), scene_lambda("main_menu"))
|
||||
const info_label = create_label(525, 50, `Time left: 120s\nScore: ${score}\nHigh Score: ${high_score}`);
|
||||
|
||||
function spawn_pumpkins() {
|
||||
pumpkins.push(spawn_pumpkin(pumpkin_spaces, used_slots));
|
||||
if (!game_over) {
|
||||
setTimeout(spawn_pumpkins, Math.random() * 1500);
|
||||
}
|
||||
}
|
||||
|
||||
setTimeout(spawn_pumpkins, Math.random() * 1500);
|
||||
|
||||
onClick("pumpkin", (pumpkin) => {
|
||||
destroy(pumpkin);
|
||||
|
||||
score += 1;
|
||||
if (score > high_score) {
|
||||
high_score = score;
|
||||
localStorage.setItem("whackapumpkin_high_score", high_score);
|
||||
}
|
||||
})
|
||||
|
||||
bg.onUpdate(() => {
|
||||
const elapsed = performance.now() - start;
|
||||
|
||||
if ((elapsed / 1000) >= 120) {
|
||||
create_label(520, 12020, `Game Over!\nScore: ${score}\nHigh Score: ${high_score}`, 48);
|
||||
|
||||
game_over = true;
|
||||
|
||||
for (const pumpkin of pumpkins) {
|
||||
destroy(pumpkin);
|
||||
}
|
||||
|
||||
destroy(bg);
|
||||
destroy(info_label);
|
||||
}
|
||||
else {
|
||||
info_label.text = `Time left: ${(120 - (elapsed / 1000)).toFixed(1)}s\nScore: ${score}\nHigh Score: ${high_score}`;
|
||||
}
|
||||
|
||||
})
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user