Coding with Minecraft: Build Taller, Farm Faster, Mine Deeper, and Automate the Boring Stuff
inTroDuCTion Just three more diamonds and then I’ll stop I remember telling myself while play ing Minecraft I needed the diamonds for a new pickaxe I needed the pickaxe to mine more obsidian I needed the obsidian to make a Nether portal I needed to go to the Nether to pick up lava And I needed the lava for what did I need the lava for again Oh right I was sculpting a giant phoenix statue in the side of a mountain and I wanted to make lava pour out from its eyes and beak Two hours later I was still playing unable to pry myself away from the task at hand Minecraft is an addictive game It has sold over 107 million copies mak ing it the second most popular video game of all time beaten only by Tetris It’s an open-ended creative platform for gathering resources and building whatever you can imagine You can build castles to protect against zombie hordes plant crops and tend to animals or team up with friends to build massive works of art Minecraft appeals to a diverse set of people children teenagers and even adults love playing it
In this book you’ll use the ComputerCraft mod also known as CC to turn your addiction to building into an addiction to coding But what exactly is ComputerCraft or a mod for that matter what Are Minecraft Mods Minecraft by itself called vanilla Minecraft is just the beginning Minecraft can be modified and extended by third-party software called mods short for modifications to provide additional features such as blocks environments items monsters and even worlds that are not included with the vanilla ver sion of the game Due to its popularity Minecraft has attracted one of the largest modding communities of any video game These fan-made mods are free to download Some mods add space exploration and rockets Others add sorcery and spells You can even find Minecraft mods for creating your own dinosaur zoos high-speed train net works and bee-breeding apiaries In this book we’ll use the ComputerCraft mod to learn how to program what is ComputerCraft ComputerCraft is a Minecraft mod that adds programmable turtles to Minecraft These turtles can do almost anything the player can do dig mines chop down trees construct buildings craft items plant seeds milk cows bake cakes and more see Figure 1 An army of these box-shaped turtles can automatically perform all the time-intensive chores the player usually must do manually Figure 1 Turtles chopping trees left and mining right The catch is that you have to learn to program first These turtles understand code written in Lua moon in Portuguese which is a pro gramming language used by professional software developers in fields such as embedded computing systems and video game development The interpreter software that runs Lua scripts is just 100KB which means that it can be easily embedded inside other pieces of software such as a Minecraft mod Lua is often used within video game code for example in World of Warcraft Dark Souls Portal 2 Factorio and many others xviii introduction
Although Lua is simpler than other programming languages it’s still fast and powerful These qualities work in your favor Lua’s simplicity makes it a good language to start with if you have no programming experience how to use This Book You’ll need a purchased copy of Minecraft for Windows or macOS the free ComputerCraft mod and the free ATLauncher software to use this book See Chapter 1 for all download and installation instructions When typing the source code from this book do not type the line num bers at the start of each line For example if you saw the following line of code you would not need to type the 9 on the left side or the one space immediately following it 9 print What is your name You’d enter only this print What is your name The numbers are there just so this book can refer to specific lines in the program They are not part of the actual program’s source code Sometimes you’ll also see an unnumbered snip line in the code This indicates that some code has been omitted for brevity The snip is not part of the code itself what’s in This Book After the first few chapters which cover basic programming concepts each chapter in this book focuses on how to write a program your turtle can run to help you survive and thrive in Minecraft You’ll also find bonus activities that help you test your programming skills Here’s what you’ll find in each chapter Chapter 1 Getting Started with ComputerCraft helps you install and set up Minecraft and the ComputerCraft mod so you can start programming Chapter 2 Programming Basics introduces you to basic programming concepts and the interactive shell Chapter 3 Talking to Your Turtle introduces the file editor which you’ll use to write your first program Chapter 4 Programming Turtles to Dance shows you how to write a program to move the turtles around the Minecraft world Chapter 5 Making a Better Dancer adds on to the previous chapter’s dancing program with some new programming instructions introduction xix