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

Terminal Glasses

Started by HVENetworks, 09 May 2018 - 03:08 PM
HVENetworks #1
Posted 09 May 2018 - 05:08 PM
I have made a program which will display different short "animated" messages, currently "Creative Age" for testing.

But it seems like it could be a lot more simplified than to write it for every frame.

It basically adds another letter each 0.2 seconds and keeps it on screen for 3 seconds, then take the letter off one by one every 0.2 seconds.

Here's my code: https://pastebin.com/jAsBzCkg

Spoiler

local p = peripheral.wrap("right")
p.clear()
function pack(...)
	return {...}
end
FORMAT_CHAR = "\194\167"
FORMAT_BOLD = FORMAT_CHAR .. 'l'
local labelPlayers = p.addText(3, 3, "C");
sleep(0.2)
local labelPlayers = p.addText(3, 3, "Cr");
sleep(0.2)
local labelPlayers = p.addText(3, 3, "Cre");
sleep(0.2)
local labelPlayers = p.addText(3, 3, "Crea");
sleep(0.2)
local labelPlayers = p.addText(3, 3, "Creat");
sleep(0.2)
local labelPlayers = p.addText(3, 3, "Creati");
sleep(0.2)
local labelPlayers = p.addText(3, 3, "Creativ");
sleep(0.2)
local labelPlayers = p.addText(3, 3, "Creative");
sleep(0.2)
local labelPlayers = p.addText(3, 3, "Creative A");
sleep(0.2)
local labelPlayers = p.addText(3, 3, "Creative Ag");
sleep(0.2)
local labelPlayers = p.addText(3, 3, "Creative Age");
sleep(3)
p.clear()
local labelPlayers = p.addText(3, 3, "Creative Ag");
sleep(0.2)
p.clear()
local labelPlayers = p.addText(3, 3, "Creative A");
sleep(0.2)
p.clear()
local labelPlayers = p.addText(3, 3, "Creative");
sleep(0.2)
p.clear()
local labelPlayers = p.addText(3, 3, "Creativ");
sleep(0.2)
p.clear()
local labelPlayers = p.addText(3, 3, "Creati");
sleep(0.2)
p.clear()
local labelPlayers = p.addText(3, 3, "Creat");
sleep(0.2)
p.clear()
local labelPlayers = p.addText(3, 3, "Crea");
sleep(0.2)
p.clear()
local labelPlayers = p.addText(3, 3, "Cre");
sleep(0.2)
p.clear()
local labelPlayers = p.addText(3, 3, "Cr");
sleep(0.2)
p.clear()
local labelPlayers = p.addText(3, 3, "C");
sleep(0.2)
p.clear()
sleep(0.2)
os.reboot()

How do I delete or move my post?
Windows10User #2
Posted 09 May 2018 - 05:10 PM
Uhh… maybe write an API that will handle the writing?
SquidDev #3
Posted 09 May 2018 - 05:13 PM
A couple of things you can do:

Use a loop instead of os.reboot() in order to loop forever:

while true do
  -- Your current code
end

Use a loop in order to display characters
If we look at the code, it's pretty much "add one character until it's all filled out, then remove one character". Let's represent this as a loop:

while true do
  local message = "Creative Age"
  for i = 1, #message do -- For the start of the string up to its length
	p.clear()
	p.addText(3, 3, message:sub(1, i)) -- Get the first i characters
	sleep(0.2)
  end
  sleep(3)
  for i = #message - 1, 1, -1 do -- From the end of the string down to the beginning again (note the -1)
	p.clear()
	p.addText(3, 3, message:sub(1, i)) -- Get the first i characters
	sleep(0.2)
  end
end

Reuse the text object
The labelPlayers object has setText method, which means we don't need to clear and recreate it every time:

p.clear()
local labelPlayers = p.addText(3, 3, "")
while true do
  local message = "Creative Age"
  for i = 1, #message do -- For the start of the string up to its length
	labelPlayers.setText(message:sub(1, i)) -- Get the first i characters
	sleep(0.2)
  end
  sleep(3)
  for i = #message - 1, 1, -1 do -- From the end of the string down to the beginning again (note the -1)
	labelPlayers.setText(message:sub(1, i)) -- Get the first i characters
	sleep(0.2)
  end
end
Edited on 09 May 2018 - 03:13 PM
HVENetworks #4
Posted 09 May 2018 - 05:49 PM
The reboot is required. This is just one part of the program.
HVENetworks #5
Posted 09 May 2018 - 06:15 PM
It works :)/>
Thank you very much
EveryOS #6
Posted 09 May 2018 - 06:37 PM
You can just edit your last comment instead of bumping

[acronym='Just press edit next to your signature in the bottom-right corner of your comment']EDIT[/acronym]: Like this. Oh, and *bump*
Edited on 09 May 2018 - 04:38 PM