Advertisement
Speedy815

Advanced News Board For ComputerCraft

Jul 25th, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. function scrollText(tStrings, nRate)
  2. nRate = nRate or 5
  3. if nRate < 0 then
  4. error("rate must be positive")
  5. end
  6. local nSleep = 1 / nRate
  7.  
  8. width, height = mon.getSize()
  9. x, y = mon.getCursorPos()
  10. sText = ""
  11. for n = 1, #tStrings do
  12. sText = sText .. tostring(tStrings[n])
  13. sText = sText .. " | "
  14. end
  15. sString = "| "
  16. if width / string.len(sText) < 1 then
  17. nStringRepeat = 3
  18. else
  19. nStringRepeat = math.ceil(width / string.len(sText) * 3)
  20. end
  21. for n = 1, nStringRepeat do
  22. sString = sString .. sText
  23. end
  24. while true do
  25. for n = 1, string.len(sText) do
  26. sDisplay = string.sub(sString, n, n + width - 1)
  27. mon.clearLine()
  28. mon.setCursorPos(1, y)
  29. mon.write(sDisplay)
  30. sleep(nSleep)
  31. end
  32. end
  33. end
  34.  
  35. Note that the variable "mon" can be set to a monitor using "mon = peripheral.wrap(sSide)", or can be replaced with "term" to write all text to a terminal.
  36.  
  37. I created an example news board script as well:
  38.  
  39. mon = peripheral.wrap("left")
  40. function lineBreak()
  41. x, y = mon.getCursorPos()
  42. if y ~= 1 then
  43. y = y + 1
  44. end
  45. mon.setCursorPos(1, y)
  46. width, height = mon.getSize()
  47. mon.write("+" .. string.rep("-", width - 2) .. "+")
  48. end
  49. function printString(sString)
  50. x, y = mon.getCursorPos()
  51. y = y + 1
  52. mon.setCursorPos(1, y)
  53. mon.write(sString)
  54. end
  55. function printStringCentre(sString)
  56. x, y = mon.getCursorPos()
  57. y = y + 1
  58. mon.setCursorPos(1, y)
  59. width, height = mon.getSize()
  60. nStringCentre = math.floor(string.len(sString) / 2)
  61. nMonitorCentre = math.floor(width / 2)
  62. x = math.floor(nMonitorCentre - nStringCentre)
  63. mon.setCursorPos(x, y)
  64. mon.write(sString)
  65. end
  66. function printStringRight(sString)
  67. width, height = mon.getSize()
  68. x, y = mon.getCursorPos()
  69. y = y + 1
  70. x = math.ceil(width - string.len(sString))
  71. mon.setCursorPos(x, y)
  72. mon.write(sString)
  73. end
  74. function scrollText(tStrings, nRate)
  75. nRate = nRate or 5
  76. if nRate < 0 then
  77. error("rate must be positive")
  78. end
  79. local nSleep = 1 / nRate
  80.  
  81. width, height = mon.getSize()
  82. x, y = mon.getCursorPos()
  83. sText = ""
  84. for n = 1, #tStrings do
  85. sText = sText .. tostring(tStrings[n])
  86. sText = sText .. " | "
  87. end
  88. sString = "| "
  89. if width / string.len(sText) < 1 then
  90. nStringRepeat = 3
  91. else
  92. nStringRepeat = math.ceil(width / string.len(sText) * 3)
  93. end
  94. for n = 1, nStringRepeat do
  95. sString = sString .. sText
  96. end
  97. while true do
  98. for n = 1, string.len(sText) do
  99. sDisplay = string.sub(sString, n, n + width - 1)
  100. mon.clearLine()
  101. mon.setCursorPos(1, y)
  102. mon.write(sDisplay)
  103. sleep(nSleep)
  104. end
  105. end
  106. end
  107. mon.clear()
  108. mon.setCursorPos(1, 1)
  109. lineBreak()
  110. printStringCentre("|News Board|")
  111. lineBreak()
  112. printString("")
  113. lineBreak()
  114. tScrollText = {}
  115. tScrollText[1] = "BREAKING NEWS"
  116. tScrollText[2] = "Giant cookies falling from sky"
  117. x, y = mon.getCursorPos()
  118. y = y - 1
  119. mon.setCursorPos(1, y)
  120. scrollText(tScrollText)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement