Last active
November 5, 2021 22:41
-
-
Save sebnozzi/d8a6d71dd76ddc9e2118fd0e95c17ecb to your computer and use it in GitHub Desktop.
Memory game for the Mini Micro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We need this for list.any, list.contains, list.remove ... | |
import "listUtil" | |
countRows=4 | |
countCols=5 | |
// How long can you look at cards before hiding them again | |
// In seconds | |
SECONDS_TO_PEEK=1 | |
foodFileNames = [ | |
"Apple.png", | |
"Bananas.png", | |
"Burger.png", | |
"Cake.png", | |
"Cheese.png", | |
"Cookie.png", | |
"Donut.png", | |
"Muffin.png", | |
"Pizza.png", | |
"Sushi.png"] | |
cardColor="#008BFFFF" | |
margin=20 | |
boxWidth=120 | |
boxHeight=120 | |
boxBorder=10 | |
boardWidth=(boxWidth+margin)*countCols-margin | |
boardHeight=(boxHeight+margin)*countRows-margin | |
boardOriginX=960/2-(boardWidth/2) | |
boardOriginY=640/2-(boardHeight/2) | |
createCardBackImg = function() | |
boxOriginX = 0 | |
boxOriginY = 0 | |
gfx.color = cardColor | |
gfx.fillRect boxOriginX,boxOriginY,boxWidth,boxHeight | |
return gfx.getImage(boxOriginX,boxOriginY,boxWidth,boxHeight) | |
end function | |
cardSprites = [] | |
CardSprite = new Sprite | |
CardSprite.fileName = "UNDEFINED" | |
CardSprite.variant = "A/B" | |
CardSprite.otherPair = null | |
CardSprite.backImg = createCardBackImg() | |
CardSprite.showingFront = false | |
CardSprite.showBack = function() | |
self.image = self.backImg | |
self.showingFront = false | |
end function | |
CardSprite.showFront = function() | |
self.image = self.frontImg | |
self.showingFront = true | |
end function | |
createCardSprite = function(variant,fileName,cardImg) | |
cardSprite = new CardSprite | |
cardSprite.variant = variant | |
cardSprite.frontImg = cardImg | |
cardSprite.fileName = fileName | |
cardSprite.localBounds = new Bounds | |
cardSprite.localBounds.width = cardImg.width | |
cardSprite.localBounds.height = cardImg.height | |
cardSprite.showBack | |
return cardSprite | |
end function | |
for fileName in foodFileNames | |
img = file.loadImage("/sys/pics/food/"+fileName) | |
boxOriginX = 0 | |
boxOriginY = 0 | |
gfx.color = cardColor | |
gfx.fillRect boxOriginX,boxOriginY,boxWidth,boxHeight | |
gfx.color = color.white | |
gfx.fillRect(boxOriginX+boxBorder, | |
boxOriginY+boxBorder, | |
boxWidth-(boxBorder*2), | |
boxHeight-(boxBorder*2)) | |
imgX = boxOriginX+(boxWidth/2)-img.width/2 | |
imgY = boxOriginY+(boxHeight/2)-img.height/2 | |
if fileName == "Apple.png" then | |
// Adjust position of apple, so that it's centered | |
imgX = imgX - 5 | |
end if | |
gfx.drawImage img,imgX,imgY | |
cardImg = gfx.getImage(boxOriginX,boxOriginY,boxWidth,boxHeight) | |
// We now have the image of the card, let's create sprites | |
cardSpriteA = createCardSprite("A",fileName,cardImg) | |
cardSpriteB = createCardSprite("B",fileName,cardImg) | |
cardSpriteA.otherPair = cardSpriteB | |
cardSpriteB.otherPair = cardSpriteA | |
cardSprites.push cardSpriteA | |
cardSprites.push cardSpriteB | |
end for | |
shuffledIndexes = range(0,len(cardSprites)-1) | |
shuffledIndexes.shuffle | |
shuffledCardSprites = [] | |
for idx in shuffledIndexes | |
sprite = cardSprites[idx] | |
shuffledCardSprites.push sprite | |
end for | |
clear | |
idx = 0 | |
for y in range(0,countRows-1) | |
for x in range(0,countCols-1) | |
cardOriginX = boardOriginX+(x*(boxWidth+margin)) | |
cardOriginY = boardOriginY+(y*(boxHeight+margin)) | |
sprite = shuffledCardSprites[idx] | |
// Adjust sprite placement because coordinates | |
// refer to the center of it, not its leftBottom corner | |
sprite.x = cardOriginX+boxWidth/2 | |
sprite.y = cardOriginY+boxHeight/2 | |
display(4).sprites.push sprite | |
idx = idx + 1 | |
end for | |
end for | |
glowCards = function(cards) | |
for i in range(0,1) | |
for transparency in range(255,80,-40) | |
tintColor = color.rgba(255,255,255,transparency) | |
cards[0].tint = tintColor | |
cards[1].tint = tintColor | |
wait 0.01 | |
yield | |
end for | |
for transparency in range(80,255,40) | |
tintColor = color.rgba(255,255,255,transparency) | |
cards[0].tint = tintColor | |
cards[1].tint = tintColor | |
wait 0.01 | |
yield | |
end for | |
end for | |
end function | |
wasButtonDown = false | |
isButtonDown = false | |
attemptedCards = [] | |
pairedCards = [] | |
hideCardsTimeout = 0 | |
attempts = 0 | |
startTime = time | |
while true | |
isButtonDown = mouse.button | |
if isButtonDown and not wasButtonDown then | |
for s in cardSprites | |
if s.contains(mouse) and pairedCards.indexOf(s) == null and len(attemptedCards) <= 1 then | |
if not s.showingFront then | |
attemptedCards.push s | |
s.showFront | |
end if | |
end if | |
end for | |
end if | |
if len(attemptedCards) == 2 and hideCardsTimeout == 0 then | |
hideCardsTimeout = time + SECONDS_TO_PEEK | |
attempts = attempts + 1 | |
end if | |
if hideCardsTimeout != 0 and time > hideCardsTimeout then | |
for s in attemptedCards | |
s.showBack | |
end for | |
attemptedCards = [] | |
hideCardsTimeout = 0 | |
end if | |
if len(attemptedCards) == 2 then | |
cardA = attemptedCards[0] | |
cardB = attemptedCards[1] | |
// Match found! | |
matchedCards = [cardA, cardB] | |
if cardA.otherPair == cardB then | |
pairedCards = pairedCards + matchedCards | |
attemptedCards = [] | |
hideCardsTimeout = 0 | |
glowCards(matchedCards) | |
end if | |
end if | |
if len(pairedCards) == len(cardSprites) then | |
// Game is won | |
endTime = time | |
timeTaken = round(endTime-startTime,0) | |
print "Solved! (in "+attempts+" attempts and "+timeTaken+" seconds)" | |
print "Enter ""run"" to play again" | |
exit | |
end if | |
if key.available then | |
k = key.get | |
if k == "q" or key.pressed("escape") then | |
exit | |
end if | |
end if | |
wasButtonDown = isButtonDown | |
yield | |
end while |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment