Timer 向我的Lua程序添加计时器
我是Lua的新手,我正在编写一个程序。这个程序是一个信件传递和收集其他信件的程序(有点像蠕虫程序)。然而,我希望这是时间。(我使用的是computercraft,它是minecraft的一个mod,但仍然使用Lua,所以我认为这不重要)我使用的是os.PullEvent(“键”),这样我可以移动字母,但os.PullEvent()会暂停程序,直到满意为止。我的问题是,我希望计时器在同一时间不停地滴答作响。关于我如何做到这一点有什么想法吗?谢谢Timer 向我的Lua程序添加计时器,timer,lua,computercraft,Timer,Lua,Computercraft,我是Lua的新手,我正在编写一个程序。这个程序是一个信件传递和收集其他信件的程序(有点像蠕虫程序)。然而,我希望这是时间。(我使用的是computercraft,它是minecraft的一个mod,但仍然使用Lua,所以我认为这不重要)我使用的是os.PullEvent(“键”),这样我可以移动字母,但os.PullEvent()会暂停程序,直到满意为止。我的问题是,我希望计时器在同一时间不停地滴答作响。关于我如何做到这一点有什么想法吗?谢谢 term.clear() w = 1 h = 1 s
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
term.clear()
w=1
h=1
分数=0
函数topLine()
term.setTextColor(colors.orange)
术语setCursorPos(5,1)
打印(“分数:”,分数)
结束
函数randLoc()
w、 h=数学随机(2,50),数学随机(3,17)
术语setCursorPos(w,h)
term.setTextColor(colors.red)
打印“O”
结束
函数drawBorder()
term.setTextColor(colors.blue)
术语setCursorPos(1,2)
打印“XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”
打印“X X”
打印“X X”
打印“X X”
打印“X X”
打印“X X”
打印“X X”
打印“X X”
打印“X X”
打印“X X”
打印“X X”
打印“X X”
打印“X X”
打印“X X”
打印“X X”
打印“X X”
打印“XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”
结束
函数checkTouch()
如果x~=w或y~=h,则
术语setCursorPos(w,h)
term.setTextColor(colors.red)
打印“O”
如果x==w,y==h
w、 h=数学随机(2,50),数学随机(3,17)
术语setCursorPos(w,h)
term.setTextColor(colors.red)
打印“O”
分数=分数+1
结束
结束
x=2
y=3
randLoc()
尽管如此
背线()
拉边()
checkTouch()
术语setCursorPos(x,y)
term.setTextColor(colors.lime)
打印“T”
本地e,move=os.pullervent(“键”)
如果移动=30或移动=203,则
x=x-1
如果x=51,则
x=50
结束
结束
如果移动==31或移动==208,则
y=y+1
如果y>=18,则
y=17
结束
结束
如果移动==17或移动==200,则
y=y-1
如果y我不确定这是否回答了您的问题,但您可以看到某些代码运行的时间如下:
local x = os.clock()
----------------------
---- Timed code ------
----------------------
print(string.format("Elapsed time: %.6f\n", os.clock() - x))
您可以使用os.StartTimer()
,它将从调用os.pullervent()
请参见我的计算机工艺程序遵循这种“多任务”设计
NetworkingLoop
,InputLoop
,DrawingLoop
和KeepAlive
将“同时”运行
(不是真的,因为lua不能那样做)
要停止程序,请将keepAlive
(注意小写的k
)设置为false
(在循环内)现在人们通常只在变量中使用os.startTimer,但还有另一种方法涉及函数中循环中的变量和if语句
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
这是你通常在微笑基础中看到的东西,但你仍然可以回答这是lua。
现在再加上它就像是每隔一秒告诉它打印一些东西
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
tick=0
时间=0
运行=真
函数计时器()
勾号=勾号+1
如果勾号=60,则
时间=时间+1
刻度=0
结束
如果勾选>15且勾选<30,则
打印(“介于15-30之间,请稍候”)
结束
结束
运行=真
跑步时做什么
计时器()
结束
现在,只需每次打印15-30个刻度之间的文本行。
这是我对这个主题表单问题的回答。您只需使用print(((“X”):rep(50))
进行边框打印即可。谢谢!我会记住的!
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