Coding with Minecraft: Build Taller, Farm Faster, Mine Deeper, and Automate the Boring Stuff
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
Chapter 6 Programming a Robot Lumberjack features a program to make a turtle chop down a single tree and collect its wood Chapter 7 Creating Modules to Reuse Your Code teaches you how to write code once and share it with multiple programs Chapter 8 Running an Automated Tree Farm extends the program from Chapter 6 to create a fully automated tree farm so your turtles can harvest wood from multiple trees Chapter 9 Building a Cobblestone Generator features a program for mining unlimited amounts of cobblestone which the turtles will use as building material for Chapters 10 through 13 Chapter 10 Making a Stone Brick Factory features a program to turn the cobblestone from Chapter 9 into stone bricks Chapter 11 Constructing Walls includes a program that uses stone bricks to build walls Chapter 12 Constructing Rooms contains a program that lets you join walls together to build rooms Chapter 13 Constructing Floors features a program to build floors and ceilings for your rooms Chapter 14 Programming a Robotic Farm features a program to make turtles plant and harvest different kinds of crops so you can keep yourself fed Chapter 15 Programming a Staircase Miner features a program that digs stairs deep into the ground to mine ore and other valuable blocks The Function Reference lists and explains how to use all the functions in this book The Name ID Reference lists commonly used Minecraft blocks and items along with their name IDs which you’ll use to identify block types and other items in your programs getting help Minecraft famously lacks a tutorial for new players It doesn’t have an instruc tion manual or even a help menu Minecraft forces you to be responsible for your own education You’ll need to do online research form questions find answers and sometimes just do some plain old experimentation Minecraft cultivates a growth mindset in players Even after falling into lava or having their base blown up by a creeper players come back determined to learn how to overcome these problems However this book is about the ComputerCraft mod not the basics of playing Minecraft To use this book you should already know how to do the following in Minecraft Mine for ore stone coal wood and other blocks Craft a workbench and tools such as axes shovels torches and pickaxes xx introduction
Craft a furnace fuel it and then smelt ore blocks or cook meat in it Craft stairs ladders chests doors fences and other parts you’ll use for buildings Plant seeds and farm food If you don’t know how to do all of these things don’t worry You can teach yourself by searching online Go to the search engine of your choice and enter the word minecraft along with what you want to learn For example you could use the search terms minecraft smelt ore minecraft bake cake or even just minecraft basic tutorial to find the information you need You can also search for Minecraft video tutorials on websites such as https www youtube com by using the same search terms you would use in a search engine As I mentioned previously because ComputerCraft isn’t made by the same people as Minecraft most Minecraft websites won’t have informa tion about CC You can learn about CC from the ComputerCraft Wiki at http www computercraft info wiki If you have questions specific to CC you can sign up for a free account on the ComputerCraft forums at http www computercraft info forums2 If you have additional questions about the programs in this book you can post those to the community at https www reddit com r turtleappstore online resources You can download all the programs in this book directly from inside the Minecraft game see Sharing and Downloading Programs Online on page 42 for instructions And although Minecraft doesn’t support copy ing and pasting text from outside the game all the code and resources for this book are available for reference on its companion website https www nostarch com codingwithminecraft There you can also download the code for the bonus activities if you get stuck and want to check out the solutions You’ll also find links to the installation files see Chapter 1 for detailed installation instructions If you want to explore other programs or share your code you can do so through https turtleappstore com which is a free website for ComputerCraft scripts see turtleappstore com on page 44 for details what You learned Minecraft is a gaming phenomenon that can be played in many ways and appeals to a diverse crowd of players In this book you’ll learn how to build more in less time using ComputerCraft a mod that lets you program turtles in the Lua programming language By learning to program with Lua and CC you can automate many jobs you would otherwise have to do by your self including mining farming building and crafting Using Minecraft and ComputerCraft you’ll solve problems indepen dently and learn basic computer programming skills along the way Let’s begin introduction xxi