【问题标题】:Adding a timer to my Lua program在我的 Lua 程序中添加一个计时器
【发布时间】:2013-05-04 22:02:48
【问题描述】:

我是 Lua 的新手,我正在编写一个程序。该程序是信件四处走动并收集其他信件(有点像蠕虫程序)。但是,我希望这是定时的。 (我在computercraft,它是我的世界的一个mod,但仍然使用Lua,所以我认为这并不重要)我正在使用os.PullEvent(“key”)以便我可以移动字母,但是os. pullEvent() 将暂停程序,直到它满意为止。我的问题是我希望计时器同时不断滴答作响。关于我如何做到这一点的任何想法?谢谢!

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

【问题讨论】:

  • 你可以简单地使用print( ("X"):rep(50) )作为边框。
  • 谢谢!我会记住这一点!

标签: timer lua computercraft


【解决方案1】:

您可以使用os.StartTimer(),这将在您对os.pullEvent() 的调用中生成"timer" 事件

ComputerCraft OS API documentation

【讨论】:

    【解决方案2】:

    我不确定这是否回答了您的问题,但您可以看到一些代码需要多长时间才能运行:

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

    【讨论】:

      【解决方案3】:

      我的 Computercraft 程序遵循这种“多任务”设计

      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 不能这样做) p>

      要停止程序,请将keepAlive(注意小写的k)设置为false(循环内)

      【讨论】:

        【解决方案4】:

        现在人们通常只在变量中使用 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
        

        这是你通常在smilebasic中看到的东西,但你仍然可以复制这是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
        

        现在每次只需打印 15 到 30 个刻度之间的那一行文本。 这是我对这个主题形式问题的回答。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-02-14
          • 1970-01-01
          • 2021-08-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-09
          • 1970-01-01
          相关资源
          最近更新 更多