ages 10+ DDoonn’’tt Jjuusstt PPllaayy SSccrraattcchh SS GGaammeess—— mMaakkee tthheemm!! cc PPrrooggrraammmmiinngg rr aa Scratch, the colorful drag-and-drop program- Fruit Slicer: a Fruit Ninja® clone tt PPllaayyggrroouunndd ming language, is used by millions of first-time cc Brick Breaker: a remake of Breakout®, learners, and in Scratch Programming Play- hh the brick-breaking classic ground, you’ll learn to program by making cool games. Get ready to destroy asteroids, Platformer: a game inspired by Super PP Learn to program by making cool games Mario Bros.® rr shoot hoops, and slice and dice fruit! oo Each game includes easy-to-follow instruc- Learning how to program shouldn’t be gg tions, review questions, and creative coding dry and dreary. With Scratch Programming challenges to make the game your own. Want Playground, you’ll make a game of it! rr A l S w e i g a r t to add more levels or a cheat code? No problem, aa just write some code. ABOUT THE AUTHOR mm You’ll learn to make games like: Al Sweigart is a software developer who mm Maze Runner: escape the maze! teaches programming to kids and adults. ii He has written several best-selling Python nn Snaaaaaake: gobble apples and avoid your books for beginners, including Automate gg own tail the Boring Stuff with Python, also from PP Asteroid Breaker: smash space rocks No Starch Press. ll aa SNAaaaaaKE Brick Breaker platformer yy gg rr oo uu nn dd THE FINEST IN GEEK ENTERTAINMENT™ s w www.nostarch.com $24.95 ($25.95 CDN) SCRATCHSHELVE IN eiga : PROGRAM rt M ING LANGUAGES/ Scratch Programming Playground S c r atc h Pr o g r a m m i n g Pl ayg r o u n d Learn to Program by Making Cool Games By Al Sweigart San Francisco scratch programming playground. Copyright © 2016 by Al Sweigart. 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. 20 19 18 17 16 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-762-8 ISBN-13: 978-1-59327-762-8 Publisher: William Pollock Production Editor: Laurel Chun Cover Illustration: Josh Ellingson Illustrator: Miran Lipovača Interior Design: Beth Middleworth Developmental Editor: Tyler Ortman Technical Reviewer: Martin Tan Copyeditor: Anne Marie Walker Compositor: Laurel Chun Proofreader: Lisa Devoto-Farrell 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: Sweigart, Al, author. Title: Scratch programming playground : learn to program by making cool games / Al Sweigart. Description: San Francisco : No Starch Press, [2016] | Audience: Ages 8+. | Includes index. Identifiers: LCCN 2016022304 (print) | LCCN 2016024406 (ebook) | ISBN 9781593277628 | ISBN 1593277628 | ISBN 9781593277963 (epub) | ISBN 1593277962 (epub) | ISBN 9781593277970 ( mobi) | ISBN 1593277970 (mobi) Subjects: LCSH: Scratch (Computer program language)--Juvenile literature. | Computer games--Programming--Juvenile literature. | Computer programming--Juvenile literature. | Microcomputers--Programming--Juvenile literature. Classification: LCC QA76.73.S345 S94 2016 (print) | LCC QA76.73.S345 (ebook) | DDC 005.13/3--dc23 LC record available at https://lccn.loc.gov/2016022304 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 inten- tion of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precau- tion 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. For Seymour Papert, So long and thanks for all the turtles (February 29, 1928 – July 31, 2016) About the Author Al Sweigart is a software developer, tech book author, and hoopy frood who really knows where his towel is. He has written several programming books for beginners, including Automate the Boring Stuff with Python, also from No Starch Press. His books are freely available under a Creative Com- mons license at his website http://www.inventwithpython.com/. About the Technical Reviewer Martin Tan helps run a Code Club in Australia, and he wrote the space-themed Scratch and Python projects for Moonhack 2016 that brought over 9,000 kids together to break the world record for children coding simultaneously. Martin also works as a penetration tester, contributing to various open source projects and research. Brief Contents Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Chapter 1: Getting Started with Scratch . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Chapter 2: Rainbow Lines in Space! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15 Chapter 3: Maze Runner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 Chapter 4: Shooting Hoops with Gravity . . . . . . . . . . . . . . . . . . . . . . . . . . .67 Chapter 5: A Polished Brick Breaker Game . . . . . . . . . . . . . . . . . . . . . . . . .93 Chapter 6: Snaaaaaake! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125 Chapter 7: Fruit Slicer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145 Chapter 8: Asteroid Breaker . . . in Space! . . . . . . . . . . . . . . . . . . . . . . . . .183 Chapter 9: Making an Advanced Platformer . . . . . . . . . . . . . . . . . . . . . . .209 Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .257