Welcome back seth wing here you’re watching my turtle programming tutorial series this is intended as a general programming tutorial for people with zero programming experience at all use the computer craft mods these turtles are from the computercraft mod and you can find that in the video description although i’m going to be Using the feed the beast modpack in these videos turtles use lua which is a programming language but the principles that uh that i’ll be teaching are common to most programming languages so it should be very useful and i encourage you to follow along on your own and hope you learned something valuable All right so the first thing we need to ask ourselves is what is a turtle a turtle is a type of computer that can move around and first thing i need to do is craft one this is the crafting recipe this this will make a computer here Computer and then to make a turtle out of the computer you surround the turtle with iron and add a chest because the turtle has inventory and the last thing i’m going to do is i make it a mining turtle so that it can mine stuff in addition to moving around as a turtle When we put it on the grounds we have our turtle this is what it looks like this is the front of the turtle it’s got kind of eyes here or a sensor and in the back it’s got i don’t know some sort of port i’m in creative mode so I’ve got an extra turtle out of that now when you right click on the turtle you get a command prompt now some of you know what this is probably a lot of you command prompt is an interactive way of well interacting with the turtle now the turtle has a directory structure so If i type dir dir i can see a list of the directories or folders in the root directory the root directory is just the default directory that’s at the top level if i type cd which stands for change directory i can go to the rom folder Here i can type dir again to see what folders reside in the rom folder if i type cd dot dot that will take me up a level back to the root level i can type mkdir to create a directory create a folder it stands for a make directory And so instance i could make a directory called prog and if i type der now that’ll show up next to rom if i go in there it’ll be empty if i type dir so just create some empty folder now if i go into the rom directory we can see there are several directories Here the helpful thing about this is that okay so rom stands for read-only memory which means you can’t edit any of the programs or any of the folders or anything that resides in this folder so if i go to programs it contains a list of the built-in programs if i type dir again We can see a list of the folders and files in this directory color computer http secret and turtle are the folders and then everything after that are files which are programs if i go to the turtle directory it contains a list of programs which are useful for turtles so These are fun to play around with on your own you might look look online to see what they do you can also look at them in order to see what code runs the program so if i type edit let’s see what’s a good program to look at maybe dance Now i’m in in a editor this is this is the code this is the lua code that runs the program called turn i don’t think there’s too much code to it it’s just a lot of turn rights turn left some backs and stuff but this is what the code looks like It’s it may look like a jumble to you right now if you don’t know anything about programming but as we learn about this stuff you’re going to learn what all of this stuff does and so i can like it says at the bottom of the terminal here i can press ctrl to access The menu and so now i can either save or exit or print i didn’t make any changes but if i did i could save those changes but save i’m just going to exit and we can actually run the dance program here and we can see the turtle is preparing to get down I can press any key to stop the groove he’s doing his little dance i guess he he stopped anyway uh so we’re going to go over a lot of the stuff that you need in order to write your own programs so i’m going to get out of out of here We go back to the root folder and so the next thing i want to show you is the lua interpreter lua is the programming programming language that runs the turtles programs if i type lua lua i’ll get an interactive lua prompt you set you can see it says call exit Paren parent to exit in this lua prompt we can actually type in source code and it will run it immediately so for instance if i take a look at the turtle he’s facing this direction if i type turtle turtle dot turn right and then open parentheses close parentheses Turtle turns right see change directions similarly i can do turtle dot turn left and the turtle turns left you’ll notice each time i i open and close the terminal it stays where it was we’re still in the lua interpreter that’s useful now i can type turtle.forward and that’ll try and make It go forward you can see it it says false after that and it didn’t move that’s because it doesn’t have any fuel so there are some things we can do that we’ll need to do in order to get the turtle fueled up if i take a look at turtle dot get item count This is a what’s called a function that will get the item count for a particular slot in the turtle’s inventory if you look to the right of my inventory you’ll see 16 slots which the turtle has as its inventory now the first slot is the top left and then it Goes in reading order so the bottom slot is the bottom or sorry the the last slot the 16th slot is the bottom right so if i type turtle.getitemcount1 it says zero if i put three coal there and repeat the commands now it’ll say three If i try it on the second slot it says zero you can see if i put another item here and now it’ll say one okay so this is how the turtle can tell how many items it has in each slot it can’t tell what type of item is in each slot so That’s something you’re going to kind of have to manage for the turtle but we’ll kind of talk about that later we can have the turtle select different slots in its inventory you’ll notice the first slot is kind of selected right now if we select slot 2 that’ll be selected select slot 16 That’ll move it move the selection to the bottom left um so these are just the different functions that the turtle has now we can refuel the turtle uh first of all let’s take a look at what the current fuel level is for the turtle we Haven’t put any fuel in it at all this is the function get fuel level has to have a capital f and capital l and it’ll return zero so there’s no fuel in it which means it can move zero blocks so if we type turtle.refuel1 What this will do is it will look in the selected slot for some fuel source and if there is a fuel source it’ll consume one item of that fuel source we’ll use one fuel and it returns true which means that it was able to consume one fuel and now if We try turtle dot get fuel level again it says 96 that means it’ll be able to move 96 blocks so if i type turtle dot forward remember before this returned false it’ll return true notice that i need the parentheses for this it’s just a convention for functions Functions are just things that do stuff and they’re very important to programming this time turtle.forward returned true and it actually did move a little bit forward let me try it again forward to return true now if i type turtle.getfuel level again we can see now it’s 94 because it’s moved to blocks Some other functions that allow the turtle to move around turtle dot up you can see it moved up by one turtle dot down moves down by one if i type turtle.down again it’s going to return false because there’s a block in the way it can’t move through a block and so It’s going to try and go down it’s not going to be able to and it’ll return false now you’ll notice each of these functions returned a value sometimes it was a number like in the case of get fuel level sometimes it was true or false which is called a boolean variable boolean value A boolean value is just something that is true or false it’s like a condition or yeah okay so we’re going to write our first program now and i’m going to call oh actually there’s one thing i want to get to first there’s a print print function so if i print hello world It will print hello world to the console and then it returned one i’m not actually sure i’m not sure what that means i think that means that it succeeded but uh yeah again i’m not really sure i’ll have to look that up later okay so now i’m going to get get out of The lua interpreter remember all of these things were code that lua code that we could execute because we were in the luba interpreter when i type exit with two parentheses i’m going to get out of the little interpreter i’m back in the command line i can type dura again and see where we Are we’re in the root one we have the product directory in the the rom directory now if i try something like turtle dot up it’s gonna be confused because that’s not a program we’re not in the interpret any interpreter anymore it’s not gonna work it’s getting night time okay so Um we’re going to create a program so in order to create a program i’m just going to type edits and then name the program i’m going to call this one check fuel and this will put us in the editor this is what we saw before when we were Editing the source code for the dance program so the first thing i want this program to do is print that it’s checking the fuel and so that’s what this line will do um now i want to use an if statement if statements are really important for programming they check if conditions are True and if they are they execute some code so for instance i’m going to type turtle.getfuel level remember this returned the fuel level of the turtle and i’m going to check if that’s less than 200. so basically we have some code here turtle.getfuel level less than 200 that’s either going to Return true or false remember turtle dot get fuel level returned an integer a number that’s going to be compared to 200 if it is less than 200 this whole thing this whole thing turtle dot get fuel level less than 200 will evaluate to true otherwise if it’s greater than or equal To 200 it’ll evaluate to false then we need a then that’s just part of the if then syntax that’s how you type it out um and then we have some other statements now some other things i could have done here i could type if true then and that would just always execute the Statements but let me let me finish this this statement here so we’re going to type if turtle.getfuel level is less than 200 then we’re going to indent this just because it’s common practice to indent blocks of code that happen within if statements turtle dot refuel 1 So it’s going to try and refuel from the selected slots and then and then we’ll play we’ll print refueling okay then we’re going to use an else so let me just finish doing all this and i’ll explain this else i’m going to print no fuel necessary and then end so These are all really important so if then else end are the critical structures to the to the if statement so we have if and then a condition and if that condition evaluates to true then it’s going to do the things in the top indented section here so turtle dot refuel one print refueling If that condition wasn’t true then it’s going to do what’s in the else part of the if statement it’s going to do the print no fuel necessary so this shall be pretty logical uh if you know if we don’t have much fuel left then we want to refuel and we can Print out what we’re doing and if we have plenty of fuel left more than 200 then we can just say that there’s no fuel necessary now if i exit this program sorry i have to save the program first i have to save all the changes that i Made and then exit it now if i type dur we can see there’s a check fuel program and if i run the check fuel program it’s going to say checking for fuel and refueling will notice that there is now one last call because it’s refueled if i type it again It’s going to do the same thing i’m going to put more coal in here so now if i type check fuel a third time it’s going to say checking fuel and it’ll say no fuel necessary and that’s because the fuel level is now over 200 we fueled it up sufficiently it doesn’t Need any more fuel so this is just basically a really really simple program and it goes over the if else and if then else end structure and this is something we’ll be using a lot if then not not we won’t always be using else but it’s it’s optional if i deleted all These lines uh this program would still work and it just wouldn’t print anything else out if the turtle had more than 200 fuel and we can actually see that if i save it and exit and i run check fuel it’ll just say checking fuel and then return to the command line So this is yeah just a basic really basic program and we’re going to be sort of learning new concepts and and writing programs like this as the tutorial series goes on i think i will leave it there for this episode tune in next time and we’ll learn about Things like variables and some other stuff that’s about it thanks for watching the gravel will throw Video Information
This video, titled ‘Programming Tutorial with Minecraft Turtles — Ep. 1: Intro to Turtles and If-Then-Else_End’, was uploaded by SethBling on 2012-12-20 22:00:17. It has garnered 1092027 views and 11683 likes. The duration of the video is 00:15:42 or 942 seconds.
In this series, I teach you about programming by using Turtles in Minecraft. This is meant as a general purpose programming tutorial, and should help you learn any language. This episode covers the Turtle command line, several basic turtle commands, and “if-then-else-end” structures.
ComputerCraft Mod: http://www.computercraft.info/
Music at the end is Cipher by Kevin MacLeod