Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessefreeman/576e291aaee97ff611fbf0b6c76cbfcb to your computer and use it in GitHub Desktop.
Save jessefreeman/576e291aaee97ff611fbf0b6c76cbfcb to your computer and use it in GitHub Desktop.
local startValue = 0
local handValue = {}
local timeDelay = 1
local timeCount = 0
local cursorPos = 0
local gameOver = true
local BLACK, WHITE, GREY = 32, 51, 27
function PadNumber(value, length)
return string.format("%0"..length.."d", value)
end
function UpdateMessage(message)
local display = Display()
-- We are going to render the message in a box as tiles. To do this, we need to wrap the
-- text, then split it into lines and draw each line.
local wrap = WordWrap(message, (display.x / 8) - 2)
local lines = SplitLines(wrap)
local total = 6
local startY = ((display.y / 8) - 1) - total
-- We want to render the text from the bottom of the screen so we offset it and loop backwards.
for i = total, 1, - 1 do
local line = lines[i] == nil and "" or lines[i]
DrawText(line .. string.rep(" ", 30 - #line), 1, startY + (i - 1), DrawMode.Tile, "default", WHITE)
end
end
function Draw()
RedrawDisplay()
DrawLargeNumber()
DrawHand()
end
function DrawLargeNumber()
local largeValue = PadNumber(startValue, 2)
for i = 1, #largeValue do
local char = largeValue:sub(i, i)
local sprite = Sprite(tonumber(char) + 16)
for i = 1, #sprite do
if(sprite[i] == 0) then
sprite[i] = 94
end
end
DrawTiles(sprite, (i * 8) + 2, 5, 8, DrawMode.Tile, WHITE)
end
end
function DrawHand()
if(gameOver) then
return
end
local offset = {x = 10, y = 15}
for i = 1, #handValue do
DrawText(
PadNumber(math.abs(handValue[i]), 2),
offset.x * 8,
offset.y * 8,
DrawMode.Sprite,
"default",
handValue[i] > 0 and WHITE or GREY
)
offset.x = offset.x + 3
end
DrawText("^", ((cursorPos + offset.x) * 24) - 4, (offset.y + 1) * 8, DrawMode.Sprite, "default", WHITE)
end
function Init()
-- Here we are manually changing the background color
BackgroundColor(32)
local tmpSprite = {}
repeat
table.insert(tmpSprite, 0)
until #tmpSprite == 64
Sprite(94, tmpSprite)
UpdateMessage("Twitch 21\n\nThis is a remake of a game I created for Ludum Dare 26.\n\nPress X to start.")
end
function Update(timeDelta)
if(gameOver == false) then
timeCount = timeCount + timeDelta
if(timeCount > timeDelay)then
timeCount = 0
startValue = startValue - 1
if(startValue < 0) then
startValue = 0
EndGame()
end
PlaySound(0, 4)
end
local cursorOffset = 0
if(Button(Buttons.Right, InputState.Released)) then
cursorOffset = 1
elseif(Button(Buttons.Left, InputState.Released)) then
cursorOffset = -1
end
cursorPos = Repeat(cursorPos + cursorOffset, #handValue)
if(Button(Buttons.A, InputState.Released)) then
UpdateValue()
elseif(Button(Buttons.B, InputState.Released)) then
EndGame()
end
else
if(Button(Buttons.A, InputState.Released)) then
StartGame()
end
end
end
function StartGame()
UpdateMessage("Twitch 21\n\nTry to reach 21 before time runs out. Use < and > to find a number, X to select and C to stand.")
gameOver = false
startValue = math.random(5, 14)
for i = 1, 5 do
table.insert(handValue, math.random(1, 12))
end
end
function UpdateValue()
local newValue = handValue[cursorPos + 1]
if(newValue > - 1) then
startValue = startValue + newValue
handValue[cursorPos + 1] = newValue * - 1
timeCount = -1
if(startValue > 21) then
EndGame()
end
end
end
function EndGame()
local message = "Game Over"
if(startValue > 15 and startValue < 22) then
message = message .. " - Win"
else
message = message .. " - Lose"
end
message = message .. "\n\nPress X to restart."
UpdateMessage(message)
gameOver = true
handValue = {}
timeCount = 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment