I hope everyone learned some new things today! We looked at getting the turtles in ComputerCraft up and running about, and also programming them to dig quarries for us! So now it’s on to you guys, there is no CoderDojo next weekend so I recommend working on something for the competition!
Remember – The competition is whoever makes the best Robot related project (Can be Scratch, ComputerCraft, LOVE, even something else) will win a robot. Nice! I’d enter the competition too but I’m really busy… 😀
For those who always seem to forget, the IP address of the server is 126.96.36.199:25572.
Anyway this post will explain what we did in the dojo today, and hopefully will explain anything I went over a bit too quick. Also I hope that what we did today put some pieces together on how Lua and ComputerCraft all work together.
First we looked at getting a turtle to move around. We had to first give them some fuel, then run the program refuel all. That takes everything in it’s inventory and increases the turtles fuel level, meaning it can move. Then we ran the go program with these arguments:
And those commands do what they say on the tin. Remember the go program is another program that someone wrote, and the next argument (forward, backward) tells the program what to do. Then, we ran the lua command prompt (the lua program, so we can type lua statements directly) and found that the turtle movement api commands are similar to what the go program did but also slightly different, like this:
go forward – Moved the turtle forward, but waited if there was something blocking it. If the turtle is out of fuel it will print this on screen.
turtle.forward() – Returns a boolean value to say if it could move forward or not. Doesn’t wait if something is blocking it, returns false instead. Also returns false if the turtle is out of fuel.
The rest were the same:
Also in the turtle API we found that mining turtles (Turtles with a diamond pickaxe) can dig too! So we wrote a little program in order for the turtle to dig a quarry for us, and here it is:
local hitBedrock = false
local amountMovedDown = 0
while not hitBedrock do
if not turtle.down() then
hitBedrock = true
amountMovedDown = amountMovedDown + 1
for i=0,amountMovedDown do
You can also get this code using a mining turtle and running the command pastebin get 69dbBGYE mineDown will download the quarry program, and you can then run it by running the mineDown program.
In the image you can see the fruits of our labour! After that, we made a mini mob farm, and had the turtles attack anything in front of them. It’s a really simple program, it’s the turtles attack function in a loop!
while true do
The pastebin code for the turtle is pastebin get a52U8pZU turtleAttack, and to run the program run turtleAttack.
Finally, for the last part we built the traditional password protected door. Except this time if you get the password wrong the turtles attack you!
The code for the computer at the door is:
while true do
print(“Please enter the password”)
password = read(“*”)
if password == “password” then
print(“You got the password right!”)
print(“You got the password wrong! lololol”)
And the code for the turtles is:
while true do
local senderID, message, distance = rednet.receive()
if message == “attack” then
The command to download the door computer code is pastebin get EmP5WE3i doorCode and called doorCode, and to get the turtle program run pastebin get 7yhYKtTm turtleListener and is called turtleListener.
The new thing in this code is the rednet api commands. These allow you to send messages from one computer to another, in this case if the user puts the password in wrong the turtles will attack whoever is standing at the door!
The rednet.open(<side>) commands basically turn on the wireless modem for the computer and the turtle. If the computer or turtle doesn’t have a wireless modem this command will return false, but won’t really affect the program… Except for it not working properly! The rednet.broadcast(<message>) command sends the message to every computer in its range that is currently listening (that is, running the rednet.receive() command to listen) and the rednet.receive() command listens for rednet.broadcast(). rednet.receive() returns a few more variables than usual functions, in this case it returns the senders ID (senderID), the message itself (message) and the distance the message travelled (distance). This program only cares about the message though, and the first thing it does is check if the message it got was the string “attack”. And if it is, the turtle will attack whoever is standing in front of it!
So I hope you guys had fun learning today! If you have any questions post it in the comments below, though I’d rather questions about the code or new code than about the server 😛