This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
martmists's profile picture

progress bar on a monitor

Started by martmists, 15 May 2015 - 07:20 PM
martmists #1
Posted 15 May 2015 - 09:20 PM
Hey everyone! i am trying to make a progress bar on a monitor. the methods on this forum only work on computers, not on monitors.

here is my code:

p = peripheral.wrap("right") -- wrap Energy cell
m = peripheral.wrap("top") -- wrap monitor
m.setTextColor(colors.green) -- set colors
m.setBackgroundColor(colors.red)
term.clear()
function getInfo()
  me = p.getMaxEnergyStored() -- get energy levels
  e = p.getEnergyStored()	 -- ..
end
function calculate()
  -- perc = percentage
  perc = e / me -- e.g. 1000/4000 returns 0.25
  perc = perc * 100 -- needed for calculation
end
function getMonitor()
  x,y = m.getSize() -- get size
  Xvalue = x / 100 -- get multiplier
end
function display()
  monperc = perc * Xvalue -- amount of pixels to fill
  fillspace = math.ceil(monperc) -- get number, e.g. not 1,5
  m.clear() -- clear monitor
  for i = 1,fillspace do
	for l = 1,y do
	  m.setCursorPos(i,l)
	  m.write(" ") -- does NOT change anything
	  l = l + 1
	end
	i = i + 1
  end
end
while true do --loop
  getInfo()
  sleep(0.1) -- short interval due to lagg issues
  calculate()
  sleep(0.1)
  getMonitor()
  sleep(0.1)
  display()
  sleep(0.3)
end

the problem is i keep a red monitor, no green.
note that it instantly fills the monitor with red
Edited on 15 May 2015 - 07:22 PM
valithor #2
Posted 15 May 2015 - 10:01 PM
Right before the for loop in your display function you need to change the background color to the color you want the progress bar to be. Just remember to change it back to red ( or whatever you want the rest of the background to be) somewhere in the beginning of that function.


function display()
  monperc = perc * Xvalue -- amount of pixels to fill
  fillspace = math.ceil(monperc) -- get number, e.g. not 1,5
  m.setBackgroundColor(colors.red) --# making sure the background is red before clearing entire screen
  m.clear() -- clear monitor
  m.setBackgroundColor(colors.green) --# changing background color here
  for i = 1,fillspace do
	for l = 1,y do
	  m.setCursorPos(i,l)
	  m.write(" ") -- does NOT change anything
	  l = l + 1 --# not needed the counter variable in for loops increase automatically
	end
	i = i + 1 --# not needed the counter variable in for loops increase automatically
  end
end

The reason it was not working before was because when you write something (m.write(" ")) it will write that with the current background color. At the point you call that the background color is red, so the area behind what you write, (" "), will be red. Unfortunately spaces do not use text color to determine the color (because there is nothing there to be the text color).

You will not be able to use text color to do this unless if you want to use something such as # for your loadbar.
Edited on 15 May 2015 - 08:11 PM