diff --git a/games/Simon-Says-Button-Edition!.js b/games/Simon-Says-Button-Edition!.js new file mode 100644 index 0000000000..eae2bd0be6 --- /dev/null +++ b/games/Simon-Says-Button-Edition!.js @@ -0,0 +1,272 @@ +const wonButton = "v" +const guessButton = "g" +const hoverButton = "h" +const clickedButton = "c" +const wrongButton = "w" +const button = "b" + + +setLegend( + [ guessButton, bitmap` +................ +................ +................ +....00000000.... +...0777777770... +...0777777770... +...0777777770... +...0777777770... +...0777777770... +...0777777770... +...0777777770... +...0555555550... +....00000000.... +................ +................ +................`], + [ wrongButton, bitmap` +................ +................ +................ +................ +................ +....00000000.... +...0333333330... +...0333333330... +...0333333330... +...0333333330... +...0333333330... +...0333333330... +....00000000.... +................ +................ +................`], + [ wonButton, bitmap` +................ +................ +................ +................ +................ +....00000000.... +...0444444440... +...0444444440... +...0444444440... +...0444444440... +...0444444440... +...0DDDDDDDD0... +....00000000.... +................ +................ +................`], + [ clickedButton, bitmap ` +................ +................ +................ +................ +................ +....00000000.... +...0111111110... +...0111111110... +...0111111110... +...0111111110... +...0LLLLLLLL0... +...0LLLLLLLL0... +....00000000.... +................ +................ +................`], + [ hoverButton, bitmap` +................ +................ +................ +....00000000.... +...0222222220... +...0222222220... +...0222222220... +...0222222220... +...0222222220... +...0222222220... +...0222222220... +...0111111110... +....00000000.... +................ +................ +................`], + [ button, bitmap` +................ +................ +................ +....00000000.... +...0222222220... +...0222222220... +...0222222220... +...0222222220... +...0222222220... +...0222222220... +...0222222220... +...0222222220... +....00000000.... +................ +................ +................`] +) +const buttonGrid = map` +..... +.bbb. +.bbb. +.bbb. +..... +` +const emptyGrid = map` +..... +..... +..... +..... +..... +` + +setMap(buttonGrid) +let hoverSprite +let currentButton = null +let currentGuess = 0 + +//the amount of rounds +const randRounds = Math.floor(Math.random()*4)+1 +addText(String(randRounds), {x: 3,y: 3}) + +//all the buttons in the game +const buttons = tilesWith(button) + .map(tile => tile[0]) + .sort((a,b) => { + if (a.y !== b.y) return a.y - b.y; + return a.x - b.x; + }); + + +//per round buttons +const avaliableButtons = buttons.slice() +const chosenButtons = [] + + +//picks the buttons that are gonna be the goal +for (let i = 0; i < randRounds; i++) { + if (avaliableButtons.length === 0) { + console.log("No buttons left to pick.") + break + } + + //rand amount of buttons + const randIndex = Math.floor(Math.random() * avaliableButtons.length) + + const pickedButton = avaliableButtons.splice(randIndex, 1)[0] + + + if (pickedButton) { + chosenButtons.push(pickedButton) + } else { + } +} + +//displays the blue color for them + +let start = false +var hoveringButton +var buttonVal = 0 + + +//in game runs the focus on what button the player is on + visually adds the hover sprite +function moveHover(dx, dy) { + if (start === true && currentButton){ + const newX = currentButton.x + dx + const newY = currentButton.y + dy + + const targetButton = buttons.find(b=> b.x === newX && b.y === newY) + + if (targetButton){ + if (hoverSprite) hoverSprite.remove() + currentButton = targetButton + addSprite(currentButton.x, currentButton.y, hoverButton) + hoverSprite = getFirst(hoverButton) + } + } +} + +//horizontal movement +onInput("a", () => moveHover(-1, 0)) +onInput("d", () => moveHover(1, 0)) + +//vertical movement +onInput("s", () => moveHover(0,1)) +onInput("w", () => moveHover(0,-1)) + +//guess +onInput("i", () => guess()) + +function guess(){ +//safety + if (!start || currentButton.type !== button) return; + + if (currentButton === chosenButtons[currentGuess]){ + if (hoverSprite) hoverSprite.remove() + currentButton.type = clickedButton + currentGuess++ + if (currentGuess === chosenButtons.length) { + winGame() + } + } else { + if (hoverSprite) hoverSprite.remove() + currentButton.type = wrongButton + start = false + + setTimeout(() => { + //removes all overlay Button sprites + getAll(hoverButton).forEach(s => s.remove()) + getAll(guessButton).forEach(s => s.remove()) + + //resets the buttons + buttons.forEach(b => b.type = button) + currentGuess = 0 + playSequence() + }, 600) + } +} + +function playSequence() { +//plays the animation at the start and on mistake of the blue sprites + setMap(buttonGrid) + start = false + for (let r = 0; r < chosenButtons.length; r++){ + const tile = chosenButtons[r] + + setTimeout(() => { + addSprite(tile.x, tile.y, guessButton) + + setTimeout(() => { + const spriteAtTile = getTile(tile.x, tile.y) + const activeGuess = spriteAtTile.find(s => s.type === guessButton) + + + if (activeGuess) activeGuess.remove() + if (r === chosenButtons.length-1){ + currentButton = buttons[0] + addSprite(currentButton.x, currentButton.y, hoverButton) + hoverSprite = getFirst(hoverButton) + start = true + console.log(start) + } + }, 1000) + + }, (r * 1000)) + } +} + +//only shows the right ones, and displays the won text +function winGame() { + start = false + setMap(emptyGrid) + chosenButtons.forEach(b => addSprite(b.x, b.y, wonButton)) + clearText() + addText("YOU WON!", { x: 3, y: 3}) +} + +playSequence() \ No newline at end of file