Computercraft程序打印新线路,而不是更新以前的行

发布于 2025-02-06 23:44:06 字数 3324 浏览 3 评论 0原文

我每次打印一条线以推动y值(基本上是newline)时,我将y(监视器的Y索引)增加一个。但是,我遇到了一个问题,但是当我完成mon_1.setCursorPos(x,y+1)时,它会尽其所能更新上一行,但是当我做y = y = y+时1 mon_1.setCursorPos(x,y)它在不更新上线的情况下打印了新行。有什么建议吗?

这是完整的代码

    --Define some colors

color_fuel = 16
color_ok = 2243
color_error = 224455
color_blank = 0
color_active = 2240

--Tables
reactors={}
max_energy={}
produced={}
percent_capacity={}

--Globals
x = 1
y = 1
i = 0

--Lamps
lamp_1 = peripheral.wrap("colorful_lamp_0")
lamp_2 = peripheral.wrap("colorful_lamp_1")
lamp_3 = peripheral.wrap("colorful_lamp_2")

--Monitors
mon_1 = peripheral.wrap("monitor_3")

--Cap banks
cap_1 = peripheral.wrap("capacitor_bank_2")

--Helper functions
function flash_fuel_lamp() 
    lamp_2.setLampColor(color_blank)
    sleep(0.01)
    lamp_2.setLampColor(color_error)
    for i = 10,1,-1 do
        lamp_1.setLampColor(color_fuel)
        sleep(1)
        lamp_1.setLampColor(color_blank)
        sleep(1)
    end
    lamp_1.setLampColor(color_fuel)
end

function clear_lamps()
    lamp_1.setLampColor(color_blank)
    lamp_2.setLampColor(color_blank)
    lamp_3.setLampColor(color_blank)
end

--Boot Sequence

--Set lamps to black and clear screen
clear_lamps()
mon_1.clear()

--Turn on the reactors and set the status light to green
for k,v in pairs(peripheral.getNames()) do
    if(string.find(v, "BigReactors")) then
        reactors[i] = peripheral.wrap(v)
        i = i + 1
    else
        --print("Other Peripheral Detected")
    end
end

for k,v in pairs(reactors) do
    v.setActive(true)
end

lamp_2.setLampColor(color_ok)
lamp_3.setLampColor(color_active)

--Main loop

for key,value in pairs(max_energy) do
        mon_1.setCursorPos(x, y)
        mon_1.write("Reactor " .. key .. " Max Energy: " .. value)
        y = y + 1
end

while true do
    --Check the fuel level
    if(redstone.getAnalogInput("bottom") == 0) then
        lamp_3.setLampColor(color_blank)
        for k,v in pairs(reactors) do
            v.setActive(false)
        end
        flash_fuel_lamp()
    end
    
    for k,v in pairs(reactors) do
        if(v.getActive() == false) then
            lamp_3.setLampColor(color_blank)
        end
        --Calculate the energies
        max_energy[k] = v.getEnergyStored()
        produced[k] = v.getEnergyProducedLastTick()
        percent_capacity[k] = (max_energy[k] / produced[k]) * 100
    end
    
    --[[max_energy = reactor.getEnergyStored()
    energy_produced = reactor.getEnergyProducedLastTick()
    percent_capacity = (max_energy / energy_produced) * 100]]

    max_energy_cap = cap_1.getMaxEnergyStored()
    energy_stored = cap_1.getEnergyStored()
    percent_capacity_cap = (energy_stored / max_energy_cap) * 100
    
    y=y+1
    mon_1.setCursorPos(x, y)
    for key,value in pairs(percent_capacity) do
        for k,v in pairs(reactors) do
            mon_1.write("Reactor " .. k .. " Percent Capacity: " .. value)
            y=y+1
            mon_1.setCursorPos(x, y)
            mon_1.write("Capacitor output per tick: " .. cap_1.getAverageOutputPerTick())
            y=y+1
            mon_1.setCursorPos(x, y)
            mon_1.write("Capacitor input per tick: " .. cap_1.getAverageInputPerTick())
            y=y+1
            mon_1.setCursorPos(x, y)
        end
    end
    sleep(0.01)
