OK, here's my chess timer:


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?
Are you sure that 31 is the right code for waitForKey()?
Yes it is and i found out my problem was that I forgot to fastCopy().

however, this code is throwing errors:


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"), dbfg=makeColor("red"), dbbg=makeColor("black")}
local continue = 0
local intro = 1
local buffer = 0
local timers = {}
local timers[1] = chesstimer.timerStart
local timers[2] = 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()
   clear()
   continue = 0
   drawText(1, 1, "WAIT...", color.fg, color.bg)
   fastCopy()
   
   wait(1)
   
   drawText(1, 1, "Press [EXE] to continue", color.fg, color.bg)
   fastCopy()
   
   waitForKey(key.EXE)
   
   intro = 0
end

local function chessTimerScreen()
   clear()
   start = timeElapsed()
   while exit ~= 1 do
      if keyDirect(key.Exit) > 0 then exit = 1
         elseif keyDirect(key.F1) > 0 then
            while keyDirect(key.F1) > 0 do keyDirectPoll() end
            toggleTurn()
      end
      
      --drawing
      drawText(1, SCREEN_HEIGHT - 20, "Next Player", color.fg, color.bg)
      drawText(1, 1, "Turn: " .. chesstimer.turn, color.fg, color.bg)
      drawText(1, 20, "White: " .. timers[1], color.fg, color.bg)
      drawText(1, 40, "Black: " .. timers[2], color.bg, color.fg)
      
      fastCopy()
      
      --math
      timeElapsedSinceTurn = timeElapsed() - start
      timers[chesstimer.turnNum] = chesstimer.timerStart - timeElapsedSinceTurn
      
      --debug
      drawText(1, 70, "DEBUG: " .. timers[1], color.dbfg, color.dbbg)
      
      keyDirectPoll()
   end
end


--main loop
while exit~=1 do
   clear()
   
   --intro screen
   if intro == 1 then fastCopy() introScreen() end
   
   --wait screen
   clear()
   drawText(1, 1, "Press F1 when ready", color.fg, color.bg)
   drawText(1, SCREEN_HEIGHT - 20, "Next Player", color.fg, color.bg)
   fastCopy()
   
   --wait
   waitForKey(key.F1)
   
   --chesstimer screen
   chessTimerScreen()
   
   --refresh screen
   fastCopy()
   --keypoll
   keyDirectPoll()
end

print("done.")
print("")


It says line 30 has an unexpected symbol near "["

line 30:

Code:

local timers[1] = chesstimer.timerStart


not sure what's wrong there... maybe a problem with the interpretor?

EDIT:

nvr, line 30 should be:

Code:
timers[1] = chesstimer.timerStart
So you ironed out all the glitches so far? Or are you still having difficulties?
I have most of the glitches ironed out, but right now i am trying to figure out how i am going to convert seconds to minutes and hours, will post if I have any more problems Wink

EDIT:

OK, i'm just about done, only problem is that when a timer runs out of time the calc freezes instead of showing the out of time message:


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"), dbfg=makeColor("red"), dbbg=makeColor("black")}
local continue = 0
local intro = 1
local buffer = 0
local timers = {}

local start=0
local timeElapsedSinceTurn = 0

--first poll and first start timer
keyDirectPoll()
local startTime = ticks()

--functions

local function timeElapsed()
   return floor((ticks()-startTime)/128)
end

local function convertTime(t, format)
   min = floor(t/60)
   sec = t-(min*60)
   if format=="m" then return min
      elseif format=="s" then return sec
   end
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()
   fastCopy()
   timers[chesstimer.turnNum + 2] = (timers[chesstimer.turnNum + 2] - timeElapsedSinceTurn) + chesstimer.add
   if chesstimer.turn == "White" then chesstimer.turn = "Black" chesstimer.turnNum = 2
      else chesstimer.turn = "White" chesstimer.turnNum = 1
   end
   start = timeElapsed()
end

local function outOfTime(message)
   clear()
   drawText(floor(SCREEN_WIDTH/2),floor(SCREEN_HEIGHT/2),message,makeColor("black"), makeColor("red"))
   fastCopy()
   waitForKey(key.Exit)
end

local function introScreen()
   clear()
   continue = 0
   drawText(1, 1, "WAIT...", color.fg, color.bg)
   fastCopy()
   min = 1
   
   wait(1)
   
   
   
   while continue==0 do
      clear()
      keyDirectPoll()
      if keyDirect(key.Up)>0 and min<15 then
         while keyDirect(key.Up) > 0 do keyDirectPoll() end
         min = min+1
         elseif keyDirect(key.Down)>0 and min>0 then
            while keyDirect(key.Down) > 0 do keyDirectPoll() end
            min = min-1
      end
      if keyDirect(key.EXE)>0 then continue = 1 end
      
      drawText(1, 1, "Press [EXE] to continue", color.fg, color.bg)
      drawText(1, 20, "minutes: " .. min, color.fg, color.bg)
      
      fastCopy()
   end
   chesstimer.timerStart = min*60
   timers[1] = chesstimer.timerStart
   timers[2] = chesstimer.timerStart
   timers[3] = chesstimer.timerStart
   timers[4] = chesstimer.timerStart
   intro = 0
end

local function chessTimerScreen()
   clear()
   start = timeElapsed()
   while exit ~= 1 do
      clear()
      if keyDirect(key.Exit) > 0 then exit = 1
         elseif keyDirect(key.F1) > 0 then
            while keyDirect(key.F1) > 0 do keyDirectPoll() end
            toggleTurn()
      end
      
      --drawing
      drawText(1, SCREEN_HEIGHT - 20, "Next Player", color.fg, color.bg)
      drawText(1, 1, "Turn: " .. chesstimer.turn, color.fg, color.bg)
      drawText(1, 20, "White: " .. convertTime(timers[1],"m") .. ":" .. convertTime(timers[1],"s"), color.fg, color.bg)
      drawText(1, 40, "Black: " .. convertTime(timers[2],"m") .. ":" .. convertTime(timers[2],"s"), color.bg, color.fg)
      
      fastCopy()
      
      --math
      timeElapsedSinceTurn = timeElapsed() - start
      timers[chesstimer.turnNum] = timers[chesstimer.turnNum + 2] - timeElapsedSinceTurn
      if timers[chesstimer.turnNum] < 0 then outOfTime(chesstimer.turn .. " ran out of time!") end
      
      --[[-- debug
      drawText(1, 70, "DEBUG: " .. timers[1], color.dbfg, color.dbbg)
      --]]--
      
      keyDirectPoll()
   end
end


--main loop
while exit~=1 do
   clear()
   
   --intro screen
   if intro == 1 then fastCopy() introScreen() end
   
   --wait screen
   clear()
   drawText(1, 1, "Press F1 when ready", color.fg, color.bg)
   drawText(1, SCREEN_HEIGHT - 20, "Next Player", color.fg, color.bg)
   fastCopy()
   
   --wait
   waitForKey(key.F1)
   
   --chesstimer screen
   chessTimerScreen()
   
   --refresh screen
   fastCopy()
   --keypoll
   keyDirectPoll()
end

print("done.")
print("")
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement