简体   繁体   English

Computercraft Lua代码未按预期工作

[英]Computercraft Lua code not working as expected

I am quite new to Lua but I feel I have a decent grasp on the basics. 我对Lua并不陌生,但是我对基本知识掌握得很好。 Recently in computercraft, I tried to design my own monitor to display whether or not my reactors were on or not. 最近在计算机技术领域,我试图设计自己的显示器来显示我的反应堆是否打开。 This is what I came up with: 这是我想出的:

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()

Unfortunately, this did not give me the desired result. 不幸的是,这没有给我期望的结果。 Instead of showing each reactor by name and whether or not it was active, it showed all reactor names, but only showed whether or not the first reactor was active. 它没有显示每个反应堆的名称以及是否处于活动状态,而是显示了所有反应堆名称,而仅显示了第一个反应堆是否处于活动状态。 The other 5 reactors had blank spaces next to their names. 其他5个反应堆的名称旁边都有空格。

This image shows what occurs on the monitor 此图显示了在监视器上发生的事情

This is what I came up with as a work around. 这是我通过变通而想到的。 It works, but it is much longer than the first: 它可以工作,但是比第一个要长得多:

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()

I would like to implement similar code for other systems but I would much prefer to do it similar to the first code rather than the second one, if possible. 我想为其他系统实现类似的代码,但是如果可能的话,我更喜欢与第一个代码相似,而不是第二个代码。

I am still quite new to coding, so I sincerely apologize if this is an obvious or stupid error. 我对编码还是很陌生,因此,如果这是一个明显的或愚蠢的错误,我深表歉意。 I've kind of just learned by looking at code and trying different things. 我只是通过看代码并尝试不同的东西而学到的。 I would sincerely appreciate any help with my problem! 衷心感谢您为我的问题提供的任何帮助!

Also, any suggestions to streamline or simplify anything at all would also be most appreciated! 此外,任何精简或简化一切的建议也将不胜感激! Thank you!! 谢谢!!

I know exactly how to help you, first of all, in the first block of code in the function test you used "while true do" which gives it no way to escape the loop (except using "break"), so its constantly checking for the first one and cannot escape to check for the others. 我确切地知道如何为您提供帮助,首先,在您使用“ while true do”的功能测试的第一段代码中,它无法摆脱循环(除非使用“ break”),因此它会不断进行检查对于第一个,无法逃避检查其他人。

Try this (untested): 试试这个(未试用):

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 already did a reactor program explaining it in a video . PS: Direwolf20已经在视频中做了一个反应堆程序对其进行了解释

  • button (XBbMUYNn) 按钮(XBbMUYNn)
  • reactor (4qNyaPav) 反应堆(4qNyaPav)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM