diff --git a/games/Defend-Your-House!.js b/games/Defend-Your-House!.js new file mode 100644 index 0000000000..7a5d26f0a3 --- /dev/null +++ b/games/Defend-Your-House!.js @@ -0,0 +1,792 @@ +/* +First time? Check out the tutorial game: +https://sprig.hackclub.com/gallery/getting_started + +@title: Defend Your House! +@author: Gabbs +@tags: [] +@addedOn: 2024-00-00 +*/ +const placed = tune` +161.29032258064515: F4^161.29032258064515 + C5^161.29032258064515, +161.29032258064515: B4^161.29032258064515 + G5^161.29032258064515, +4838.709677419354` +const start = tune` +161.29032258064515: F4^161.29032258064515 + C5^161.29032258064515, +161.29032258064515: B4^161.29032258064515 + G5^161.29032258064515, +161.29032258064515: C5^161.29032258064515 + G5^161.29032258064515 + A5^161.29032258064515 + F5~161.29032258064515 + B5/161.29032258064515, +161.29032258064515: B5/161.29032258064515, +161.29032258064515: B5/161.29032258064515, +161.29032258064515: B5/161.29032258064515, +4193.548387096774` +const gameOver = tune` +500: F5-500, +500: B4-500 + C4/500, +500: E4-500, +500: C4-500, +500: C4/500, +500: C4-500, +13000` +const collision = tune` +84.50704225352112: D5/84.50704225352112, +84.50704225352112: C5/84.50704225352112, +84.50704225352112: E5/84.50704225352112, +2450.7042253521126` +const win = tune` +178.57142857142858: E5/178.57142857142858, +178.57142857142858: E5/178.57142857142858, +178.57142857142858: F5/178.57142857142858 + E5-178.57142857142858 + G5^178.57142857142858, +178.57142857142858: G5/178.57142857142858, +178.57142857142858: G5/178.57142857142858, +178.57142857142858: F5/178.57142857142858 + E5-178.57142857142858 + G5^178.57142857142858, +178.57142857142858: E5/178.57142857142858, +178.57142857142858: D5/178.57142857142858 + C5~178.57142857142858, +178.57142857142858: C5/178.57142857142858, +178.57142857142858: C5/178.57142857142858, +178.57142857142858: D5/178.57142857142858 + E5-178.57142857142858 + C5~178.57142857142858, +178.57142857142858: E5/178.57142857142858 + D5~178.57142857142858, +178.57142857142858: E5/178.57142857142858, +178.57142857142858: D5/178.57142857142858 + E5-178.57142857142858, +178.57142857142858: D5/178.57142857142858, +178.57142857142858: E5/178.57142857142858 + D5~178.57142857142858, +178.57142857142858: E5/178.57142857142858, +178.57142857142858: F5/178.57142857142858 + E5-178.57142857142858 + G5^178.57142857142858, +178.57142857142858: G5/178.57142857142858, +178.57142857142858: G5/178.57142857142858, +178.57142857142858: F5/178.57142857142858 + E5-178.57142857142858 + G5^178.57142857142858, +178.57142857142858: E5/178.57142857142858, +178.57142857142858: D5/178.57142857142858, +178.57142857142858: C5/178.57142857142858, +178.57142857142858: C5/178.57142857142858, +178.57142857142858: D5/178.57142857142858 + E5-178.57142857142858 + C5~178.57142857142858, +178.57142857142858: E5/178.57142857142858, +178.57142857142858: D5/178.57142857142858 + E5-178.57142857142858 + C5~178.57142857142858, +178.57142857142858: C5/178.57142857142858, +178.57142857142858: C5/178.57142857142858, +178.57142857142858: C4-178.57142857142858, +178.57142857142858: C4-178.57142857142858` + +var z = 1; +var pos = 4; + + +const cursor = "c" +const pizzaOne = "p" +const wall = "w" +const roof = "r" +const roofl = "R" +const centerR = "C" +const center = "ç" +const centerL = "O" +const white = "W" +const wood = "o"; +const downLeftDoor = "d"; +const downRightDoor = "D"; +const upLeftDoor = "u"; +const upRightDoor = "U"; +const ZAP = "z"; +const ROCK = "F"; +const ROCKF = "f"; +var numberOfWood = 15; +var roundSurvived = 0; +var events = 5; +var challenge = false; +var challengeState = false; +var youLost = false; +var randomChallenge = 0; +setLegend( + [ cursor, bitmap` +LLLLLLLLLLLLLLLL +L..............L +L.1........111.L +L..1.........1.L +L............1.L +L..............L +L..............L +L..............L +L..............L +L..............L +L..............L +L..........1.1.L +L.1.........11.L +L.11.......111.L +L..............L +LLLLLLLLLLLLLLLL` ], + [pizzaOne, bitmap ` +CCCCCCCCCCCCCCCC +C33333399333999C +C33333393999333C +C33333339999333C +C99993933333399C +C33339933333999C +C33339999939333C +C99933333399333C +C99993333333999C +C33339999993339C +C33333999939339C +C99993333399339C +C93333333399993C +C93339999933339C +C39993999933339C +CCCCCCCCCCCCCCCC`], + [wall, bitmap` +LLLLLL1LLLLLLLLL +LLLLL11LL1LL11LL +LLLLL111L1LL11LL +11111111L1L111LL +11111111L1L111LL +LL1111LLLLLLLLLL +LLLLLLLLLLLLLLLL +111LL11LL1111111 +LLLLL11111111LLL +L111111111111LLL +L1111111LL111111 +LLLLLLLLLLLLLLLL +LLLLLLLLLLLLLLLL +111111111LL11111 +1111111LLLL11111 +LLLLLLLLLLL111LL`], + [roof, bitmap` +...............C +..............CC +.............CC6 +............CC66 +...........CC6F6 +..........CC66F6 +.........CC6F6F6 +........CC66F6F6 +.......CC6F6F6F6 +......CC66F6F6F6 +.....CC6F6F6F6F6 +....CC66F6F6F6F6 +...CC6F6F6F6F6F6 +..CC66F6F6F6F6F6 +.CC6F6F6F6F6F6F6 +CC66F6F6F6F6F6F6`], + [roofl, bitmap` +C............... +CC.............. +6CC............. +66CC............ +6F6CC........... +6F66CC.......... +6F6F6CC......... +6F6F66CC........ +6F6F6F6CC....... +6F6F6F66CC...... +6F6F6F6F6CC..... +6F6F6F6F66CC.... +6F6F6F6F6F6CC... +6F6F6F6F6F66CC.. +6F6F6F6F6F6F6CC. +6F6F6F6F6F6F66CC`], + [centerR, bitmap` +CCF6F6F6F6F6F6F6 +C6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6`], + [center, bitmap` +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6 +F6F6F6F6F6F6F6F6`], + [centerL, bitmap` +6F6F6F6F6F6F6FCC +6F6F6F6F6F6F6F6C +6F6F6F6F6F6F6F6F +6F6F6F6F6F6F6F6F +6F6F6F6F6F6F6F6F +6F6F6F6F6F6F6F6F +6F6F6F6F6F6F6F6F +6F6F6F6F6F6F6F6F +6F6F6F6F6F6F6F6F +6F6F6F6F6F6F6F6F +6F6F6F6F6F6F6F6F +6F6F6F6F6F6F6F6F +6F6F6F6F6F6F6F6F +6F6F6F6F6F6F6F6F +6F6F6F6F6F6F6F6F +6F6F6F6F6F6F6F6F`], + [white, bitmap` +2222222222222222 +2222222222222222 +2222222222222222 +2222222222222222 +2222222222222222 +2222222222222222 +2222222222222222 +2222222222222222 +2222222222222222 +2222222222222222 +2222222222222222 +2222222222222222 +2222222222222222 +2222222222222222 +2222222222222222 +2222222222222222`], + [wood, bitmap` +CCCCCCCCCCCCCCCC +C11111111111111C +CFFFFFFFFFFFFFFC +CCCCCCCCCCCCCCCC +C11111111111111C +CFFFFFFFFFFFFFFC +CCCCCCCCCCCCCCCC +C11111111111111C +CFFFFFFFFFFFFFFC +CCCCCCCCCCCCCCCC +C11111111111111C +CFFFFFFFFFFFFFFC +CCCCCCCCCCCCCCCC +C11111111111111C +CFFFFFFFFFFFFFFC +CCCCCCCCCCCCCCCC`], + [downLeftDoor, bitmap` +C6CCCCCCCCCCC666 +C6CCCCCCCCCCCCCC +C6CCCCCCCCCCCCCC +C6CCCCCCCCCCCCCC +C6CCCCCCCCCCCCCC +C6CC66666666666C +C6CC66666666666C +C6CC66111111116C +C6CC66111111116C +C6CC66111111116C +C6CC66666666666C +C6CC66666666666C +C6CCCCCCCCCCCCCC +C6CCCCCCCCCCCCCC +C666666666666666 +CCCCCCCCCCCCCCCC`], + [downRightDoor, bitmap` +666CCCCCCCCCCC6C +CCCCCCCCCCCCCC6C +CCCCCCCCCCCCCC6C +CCCCCCCCCCCCCC6C +CCCCCCCCCCCCCC6C +C66666666666CC6C +C66666666666CC6C +C61111111166CC6C +C61111111166CC6C +C61111111166CC6C +C66666666666CC6C +C66666666666CC6C +CCCCCCCCCCCCCC6C +CCCCCCCCCCCCCC6C +666666666666666C +CCCCCCCCCCCCCCCC`], + [upLeftDoor, bitmap` +CCCCCCCCCCCCCCCC +C666666666666666 +C6CCCCCCCCCCCCCC +C6CCCCCCCCCCCCCC +C6CCCCCCCCCCCCCC +C6CCC999999999CC +C6CCC999999999CC +C6CCC999999999CC +C6CCC999999999CC +C6CCCCCCCCCCCCCC +C6CCCCCCCCCCCCCC +C6CCCCCCCCCCCCCC +C6CCCCCCCCCCCCCC +C6CCCCCCCCCCCCCC +C6CCCCCCCCCCCCCC +C6CCCCCCCCCCC661`], + [upRightDoor, bitmap` +CCCCCCCCCCCCCCCC +666666666666666C +CCCCCCCCCCCCCC6C +CCCCCCCCCCCCCC6C +CCCCCCCCCCCCCC6C +CC999999999CCC6C +CC999999999CCC6C +CC999999999CCC6C +CC999999999CCC6C +CCCCCCCCCCCCCC6C +CCCCCCCCCCCCCC6C +CCCCCCCCCCCCCC6C +CCCCCCCCCCCCCC6C +CCCCCCCCCCCCCC6C +CCCCCCCCCCCCCC6C +166CCCCCCCCCCC6C`], + [ROCK, bitmap` +LLLLLLLLLLLLLLLL +LLLLLLLLLLL1111L +LL11111L1111111L +LL11111L1111111L +LL11111L1111111L +LLLLLLLL1111111L +LLLL11LL111LLLLL +L1LL11LL111LLLLL +L1LL11LLLLL111LL +L11111LLLLL111LL +LL11111111L1111L +LLLLLL1111L1111L +LL111L1111L1111L +L1111L1111L1111L +L1LLLL1111111LLL +LLLLLL1111111LLL`], + [ROCKF, bitmap` +LLLLLLLLLLLLLLLL +L1111LLLLLLLLLLL +L1111111L11111LL +L1111111L11111LL +L1111111L11111LL +L1111111LLLLLLLL +LLLLL111LL11LLLL +LLLLL111LL11LL1L +LL111LLLLL11LL1L +LL111LLLLL11111L +L1111L11111111LL +L1111L1111LLLLLL +L1111L1111L111LL +L1111L1111L1111L +LLL1111111LLLL1L +LLL1111111LLLLLL`], + [ZAP, bitmap` +......55555555.. +.....577777775.. +....576666675... +...576666675.... +..576666675..... +...5766675...... +....5766675..... +.....5766675.... +......5766675... +.......5766675.. +........5766675. +.........57675.. +........57675... +.......57675.... +......57675..... +.......555......`] + + +) + +const level = map` +WWWWWWWWWWWWWWWWWWWW +WWWWWWWWWWWWWWWWWWWW +WWWWWWWWWWWWWWWWWWWW +WWWWWWWWWWWWWWWWWWWW +WWW..............WWW +WWW......c.......WWW +WWW..............WWW +WWW..............WWW +WWW..............WWW +WWW......rR......WWW +WWW.....rCOR.....WWW +WWW....rCççOR....WWW +WWW....pppppp....WWW +WWW....ppuUpp....WWW +WWW....ppdDpp....WWW +wwwwwwwwwwwwwwwwwwww` +setMap(level) + +onInput("k", () => { + // Move the player one tile to the right + updateWoodCount(); +}) + +onInput("d", () => { + // Move the player one tile to the right + getFirst(cursor).x += 1 +}) + +onInput("s", () => { + // Move the player one tile down + getFirst(cursor).y += 1 +}) + +onInput("a", () => { + // Move the player one tile to the left + getFirst(cursor).x -= 1 +}) + +onInput("w", () => { + // Move the player one tile up + getFirst(cursor).y -= 1 +}) + +onInput("j", () => { + // Start the event + clearText(); + if(youLost == false) + { + if(challenge == false) + { + challenge = true; + + } + if(challenge == true && challengeState == false) + { + EventSystem(); + + } + else if(challenge == true && challengeState == true) + { + addText(`Wait until`, { + x: 1, + y: 1, + color: color`4` + }); + addText(`the event finishes!`, { + x: 1, + y: 2, + color: color`4` + }); + } + } + else + { + addText(`Restarted! press K`, { + x: 1, + y: 1, + color: color`4` + }); + numberOfWood = 15; + roundSurvived = 0; + z = 1; + events = 5; + challenge = false; + challengeState = false; + setMap(level); // Ricarica il livello + youLost = false; + } + + + +}) + +// Funzione per visualizzare il numero di blocchi rimanenti +function updateWoodCount() { + clearText(); // Cancella il conteggio precedente + addText(`Wood Remaining: ${numberOfWood}`, { + x: 1, + y: 1, + color: color`L` + }); + addText(`Round survived: ${roundSurvived}`, { + x: 1, + y: 2, + color: color`L` + }); +} + +// Aggiornamento iniziale del conteggio dei blocchi rimanenti +updateWoodCount(); +onInput("l", () => { + + if (challenge == false) { + PlaceWood(); + playTune(placed); + } +}); + +// Definisci una funzione per eseguire il codice in loop +function gameLoop() { + console.log(randomChallenge); + if (challenge && youLost == false) { + if(randomChallenge == 1) + { + ZapEvent(); + } + else if (randomChallenge == 2) + { RockLEvent(); + } + else if(randomChallenge == 3) + { RockREvent(); + } + } +} + +// Imposta l'intervallo per eseguire la funzione gameLoop ogni tot millisecondi +const interval = setInterval(gameLoop, 500); // Esegui ogni secondo (1000 millisecondi) + +function ZapEvent() +{ + console.log("The value of z is: " + z); + +// Move the ZAP sprite down + if (z == 1) { + z = 1; + pos = 4; + var randomX = Math.floor(Math.random() * (16 - 3 + 1)) + 3; + challengeState = true; + addSprite(randomX, pos, ZAP); + z = 2; + } else { + const zapSprite = getFirst(ZAP); + zapSprite.y += 1; // Move the ZAP sprite down + + + // Check if the ZAP sprite hits a wood block + const spritesAtZap = getTile(zapSprite.x, zapSprite.y); + + spritesAtZap.forEach(sprite => { + if (sprite.type === wood) { + // If the ZAP hits a wood block, remove both sprites + sprite.remove(); // Remove the wood block + zapSprite.remove(); // Remove the ZAP sprite + playTune(collision); + + z = 3; + } + else if (sprite.type === roof || sprite.type === roofl||sprite.type === pizzaOne) { + // If the ZAP hits a wood block, remove both sprites + sprite.remove(); // Remove the wood block + zapSprite.remove(); // Remove the ZAP sprite + + z = 4; + } + + }); + + if (zapSprite.y == 15 || z == 3) { + zapSprite.remove(); + challengeState = false; + challenge = false; + z = 1; + pos = 4; + addText("press J", { + x: 7, + y: 5, + color: color`4` + }) + }else if(z == 4) + {challengeState = false; + GameOver(); + } + } + + +} +function RockLEvent() +{ + console.log("The value of z is: " + z); + +// Move the ROCK sprite down + if (z == 1) { + z = 1; + + pos = 3; + var randomY = Math.floor(Math.random() * (14 - 4 + 1)) + 4; + challengeState = true; + addSprite(pos, randomY, ROCK); + z = 2; + } else { + const rockSprite = getFirst(ROCK); + rockSprite.x += 1; // Move the ROCK sprite down + + + // Check if the ROCK sprite hits a wood block + const spritesAtRock = getTile(rockSprite.x, rockSprite.y); + + spritesAtRock.forEach(sprite => { + if (sprite.type === wood) { + // If the ROCK hits a wood block, remove both sprites + sprite.remove(); // Remove the wood block + rockSprite.remove(); // Remove the ROCK sprite + playTune(collision); + + z = 3; + }else if (sprite.type === roof || sprite.type === roofl||sprite.type === pizzaOne) { + // If the ZAP hits a wood block, remove both sprites + sprite.remove(); // Remove the wood block + rockSprite.remove(); // Remove the ZAP sprite + + z = 4; + } + + }); + + if (rockSprite.x == 18 || z == 3) { + rockSprite.remove(); + challengeState = false; + challenge = false; + z = 1; + pos = 4; + addText("press J", { + x: 7, + y: 5, + color: color`4` + }) + }else if(z == 4) + {challengeState = false; + GameOver(); + } + } + + +} +function RockREvent() +{ + console.log("The value of z is: " + z); + +// Move the ROCKF sprite down + if (z == 1) { + z = 1; + + pos = 17; + var randomY = Math.floor(Math.random() * (14 - 4 + 1)) + 4; + challengeState = true; + addSprite(pos, randomY, ROCKF); + z = 2; + } else { + const rockfSprite = getFirst(ROCKF); + rockfSprite.x -= 1; // Move the ROCK sprite down + + + // Check if the ROCK sprite hits a wood block + const spritesAtRockf = getTile(rockfSprite.x, rockfSprite.y); + + spritesAtRockf.forEach(sprite => { + if (sprite.type === wood) { + // If the ROCKF hits a wood block, remove both sprites + sprite.remove(); // Remove the wood block + rockfSprite.remove(); // Remove the ROCKF sprite + playTune(collision); + + z = 3; + }else if (sprite.type === roof || sprite.type === roofl||sprite.type === pizzaOne) { + // If the ZAP hits a wood block, remove both sprites + sprite.remove(); // Remove the wood block + rockfSprite.remove(); // Remove the ZAP sprite + + z = 4; + } + + }); + + if (rockfSprite.x == 2 || z == 3) { + rockfSprite.remove(); + challengeState = false; + challenge = false; + z = 1; + pos = 4; + addText("press J", { + x: 7, + y: 5, + color: color`4` + }) + }else if(z == 4) + { + + challengeState = false; + GameOver(); + } + + } + + +} +function PlaceWood() +{ + const cursorSprite = getFirst(cursor); + const cursorX = cursorSprite.x; + const cursorY = cursorSprite.y; + + // Verifica se il posto nella mappa è vuoto + const spritesAtCursor = getTile(cursorX, cursorY); + const isEmpty = spritesAtCursor.every(sprite => sprite.type === cursor); + + if (isEmpty && numberOfWood > 0) { + // Aggiungi lo sprite alla posizione del cursore sulla mappa solo se è vuoto + addSprite(cursorX, cursorY, wood); // Sostituisci "wood" con lo sprite desiderato + numberOfWood--; + updateWoodCount(); + } + else if(numberOfWood == 0) + { + addText(`Press j to continue!`, { + x: 0, + y: 2, + color: color`L` + }); + } + +} +function EventSystem() +{ +if(events == 5) + {playTune(start); + randomChallenge = Math.floor(Math.random() * 3) + 1; + addText("Event started", { + x: 4, + y: 4, + color: color`4` + }) + addText("press J", { + x: 7, + y: 5, + color: color`4` + }) + + events--; + } + else if(events > 0) + { + randomChallenge = Math.floor(Math.random() * 3) + 1; + clearText(); + + events--; + } + else + {playTune(win) + challenge = false; + roundSurvived++; + numberOfWood= 3; + updateWoodCount(); + events = 5; + } + + + +} +function GameOver() +{ + playTune(gameOver); + clearText(); + youLost = true; + + addText("Game Over!", { + x: 5, + y: 5, + color: color`3` + }) + addText(`you survived`, { + x: 1, + y: 6, + color: color`3` + }) + addText(`for: ${roundSurvived} round!`, { + x: 6, + y: 7, + color: color`3` + }) + +} diff --git a/games/img/Defend-Your-House!.png b/games/img/Defend-Your-House!.png new file mode 100644 index 0000000000..7c13e76012 Binary files /dev/null and b/games/img/Defend-Your-House!.png differ