Jump to content




Fake Name (testing os.pullevent)

computer

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

#1 Xab

  • Members
  • 9 posts

Posted 20 June 2014 - 05:14 PM

Wrote a fun program to get used to using os.pullevent, and to make a prompt with a bit of personality :)
--[[
fakeName
Prompts user for name, but writes out a different name.
--]]

function prompt(message)
  message = message or ""
  write(tostring(message))
  return read()
end

function promptYesNo(message)
  local value = string.upper(prompt(message))
  if value == "Y" or value == "YES" then return true end
  if value == "N" or value == "NO" then return false end
  return nil
end

function main()
  write("What is your name?: ")
  local fName = "Doofus"		  -- fake name
  local rName = ""				-- real name
  local fnIndex = 0			   -- fake name letter index location
  local fnLen = string.len(fName) -- fake name length
  while true do
	local event, v = os.pullEvent()
	if event == "key" then
	  if v == keys.enter and string.len(rName) > 0 then break end  
	elseif event == "char" then
	  rName = rName..v
	  fnIndex = fnIndex + 1
	  if fnIndex <= fnLen then
		write(string.sub(fName,fnIndex,fnIndex))
	  end
	end
  end
  -- finish writing fake name
  if fnIndex < fnLen then
	write(string.sub(fName,fnIndex+1,fnLen))
	sleep(1)
  end

  if not promptYesNo("\nHaha, just kidding. Is your name "..tostring(rName).."?(y/n): ") then
	rName = prompt("What is your name?: ")
  end
  print("Nice to meet you "..rName..".")
end

main()






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users