我是Lua的新手,但我觉得我对基础知识已经有了一个很好的掌握。最近在computercraft上,我试图设计自己的监视器来显示我的反应堆是否打开。这是我想出来的:
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个反应堆的名称旁边都有空格。
This image shows what occurs on the monitor
这就是我想出的一个变通办法。它可以工作,但比第一个要长得多:
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()
我想为其他系统实现类似的代码,但如果可能的话,我更喜欢做类似于第一个代码而不是第二个代码的代码。
我对编码还是很陌生的,所以如果这是一个明显的或愚蠢的错误,我真诚地道歉。我只是通过查看代码和尝试不同的东西来学习。我将真诚地感谢任何人对我的问题的帮助!
此外,任何关于精简或简化任何内容的建议也将非常受欢迎!谢谢你!!
发布于 2016-06-14 08:00:23
首先,我知道如何帮助你,首先,在你的函数测试的第一段代码中,你使用了"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已经做了一个反应堆程序explaining it in a video。
https://stackoverflow.com/questions/37758543
复制相似问题