我对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个反应堆的名称旁边有空格。
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()
我想为其他系统实现类似的代码,但如果可能的话,我更愿意与第一个代码而不是第二个代码类似。
我对编码还很陌生,所以如果这是一个明显或愚蠢的错误,我真诚地道歉。我通过查看代码和尝试不同的东西来学习。我真诚地感谢任何有关我的问题的帮助!
此外,任何简化或简化任何建议的建议也将非常受欢迎!谢谢!!
答案 0 :(得分: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已经完成了反应堆计划explaining it in a video。