簡體   English   中英

Computercraft Lua代碼未按預期工作

[英]Computercraft Lua code not working as expected

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

我想為其他系統實現類似的代碼,但是如果可能的話,我更喜歡與第一個代碼相似,而不是第二個代碼。

我對編碼還是很陌生,因此,如果這是一個明顯的或愚蠢的錯誤,我深表歉意。 我只是通過看代碼並嘗試不同的東西而學到的。 衷心感謝您為我的問題提供的任何幫助!

此外,任何精簡或簡化一切的建議也將不勝感激! 謝謝!!

我確切地知道如何為您提供幫助,首先,在您使用“ 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM