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

Monitor Display different Colours text

Started by SkyRamon, 07 February 2014 - 05:41 PM
SkyRamon #1
Posted 07 February 2014 - 06:41 PM
Look what i want is an sort of rainbow effect with my text.
my text that i want to show up is Computercraft Shop!!
i want it ti display in different colors.
my code looks like this i get alot of errors everytime and i dont know how to fix.
heres my code


local mon = peripheral.wrap("back")
mon.setTextScale(4)
while true do
mon.clear()
mon.setCursorPos(1,1)
mon.setTextColor(2)
mon.write("Computercraft Shop!!")
sleep(1)
mon.clear()
mon.setCursorPos(1,1)
mon.setTextColor(3)
mon.write("Computercraft Shop!!")
sleep(1)
mon.clear()
mon.setCursorPos(1,1)
mon.setTextColor(4)
mon.write("Computercraft Shop!!")
sleep(1)
mon.clear()
mon.setCursorPos(1,1)
mon.setTextColor(5)
mon.write("Computercraft Shop!!")
sleep(1)
mon.clear()
mon.setCursorPos(1,1)
mon.setTextColor(6)
mon.write("Computercraft Shop!!")
sleep(1)
end
awsmazinggenius #2
Posted 07 February 2014 - 06:47 PM
Please indent. You use colors with the colors API (search it up on the wiki) like this:

local mon = peripheral.wrap("back")
mon.setTextScale(4)
mon.clear()
while true do
  mon.setCursorPos(1,1)
  mon.setTextColor(colors.red)
  mon.write("Computercraft Shop!!")
  sleep(1)
  mon.setCursorPos(1,1)
  mon.setTextColor(colors.orange)
  mon.write("Computercraft Shop!!")
  sleep(1)
  mon.setCursorPos(1,1)
  mon.setTextColor(colors.yellow)
  mon.write("Computercraft Shop!!")
  sleep(1)
  mon.setCursorPos(1,1)
  mon.setTextColor(colors.lime)
  mon.write("Computercraft Shop!!")
  sleep(1)
  mon.setTextColor(colors.blue)
  mon.write("Computercraft Shop!!")
  sleep(1)
  mon.setCursorPos(1, 1)
  mon.setTextColor(colors.purple)
  mon.write("Computercraft Shop!!")
  sleep(1)
end
Also, please describe the error in more detail.
Edited on 07 February 2014 - 05:48 PM
CometWolf #3
Posted 07 February 2014 - 06:51 PM
3,5 and 6 aren't even colors… clearing the monitor when you are overwriting the text that's already on it is unnesacary, and there is a much easier way to do this.

local mon = peripheral.wrap("back")
mon.setTextScale(4)
while true do
  for k,v in pairs(colors) do -- iterate through the color table
    if type(v) == "number" then -- since it contains more than just the color numbers, we have to make sure the value we're using is a number
	  mon.setCursorPos(1,1)
	  mon.setTextColor(v) -- set text color
	  mon.write("Computercraft Shop!!") --this will overwrite the previously written text, if any
	  sleep(1)
    end
  end
end
Bomb Bloke #4
Posted 07 February 2014 - 06:55 PM
Refer to the colours API to see the different shades available - you'll notice that there's none with a value of 3, 5, or 6: They're all powers of two.

A "for" loop would also come in handy here.

local mon = peripheral.wrap("back")
mon.setTextScale(4)
mon.clear()
while true do
	for i=1,5 do
		mon.setCursorPos(1,1)
		mon.setTextColor(math.pow(2,i))
		mon.write("Computercraft Shop!!")
		sleep(1)
	end
end

If i = 1, then math.pow(2,i) raises two to the power of one (returning two).

If i = 2, then math.pow(2,i) raises two to the power of two (returning four).

And so on.
Edited on 07 February 2014 - 05:56 PM
SkyRamon #5
Posted 07 February 2014 - 07:00 PM
Refer to the colours API to see the different shades available - you'll notice that there's none with a value of 3, 5, or 6: They're all powers of two.

A "for" loop would also come in handy here.

local mon = peripheral.wrap("back")
mon.setTextScale(4)
mon.clear()
while true do
	for i=1,5 do
		mon.setCursorPos(1,1)
		mon.setTextColor(math.pow(2,i))
		mon.write("Computercraft Shop!!")
		sleep(1)
	end
end

If i = 1, then math.pow(2,i) raises two to the power of one (returning two).

If i = 2, then math.pow(2,i) raises two to the power of two (returning four).

And so on.

i get this error while using your code: startup:2: attempt to index ? (a nil value)
CometWolf #6
Posted 07 February 2014 - 07:08 PM
Put a monitor behind your computer…
awsmazinggenius #7
Posted 07 February 2014 - 07:10 PM
The monitor could be facing behind the computer like this:

c = computer
m = monitor
<---- = direction of the front (black) side

<---- c m ---->
surferpup #8
Posted 09 February 2014 - 12:23 AM
If you work through The Complete Monitor Buttons and Control Tutorial Part I you will become a pro at displaying text on monitors, and you will learn a few things about Lua, ComputerCraft and programming. It is why we wrote it – so you could become awesome.
awsmazinggenius #9
Posted 10 February 2014 - 11:39 AM
Yes, the monitor tutorial surfer linked is quite good. (I admit, I helped with fixing typos and grammar and stuff.) Using touch screens might be a little outside the scope of this issue, but you don't have to read that bit.