Advertisement
Brodur

Lua Tut program

Jul 28th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- BaseLine Variables
  2. CurrentChapter = 1
  3. CurrentSection = 1
  4. ChapterTitles = {
  5.  "How to Create a Program",
  6.  "How to Display and clear Text",
  7.  "How to Use and Display Variables",
  8.  "How to get User Input",
  9.  "How to use IF statements",
  10.  "How to use loops",
  11.  "How to use redstone",
  12.  "How to use redpower bundles",
  13.  "How to use events",
  14.  "How to use functions",
  15.  "Extra Tips and Extra Functions"
  16. }
  17. Chapter = {
  18.                  [1] = {
  19.                  "Key Points in this Chapter:\n1. Learning how to create a program.\n2. Learning how to save that program.\n3. Learning how to run that program.",
  20.                  "1.1 - Learning how to create a program.\n\nOk so first things first right? We gotta learn how to create our first program. Creating a program is very simple in CC.\n\nedit programname\n\nedit means we want to create or edit program, and programname is the name of the program we wish to edit or create.",
  21.                  "1.2 - Learning how to save that program.\n\nSo now your inside the editing feature of CC and you can move around almost like a notepad. We want to press the [Control] key which will bring up a menu at the bottom of your screen. Pressing the [Left] and [Right] arrow keys will change your selection in this menu. [SAVE] will save the program. [QUIT] will quit editing the program. By pressing [ENTER] we can choose our selection.",
  22.                  "1.3 - Learning how to run that program.\n\nWe've created our little program, but how do we run it? Well thats simple. We type the program name into our terminal and press [ENTER], but remember all things in LUA are case-sensitive. Your program named \"Hello\" is not the same as your program named \"hello\".",
  23.                  "1.4 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  24.                  "SIM"               
  25.                  },
  26.                  [2] = {
  27.                  "Key Points in this Chapter:\n1. How to use print\n2. How to use write\n3. How to clear the screen.\n4. How to use the cursor position.\n5. How to clear a specific line.",
  28.                  "2.1 - How to use print.\n\nTo show text to the user is a simple task. We do so by the following.\n\nprint (\"Hello User\")\n\nprint means to display the text to the user, and Hello User is the text we wish to display.",
  29.                  "2.2 - How to use write.\n\nWhen you print text, the program automatically adds a linebreak after your print command, sometimes you may not wish to do this.\n\nwrite (\"Hello\")\nprint (\"User\")\n\nThese two lines would make the exact same appearance as the print line we made previously. The reason is because the write command does not generate a linebreak and we can use this to keep our current position and continue writing on the same line.",
  30.                  "2.3 - How to clear the screen.\nQuite often you'll want to clear the screen automatically in your program.\n\nterm.clear()\n\nUsing this line we can clear the screen for our user to remove anything we don't want cluttering up the screen.",
  31.                  "2.4 - How to use the cursor position.\nThe cursor position is a very powerful thing. For example, when you clear the screen, the cursor still stays on it's previous line. Meaning that after you clear the screen, your next print statement very well may appear at the bottom of the screen.",
  32.                  "2.4 - How to use the cursor position.\nTo remedy this problem we've been given the command.\n\nterm.setCursorPos(1,1)\n\nThe first 1 in our statment is the horizontal position and the second 1 on our statement is the vertical posistion.",
  33.                  "2.4 - How to use the cursor position.\nBy using the term.setCursorPos(1,1) directly after a term.clear(), we can make sure that the next text we show the user appears at the top of his screen.\n\nRemember, lua is case sensitive. term.setcursorpos(1,1) is not right.",
  34.                  "2.5 - How to clear a specific line.\nBy using the term.setCursorPos we can create some pretty nifty tricks, like reprinting over lines already on the screen, or even clearing a certain line and printing something new.",
  35.                  "2.5 - How to clear a specific line.\nterm.setCursorPos(1,1)\nprint (\"You won't see this\")\nterm.setCursorPos(1,1)\nterm.clearLine()\nprint (\"Hello User\")\n\nWe used the term.clearLine() to remove the line at 1,1 and then we printed a new line where the old line used to be.",
  36.                  "2.6 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  37.                  "SIM"
  38.                  },
  39.                  [3] = {
  40.                  "Key Points in this Chapter:\n1. What is a variable\n2. How to use a variable\n3. How to display a variable\n4. How to convert a variable",
  41.                  "3.1 - What is a variable.\n\nThink of a variable as a container in which you can place text or a number. Using variables allows you to store information and modify it on the fly. Using variables correctly is the key to making a sucessful program.",
  42.                  "3.2 - How to use a variable.\n\nThe first thing we should almost always do when we want to use a variable in a program is to define the variable. We do this by giving the variable a default piece of data. If we don't define a variable then the variable is considered NIL until it has been set.",
  43.                  "3.2 - How to use a variable.\n\nA NIL variable can cause lots of problems in your program, if you try to add together 2 variables and one is NIL, you'll get an error, if you try printing a NIL variable you'll get an error.",
  44.                  "3.2 - How to use a variable.\n\nWe'll be using the variable x from this point onward. Remember that x is different then X in lua.\n\nx = 0\n\nThis defined x to have a default value of 0. You could also do x = \"hello\" if you wanted it to contain text.",
  45.                  "3.2 - How to use a variable.\n\nA variable will hold it's data until you change it. Therefor x = 0 means that x will be 0 until you tell your program that x is something different.",
  46.                  "3.2 - How to use a variable.\n\nYou can also set variables as booleans, booleans are true and false.\nx = true\nThis would set x to true, which is different then \"true\". Using booleans gives you 1 more way to define a variable to be used later in your program.",
  47.                  "3.3 - How to display a variable.\n\nBeing able to show a variable to the user is very important. Who wants to just save the user's name in a variable, but not show it to them during the program? When displaying variables we can use the print or write commands.",
  48.                  "3.3 - How to display a variable.\nx = 0\nprint (x)\nx = \"Bob\"\nprint (\"Hello\"..x)\n\nYou'll notice in the last line that we used our x variable along with another piece of text. To do so we used .. which tells the program to add the x variable directly after the word Hello. In this syntax that would show the user   HelloBob  since we didn't add a space ;)",
  49.                  "3.3 - How to display a variable.\n\nRemember variables are case sensitive, and that variable1 is different then Variable1.\nIf you wanted to place a variable inbetween text you could do.\n\nprint (\"Hello \"..x..\" how are you?\")\n\nThis would print   Hello Bob how are you?",
  50.                  "3.4 - How to convert a variable.\n\nSometimes a variable might need to be converted, this is mainly the case when you want to use the variable as a number, but it's currently a string. For example:\nx = \"0\"\nx = x + 1\n\nThis will not work, as x equals \"0\"",
  51.                  "3.4 - How to convert a variable.\n\nThe difference between 0 and \"0\" is that one is a number and the other is a string. You can't use the basic math functions on strings. Lua can convert a number to a string automatically but it cannot convert a string to a number automatically.",
  52.                  "3.4 - How to convert a variable.\n\nx = 0\ny = \"1\"\nWe can't add those together atm, so we need to convert our string to a number so that we can add it to x.\ny = tonumber(y)\nThis converts y to a number (if it can't be converted to a number then y will be NIL)",
  53.                  "3.4 - How to convert a variable.\n\nNow that we've converted y to a number we can do.\nx = x + y\nThis would make x equal to x(0) + y(1). This means that x is now equal to 1 and y hasn't changed so it's still 1 as well.",
  54.                  "3.4 - How to convert a variable.\n\nIf we want to add a string to another string, we don't use the math symbols, we simply use ..\nx = \"Hello\"\ny = \"Bob\"\nx = x..y\nThis would make x = \"HelloBob\"",
  55.                  "3.4 - How to convert a variable.\n\nRemember that Lua can convert a number to a string, but not the other way around. If you always want to be 100% positive what your variables are, use the functions tonumber(x) and tostring(x).",
  56.                  "3.5 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  57.                  "SIM"               
  58.                  },
  59.                  [4] = {
  60.                  "Key Points in this Chapter:\n1. How to get user input",
  61.                  "4.1 - How to get user input.\n\nWhat's the point of having all these cool variables if your user can't make a variable be something they want? We fix this by allowing the user to input a variable into the program.",
  62.                  "4.1 - How to get user input.\n\nx = io.read()\nThe io.read() tells the program to stop and wait for user input, when the user presses enter that information is then stored in the variable x and the program continues.\nUser input is always stored as a string, therefor if the user types 1 and presses enter, x will be come \"1\", not 1",
  63.                  "4.1 - How to get user input.\n\nOnce the user's input is entered into the x variable you can use that variable like you would any other variable. This means if you then wanted to show the user their input you could follow your io.read() line with print(x)",
  64.                  "4.1 - How to get user input.\n\nBy using the write command before an io.read() we can show text then have the user type after that text.\nwrite(\"Enter Your Name -\"\nname = io.read()\nThis would have the user type their name directly after the - in the write statement.",
  65.                  "4.2 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  66.                  "SIM"               
  67.                  },
  68.                  [5] = {
  69.                  "Key Points in this Chapter:\n1. What is an IF statement\n2. The ELSE statement\n3. The ELSEIF statement\n4. Complex IF's",
  70.                  "5.1 - What is an IF statement.\n\nWe use IF statements to control the programs direction based on certain criteria that you define. Doing this allows us to do only certain things based on certain conditions.",
  71.                  "5.1 - What is an IF statement.\n\nif name == \"Bob\" then\nprint (\"Hello Again Bob\")\nend\n\nThe 1st line says, if the variable name is equal to \"Bob\" then enter this IF statement. The next line is the code that will run if name does equal Bob. The 3rd line says to end the IF statement, if the name was not Bob the program would skip to the line directly after end.",
  72.                  "5.1 - What is an IF statement.\n\nWe have many options in the IF statement, we could do:\nif x >= 1\nRemember we can't do that IF statement if x is a string x = \"1\".\nif name ~= \"Bob\"\nThe ~= option means is not equal too. This if statement would pass if the name was NOT Bob.",
  73.                  "5.2 - The ELSE statement.\n\nSometimes we want to do 1 thing if our IF statement is true and something else if it's false.\nif name == \"Bob\" then\nprint (\"Hello Bob\")\nelse\nprint(\"Your not Bob\")\nend\n\n",
  74.                  "5.2 - The ELSE statement.\n\nNotice how their is only 1 end statement as the last line of the entire IF statement. The ELSE line is a part of the current IF statement.",
  75.                  "5.3 - The ELSEIF statement.\n\nSometimes we want to check for multiple outcomes inside an IF statement, we can achieve this using the ELSEIF statement. The key things to remember is that you still only need 1 end as the last line, because this is 1 full statement, and that you will need to include a then for an ELSEIF.",
  76.                  "5.3 - The ELSEIF statement.\n\nif name == \"Bob\" then\nprint (\"Hello Bob\")\nelseif name == \"John\" then\nprint (\"Hello John\")\nelse\nprint (\"Hello Other\")\nend",
  77.                  "5.3 - The ELSEIF statement.\n\nI still included the final else, which tells the program if the name wasn't Bob or John, then do something else. Notice again that there was only a single end as the last line, because this was 1 full statement.",
  78.                  "5.4 - Complex IF's.\n\nIf's can become very complex depending on your situation. I will show some examples of more complex IF's:\nif name == \"Bob\" and name ~= \"John\" then\nWe are checking the variable twice in 1 if statement by using the AND statement. We could also use the OR statement",
  79.                  "5.4 - Complex IF's.\n\nYou can also place IF statements inside other IF statements, just make sure you place an END statement at the correct place for each IF statement. Next page is an example of a pretty complex IF statement.",
  80.                  "5.4 - Complex IF's.\nif name == \"Bob\" then\n     if x == 1 then\n     print(x)\n     else\n     print(\"Not 1\")\n     end\nprint (\"Hi Bob\")\nelse\nprint (\"Your not Bob\")\nend",
  81.                  "5.4 - Complex IF's.\nWith precise placement of IF statements you can control the flow of your program to a great degree, this allows you to make sure the user is only seeing what you want them to see at all times.",
  82.                  "5.5 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  83.                  "SIM"                               
  84.                  },
  85.                  [6] = {
  86.                  "Key Points in this Chapter:\n1.What is a loop\n2.How to exit a loop\n3.Different kinds of loops",
  87.                  "6.1 - What is a loop\n\nA loop is a section of code that will continually run until told otherwise. We use these to repeat the same code until we say otherwise.\n\nwhile true do\nend\n\nThis is a while loop that has no exit, it will continually run over and over again until the program crashes.",
  88.                  "6.2 - How to exit a loop\nSince we don't want our loops to run until they crash we have to give them a way to stop. We do this by using the BREAK command.The break command is mostly placed inside an IF statement as just placing it in the loop itself would break the loop right away.",
  89.                  "6.2 - How to exit a loop\n\nx = 0\nwhile true do\n     x = x + 1\n     if x == 10 then\n     break\n     end\nend\n\nNotice how are while statements have their own end? This would run the loop and continually add 1 to x until x == 10 then it will break out of the loop.",
  90.                  "6.3 - Different kinds of loops\n\nYou don't always need to use the BREAK command. You can also include a condition in the WHILE statement for how long to run the loop.\n\nx = 0\nwhile x < 10 do\nx = x + 1\nend\n\nWe could also end this early with a BREAK as well.",
  91.                  "6.3 - Different kinds of loops\n\nHeck we don't even have to use the WHILE statement to create a loop.\n\nfor x = 0, 10, 1 do\nprint (x)\nend\n\nThe first line says - x starts at 0, and continues until 10, increasing by 1 every time we come back to this line.",
  92.                  "6.3 - Different kinds of loops\n\nSo using the for statement we could do.\n\nx = 5\nfor x, 50, 5 do\nprint (x)\nend\n\nThis would printout 5 then 10 then 15 ect ect until reaching 50.",
  93.                  "6.4 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  94.                  "SIM"                               
  95.                  },
  96.                  [7] = {
  97.                  "Key Points in this Chapter:\n1. Turning on and off redstone\n2. Checking and Setting Redstone",
  98.                  "7.1 - Turning on and off redstone\n\nOne of the greatest features of CC is that your computer can not only receive redstone signals, but it can also send them as well. We have 6 directions to choose from and they are:\ntop, bottom, front, back, left, right",
  99.                  "7.1 - Turning on and off redstone\n\nWe can control redstone with our computer using 2 basic commands, redstone.getInput(side) and redstone.setOutput(side, boolean).\nWe have to remember to place our sides in quotes though IE \"left\"",
  100.                  "7.2 - Checking and Setting Redstone\n\nredstone.setOutput(\"back\", true)\nThis tells the computer to turn on the redstone output on the back of the computer. We can replace true with false to turn off the redstone output to the back.",
  101.                  "7.2 - Checking and Setting Redstone\n\nif redstone.getInput(\"back\") == true then\nprint \"Redstone is on\"\nend\n\nThis would enter the IF statement if their was power running to the back of the computer.",
  102.                  "7.2 - Checking and Setting Redstone\n\nBy checking and setting different redstone sources while using IF statements we can control multiple things connected to our computer based on the redstone connections.",
  103.                  "7.3 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  104.                  "SIM"                                               
  105.                  },
  106.                  [8] = {
  107.                  "Key Points in this Chapter:\n1. How to turn on a single color\n2. How to turn off a single color\n3. Using multiple colors\n4. Testing Inputs\n5. Turning off all colors.",
  108.                  "8.1 - How to turn on a single color\n\nrs.setBundledOutput(\"back\", colors.white)\n\nThis would turn on the white output in the back.",
  109.                  "8.2 - How to turn off a single color\n\nrs.setBundledOutput(\"back\", rs.getBundledOutput(\"back\") - colors.white)\n\n This would turn off only the color white in the back.",
  110.                  "8.3 - Using Multiple colors\nUsing multiple colors is much easier when you use the colors.combine colors.subtract functions.",
  111.                  "8.3 - Using Multiple colors\n\nout = colors.combine(colors.blue, colors.white)\nrs.setBundledOutput(\"back\", out)\n\nThis would turn on blue and white at the back\nout = colors.subtract(out, colors.blue)\nrs.setBundledOutput(\"back\", out)\nThis would turn off blue, but leave white on.",
  112.                  "8.4 - Testing Inputs\n\nin = rs.getBundledInput(\"back\")\nif colors.test(in, colors.white) == true then\n\nThis would get the current input and store it in the in variable. We then use colors.test on the in variable to see if white is on.",
  113.                  "8.5 - Turning off all colors\n\nrs.setBundledOutput(\"back\", 0)\n\nSetting the output to 0 is the quickest and most efficient way to turn off all colors at the same time.",
  114.                  "8.6 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  115.                  "SIM"                                                               
  116.                  },
  117.                  [9] = {
  118.                  "Key Points in this Chapter:\n1. What is an event?\n2. How do we check for events\n3. What types of events are there?\n4. Using event loops\n5. WHATS GOING ON!",
  119.                  "9.1 - What is an event?\n\nAn event can be many things, from redstone to timers, as well as smashing your face on the keyboard. These can all trigger events within a program, and by correctly using the os.pullEvent() command we can make sure that no matter what happens, we'll know about it!",
  120.                  "9.2 - How do we check for events\n\nevent, param1, param2 = os.pullEvent()\n\nPlacing this line in your code will stop the program until ANY event triggers. So just by pressing a key, you will pass this statement because a keypress is an event",
  121.                  "9.2 - How do we check for events\n\nThe easiest way to check for an event happening is the IF statement.\n\nif event == \"char\" and param1 == \"q\" then\n This line would trigger if you pressed q on your keyboard.",
  122.                  "9.3 - What types of events are there\n\nchar - triggers on keypress\nkey - triggers on keypress\ntimer - triggers on a timer running out\nalarm - triggers on an alarm going off\nredstone - triggers on any change to redstone",
  123.                  "9.3 - What types of events are there\n\ndisk - triggers on disk insertion\ndisk_eject - triggers on disk ejection",
  124.                  "9.4 - Using event loops\n\nUsing the pullEvent() function inside a loop is a great way to determine when to break the loop. For instance:\n\nwhile true do\nevent, param1, param2 = os.pullEvent()\n     if event == \"char\" and param1 == \"q\" then\n     break\n     end\nend",
  125.                  "9.5 - WHATS GOING ON!\nThis is a cool test program to make, so that you can see exactly whats happening in events.\n\nwhile true do\nevent, param1, param2, param3 = os.pullEvent()\nprint (\"Event = \"..event)\nprint (\"P1 = \"..param1)\nprint (\"P2 = \"..param2)\nprint (\"P3 = \"..param3)\nend\n\nDon't Forget to Hold control+t to exit the loop.",
  126.                  "9.6 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  127.                  "SIM"                                                                               
  128.                  },
  129.                  [10] = {
  130.                  "Key Points in this Chapter:\n1. What is a function?\n2. How to use a basic function\n3. How to get a return value from a function",
  131.                  "10.1 - What is a function\n\nThink of a function as a part of your code that can be ran from anywhere inside your code. Once your function is done running, it will take you back to where your code left off.",
  132.                  "10.2 - How to use a basic function\n\nfunction hello()\nprint (\"Hello\")\nend\n\n Here we created a function named hello, and inside that function we placed a print statement that says hello.",
  133.                  "10.2 - How to use a basic function\n\nNow that we have our function created, anytime and anywhere in our program, we can place\nhello()\nThis will jump to the function, run the functions code, then come back to where you called the function.",
  134.                  "10.3 - How to get a return value from a function\n\nMost of the time we want our functions to do repetitious tasks for us though, to do this we can create a more advanced function that will return a value based on what happens in the function.",
  135.                  "10.3 - How to get a return value from a function\n\nfunction add(num1, num2)\nresult = tonumber(num1) + tonumber(num2)\nreturn result\nend\n\nThis is our adding function, it takes 2 numbers that we supply and returns the result.",
  136.                  "10.3 - How to get a return value from a function\n\nTo call our adding function we use.\nx = add(5,5)\nThis makes the add function use the numbers 5 and 5, which it will then add together and return to us. We save that return data in x",
  137.                  "10.3 - How to get a return value from a function\n\nBy combining what we already know, we must assume that we can use variables instead of numbers in our function. Therefor making:\nx = add(x, y)\nA perfectly good way to add 2 variables together.",
  138.                  "10.4 - Practice What We've Learned!\n\nYou'll see a new option at the bottom of your screen now. You can press [SPACE] to continue onward to the next chapter, you can also press [ENTER] to run this chapters simulation.\nDon't worry, you won't hurt my feelings by not practicing.",
  139.                  "SIM"                                                                                               
  140.                  },
  141.                  [11] = {
  142.                  "This is not a Chapter, this is just random blurbs of extra information about other features and functions.",
  143.                  "Blurb 0 (The Most Important) - Use NOTEPAD++ to edit your code, the in-game edit kinda sucks\n\nFind your code in saves/world/computer/#",
  144.                  "Blurb 1 - sleep(1) will pause your code for 1 second",
  145.                  "Blurb 2 - timername = os.startTimer(1) will cause a timer to go off in 1 second, use events to check for the name",
  146.                  "Blurb 3 - Making your code readable helps everyone\nwhile true do\nif x == 5 then\nend\nend\nCOULD BE\nwhile true do\n     if x == 5 then\n     end\nend",
  147.                  "Blurb 4 - Atleast 75% of the time, an error tells you exactly whats wrong with your program.\nSomething with NIL means your trying to use a NIL variable.",
  148.                  "Blurb 5 - Google Is your friend, just try \"lua strings tutorial\"",
  149.                  "Blurb 6 - Theres DOZENS of functions I didn't go over, you should read about them on the interwebs\nstring.len()\nstring.sub()\nstring.find()\nio.open()\nio.close()",
  150.                  "Blurb 7 - No one will help you if you don't work on the code yourself and provide them with it.",
  151.                  "Blurb 8 - When your ready for more advanced coding, start looking into the default programs as well as other user created content.",
  152.                  "Blurb 9 - Using matrice's is a good test to see if your grasping lua",
  153.                  "Blurb 10 - You don't have to use functions if you trully don't understand them, they can make your life easier, but they can also make your life much harder.",
  154.                  "Blurb 11 - Find help on IRC, but prepare to have code ready to be shown. http://webchat.esper.net/?channels=#computercraft",
  155.                  "Blurb 12 - You can do almost anything your imagination can think up.....except magic",
  156.                  "END"
  157.                  }
  158. }
  159.  
  160. function mainmenu()
  161. while true do
  162. term.clear()
  163. term.setCursorPos(1,1)
  164. print "--------------------------------------"
  165. print "| ComputerCraft Interactive Tutorial |"
  166. print "|            By: Casper7526          |"
  167. print "--------------------------------------"
  168. print "|                                    |"
  169. print "| 1. Start                           |"
  170. print "| 2. Choose Chapter                  |"
  171. print "| 3. Exit                            |"
  172. print "|                                    |"
  173. print "--------------------------------------"
  174. event, param1, param2, param3 = os.pullEvent()
  175. if event == "char" and param1 == "3" then break end
  176. if event == "char" and param1 == "1" then chapter = 1 LoadChapter(chapter) end
  177. if event == "char" and param1 == "2" then ChooseChapter() end
  178. end
  179. end
  180.  
  181. function ChooseChapter()
  182. while true do
  183. term.clear()
  184. term.setCursorPos(1,1)
  185. print "--------------- Chapter Index ---------------"
  186. print "---------------------------------------------"
  187. print ""
  188. local i = 1
  189.     while true do
  190.     if ChapterTitles[i] == nil then break end
  191.     print (i..". "..ChapterTitles[i])
  192.     i = i + 1
  193.     end
  194. print ""
  195. print "q. Quit"
  196. print "---------------------------------------------"
  197. write "Choice - "
  198. choice = io.read()
  199. if string.lower(choice) == "q" then break end
  200. if ChapterTitles[tonumber(choice)] == nil then print "Thats not a valid chapter." sleep(1) else
  201. LoadChapter(tonumber(choice)) break end
  202. end
  203. end
  204.  
  205. function LoadChapter(chapter)
  206. while true do
  207. term.clear()
  208. term.setCursorPos(1,1)
  209. print ("Chapter "..chapter.." - "..ChapterTitles[chapter])
  210. print ("---------------------------------------------")
  211. print (Chapter[chapter][CurrentSection])
  212. print ""
  213. if Chapter[chapter][CurrentSection + 1] == "END" then print "THATS ALL FOLKS!" else
  214. print "Press [Space] To Continue"
  215. end
  216. print "[q] - Main Menu [b] - Previous Page."
  217. if Chapter[chapter][CurrentSection + 1] == "SIM" then print "Press [Enter] To Run Simulation" end
  218. event, param1, param2, param3 = os.pullEvent()
  219.     if event == "key" and param1 == 28 and Chapter[chapter][CurrentSection + 1] == "SIM" then Sim(chapter) EndSim(chapter) chapter = chapter + 1 CurrentSection = 1 end
  220.     if event == "char" and param1 == "q" then CurrentSection = 1 break end
  221.     if event == "char" and param1 == "b" then
  222.     CurrentSection = CurrentSection - 1
  223.     if CurrentSection == 0 then CurrentSection = 1 end
  224.     end
  225.     if event == "char" and param1 == " " and Chapter[chapter][CurrentSection + 1] ~= "END" then
  226.     if Chapter[chapter][CurrentSection + 1] == "SIM" then chapter = chapter + 1 CurrentSection = 1 else CurrentSection = CurrentSection + 1 end
  227.     end
  228. end
  229. end
  230.  
  231. function EndSim(chapter)
  232. while true do
  233. term.clear()
  234. term.setCursorPos(1,1)
  235. print "Great work back there!"
  236. print ""
  237. print "Press [ENTER] to move on to the next chapter"
  238. event, param1, param2 = os.pullEvent()
  239. if event == "key" and param1 == 28 then shell.run("rm", "tmptut") break end
  240. end
  241. end
  242.  
  243. function pressany()
  244. term.setCursorPos(1,17)
  245. print "Press Any Key To Continue"
  246. event = os.pullEvent()
  247. end
  248.  
  249. function Sim(chapter)
  250. stage = 1
  251. while true do
  252. term.clear()
  253. term.setCursorPos(1,1)
  254.     if chapter == 1 then
  255.     print "Your Goals:"
  256.     print ""
  257.     print "* Create a program named hello."
  258.     print "* Type anything you wish inside that program."
  259.     print "* Save and Exit the program."
  260.     print "* Run the program."
  261.     print ""
  262.     print "quit   will exit the sim early."
  263.     write (">") input = io.read()
  264.     if input == "quit" then break end
  265.         --------------------------------
  266.         if stage == 1 then
  267.             if input == "edit hello" then
  268.             shell.run("edit", "tmptut")
  269.             print "Great Job, now let's run our program!"
  270.             sleep(2)
  271.             stage = 2
  272.             else
  273.             print "Remember, lua is case sensitive."
  274.             print "Try"
  275.             print "edit hello"
  276.             sleep(2)           
  277.             end
  278.         elseif stage == 2 then
  279.             if input == "hello" then break
  280.             else
  281.             print "Remember, lua is case sensitive."
  282.             print "Try"
  283.             print "hello"
  284.             sleep(2)           
  285.             end
  286.         end
  287.     end
  288.  
  289.     if chapter == 2 then
  290.     print "Your Goals:"
  291.     print ""
  292.     print "* Create a program named hello."
  293.     print "* Clear the Screen"
  294.     print "* Set the Cursor Pos to 1,1"
  295.     print "* Print \"Hello Loser\" on line 1 of the screen."
  296.     print "* Print \"Welcome\" on line 2 of the screen."
  297.     print "* Clear the 1st line."
  298.     print "* Print \"Hello User\" on line 1 of the screen."
  299.     print "* Run your program!"
  300.     print ""
  301.     print "You can type \"example\" at anytime to see the correct syntax."
  302.     print "quit   will exit the sim early."
  303.     print ""
  304.     write (">") input = io.read()
  305.         if input == "quit" then break end
  306.         if input == "edit hello" then shell.run("edit", "tmptut") end
  307.         if input == "hello" then shell.run("tmptut") pressany()
  308.         term.clear()
  309.         term.setCursorPos(1,1)
  310.         print "Did you program work as you expected?"
  311.         print ""
  312.         print "Press [ENTER] to end the simulation."
  313.         print "Press Any Other Key to go back and work on your program."
  314.         event, param1, param2 = os.pullEvent()
  315.         if event == "key" and param1 == 28 then break end
  316.         end
  317.         if string.lower(input) == "example" then
  318.         term.clear()
  319.         term.setCursorPos(1,1)
  320.         print ("term.clear()")
  321.         print ("term.setCursorPos(1,1)")
  322.         print ("print (\"Hello Loser\"")
  323.         print ("print (\"Welcome\"")
  324.         print ("term.setCursorPos(1,1)")
  325.         print ("term.clearLine()")
  326.         print ("print (\"Hello User\")")
  327.         pressany()
  328.         end
  329.     end
  330.  
  331.     if chapter == 3 then
  332.     print "Your Goals:"
  333.     print ""
  334.     print "--Use the program hello--"
  335.     print "* Create the following variables."
  336.     print "  x = 1"
  337.     print "  y = \"2\""
  338.     print "  z = 0"
  339.     print "  text = \"Output \""
  340.     print "* Add x and y together and store that value in z, then print text and z to the user on the same line."
  341.     print "* Run your program!"
  342.     print ""
  343.     print "You can type \"example\" at anytime to see the correct syntax."
  344.     print "quit   will exit the sim early."
  345.     print ""
  346.     write (">") input = io.read()
  347.         if input == "quit" then break end
  348.         if input == "edit hello" then shell.run("edit", "tmptut") end
  349.         if input == "hello" then shell.run("tmptut") pressany()
  350.         term.clear()
  351.         term.setCursorPos(1,1)
  352.         print "Did you program work as you expected?"
  353.         print ""
  354.         print "Press [ENTER] to end the simulation."
  355.         print "Press Any Other Key to go back and work on your program."
  356.         event, param1, param2 = os.pullEvent()
  357.         if event == "key" and param1 == 28 then break end
  358.         end
  359.         if string.lower(input) == "example" then
  360.         term.clear()
  361.         term.setCursorPos(1,1)
  362.         print ("term.clear()")
  363.         print ("term.setCursorPos(1,1)")
  364.         print ("x = 1")
  365.         print ("y = \"2\"")
  366.         print ("z = 0")
  367.         print ("text = \"Output \"")
  368.         print ("y = tonumber(y)")
  369.         print ("z = x + y")
  370.         print ("print (text..z)")
  371.         pressany()
  372.         end
  373.     end
  374.    
  375.     if chapter == 4 then
  376.     print "Your Goals:"
  377.     print ""
  378.     print "--Use the program hello--"
  379.     print "* Ask the user for their name"
  380.     print "* Show them the line:"
  381.     print "  Hello name how are you today?"
  382.     print "  With name replaced by their input."
  383.     print "* Run your program!"
  384.     print ""
  385.     print "You can type \"example\" at anytime to see the correct syntax."
  386.     print "quit   will exit the sim early."
  387.     print ""
  388.     write (">") input = io.read()
  389.         if input == "quit" then break end
  390.         if input == "edit hello" then shell.run("edit", "tmptut") end
  391.         if input == "hello" then shell.run("tmptut") pressany()
  392.         term.clear()
  393.         term.setCursorPos(1,1)
  394.         print "Did you program work as you expected?"
  395.         print ""
  396.         print "Press [ENTER] to end the simulation."
  397.         print "Press Any Other Key to go back and work on your program."
  398.         event, param1, param2 = os.pullEvent()
  399.         if event == "key" and param1 == 28 then break end
  400.         end
  401.         if string.lower(input) == "example" then
  402.         term.clear()
  403.         term.setCursorPos(1,1)
  404.         print ("term.clear()")
  405.         print ("term.setCursorPos(1,1)")
  406.         print ("write(\"Whats your name? \")")
  407.         print ("name = io.read()")
  408.         print ("print (\"Hello \"..name..\" how are you today?\")")
  409.         pressany()
  410.         end
  411.     end
  412.    
  413.    
  414.     if chapter == 5 then
  415.     print "Your Goals:"
  416.     print ""
  417.     print "--Use the program hello--"
  418.     print "* Ask the user for their name"
  419.     print "* If their name is Bob or John then welcome them."
  420.     print "* If their name isn't Bob or John, then tell them to get lost!"
  421.     print "* Run your program!"
  422.     print ""
  423.     print "You can type \"example\" at anytime to see the correct syntax."
  424.     print "quit   will exit the sim early."
  425.     print ""
  426.     write (">") input = io.read()
  427.         if input == "quit" then break end
  428.         if input == "edit hello" then shell.run("edit", "tmptut") end
  429.         if input == "hello" then shell.run("tmptut") pressany()
  430.         term.clear()
  431.         term.setCursorPos(1,1)
  432.         print "Did you program work as you expected?"
  433.         print ""
  434.         print "Press [ENTER] to end the simulation."
  435.         print "Press Any Other Key to go back and work on your program."
  436.         event, param1, param2 = os.pullEvent()
  437.         if event == "key" and param1 == 28 then break end
  438.         end
  439.         if string.lower(input) == "example" then
  440.         term.clear()
  441.         term.setCursorPos(1,1)
  442.         print ("term.clear()")
  443.         print ("term.setCursorPos(1,1)")
  444.         print ("write(\"Whats your name? \")")
  445.         print ("name = io.read()")
  446.         print ("if name == \"Bob\" or name == \"John\" then ")
  447.         print ("print (\"Welcome \"..name)")
  448.         print ("else")
  449.         print ("print (\"Get lost!\")")
  450.         print ("end")
  451.         pressany()
  452.         end
  453.     end
  454.    
  455.    
  456.     if chapter == 6 then
  457.     print "Your Goals:"
  458.     print ""
  459.     print "--Use the program hello--"
  460.     print "* Create a loop that continually asks the user for their name."
  461.     print "* Only exit that loop if they enter Bob as their name."
  462.     print "* Try using the BREAK statement as well as without."
  463.     print "* Run your program!"
  464.     print ""
  465.     print "You can type \"example\" at anytime to see the correct syntax."
  466.     print "quit   will exit the sim early."
  467.     print ""
  468.     write (">") input = io.read()
  469.         if input == "quit" then break end
  470.         if input == "edit hello" then shell.run("edit", "tmptut") end
  471.         if input == "hello" then shell.run("tmptut") pressany()
  472.         term.clear()
  473.         term.setCursorPos(1,1)
  474.         print "Did you program work as you expected?"
  475.         print ""
  476.         print "Press [ENTER] to end the simulation."
  477.         print "Press Any Other Key to go back and work on your program."
  478.         event, param1, param2 = os.pullEvent()
  479.         if event == "key" and param1 == 28 then break end
  480.         end
  481.         if string.lower(input) == "example" then
  482.         term.clear()
  483.         term.setCursorPos(1,1)
  484.         print ("term.clear()")
  485.         print ("term.setCursorPos(1,1)")
  486.         print ""
  487.         print ("while name ~= \"Bob\" do")
  488.         print ("write(\"Whats your name? \")")
  489.         print ("name = io.read()")
  490.         print ("end")
  491.         print ""
  492.         print ("while true do")
  493.         print ("write(\"Whats your name? \")")
  494.         print ("name = io.read()")
  495.         print ("    if name == \"Bob\" then")
  496.         print ("    break")
  497.         print ("    end")
  498.         print ("end")
  499.         pressany()
  500.         end
  501.     end
  502.    
  503.    
  504.     if chapter == 7 then
  505.     print "Your Goals:"
  506.     print ""
  507.     print "--Use the program hello--"
  508.     print "* Check to see if there is redstone current coming into the back of your computer"
  509.     print "* If there is current coming in the back then turn on the current to the front"
  510.     print "* If there isn't current coming in the back, then turn off the current to the front"
  511.     print "* Tell the user if you turned the current on or off."
  512.     print "* Run your program!"
  513.     print ""
  514.     print "You can type \"example\" at anytime to see the correct syntax."
  515.     print "quit   will exit the sim early."
  516.     print ""
  517.     write (">") input = io.read()
  518.         if input == "quit" then break end
  519.         if input == "edit hello" then shell.run("edit", "tmptut") end
  520.         if input == "hello" then shell.run("tmptut") pressany()
  521.         term.clear()
  522.         term.setCursorPos(1,1)
  523.         print "Did you program work as you expected?"
  524.         print ""
  525.         print "Press [ENTER] to end the simulation."
  526.         print "Press Any Other Key to go back and work on your program."
  527.         event, param1, param2 = os.pullEvent()
  528.         if event == "key" and param1 == 28 then break end
  529.         end
  530.         if string.lower(input) == "example" then
  531.         term.clear()
  532.         term.setCursorPos(1,1)
  533.         print ("term.clear()")
  534.         print ("term.setCursorPos(1,1)")
  535.         print ("if redstone.getInput(\"back\") == true then")
  536.         print ("redstone.setOutput(\"front\", true)")
  537.         print ("print (\"Front is now on.\")")
  538.         print ("else")
  539.         print ("redstone.setOutput(\"front\", false)")
  540.         print ("print (\"Front is now off.\")")
  541.         print ("end")
  542.         pressany()
  543.         end
  544.     end
  545.    
  546.     if chapter == 8 then
  547.     print "Your Goals:"
  548.     print ""
  549.     print "--Use the program hello--"
  550.     print "--Use the back output of the computer--"
  551.     print "* Turn on white"
  552.     print "* Turn on blue"
  553.     print "* Turn on purple"
  554.     print "* Turn off blue"
  555.     print "* Turn off all colors"
  556.     print "* Check to see if white is coming in the front"
  557.     print "* Run your program!"
  558.     print ""
  559.     print "You can type \"example\" at anytime to see the correct syntax."
  560.     print "quit   will exit the sim early."
  561.     print ""
  562.     write (">") input = io.read()
  563.         if input == "quit" then break end
  564.         if input == "edit hello" then shell.run("edit", "tmptut") end
  565.         if input == "hello" then shell.run("tmptut") pressany()
  566.         term.clear()
  567.         term.setCursorPos(1,1)
  568.         print "Did you program work as you expected?"
  569.         print ""
  570.         print "Press [ENTER] to end the simulation."
  571.         print "Press Any Other Key to go back and work on your program."
  572.         event, param1, param2 = os.pullEvent()
  573.         if event == "key" and param1 == 28 then break end
  574.         end
  575.         if string.lower(input) == "example" then
  576.         term.clear()
  577.         term.setCursorPos(1,1)
  578.         print ("term.clear()")
  579.         print ("term.setCursorPos(1,1)")
  580.         print ("out = colors.combine(colors.white, colors.blue, colors.purple)")
  581.         print ("rs.setBundledOutput(\"back\", out)")
  582.         print ("out = colors.subtract(out, colors.blue)")
  583.         print ("rs.setBundledOutput(\"back\", out)")
  584.         print ("rs.setBundledOutput(\"back\", 0)")
  585.         print ("in = rs.getBundledInput(\"front\")")
  586.         print ("if colors.test(in, colors.white) == true then")
  587.         print ("print (\"White is on in front\")")
  588.         print ("else")
  589.         print ("print (\"White is off in front\")")
  590.         print ("end")
  591.         pressany()
  592.         end
  593.     end
  594.    
  595.     if chapter == 9 then
  596.     print "Your Goals:"
  597.     print ""
  598.     print "--Use the program hello--"
  599.     print "* Create an event loop"
  600.     print "* Print the char that was pressed"
  601.     print "* Stop the loop when the q key is pressed"
  602.     print "* Stop the loop if the redstone event happens"
  603.     print "* Run your program!"
  604.     print ""
  605.     print "You can type \"example\" at anytime to see the correct syntax."
  606.     print "quit   will exit the sim early."
  607.     print ""
  608.     write (">") input = io.read()
  609.         if input == "quit" then break end
  610.         if input == "edit hello" then shell.run("edit", "tmptut") end
  611.         if input == "hello" then shell.run("tmptut") pressany()
  612.         term.clear()
  613.         term.setCursorPos(1,1)
  614.         print "Did you program work as you expected?"
  615.         print ""
  616.         print "Press [ENTER] to end the simulation."
  617.         print "Press Any Other Key to go back and work on your program."
  618.         event, param1, param2 = os.pullEvent()
  619.         if event == "key" and param1 == 28 then break end
  620.         end
  621.         if string.lower(input) == "example" then
  622.         term.clear()
  623.         term.setCursorPos(1,1)
  624.         print ("term.clear()")
  625.         print ("term.setCursorPos(1,1)")
  626.         print ("while true do")
  627.         print ("event, param1, param2 = os.pullEvent()")
  628.         print ("     if event == \"redstone\" then")
  629.         print ("     break")
  630.         print ("     end")
  631.         print ("     if event == \"char\" and param1 == \"q\" then")
  632.         print ("     break")
  633.         print ("     else")
  634.         print ("     print (\"You pressed - \"..param1)")
  635.         print ("     end")
  636.         print ("end")
  637.         pressany()
  638.         end
  639.     end
  640.    
  641.     if chapter == 10 then
  642.     print "Your Goals:"
  643.     print ""
  644.     print "--Use the program hello--"
  645.     print "* Ask the user for their first name."
  646.     print "* Ask the user for their last name."
  647.     print "* Combine the 2 strings using a function"
  648.     print "  return the result into the fullname variable"
  649.     print "* Show the user their full name"
  650.     print "* Run your program!"
  651.     print ""
  652.     print "You can type \"example\" at anytime to see the correct syntax."
  653.     print "quit   will exit the sim early."
  654.     print ""
  655.     write (">") input = io.read()
  656.         if input == "quit" then break end
  657.         if input == "edit hello" then shell.run("edit", "tmptut") end
  658.         if input == "hello" then shell.run("tmptut") pressany()
  659.         term.clear()
  660.         term.setCursorPos(1,1)
  661.         print "Did you program work as you expected?"
  662.         print ""
  663.         print "Press [ENTER] to end the simulation."
  664.         print "Press Any Other Key to go back and work on your program."
  665.         event, param1, param2 = os.pullEvent()
  666.         if event == "key" and param1 == 28 then break end
  667.         end
  668.         if string.lower(input) == "example" then
  669.         term.clear()
  670.         term.setCursorPos(1,1)
  671.         print ("term.clear()")
  672.         print ("term.setCursorPos(1,1)")
  673.         print ("function combine(s1, s2)")
  674.         print ("result = s1..s2")
  675.         print ("return result")
  676.         print ("end")
  677.         print ("write(\"What's your first name? \")")
  678.         print ("firstname = io.read()")
  679.         print ("write(\"What's your last name? \")")
  680.         print ("lastname = io.read()")
  681.         print ("fullname = combine(firstname, lastname)")
  682.         print ("print (\"Hello \"..fullname)")
  683.         pressany()
  684.         end
  685.     end
  686.    
  687.    
  688. end
  689.    
  690. end
  691.  
  692.  
  693. mainmenu()
  694.  
  695. print "You don't need to thank me."
  696. print "Thank yourself for learning!"
  697. print "To learn more search online!"
  698. print "You can also type help <topic>!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement