0

我对 Lua 很陌生,但我觉得我对基础知识有很好的掌握。最近在计算机领域,我尝试设计自己的显示器来显示我的反应堆是否打开。这就是我想出的:

function screen()
  monitor = peripheral.wrap("top")
  monitor.clear()
  monitor.setCursorPos(1,1)
  monitor.setTextColor(colors.white)
  monitor.write("Reactor 1: ")
  monitor.setCursorPos(1,3)
  monitor.write("Reactor 2: ")
  monitor.setCursorPos(1,5)
  monitor.write("Reactor 3: ")
  monitor.setCursorPos(1,7)
  monitor.write("Reactor 4: ")
  monitor.setCursorPos(1,9)
  monitor.write("Reactor 5: ")
  monitor.setCursorPos(1,11)
  monitor.write("Reactor 6: ")
end

function test(color,cursor1,cursor2)
while true do
  if colors.test(rs.getBundledInput("right"), color) == true then
    monitor.setCursorPos(cursor1,cursor2)
    monitor.setTextColor(colors.green)
    monitor.write("Active  ")
  elseif colors.test(rs.getBundledInput("right"), color) == false then
    monitor.setCursorPos(cursor1,cursor2)
    monitor.setTextColor(colors.red)
    monitor.write("Inactive")
  end
  sleep(0.1)
end
sleep(0.1)
end
sleep(0.1)

function status()
  screen()
  test(colors.red,12,1)
  test(colors.orange,12,3)
  test(colors.yellow,12,5)
  test(colors.green,12,7)
  test(colors.blue,12,9)
  test(colors.purple,12,11)
  sleep(0.1)
end

status()

不幸的是,这并没有给我想要的结果。它不是按名称显示每个反应堆以及它是否处于活动状态,而是显示所有反应堆名称,但只显示第一个反应堆是否处于活动状态。其他 5 个反应堆的名称旁边有空格。

此图显示了监视器上发生的情况

这就是我想出的解决方法。它有效,但比第一个长得多:

function test(color,cursor1,cursor2)
while true do
  if colors.test(rs.getBundledInput("right"), color) == true then
    monitor.setCursorPos(cursor1,cursor2)
    monitor.setTextColor(colors.green)
    monitor.write("Active  ")
  elseif colors.test(rs.getBundledInput("right"), color) == false then
    monitor.setCursorPos(cursor1,cursor2)
    monitor.setTextColor(colors.red)
    monitor.write("Inactive")
  end
  sleep(0.1)
end
sleep(0.1)
end
sleep(0.1)

function status()
  screen()
  test(colors.red,12,1)
  test(colors.orange,12,3)
  test(colors.yellow,12,5)
  test(colors.green,12,7)
  test(colors.blue,12,9)
  test(colors.purple,12,11)
  sleep(0.1)
end

status()


function screen()
  monitor = peripheral.wrap("top")
    monitor.clear()
    monitor.setCursorPos(1,1)
    monitor.setTextColor(colors.white)
    monitor.write("Reactor 1: ")
    monitor.setCursorPos(1,3)
    monitor.write("Reactor 2: ")
    monitor.setCursorPos(1,5)
    monitor.write("Reactor 3: ")
    monitor.setCursorPos(1,7)
    monitor.write("Reactor 4: ")
    monitor.setCursorPos(1,9)
    monitor.write("Reactor 5: ")
    monitor.setCursorPos(1,11)
    monitor.write("Reactor 6: ")
end