end```

I increment y (the y index of the monitor) by one each time I print a line to advance the y value (basically a newline). I'm running into an issue however, when I have done mon_1.setCursorPos(x, y+1) it updates the previous line as it should, however when I do y = y+1 mon_1.setCursorPos(x, y) it prints a new line without updating the previous line. Any suggestions?

Here's the full code

    --Define some colors

color_fuel = 16
color_ok = 2243
color_error = 224455
color_blank = 0
color_active = 2240

--Tables
reactors={}
max_energy={}
produced={}
percent_capacity={}

--Globals
x = 1
y = 1
i = 0

--Lamps
lamp_1 = peripheral.wrap("colorful_lamp_0")
lamp_2 = peripheral.wrap("colorful_lamp_1")
lamp_3 = peripheral.wrap("colorful_lamp_2")

--Monitors
mon_1 = peripheral.wrap("monitor_3")

--Cap banks
cap_1 = peripheral.wrap("capacitor_bank_2")

--Helper functions
function flash_fuel_lamp() 
    lamp_2.setLampColor(color_blank)
    sleep(0.01)
    lamp_2.setLampColor(color_error)
    for i = 10,1,-1 do
        lamp_1.setLampColor(color_fuel)
        sleep(1)
        lamp_1.setLampColor(color_blank)
        sleep(1)
    end
    lamp_1.setLampColor(color_fuel)
end

function clear_lamps()
    lamp_1.setLampColor(color_blank)
    lamp_2.setLampColor(color_blank)
    lamp_3.setLampColor(color_blank)
end

--Boot Sequence

--Set lamps to black and clear screen
clear_lamps()
mon_1.clear()

--Turn on the reactors and set the status light to green
for k,v in pairs(peripheral.getNames()) do
    if(string.find(v, "BigReactors")) then
        reactors[i] = peripheral.wrap(v)
        i = i + 1
    else
        --print("Other Peripheral Detected")
    end
end

for k,v in pairs(reactors) do
    v.setActive(true)
end

lamp_2.setLampColor(color_ok)
lamp_3.setLampColor(color_active)

--Main loop

for key,value in pairs(max_energy) do
        mon_1.setCursorPos(x, y)
        mon_1.write("Reactor " .. key .. " Max Energy: " .. value)
        y = y + 1
end

while true do
    --Check the fuel level
    if(redstone.getAnalogInput("bottom") == 0) then
        lamp_3.setLampColor(color_blank)
        for k,v in pairs(reactors) do
            v.setActive(false)
        end
        flash_fuel_lamp()
    end
    
    for k,v in pairs(reactors) do
        if(v.getActive() == false) then
            lamp_3.setLampColor(color_blank)
        end
        --Calculate the energies
        max_energy[k] = v.getEnergyStored()
        produced[k] = v.getEnergyProducedLastTick()
        percent_capacity[k] = (max_energy[k] / produced[k]) * 100
    end
    
    --[[max_energy = reactor.getEnergyStored()
    energy_produced = reactor.getEnergyProducedLastTick()
    percent_capacity = (max_energy / energy_produced) * 100]]

    max_energy_cap = cap_1.getMaxEnergyStored()
    energy_stored = cap_1.getEnergyStored()
    percent_capacity_cap = (energy_stored / max_energy_cap) * 100
    
    y=y+1
    mon_1.setCursorPos(x, y)
    for key,value in pairs(percent_capacity) do
        for k,v in pairs(reactors) do
            mon_1.write("Reactor " .. k .. " Percent Capacity: " .. value)
            y=y+1
            mon_1.setCursorPos(x, y)
            mon_1.write("Capacitor output per tick: " .. cap_1.getAverageOutputPerTick())
            y=y+1
            mon_1.setCursorPos(x, y)
            mon_1.write("Capacitor input per tick: " .. cap_1.getAverageInputPerTick())
            y=y+1
            mon_1.setCursorPos(x, y)
        end
    end
    sleep(0.01)
end```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫﹏色ふ单纯 2025-02-13 23:44:06

因此,我认为您可能会忘记,当您进行mon_1.setCursorPos(x,y+1)时,您不会增加y变量,您只会通过称为函数的更高值,而使用y = y+1 mon_1.setCursorPos(x,y) y

这解释了为什么在增加y时未更新上一行的行,因为代码底部的嵌套前面也使用y变量。

So I think that you might be forgetting that when you do mon_1.setCursorPos(x, y+1) you're not incrementing the y variable, you're only passing a higher value to the called function, while with y = y+1 mon_1.setCursorPos(x, y) you are incrementing y.

This explains why the previous line is not being updated when incrementing y, since the nested for-loops at the bottom of your code use that y variable as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文