Jump to content




Missile Launch System


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

#1 hbomb79

  • Members
  • 352 posts
  • LocationOrewa, New Zealand

Posted 16 June 2014 - 07:12 AM

Hi there, in my code i have a for loop counting down from sixty and i would like the ability to type a password to stop it, how would i do this?

here is the part of the code:
for y = 1, 60 do
    clear()
   x = x - 1
  
   if x < 61 and x > 30 then
	 term.setTextColor(colors.lime)
	 print ("Missile Silo ONE Launch In T-"..x.."")
   elseif  x < 30 and x > 10 then
	 term.setTextColor(colors.orange)
	 print ("Missile Silo ONE Launch In T-"..x.."")
   elseif x < 10 and x > 1 then
   rs.setOutput("right", true)
	 term.setTextColor(colors.red)
	 print ("Missile Silo ONE Launch In T-"..x.."")
   elseif x == 0 then
	  rs.setOutput("back", true)
	 term.setTextColor(colors.red)
	 print "Launching Missile!"
	 sleep(10)
	    rs.setOutput("top",false)
		   rs.setOutput("bottom",false)
		    rs.setOutput("back",false)
		   rs.setOutput("right",false)
		   rs.setOutput("left",false)
   end
   sleep(1)
   clear()
 
  end


#2 Bomb Bloke

    Hobbyist Coder

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

Posted 16 June 2014 - 08:55 AM

This is where you want the parallel API.

Rig one function to perform the countdown, and the other to take in the password. Every time the countdown function sleeps, it needs to put the cursor back to where it was when it last finished sleeping.

#3 hbomb79

  • Members
  • 352 posts
  • LocationOrewa, New Zealand

Posted 16 June 2014 - 09:01 AM

I havent used this before, could you give me an example, im a beginner at this...

#4 Bomb Bloke

    Hobbyist Coder

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

Posted 16 June 2014 - 09:26 AM

I've not tested it, but something along these lines should work.

local password = "panda"
local explode = true

local function counter()
	for x = 100, 1, -1 do
		local oldX, oldY = term.getCursorPos()
		
		term.setCursorPos(1,1)
		term.clearLine()
		term.write(x)
		
		term.setCursorPos(oldX, oldY)
		sleep(1)
	end
end

local function getPassword()
	while explode do
		term.setCursorPos(1,2)
		term.clearLine()
		term.write("Enter the shutdown password: ")
		
		local input = read("*")
		explode = not (input == password)
	end
end

parallel.waitForAny(counter, getPassword)

if explode then detonate() end


#5 hbomb79

  • Members
  • 352 posts
  • LocationOrewa, New Zealand

Posted 16 June 2014 - 11:33 PM

Why must it go back to where it was and then to 1,1? I don't understand why it can't just go to 1,1 each time? Maybe it's obvious, I'm not sure tho

#6 Bomb Bloke

    Hobbyist Coder

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

Posted 17 June 2014 - 01:13 AM

The idea is that control switches between the functions running in "parallel" whenever they yield. A yield is triggered on most any action that causes a pause - like when you wait for the user to type something (eg using read()), or when you sleep.

One function is sitting there waiting for you to type a password. When you press a key, you expect the result to show up after the last character you typed. Each press moves the cursor along to the right.

But what if, between keypresses, the cursor gets moved off to 1,1 in order to print the countdown value? Your next keypress will appear next to that, unless you take care to put the cursor back where it was taken from before passing control back to the input reading function!

#7 hbomb79

  • Members
  • 352 posts
  • LocationOrewa, New Zealand

Posted 17 June 2014 - 02:35 AM

I think I get it know, thank you very much





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users