OK, here's my chess timer:
Code:
For some reason it freezes at "Press [EXE] to continue". I've been debugging it for a half an hour with no results so maybe someone with a fresh brain can help me see where my problem is?
Code:
print("Chess timer starting...")
--function variables
local drawRectFill = zmg.drawRectFill
local fastCopy = zmg.fastCopy
local makeColor = zmg.makeColor
local drawPoint = zmg.drawPoint
local keyMenuFast = zmg.keyMenuFast
local clear = zmg.clear
local drawText = zmg.drawText
local keyDirectPoll = zmg.keyDirectPoll
local keyDirect = zmg.keyDirect
local floor = math.floor
local random = math.random
local time = zmg.time
local ticks = zmg.ticks
--screen vars
local SCREEN_WIDTH = 384
local SCREEN_HEIGHT = 216
local exit = 0
--game variables
local key = {F1=79, F2=69, F3=59, F4=49, F5=39, F6=29, Alpha=77, Exit=47, Optn=68, Up=28, Down=37, Left=38, Right=27, EXE=31}
local chesstimer = {timerStart=60, add=0, turn="White", turnNum=1}
local color = {bg=makeColor("white"), fg=makeColor("black")}
local continue = 0
local intro = 1
local buffer = 0
local timers = {white=chesstimer.timerStart, black=chesstimer.timerStart}
local start=0
--first poll and first start timer
keyDirectPoll()
local startTime = ticks()
--functions
local function timeElapsed()
return floor((ticks()-startTime)/100)
end
local function wait(timeToWait)
local start = timeElapsed()
while timeElapsed() - start < timeToWait do end
end
local function waitForKey(key)
while keyDirect(key) == 0 do
keyDirectPoll()
end
end
local function toggleTurn()
if chesstimer.turn == "White" then chesstimer.turn = "Black" chesstimer.turnNum = 2
else chesstimer.turn = "White" chesstimer.turnNum = 1
end
start = timeElapsed()
end
local function introScreen()
continue = 0
drawText(1, 1, "WAIT...", color.fg, color.bg)
fastCopy()
wait(3)
drawText(1, 1, "Press [EXE] to continue", color.fg, color.bg)
fastCopy()
waitForKey(key.EXE)
intro = 0
end
local function chessTimerScreen()
start = timeElapsed()
while exit ~= 1 do
if keyDirect(key.Exit) > 0 then exit = 1
elseif keyDirect(key.F1) > 0 then toggleTurn()
end
--drawing
drawText(1, SCREEN_HEIGHT - 10, "Next Player", color.fg, color.bg)
drawText(1, 1, "Turn: " .. chesstimer.turn, color.fg, color.bg)
drawText(1, 10, "White: " .. timers.white, color.fg, color.bg)
drawText(1, 20, "Black: " .. timers.black, color.bg, color.fg)
--math
timeElapsedSinceTurn = timeElapsed - start
timers[chesstimer.turnNum] = chesstimer.timerStart - timeElapsedSinceTurn
keyDirectPoll()
end
exit=0
intro=1
end
--main loop
while exit~=1 do
clear()
--intro screen
if intro == 1 then fastCopy() introScreen() end
--wait screen
drawText(1, 1, "Press F1 when ready", color.fg, color.bg)
drawText(1, SCREEN_HEIGHT - 10, "Next Player", color.fg, color.bg)
--wait
waitForKey(key.F1)
--chesstimer screen
chessTimerScreen()
--refresh screen
fastCopy()
--keypoll
keyDirectPoll()
end
print("done.")
print("")
For some reason it freezes at "Press [EXE] to continue". I've been debugging it for a half an hour with no results so maybe someone with a fresh brain can help me see where my problem is?