Jump to content




Draw to the Screen Until User Presses key?

lua computer

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

#1 legokingmi

  • New Members
  • 2 posts

Posted 29 August 2014 - 03:09 PM

Hello, I am making a screen saver type program and I want the program to place pixels randomly all over the computer screen while waiting for a keypress.

I am having trouble at the part where it repeats until the key is pressed. It will draw one pixel, then wait for a key without drawing other pixels.

shell.run("pastebin","get","sZTVBCue","VEIos") --Get the background image program.

clear=0
iAmAway = paintutils.loadImage("VEIos")
paintutils.drawImage(iAmAway,1,1)

repeat
   x = math.random(1,51)
   y = math.random(1,19)
   paintutils.drawPixel(x,y,8) -- Draw the random pixel in light blue.
   clear=1+1
   sleep(0.5)

   if clear == 60 then -- Clear screen every 60 runs.
	  paintutils.drawImage(iAmAway,1,1)
	  clear = 0
   end

until os.pullEvent("key") -- SUPPOSED to repeat until the user presses a key but only does one round then just waits.

sleep(1)

term.setBackgroundColor(32768)
term.clear()
term.setCursorPos(1,1)


Thanks much! :)

-Lego

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 29 August 2014 - 03:20 PM

Timers are the key here. We use them to replace the use of "sleep", then set up an event listening loop that waits for timer or keypress events.

Something along these lines:

local myEvent
local myTimer = os.startTimer(0)

while true do
   myEvent = {os.pullEvent()}  -- Grab any event, stick the details in a table.

   -- Now, depending on the type of event that occurred:
   if myEvent[1] == "timer" and myEvent[2] == myTimer then
	  x = math.random(1,51)
	  y = math.random(1,19)
	  paintutils.drawPixel(x,y,8) -- Draw the random pixel in light blue.
	  clear=clear+1
	  
	  if clear == 60 then -- Clear screen every 60 runs.
			 paintutils.drawImage(iAmAway,1,1)
			 clear = 0
	  end
	  
	  myTimer = os.startTimer(1)
   elseif myEvent[1] == "key" then
	  break
   end
end

Edited by Bomb Bloke, 29 August 2014 - 03:21 PM.


#3 legokingmi

  • New Members
  • 2 posts

Posted 29 August 2014 - 03:35 PM

Thank you very much :)

-Lego





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users