Skip to content

Commit

Permalink
slot_machine (hackclub#1705)
Browse files Browse the repository at this point in the history
* Add files via upload

* Add files via upload

* Fixing metadata

* Update slot_machine.js

---------

Co-authored-by: graham <graham@hackclub.com>
  • Loading branch information
SpiritByte and grymmy authored May 28, 2024
1 parent 5df20bd commit d08edf2
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
Binary file added games/img/slot_machine.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
164 changes: 164 additions & 0 deletions games/slot_machine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
@title: Slot Machine
@author: Alexander Li
@discord: thefaix.
@tags: ["classic", "tool"]
@img: ""
@addedOn: 2024-05-23
*/

const slot = "s"
const symbol7 = "7"
const symbolC = "C"
const symbolB = "B"
const symbolX = "X"
const winSign = "W"

const symbols = [symbol7, symbolC, symbolB, symbolX]

setLegend(
[ slot, bitmap`
................
................
................
................
................
................
................
................
................
................
................
................
................
................
................
................` ],
[ symbol7, bitmap`
................
..444...........
..44444444......
..4444444444....
.....4444..44...
........44.4....
.........444....
...4444.444..44.
...444444444444.
...444444444444.
......4444......
......444.......
.....444........
....4444........
....444.........
................` ],
[ symbolC, bitmap`
................
.......33.......
......3333......
.....333333.....
....333...33....
...3333....33...
....33333333....
.....333333.....
....3333...3....
...333333..33...
..333333333333..
...3333333333...
....33333333....
.....333333.....
......3333......
................` ],
[ symbolB, bitmap`
................
.........9......
.......999......
.......9........
.....999999.....
...999999.999...
...999999...9...
..99999999...9..
..999999999.99..
..999999999999..
..999999999999..
...9999999999...
...9999999999...
.....9999999....
.......9999.....
................` ],
[ symbolX, bitmap`
................
................
.......C........
.....CCCCCC.....
...CCC....CC....
...C......CC....
..CCCCCCCC.CC...
..CC......C.CCC.
..C...........CC
.CCCCCCCCCCCC..C
.C...........CCC
.CC.........CCC.
..CCCCCCCCCCC...
................
................
................` ]
)

setSolids([])

const level = 0
const levels = [
map`
.s.s.s.`
]

setMap(levels[level])

function getRandomSymbol() {
return symbols[Math.floor(Math.random() * symbols.length)]
}

function spinSlots() {
clearBoard()
const slotPositions = getAll(slot)
const results = []
slotPositions.forEach(pos => {
const symbol = getRandomSymbol()
addSprite(pos.x, pos.y, symbol)
results.push(symbol)
})
checkWin(results)
}

function clearBoard() {
const slotPositions = getAll(slot)
slotPositions.forEach(pos => {
clearTile(pos.x, pos.y)
addSprite(pos.x, pos.y, slot)
})
clearText()
}

function checkWin(results) {
if (results.every(symbol => symbol === results[0])) {
displayWin()
}
}

function displayWin() {
// Display "You Win!" message
clearText()
addText("YOU WIN!", {
x: 2,
y: 5,
color: color`3`
})
}

onInput("s", () => {
spinSlots()
})

afterInput(() => {
// No additional actions needed after input
})

0 comments on commit d08edf2

Please sign in to comment.