function test()
local monitor = peripheral.wrap("top")
  while true do
    if colors.test(rs.getBundledInput("right"), colors.red) == true then
      monitor.setCursorPos(12,1)
      monitor.setTextColor(colors.green)
      monitor.write("Active  ")
    elseif colors.test(rs.getBundledInput("right"), colors.red) == false then
      monitor.setCursorPos(12,1)
      monitor.setTextColor(colors.red)
      monitor.write("Inactive")
    end
    if colors.test(rs.getBundledInput("right"), colors.orange) == true then
      monitor.setCursorPos(12,3)
      monitor.setTextColor(colors.green)
      monitor.write("Active  ")
    elseif colors.test(rs.getBundledInput("right"), colors.orange) == false then
      monitor.setCursorPos(12,3)
      monitor.setTextColor(colors.red)
      monitor.write("Inactive")
    end
    if colors.test(rs.getBundledInput("right"), colors.yellow) == true then
      monitor.setCursorPos(12,5)
      monitor.setTextColor(colors.green)
      monitor.write("Active  ")
    elseif colors.test(rs.getBundledInput("right"), colors.yellow) == false then
      monitor.setCursorPos(12,5)
      monitor.setTextColor(colors.red)
      monitor.write("Inactive")
    end
    if colors.test(rs.getBundledInput("right"), colors.green) == true then
      monitor.setCursorPos(12,7)
      monitor.setTextColor(colors.green)
      monitor.write("Active  ")
    elseif colors.test(rs.getBundledInput("right"), colors.green) == false then
      monitor.setCursorPos(12,7)
      monitor.setTextColor(colors.red)
      monitor.write("Inactive")
    end
    if colors.test(rs.getBundledInput("right"), colors.blue) == true then
      monitor.setCursorPos(12,9)
      monitor.setTextColor(colors.green)
      monitor.write("Active  ")
    elseif colors.test(rs.getBundledInput("right"), colors.blue) == false then
      monitor.setCursorPos(12,9)
      monitor.setTextColor(colors.red)
      monitor.write("Inactive")
    end
    if colors.test(rs.getBundledInput("right"), colors.purple) == true then
      monitor.setCursorPos(12,11)
      monitor.setTextColor(colors.green)
      monitor.write("Active  ")
    elseif colors.test(rs.getBundledInput("right"), colors.purple) == false then
      monitor.setCursorPos(12,11)
      monitor.setTextColor(colors.red)
      monitor.write("Inactive")
    end
  sleep(0.1)
end
sleep(0.1)
end
sleep(0.1)

function run()
  screen()
  test()
end

run()

我想为其他系统实现类似的代码,但如果可能的话,我更喜欢类似于第一个代码而不是第二个代码。

我对编码还是很陌生,所以如果这是一个明显或愚蠢的错误,我真诚地道歉。我只是通过查看代码和尝试不同的东西来学习。我真诚地感谢任何解决我的问题的帮助!

此外,任何简化或简化任何事情的建议也将不胜感激!谢谢!!

4

1 回答 1

0

我确切地知道如何帮助你,首先,在你使用“while true do”的函数测试的第一个代码块中,它无法逃脱循环(除了使用“break”),所以它不断检查对于第一个,无法逃脱检查其他人。

试试这个(未经测试):

local monitor = peripheral.wrap( "top" )
monitor.clear()

function screen()
  monitor.setTextColor( colors.white )
  for i = 1, 6 do
    monitor.setCursorPos( 1, i*2-1 )
    monitor.write( "Reactor " .. i .. ": " )
  end
end

function test( color, x, y )
  if colors.test( rs.getBundledInput( "right" ), color ) then
    monitor.setCursorPos( x, y )
    monitor.setTextColor( colors.green )
    monitor.write("Active  ")
  else
    monitor.setCursorPos( x, y )
    monitor.setTextColor( colors.red )
    monitor.write( "Inactive" )
  end
end

local rscolors = {
  colors.red = 1,
  colors.orange = 3,
  colors.yellow = 5,
  colors.green = 7,
  colors.blue = 9,
  colors.purple = 11
}

while true do
  for k, v in pairs( rscolors ) do
    test( k, 12, v )
  end
  sleep( 0.1 )
end

PS:Direwolf20已经做了一个反应堆程序,在视频中对其进行了解释

  • 按钮 (XBbMUYNn)
  • 反应堆(4qNyaPav)
于 2016-06-14T00:00:22.827 回答