Skip to main content

Mavis Minecraft Teaches Coding - Part 1

Learn to code the easy-ish way

Actually this isn't written by someone called Mavis, but instead by RPS chum and former Wired staffer Duncan Geere, who you may remember from his Sim City 2000 retrospective. In this first of four parts, he takes you through the ropes of basic coding via the modern marvel that is Minecraft. Everything's done in game, and it all begins by punching a tree. Take it away, Duncan.

New year! New you! New Kids On The Block! New Romantics! Nu-Metal! New Games Journalism! January is the perfect time for personal improvement - most people try and lose weight, quit drinking or speak a new language. But you're going to learn how to code, and you're going to do it by playing Minecraft.

There's a brilliant mod called ComputerCraft that was released just over a year ago, on Christmas Eve 2011. It's the creation of a very smart chap called Dan200, and it adds in-game computers with a full OS, filesystem, and a selection of built-in programs. Over the course of a short series of articles, I'm going to teach you how to write software that can password doors, automatically mine, make a big digital clock for your base, and even send chat messages to other players' in-game computers.

That's just the beginning of what's possible. If you explore further, you'll find printers, touchscreen monitors, printers, programmable jukeboxes, games-within-games, or even pull in the contents of Minepedia so you can get Minecraft help from a computer in Minecraft.

It runs on the widely-used and easy-to-learn programming language Lua, so as well as learning some basics of programming languages in general, you'll also be able to "update your CV" with a "transferrable skill". Eat that, careers advisor.

Getting started is easy. You can download ComputerCraft from the Minecraft forums. It's free. However, it does require a little bit of fiddling around in java folders, so there's actually an easier option.

The awesome Technic Pack includes ComputerCraft by default and doesn't require any mucking about, so if you prefer then you can just download the Technic Pack and run it that way instead. If you do that, make sure you select "Tekkit" in the drop-down when you login. In these tutorials I'll be working from version 1.481 of ComputerCraft, running in 3.1.2 of Tekkit Classic.

So, where do we start? Well, it's Minecraft, so go punch a tree. Then, build a shelter, start a mine and so on. You don't need to go overboard, but make sure you've got a creeper-proof place to work from - either below the surface or raised above the ground. The last thing you want when you're coding is a green face leering over your shoulder.

You'll also need some supplies, but not much. For this tutorial, you'll need about ten blocks of stone (not cobblestone, you need to run it through a furnace first), four redstone, one bit of paper, and a single pane of glass. Once you've got all that together, you're ready to go.

The first thing you'll want to build is the computer itself. For this, place a pile of redstone dust in the centre of your crafting bench as a CPU, a pane (not block) of glass below it as the screen, and fill out the rest with a stone shell. Voila. You're Alan Sugar. Now to turn you into Bill Gates.

Put your computer down somewhere nice (a fence post or two makes a nice desk) and right click it. You'll get a command prompt.

This will now behave quite a lot like a regular computer. It has a text editor, a file system with directories, and so on. For those of you that remember fiddling around with DOS, you'll immediately be somewhat at home. If you don't, don't worry - I'll talk you through the very basics.

First, let's explore around a bit. Type "ls" and it'll list the files in your current directory. There should only be one, called "rom". Go into that directory by typing "cd rom" - which translates to 'change directory rom'. If you ls again here, you'll see a bunch of files and folders. Type "cd .." to go back up a level to what's called the root folder - the top of the chain. If you get lost, typing "cd /" will take you back to the start from wherever you are.

It's worth noting that files and directories look identical in this interface. To work out whether something is one or the other, use the type command. For example, in the root directory, typing "type rom" should return the word "directory", indicating that it's a directory.

Let's get coding. We'll start with something very simple.

Type "edit helloworld" at the command prompt to open a new folder in the text editor called helloworld. A blank screen will appear with a flashing cursor and "Press Ctrl to access menu" at the bottom. At the top, type the following, paying careful attention to upper and lower case.

print("Hello world!")

Then hit control, then enter to save that file. Then control again, right arrow to move to exit, and then enter to exit the text editor. The "print" command tells the computer to output something to the screen, so all we're doing here is telling the computer to output the words "Hello world!" to the screen.

Now type "helloworld" at the command prompt to run your program. If all goes well, it should look something like this.

If not, go back through the steps again making sure you didn't miss anything out. If it hangs at any point, hold ctrl-T to terminate the current program. If you want to start completely from scratch, hit Esc to leave the ComputerCraft interface, whack your computer with a pickaxe until it drops and place it down again. Or just type "reboot" or hold down ctrl-R for a second. But the first option is more fun.

For kicks, you can type "ls" again to see that there's now a file in the root directory called "helloworld". That'll stay there until you reboot the computer, but to keep it more permanently (or perhaps transfer it between machines), you'll want a disk drive.

Build one of those by placing a pile of redstone dust in the middle and centre bottom slots of the crafting bench and filling the rest out with stone again.

Place the disk drive next to one of the faces of the computer. But a disk drive is useless without a disk, so to make that, combine one sheet of paper with a pile of redstone dust. Right-click the disk drive, and put it in the slot. If you've been lucky enough to find a record on your travels, you can also put that in the disk drive and use the "dj" command to play it.

Now if you use the computer and type "ls" you should see a directory called "disk". Anything you put in there will stay there, permanently, so long as you keep hold of that disk. To copy our helloworld program there for safe keeping, go back to the root directory and type "cp helloworld disk/helloworld". This is basically telling the computer: "copy the file called helloworld to a new file called helloworld in the directory called disk".

To check it's worked, destroy the computer with your pickaxe (and even the disk drive if you want), and then place it all back -- not forgetting to put the disk back in the drive. Then type either "cd disk" then "helloworld" or just "disk/helloworld" to run the program.

Congratulations, you're a programmer. A bad one, sure, but you now know how to navigate the Lua filesystem, create files, and save them to disk. You also know how to print text to the screen. Next week, I'll show you how to create an intruder alarm, a passworded door and a digital clock for your base.

In the meantime, feel free to have a poke around the filesystem and particularly the /rom/programs directory. If you want to look inside a file, just type "edit ". You can't do any damage that simply breaking the computer with your pickaxe and placing it down again won't fix.

If you want to be extra-prepared for next week, gather a bunch more of each of the resources from above (particularly redstone), as well as some iron bars. Good luck hacker, see you then.

Read this next