Getting Around in CC.
We will be learning:
- How to move around programs in CC
- How to make a computer
- How to look at how many programs you have
- How to edit programs
Getting around in computercraft can be quite simple. After making a computer, using this recipe (s = stone, r = redstone, g = glass panes):
s s ss r ss g s
Place it down and open it up. Let’s get started! First, type
> ls
(LS). This will bring up all the programs that are in your computer currently. If you are just making a new computer, all you will see is “rom”. You must be wondering, what is this rom you speak of? Well, let’s find out. Type
> cd rom
it will look somewhat like the above code. this brings you into another folder! Congrats, you entered your first folder. Now if you type ls again, you see there are more programs! Lets try opening a file. Type
> edit startup
(This is really important ^^^, you will use it to create programs with ease later on, so remember it!)
This will bring you to a file. You are now editing your first file! This is where the magic happens, where you code all your stuff! This file that you are in will do what you program it to do every time you turn on your computer. To exit your file, press ctrl (usually found in the bottom left corner) and press right on your arrow keys once. Press enter. You should be in the rom folder again. One useful little tool is the previous tool. you can type
> cd ..
and it will bring you to the previous page (our home page, if you will). This is a lot to take in, and only the basics. I covered a lot, but might cover more later in the tutorial. That is it for navigating!
Basic Syntax
We will be learning:
- How to print stuff to a computer
- How to do math with a computer
- How to run a program
Now that you know how to look around in your computer, let’s create our first program! Go ahead and open up your computer. Once you get there, let’s make a program. Remember that edit command we used earlier? This is where it comes in handy! Go ahead and think of a name for your program. My program name is going to be “math”. Go ahead and make yours anything you like.
> edit math
This will make a program called math, and put us inside it! Now, let’s put some code in there. We have all been in 1st grade right? This should be easy then! We all know that an addition problem is written using two numbers and a plus sign, right? Well, go ahead and do that! My program looks like:
1+1
Oh wait, that won’t work! We have to learn about the print syntax! This is simple. Look at it this way: Your computer likes to interact with you. It wants to talk with you, but has no mouth! So in order to talk with you, it needs to write it out. That is what it does with print! It returns what you said in the program. So lets try this out. The syntax for print looks like:
print()
Yep. That easy. Now, I know you are excited to run your first program. Wait wait, but if you run it now, it doesnt know what to do! You need to give it words to say. Well, your computer has gone through all those grades of math, so it understands 1+1! Lets try it now!
print(1+1)
Your code should look similar to mine. Now to try it out. Press ctrl again and press enter. This saves your program, so you dont lose it! Now press ctrl again and press right once, so you are hovering over exit, and press enter. You are now at the homepage. To run a program, simply type
> programnamehere So for me it would look like > math
Ok, so what did I get…. 2! Computers grow up so fast… Well, while I go and tear up in a corner, you mess around with your newly found program! It knows all kinds of math!
print(1+1) print(1-1) print(1*1) print(1/1)
Those all work So have fun, and see you in the next part!
Basic Syntax 2
We will be learning:
- Variables
- How to print variables
- Strings
So. You are excited for our next lesson? Well, here it is! We are gonna learn variables! Get started by opening your computer and making a new program. You remember how to do that right? Well, if you dont, go back to the previous lesson Make a new program, and name it anything you like. My program will be named variable. Start by declaring some variables. Variables are things that you can store things in. Think of a variable like a bank. You have multiple different banks, and you store your stuff in there, but only numbers and strings, no money. And no, I dont mean a piece of string, I mean a string! Most of our things we want the computer to say using print will use strings! Strings go in quotation marks, like this:
print("Hello World!") Now that we have that handeled, lets declare a variable! My variable will be called myVariable.
local myVariable = 10
This is what the syntax for a variable looks like! The stuff after the = sign is the stuff that will be stored in the variable. I used a number, but you can use our newly learned strings. Remember to use quotation marks! So now that we have a variable, lets make the computer say our variable! Do you know how to do this? Well, it looks like this!
print(YourVariableNameHere) So for me it would be print(myVariable)
Now run your code and see what happens! The stuff you stored in the variable should show up! Pretty cool, right? That is all for this lesson, see you in the next one!
Basic Syntax 3
We will be learning:
- If Statement Syntax
- Booleans
- Operators
Did you have fun running your math program? Good, because we are starting to learn more and more about computercraft. During these lessons, feel free to go back and re learn something if you forgot how to do it. We are going to learn about the syntax for an if statement. Make a new program called if. Now make 2 variables. For these two variables, we will set booleans! What are booleans, you ask? Well, dont get scared of this new word, it is just a fancy way to say “true or false”. So, make a variable and set it to false. Make another variable and set it to true. Now we make an if statement. Dont be scared, just follow what I do!
local myVariable = true local yourVariable = false if then end
Altogether, you should have what I have above. Now I will introduce my best friends, operators! The two operators we are using today are the or and the and operators. It is easy to do this, all you need is a little help. What we can do is check our variables to see if they are true or false! Let’s try it out!
local myVariable = true local yourVariable = false if myVariable and yourVariable then --This means if myVariable and yourVariable are both true, then run the if statement. print("We are both true!") elseif myVariable or yourVariable then --If they arent both true then check if either of them are true print("One of us is true!") else --When all else fails, do this code. print("We are both false
/>/>/>/>") end Now run your code. If you followed my code, you should get a message from the computer saying “One of us is true!” Mess around with the variables a bit, switch the booleans and see what happens!
Mid tutorial breather/Let’s build a program!
We will be building:
- A calculator
Wow, we learned a lot! Let’s make a calculator! Using if statements, booleans, operators, print, and the stuff we learned, we can make a calculator! So lets get started by adding the skeleton of our program.
local input = read()[/size][/size][/size][/size] [size=6][size=2][size=3][size=4]term.clear() print(" ") print("Do you want to add, multiply, subtract, or divide?") print("What is the first number that we shall calculate?") num1 = num(read()) print(" ") print("What is the second number that we shall calculate?") num2 = num(read()) print(" ") So basically, it asks us if we want to add, multiple, subtract, or divide, and asks you which two numbers it should use! What is this new read() statement? Well, it basically “reads” the users input! Then we declare variables that will hold the user’s input on the numbers! Let’s work on the second part of the code.
if input == "add" then print(num1+num2) end if input == "subtract" then print(num1-num2) end if input == "multiply" then print(num1*num2) end if input == "divide" then print(num1/num2) end
WARNING: This code isnt fully functional. It has a few errors but still works.
Computer Craft Coding Tutorial





0 nhận xét:
Đăng nhận xét