2013-05-04 36 views
1

我對Lua相當陌生,而且我正在編寫一個程序。該程序是信件四處轉發並收集其他信件(有點像蠕蟲程序)。但是,我希望這是時間。 (我在電腦上,這是一個國防部的mod,但仍然使用Lua,所以我不認爲這很重要)我使用os.PullEvent(「key」),以便我可以移動該字母,但操作系統。 pullEvent()會暫停程序直到滿足。我的問題是,我想要一個計時器不斷在同一時間滴答作響。任何想法如何我可以做到這一點?謝謝!添加一個計時器到我的Lua程序

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 
+0

您可以簡單地使用'print((「X」):rep(50))'邊界。 – hjpotter92 2013-05-04 22:42:06

+0

謝謝!我會記住這一點! – user1510082 2013-05-04 23:07:19

回答

0

我不知道這是否回答你的問題,但你可以看到一些代碼需要多長時間可以這樣運行:

local x = os.clock() 

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

print(string.format("Elapsed time: %.6f\n", os.clock() - x)) 
0

我Computercraft程序下面這個「MUL titasking」設計

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) 

NetworkingLoopInputLoopDrawingLoopKeepAlive將在運行 「同時」
(不是真的「的原因LUA不能做到這一點)

要停止該程序集keepAlive(注意小寫字母k)到false任何地方(內部循環)

0

現在人們通常只是使用os.startTimer功能

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 

在這個時候,你usally在smilebasic看,但你仍然可以replecate這是盧阿內循環變量,但存在涉及變量的另一種方法,如果語句。 現在增加了它會像告訴它每秒鐘告訴它打印東西

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 

現在將只是簡單的打印行文本其間15-30的蜱每次。 這是我對這個主題的答案形式問題。

相關問題