Advertisement
Rolcam

Computercraft Coding Tutorial V2 [Old]

Jun 18th, 2020 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.06 KB | None | 0 0
  1. print("Tutor V3 - Made by Rolcam")
  2. --https://pastebin.com/u/Rolcam
  3.  
  4. -- Auto detects monitor
  5. tSides = {"left","right","bottom","top","front","back"}
  6.  
  7. for i = 1, #tSides do
  8.   monitor = peripheral.wrap(tSides[i])
  9.   if monitor then
  10.         side = tSides[i]
  11.         break
  12.   end
  13. end
  14.  
  15. term.redirect(peripheral.wrap(side))
  16.  
  17. -- ComputerCraft Programming Tutorial V2 by Rolcam
  18. os.pullEvent = os.pullEventRaw
  19. term.clear()
  20.  
  21. -- Resets Screen
  22. function reset()
  23. term.setTextColor(colors.white)
  24. term.setBackgroundColor(colors.black)
  25. term.clear()
  26. term.setCursorPos(1,1)
  27. end
  28.  
  29. reset()
  30.  
  31. -- Section 1 - Intro
  32. print("Section 1: Program Introduction \n ")
  33. print("This is a crash course on coding in ComputerCraft")
  34. print("Some things to note: the IDE (or programming environment) IS case sensitive. \"term.clear()\" will work but \"tErm.clear()\" won't for example.")
  35. print("\"term.setBackgroundColor\" will work, but \"term.setbackgroundcolor\" won't.")
  36. print(" ")
  37. print("Touch the screen to continue")
  38. os.pullEvent("monitor_touch")
  39. reset()
  40.  
  41. -- Section 2 - Basics
  42. print("Section 2: Basic Commands \n ")
  43. print("term.clear() - Clears the entire screen")
  44. print("This will also fill the screen with the background color selected")
  45. print("sleep(#) - Pauses the program for \"#\" number of seconds")
  46. print("term.setCursorPos(1,2) - Sets the cursor to the position listed. Note the comma separating the numbers. The first number is X position, the second is Y position")
  47. print("break - Used to break out of a loop \n ")
  48. print("Touch the screen to continue")
  49. os.pullEvent("monitor_touch")
  50. reset()
  51.  
  52. -- Section 2.1
  53. print("Section 2.1: Variables \n")
  54. print("a = \"test\" - Declares a variable \"a\" as a string with the text \"test\" ")
  55. print("a = 2 - Declares a variable \"a\" with a value of 2")
  56. print("It is highly recommended you declare variables like so: local a = <value> \n ")
  57. print("Touch the screen to continue")
  58. os.pullEvent("monitor_touch")
  59. reset()
  60.  
  61.  
  62. -- Section 3 - Text
  63. print("Section 3: Text \n ")
  64. print("print(\"Text\") - Displays text on a new line \n ")
  65. print("write(\"Text\") - Writes text on the same line. Useful for multi-colored text or showing variables")
  66. print(" ")
  67. textutils.slowPrint("textutils.slowPrint(\"Text\") ")
  68. term.setCursorPos(29,8)
  69. term.write("- Slowly prints text.")
  70. print("Try to use this sparingly. It can get old rather quickly. \n ")
  71. print("Note: You need to use quotes for the text commands. If you don't, it sees the text as a variable and it will show the value of that variable instead. Be careful and keep that in mind. \n ")
  72. print("Touch the screen to continue")
  73. os.pullEvent("monitor_touch")
  74. reset()
  75.  
  76.  
  77. -- Section 4 - Colors
  78. print("Section 4: Colors \n ")
  79. term.setTextColor(colors.cyan)
  80. print("term.setTextColor(colors.cyan) - changes the color of any new text being printed. It WON'T change the text already on screen")
  81. term.setBackgroundColor(colors.gray)
  82. print("term.setBackgroundColor(colors.gray) - Changes the background for new text being printed")
  83. print("to change the entire background, use \"term.clear()\" after this")
  84. print("You can use any of the 16 vanilla dye colors for the colors utility")
  85. term.setTextColor(colors.white)
  86. term.setBackgroundColor(colors.black)
  87. print(" ")
  88. print("Touch the screen to continue")
  89. os.pullEvent("monitor_touch")
  90. reset()
  91.  
  92.  
  93. -- Section 5 - If/Else Statements
  94. print("Section 5 - If/Else Statements \n ")
  95. print("These are useful in more complex programs. They provide basic logic to a program. \n ")
  96. print("if a == 1 then - If the variable \"a\" equals 1, then the program will print \"Hello\"")
  97. print("print(\"Hello\")")
  98. print("elseif a == 2 then - Otherwise, if \"a\" is equal to 2 then the program will print ")
  99. print("print(\"Goodbye\")")
  100. print("\"else\" - Otherwise the program will print \"Hello World\"")
  101. print("print(\"Hello World\")")
  102. print("end - Designates the end of the if statement \n ")
  103. print("Touch the screen to continue")
  104. os.pullEvent("monitor_touch")
  105. reset()
  106.  
  107.  
  108. -- Section 6 - Loops
  109. print("Section 6 - Loops \n ")
  110. print("These are also useful in more complex programs. They can provide logic to a program.")
  111. print("Remember to always put \"end\" as the last line to close the loop\n ")
  112. print("while a == 1 do - While the variable \"a\" is equal to 1, keep looping /n")
  113. print("for <start>, <end>, <interval> do - this is a more advanced loop.")
  114. print("for a = 1, 10 do - Interval is optional. It sets a local variable to the loop \"a\" as 1 and")
  115. print("stops looping when \"a\" is equal to 10 \n ")
  116. print("Touch the screen to continue")
  117. os.pullEvent("monitor_touch")
  118. reset()
  119.  
  120. -- Section 6 Cont.
  121. print("Section 6 - Loops cont.\n ")
  122. print("repeat - Opposite of a while loop. This will keep repeating until the condition is true")
  123. print("blah blah blah - fake code stuff")
  124. print("until a == 5 - This is used to close this loop instead of \"end\"")
  125. print("If the above condition is true, the loop will end. As this is on the bottom, this loop will run at least once")
  126. print("Touch the screen to continue")
  127. os.pullEvent("monitor_touch")
  128. reset()
  129.  
  130.  
  131. -- Section 7 - Redstone
  132. print("Section 7 - Redstone \n ")
  133. print("rs.setOutput(\"Side\", true/false) - Sets the redstone output for the computer. Sides: top, bottom, left, right, front, back")
  134. print(" ")
  135. print("rs.setBundledOutput(\"Side\",colors.red) - Sets the bundled redstone output for a computer. Only one state can be set per side")
  136. print(" ")
  137. print("To set multiple colors, use colors.combine(colors.red, colors.blue). This will set the output of red and blue to true.")
  138. print(" ")
  139. print("To turn off the bundled output, just use \"0\" instead of colors")
  140. print(" ")
  141. print("Touch the screen to continue")
  142. os.pullEvent("monitor_touch")
  143. reset()
  144.  
  145. -- Section 7 - Redstone Continued
  146. print("Section 7 - Redstone cont. \n ")
  147. print("redstone.getInput(\"back\") - Gets state of redstone input on the selected side as a Boolean (True/False)")
  148. print(" ")
  149. print("redstone.getOutput(\"back\") - Gets state of redstone output to the selected side as a Boolean")
  150. print(" ")
  151. print("redstone.getBundledOutput(\"side\") - Gets the colored cables that are active")
  152. print(" ")
  153. print("redstone.testBundledInput(\"side\", color) - Gets state of bundled cable input on the selected side as a Boolean")
  154. print(" ")
  155. print("Touch the screen to continue")
  156. os.pullEvent("monitor_touch")
  157. reset()
  158.  
  159. -- Section 8 - Example Program
  160. print("Running Example Program...")
  161. print("sleep(1) is used for the pause between commands")
  162. print(" ")
  163. print("Redstone Example Program")
  164. print(" ")
  165. print("Please put a redstone powered device like a door or lamp to the left of the computer")
  166. print("<---------")
  167. print(" ")
  168. print("Touch the screen once the device is placed")
  169. os.pullEvent("monitor_touch")
  170. term.clear()
  171. term.setCursorPos(1,1)
  172. print("rs.setOutput(\"left\", true)")
  173. rs.setOutput("left", true)
  174. sleep(1)
  175. print("Touch the screen to continue")
  176. os.pullEvent("monitor_touch")
  177. print("rs.setOutput(\"left\", false)")
  178. rs.setOutput("left", false)
  179. print("end of redstone demo")
  180. print(" ")
  181. print("Touch the screen to continue")
  182. os.pullEvent("monitor_touch")
  183. reset()
  184. term.restore()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement