Jump to content




Screensaver code.


  • You cannot reply to this topic
11 replies to this topic

#1 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

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

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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.

#3 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 16 December 2012 - 07:55 PM

 TheOriginalBIT, on 16 December 2012 - 07:50 PM, said:

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".

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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.

#5 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 16 December 2012 - 07:59 PM

 TheOriginalBIT, on 16 December 2012 - 07:57 PM, said:

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...

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 16 December 2012 - 08:02 PM

try putting shell.exit() instead of error?

#7 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 16 December 2012 - 08:06 PM

 TheOriginalBIT, on 16 December 2012 - 08:02 PM, said:

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.

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 16 December 2012 - 08:07 PM

Just trying to be helpful. As I cannot replicate the issue you are getting.

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 16 December 2012 - 08:09 PM

and fine, use return instead of error()

#10 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 16 December 2012 - 08:11 PM

 TheOriginalBIT, on 16 December 2012 - 08:09 PM, said:

and fine, use return instead of error()

That fixed the exit part of the program, but the screensaver does not show up.

#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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.

#12 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 16 December 2012 - 08:23 PM

Fully functioning. Adds lots to the screen then resets after 5 seconds :)
Spoiler






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users