For kids aged 10+ (and their parents) LLeeaarrnn ttoo AA BBlloocckkyy Iinnttrroodduuccttiioonn ttoo PPrrooggrraamm wwiitthh PPrrooggrraammmmiinngg You’ve bested creepers, traveled deep into (cid:44) Show exactly where to dig for rare blocks MMiinneeccrraafftt® caves, and maybe even gone to The End (cid:44) Cast a spell so that a cascade of flowers and back—but have you ever transformed (or dynamite if you’re daring!) follows a sword into a magic wand? Built a palace your every move Transform Your World in the blink of an eye? Designed your own color-changing disco dance floor? In Learn to (cid:44) Make mischief with dastardly lava traps with the Power of Python Program with Minecraft®, you’ll do all this and and watery curses that cause huge floods more with the power of Python, a free language Whether you’re a Minecraft megafan or a used by millions of professional and first-time newbie, you’ll see Minecraft in a whole new C r a i g R i c h a r d s o n programmers! light while learning the basics of programming. Begin with some short, simple Python Sure, you could spend all day mining for pre- lessons and then use your new skills to modify cio us resources or building your mansion by Minecraft to produce instant and totally awe- hand, but with the power of Python, those some results. Learn how to customize Minecraft days are over! to make mini-games, duplicate entire buildings, ABOUT THE AUTHOR and turn boring blocks into gold. You’ll also write programs that: Craig Richardson is a software developer and Python educator. He has worked for the (cid:44) Take you on an automated teleportation Raspberry Pi Foundation, taught high school tour around your Minecraft world computing classes, and led many workshops (cid:44) Build massive monuments, pyramids, on Python programming with Minecraft. forests, and more in a snap! (cid:44) Make secret passageways that open when The code in this book will run on you activate a hidden switch Windows 7 or later, OS X 10.10 or later, or the Raspberry Pi. (See the (cid:44) Create a spooky ghost town that vanishes last page for detailed requirements.) and reappears elsewhere wTHwE wF.InNoEsStTa rIcNh G.cEoEmK ENTERTAINMENT™ $29.95 ($34.95 CDN) PROGRAMSHELVE IN M: IN G LAN G UAG This book is not authorized ES/PYTH or endorsed by Mojang. ON www.allitebooks.com Learn to Program with Minecraft® www.allitebooks.com www.allitebooks.com Le a r n to P r o g r a m w it h M i n e c r a f t® Transform Your World with the Power of Python by Craig Richardson San Francisco www.allitebooks.com 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-59327-670-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; [email protected] www.nostarch.com Library of Congress Cataloging-in-Publication Data Names: Richardson, Craig (Software developer), author. Title: Learn to program with Minecraft : transform your world with the power of python / by Craig Richardson. Description: San Francisco : No Starch Press, [2016] | Includes index. Identifiers: LCCN 2015035298| ISBN 9781593276706 | ISBN 1593276702 Subjects: LCSH: Python (Computer program language) | Computer games--Programming. | Minecraft (Game) | Raspberry Pi (Computer) Classification: LCC QA76.73.P98 R53 2016 | DDC 005.13/3--dc23 LC record available at http://lccn.loc.gov/2015035298 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather 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. www.allitebooks.com 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. www.allitebooks.com 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. www.allitebooks.com BRIEF CONTENTS Acknowledgments ....................................................xvii Introduction.........................................................xix Chapter 1: Setting Up for Your Adventure.....................................1 Chapter 2: Teleporting with Variables ......................................27 Chapter 3: Building Quickly and Traveling Far with Math.........................47 Chapter 4: Chatting with Strings ..........................................65 Chapter 5: Figuring Out What’s True and False with Booleans .....................81 Chapter 6: Making Mini-Games with if Statements ............................103 Chapter 7: Dance Parties and Flower Parades with while Loops....................123 Chapter 8: Functions Give You Superpowers.................................145 Chapter 9: Hitting Things with Lists and Dictionaries............................167 Chapter 10: Minecraft Magic with for Loops.................................195 Chapter 11: Saving and Loading Buildings with Files and Modules .................231 Chapter 12: Getting Classy with Object-Oriented Programming ...................257 Afterword .........................................................283 Block ID Cheat Sheet .................................................285 Index ............................................................291 www.allitebooks.com www.allitebooks.com CONTENTS IN DETAIL Acknowledgments xvii Introduction xix Why Learn to Program? ..............................................xx Why Python?......................................................xx Why Minecraft?....................................................xx What’s in This Book? ................................................xx Online Resources..................................................xxii Let the Adventure Begin!.............................................xxii 1 Setting Up for Your Adventure 1 Setting Up Your Windows PC .......................................... 2 Installing Minecraft ........................................... 2 Installing Python ............................................. 3 Installing Java............................................... 4 Installing the Minecraft Python API and Spigot ........................ 6 Running Spigot and Creating a Game.............................. 7 Starting Over with a New World ................................. 8 Playing Offline .............................................. 9 Switching to Survival Mode .................................... 10 Setting Up Your Mac ............................................... 11 Installing Minecraft .......................................... 12 Installing Python ............................................ 13 Installing Java.............................................. 14 Installing the Minecraft Python API and Spigot ....................... 15 Running Spigot and Creating a Game............................. 16 Starting Over with a New World ................................ 17 Playing Offline ............................................. 18 Switching to Survival Mode .................................... 18 Setting Up Your Raspberry Pi.......................................... 18 Getting to Know IDLE ............................................... 20 Getting to Know the Python Shell ................................ 20 Say Hello to IDLE’s Text Editor .................................. 21 When to Use the Python Shell and When to Use the Text Editor ........... 23 The Prompts Used in This Book .................................. 23 Testing Your Minecraft Python Setup..................................... 24 www.allitebooks.com