1

I'm fairly new to Lua and I'm coding a program. The program is letter going around and collecting other letters (kinda like the worm program). However, i want this to be timed. (I'm on computercraft which is a mod for minecraft but still uses Lua, so i don't think that matters) I'm using an os.PullEvent( "key" ) so that i can move the letter, but os.pullEvent() will pause the program until it's satisfied. My problem is that i want a timer to be constantly ticking at the same time. Any ideas as to how i could do this? Thanks!

term.clear()
w = 1
h = 1
score = 0
function topLine()
  term.setTextColor(colors.orange)
  term.setCursorPos(5,1)
  print("Score: ", score)
end  
function randLoc()
  w,h = math.random(2,50) , math.random(3,17)
  term.setCursorPos(w,h)
  term.setTextColor(colors.red)
  print"O"
end  
function drawBorder()
  term.setTextColor(colors.blue)
  term.setCursorPos(1,2)
  print"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  print"X                                                 X"
  print"X                                                 X"
  print"X                                                 X"
  print"X                                                 X"
  print"X                                                 X"
  print"X                                                 X"
  print"X                                                 X"
  print"X                                                 X"
  print"X                                                 X"
  print"X                                                 X"
  print"X                                                 X"
  print"X                                                 X"
  print"X                                                 X"
  print"X                                                 X"
  print"X                                                 X"
  print"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
end
function checkTouch()
  if x ~= w or y ~= h then
    term.setCursorPos(w,h)
    term.setTextColor(colors.red)
    print"O"  
  elseif x == w and y == h then
    w,h = math.random(2,50) , math.random(3,17)
    term.setCursorPos(w,h)
    term.setTextColor(colors.red)
    print"O"
    score=score+1
  end
end                
x = 2
y = 3
randLoc()
while true do
  topLine() 
  drawBorder()
  checkTouch()
  term.setCursorPos(x,y)
  term.setTextColor(colors.lime)
  print"T"
  local e,move = os.pullEvent( "key" )  
  if move == 30 or move == 203 then
    x=x-1
    if x <= 1 then
      x = 2
    end
  end
  if move == 32 or move == 205 then
    x=x+1
    if x >= 51 then
      x = 50
    end
  end
  if move == 31 or move == 208 then
    y=y+1
    if y >= 18 then
      y = 17
    end
  end
  if move == 17 or move == 200 then
    y=y-1
    if y <= 2 then
      y = 3
    end
  end  
  term.clear()
end
Ryan Stein
  • 7,930
  • 3
  • 24
  • 38
user1510082
  • 35
  • 1
  • 3

4 Answers4

3

You can use an os.StartTimer() which will generate a "timer" event from your call to os.pullEvent()

See the ComputerCraft OS API documentation

Doug Currie
  • 40,708
  • 1
  • 95
  • 119
0

I'm not sure if this answers your question but you can see how long some code takes to run like this:

local x = os.clock()

----------------------
---- Timed code ------
----------------------

print(string.format("Elapsed time: %.6f\n", os.clock() - x))
Odhran Roche
  • 1,111
  • 1
  • 7
  • 14
0

My Computercraft programs are following this "multitasking" design

local keepAlive = true

local function NetworkingLoop()
    while true do
        -- Do Networking
    end
end

local function InputLoop()
    while true do
        -- Do Input
    end
end

local function DrawingLoop()
    while true do
        -- Do drawing
    end
end

local function KeepAlive()
    while keepAlive do
        os.sleep(1)
    end
end

parallel.waitForAny(NetworkingLoop, InputLoop, DrawingLoop, KeepAlive)

NetworkingLoop, InputLoop, DrawingLoop and KeepAlive will run at the "same time"
(not really 'cause lua can't do that)

To stop the program set keepAlive (note the lowercase k) to false anywhere (inside the loops)

Mischa
  • 1,303
  • 12
  • 24
0

now people usually just use the os.startTimer in a variable but there is another method involving variables and if statements in loops within functions

tick = 0
time = 0
function timer()
  tick=tick+1
  if tick == 60 then
    time=time+1
    tick=0
  end
end

running=true
while running do
  timer()
  sleep(.1)
end

that's something that you usally see in smilebasic but you can still replecate this is lua. now adding to it would be like telling it every seconds tell it to print something

tick=0
time=0
running=true
function timer()
  tick=tick+1
  if tick==60 then
    time=time+1
    tick=0
  end
  if tick > 15 and tick < 30 then
    print("it inbetween 15 - 30 please wait")
  end
end

running=true
while running do
  timer()
end

Now that will just simply print that line of text for inbetween 15-30 ticks every time. that's my answer to this topic forms question.

user7103188
  • 148
  • 1
  • 12
ADEPrime
  • 1
  • 2