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

Screensaver code.

Started by Dlcruz129, 16 December 2012 - 06:39 PM
Dlcruz129 #1
Posted 16 December 2012 - 07:39 PM
Hey guys.

Here's the code for a screensaver code I'm writing. It is supposed to randomly draw pixels on the screen until a key is pressed. The problem is, it doesn't exit when you press a key, and cannot be terminated.


local maxX, maxY = term.getSize()
local count = 1
term.clear()
while true do
  local function screensaver()
    local xPos = math.random(1,maxX)
    local yPos = math.random(1,maxY)
    term.setCursorPos(xPos,yPos)
    local txtColor = 2^math.random(0,15)
    term.setBackgroundColor(txtColor)
    write(" ")
    count = count+1
    sleep(0.1)
    if count == 250 then
	  count = 1
	  term.setBackgroundColor(colors.black)
	  term.clear()
    end
  end
 
  local function exit()
    while true do
	  sleep(0.1)
	  event = os.pullEvent()
	  if event == "char" then
	    term.clear()
	    term.setCursorPos(1,1)
	    error()
	  end
    end
  end
  parallel.waitForAny(screensaver, exit)
end

Any help is appreciated. Thanks!

–Dlcruz
theoriginalbit #2
Posted 16 December 2012 - 07:50 PM
try this:



local maxX, maxY = term.getSize()
local count = 1
term.clear()
while true do
  event = os.pullEvent()

  local xPos = math.random(1,maxX)
  local yPos = math.random(1,maxY)
  term.setCursorPos(xPos,yPos)
  local txtColor = 2^math.random(0,15)
  term.setBackgroundColor(txtColor)
  write(" ")
  count = count + 1
  sleep(0.1)
  if count == 250 then
   count = 1
    term.setBackgroundColor(colors.black)
    term.clear()
  end

  if event == "key" then
   term.clear()
   term.setCursorPos(1,1)
   error()
  end
end

the problem was that you had the whole thing surrounded by a while loop.
Dlcruz129 #3
Posted 16 December 2012 - 07:55 PM
try this:



local maxX, maxY = term.getSize()
local count = 1
term.clear()
while true do
  event = os.pullEvent()

  local xPos = math.random(1,maxX)
  local yPos = math.random(1,maxY)
  term.setCursorPos(xPos,yPos)
  local txtColor = 2^math.random(0,15)
  term.setBackgroundColor(txtColor)
  write(" ")
  count = count + 1
  sleep(0.1)
  if count == 250 then
   count = 1
	term.setBackgroundColor(colors.black)
	term.clear()
  end

  if event == "key" then
   term.clear()
   term.setCursorPos(1,1)
   error()
  end
end

the problem was that you had the whole thing surrounded by a while loop.

Nope, the screen just goes black. Then crashes with the error msg "11".
theoriginalbit #4
Posted 16 December 2012 - 07:57 PM
11? oh you mean it says something like "bios:some number [string "progname"] Error: 11: number expected" or some error message like that. yes thats because of this

2^math.random(0,15)

this isnt a valid colour.
Dlcruz129 #5
Posted 16 December 2012 - 07:59 PM
11? oh you mean it says something like "bios:some number [string "progname"] Error: 11: number expected" or some error message like that. yes thats because of this

2^math.random(0,15)

this isnt a valid colour.

The program worked fine before. It raises 2 to a power of a random number between 0-15, therefore generating a random color. It simply prints "11" to the console. 11 means something special, I just can't remember what…
theoriginalbit #6
Posted 16 December 2012 - 08:02 PM
try putting shell.exit() instead of error?
Dlcruz129 #7
Posted 16 December 2012 - 08:06 PM
try putting shell.exit() instead of error?

No. You should NEVER use shell.exit(). Its pretty much an alias for os.shutdown(). Please make sure you know what you are talking about before you try to help me.
theoriginalbit #8
Posted 16 December 2012 - 08:07 PM
Just trying to be helpful. As I cannot replicate the issue you are getting.
theoriginalbit #9
Posted 16 December 2012 - 08:09 PM
and fine, use return instead of error()
Dlcruz129 #10
Posted 16 December 2012 - 08:11 PM
and fine, use return instead of error()

That fixed the exit part of the program, but the screensaver does not show up.
theoriginalbit #11
Posted 16 December 2012 - 08:12 PM
ohh oops. I see why. I missed the sleep in the code. sorry. disadvantage of typing it quick.
theoriginalbit #12
Posted 16 December 2012 - 08:23 PM
Fully functioning. Adds lots to the screen then resets after 5 seconds :)/>
Spoiler


local maxX, maxY = term.getSize()
local count = 1
local d = {}

resetTimer = os.startTimer(5)
addTimer = os.startTimer(0.1)

local function clear()
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1,1)
end

while true do
event = { os.pullEventRaw() }

if event[1] == "key" then
clear()
return
elseif event[1] == "timer" then
if event[2] == addTimer then
t = {
["x"] = math.random(1,maxX),
["y"] = math.random(1,maxY),
["color"] = 2^math.random(0,15)
}

table.insert(d, t)
addTimer = os.startTimer(0.1)
elseif event[2] == resetTimer then
d = {}
resetTimer = os.startTimer(5)
end
end

clear()
for _, v in pairs( d ) do
term.setCursorPos( v["x"], v["y"] )
term.setBackgroundColor(v["color"])
write(" ")
end
end