Created
May 4, 2024 14:20
-
-
Save sebnozzi/94c7890fc065de192fe6b3dfff02f6f8 to your computer and use it in GitHub Desktop.
Coin Collector Game for the Mini Micro
This file contains hidden or 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
// Fox | |
foxImg = file.loadImage("fox.png") | |
fox = new Sprite | |
fox.image = foxImg | |
fox.localBounds = new Bounds | |
fox.localBounds.width = foxImg.width | |
fox.localBounds.height = foxImg.height | |
fox.speed = 10 | |
fox.x = 200 | |
fox.y = 200 | |
// Coin Sprite | |
coinImg = file.loadImage("coin.png") | |
coin = new Sprite | |
coin.image = coinImg | |
coin.localBounds = new Bounds | |
coin.localBounds.width = coinImg.width | |
coin.localBounds.height = coinImg.height | |
coin.x = 500 | |
coin.y = 500 | |
coin.moveToRandomLocation = function | |
self.x = floor(rnd * 960) | |
self.y = floor(rnd * 640) | |
end function | |
// Score | |
score = {} | |
score.value = 10 | |
score.print = function | |
eraseText 25, 5, "SCORE: 999999".len | |
printTextAtPosition "SCORE: "+self.value, 25, 5, color.white | |
end function | |
score.increase = function | |
score.value += 10 | |
score.print | |
end function | |
// Countdown | |
countdown = {} | |
// Countdown value in seconds | |
countdown.value = 0 | |
// Timestamp of the last check | |
countdown.lastDecreaseTs = 0 | |
countdown.start = function(initialValue) | |
self.value = initialValue | |
self.lastDecreaseTs = time | |
self.print | |
end function | |
countdown.print = function | |
eraseText 25, 50, "TIME: 1000".len | |
printTextAtPosition "TIME: "+self.value, 25, 50, color.white | |
end function | |
countdown.update = function | |
currentTs = time | |
elapsed = currentTs - self.lastDecreaseTs | |
// A second elapsed since last check | |
if elapsed > 1 then | |
self.decrease | |
self.lastDecreaseTs = currentTs | |
end if | |
end function | |
countdown.decrease = function | |
self.value -= 1 | |
if self.value < 0 then self.value = 0 | |
self.print | |
end function | |
countdown.isFinished = function | |
return self.value <= 0 | |
end function | |
// Text helper functions | |
eraseText = function(row,column,length) | |
text.row = row | |
text.column = column | |
// Print "length" amount of spaces, with no "newline" | |
print " " * length, "" | |
end function | |
printTextAtPosition = function(txt,row,column,txtColor) | |
// Save current color and set the one passed as parameter | |
previousColor = text.color | |
text.color = txtColor | |
// Move to position and print text | |
text.row = row | |
text.column = column | |
print txt | |
// Restore previous color | |
text.color = previousColor | |
end function | |
printTextCentered = function(txt,row,txtColor) | |
totalColumns = 68 | |
column = totalColumns / 2 - txt.len / 2 | |
printTextAtPosition txt,row,column,txtColor | |
end function | |
// Game setup | |
clear | |
gfx.clear "#308A37" | |
sprd = display(4) | |
sprd.sprites.push fox | |
sprd.sprites.push coin | |
score.print | |
countdown.start 20 | |
// Game Loop | |
while true | |
countdown.update | |
if countdown.isFinished then break | |
if key.pressed("left") then | |
fox.x -= fox.speed | |
else if key.pressed("right") then | |
fox.x += fox.speed | |
else if key.pressed("up") then | |
fox.y += fox.speed | |
else if key.pressed("down") then | |
fox.y -= fox.speed | |
end if | |
if fox.overlaps(coin) then | |
coin.moveToRandomLocation | |
score.increase | |
end if | |
yield | |
end while | |
// Game ending | |
printTextCentered "FINAL SCORE: " + score.value, 16, color.white | |
printTextCentered "Do you want to play again? (y/n)", 14, color.white | |
while true | |
k = key.get.lower | |
if k == "y" then run | |
if k == "n" then break | |
end while | |
print "Bye!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment