LEARN TO PROGRAM WITH ® MINECRAFT TRANSFORM YOUR WORLD WITH THE POWER OF PYTHON BY CRAIG RICHARDSON San Francisco LEARN TO PROGRAM WITH MINECRAFT. Copyright © 2016 by Craig Richardson. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Printed on demand in USA. ISBN-10: 1-59327-670-2 ISBN-13: 978-1-59327670-6 Publisher: William Pollock Production Editor: Riley Hoffman Cover Illustration: Josh Ellingson Developmental Editors: Hayley Baker and Tyler Ortman Technical Reviewer: John Lutz Copyeditor: Anne Marie Walker Compositor: Riley Hoffman Proofreader: Paula L. Fleming For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 415.863.9900; than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Minecraft is a registered trademark of Mojang Synergies AB, which does not authorize or endorse this book. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. To the countless adults and children that read the early drafts of this book, used my recipe cards, and attended my talks and workshops: thank you so much for your enthusiasm and support. This book is for you. ABOUT THE AUTHOR Craig Richardson is a software developer and Python educator. He has worked for the Raspberry Pi Foundation, taught high school computing classes, and led many workshops on Python programming with Minecraft. ABOUT THE TECHNICAL REVIEWER John Lutz is a math teacher in the New Orleans public school system, where he also teaches extracurricular courses on Scratch, Arduino robotics, and 3D printing. He piloted his school’s computer science program, which continues to grow and attract bright new minds to coding. Since helping with this book, John is working on a Python program that will destroy all baby zombies in his Minecraft world, forever. BRIEF CONTENTS Acknowledgments Introduction Chapter 1: Setting Up for Your Adventure Chapter 2: Teleporting with Variables Chapter 3: Building Quickly and Traveling Far with Math Chapter 4: Chatting with Strings Chapter 5: Figuring Out What’s True and False with Booleans Chapter 6: Making Mini-Games with if Statements Chapter 7: Dance Parties and Flower Parades with while Loops Chapter 8: Functions Give You Superpowers Chapter 9: Hitting Things with Lists and Dictionaries Chapter 10: Minecraft Magic with for Loops Chapter 11: Saving and Loading Buildings with Files and Modules Chapter 12: Getting Classy with Object-Oriented Programming Afterword Block ID Cheat Sheet Index CONTENTS IN DETAIL ACKNOWLEDGMENTS INTRODUCTION Why Learn to Program? Why Python? Why Minecraft? What’s in This Book? Online Resources Let the Adventure Begin! 1 SETTING UP FOR YOUR ADVENTURE Setting Up Your Windows PC Installing Minecraft Installing Python Installing Java Installing the Minecraft Python API and Spigot Running Spigot and Creating a Game Starting Over with a New World Playing Offline Switching to Survival Mode Setting Up Your Mac Installing Minecraft Installing Python Installing Java Installing the Minecraft Python API and Spigot Running Spigot and Creating a Game Starting Over with a New World Playing Offline Switching to Survival Mode Setting Up Your Raspberry Pi Getting to Know IDLE Getting to Know the Python Shell