Adding a timer to my Lua program -
i'm new lua , i'm coding program. program letter going around , collecting other letters (kinda worm program). however, want timed. (i'm on computercraft mod minecraft still uses lua, don't think matters) i'm using os.pullevent( "key" ) can move letter, os.pullevent() pause program until it's satisfied. problem want timer ticking @ same time. ideas how 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 term.setcursorpos(w,h) term.settextcolor(colors.red) print"o" elseif x == w , y == h 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 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 x=x-1 if x <= 1 x = 2 end end if move == 32 or move == 205 x=x+1 if x >= 51 x = 50 end end if move == 31 or move == 208 y=y+1 if y >= 18 y = 17 end end if move == 17 or move == 200 y=y-1 if y <= 2 y = 3 end end term.clear() end
you can use os.starttimer()
generate "timer"
event call os.pullevent()
Comments
Post a Comment