Scratchtm Programming for Teens Jerry Lee Ford, Jr. Course Technology PTR A part of Cengage Learning Australia . Brazil . Japan . Korea . Mexico . Singapore . Spain . UnitedKingdom . UnitedStates ScratchTMProgrammingforTeens ©2009CourseTechnology,apartofCengageLearning. JerryLeeFord,Jr. ALLRIGHTSRESERVED.Nopartofthisworkcoveredbythecopyright hereinmaybereproduced,transmitted,stored,orusedinanyformor PublisherandGeneralManager,Course byanymeansgraphic,electronic,ormechanical,includingbutnot TechnologyPTR:StacyL.Hiquet limitedtophotocopying,recording,scanning,digitizing,taping,Web AssociateDirectorofMarketing: distribution,informationnetworks,orinformationstorageandretrieval SarahPanella systems,exceptaspermittedunderSection107or108ofthe1976 ManagerofEditorialServices: UnitedStatesCopyrightAct,withoutthepriorwrittenpermissionofthe HeatherTalbot publisher. MarketingManager:MarkHughes AcquisitionsEditor:MitziKoontz Forproductinformationandtechnologyassistance,contactusat ProjectEditor:JennyDavidson CengageLearningCustomer&SalesSupport,1-800-354-9706 TechnicalReviewer:ParkerHiquet Forpermissiontousematerialfromthistextorproduct,submitall TeenReviewer:HannahWittig requestsonlineatcengage.com/permissions Furtherpermissionsquestionscanbee-mailedto PTREditorialServicesCoordinator: [email protected] ErinJohnson InteriorLayoutTech: ICCMacmillanInc. ScratchisaprojectoftheLifelongKindergartengroupattheMITMedia Lab.Scratch,theScratchlogo,andtheScratchcataretrademarksof CoverDesigner:MikeTanamachi theMassachusettsInstituteofTechnology. CD-ROMProducer:BrandonPenticuff Allothertrademarksarethepropertyoftheirrespectiveowners. Indexer:SharonShock Proofreader:GeneRedding LibraryofCongressControlNumber:2008902386 ISBN-13:978-1-59863-536-2 ISBN-10:1-59863-536-0 eISBN-10:1-59863-699-5 CourseTechnology 25ThomsonPlace Boston,MA02210 USA CengageLearningisaleadingproviderofcustomizedlearningsolutions withofficelocationsaroundtheglobe,includingSingapore,theUnited Kingdom,Australia,Mexico,Brazil,andJapan.Locateyourlocalofficeat: international.cengage.com/region CengageLearningproductsarerepresentedinCanadabyNelson Education,Ltd. Foryourlifelonglearningsolutions,visitcourseptr.com Visitourcorporatewebsiteatcengage.com Printed in Canada 1 2 3 4 5 6 7 11 10 09 To my mother and father for always being there, and to my wonderful children, Alexander, William, and Molly, and my beautiful wife, Mary. Acknowledgments ThereareanumberofindividualstowhomIowemanythanksfortheirhelpand assistance in the development of this book. For starters I need to thank Mitzi Koontzwhoservedasthebook’sacquisitionseditor.Specialthanksalsogooutto Jenny Davidson for serving as the book’s project editor. I also want to thank Parker Hiquet and Hannah Wittig for all the valuable input and advice. In addition, I would like to thank everyone else at Cengage Learning for all their hard work. Special thanks to the Scratch development team at the MIT Media Lab for providing such an excellent programming langauge and website. About the Author Jerry Lee Ford, Jr. is an author, educator, and an IT professional with over 18years’experienceininformationtechnology,includingrolesasanautomation analyst, technical manager, technical support analyst, automation engineer, and security analyst. He is the author of 24 other books and co-author of two additionalbooks.HispublishedworksincludeAppleScriptStudioProgramming for the Absolute Beginner, Microsoft Windows PowerShell Programming for the Absolute Beginner, Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner, Microsoft VBScript ProfessionalProjects,Microsoft Windows Shell Scripting and WSH Administrator’s Guide, Microsoft Windows Shell Script Programming for the Absolute Beginner, Learn JavaScript in a Weekend, Second Edition,andMicrosoftWindowsXPProfessionalAdministrator’sGuide.Jerryhas a master’s degree in business administration from Virginia Commonwealth University in Richmond, Virginia, and he has over five years’ experience as an adjunct instructor teaching networking courses in information technology. Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv PART I SCRATCH BASICS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 1 Introducing Scratch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Getting to Know Scratch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Imagine—Program—Share! . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Scratch Uncovered . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Scratch’s Building Block Approach to Programming . . . . . . . . . . 6 Installing Scratch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Installing Java on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Installing Scratch on Windows . . . . . . . . . . . . . . . . . . . . . . . . . 10 Installing Scratch on Mac OS X. . . . . . . . . . . . . . . . . . . . . . . . . 12 Creating Your First Scratch Application . . . . . . . . . . . . . . . . . . . . . 14 Creating a New Scratch Project . . . . . . . . . . . . . . . . . . . . . . . . 15 Changing Sprite Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Adding Code Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Saving Your Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Joining Scratch’s Global Community . . . . . . . . . . . . . . . . . . . . . . . 19 Sharing Your Application Projects . . . . . . . . . . . . . . . . . . . . . . 21 Registering with the Scratch Website . . . . . . . . . . . . . . . . . . . . 23 Keeping In Touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 vi Contents vii Chapter 2 Getting Comfortable with the Scratch Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Getting Comfortable with the Scratch IDE. . . . . . . . . . . . . . . . . . . 28 Getting Familiar with Menu Bar Commands . . . . . . . . . . . . . . . 29 Running Scratch Applications on the Stage. . . . . . . . . . . . . . . . 31 Running Applications in Presentation Mode . . . . . . . . . . . . . . . 33 Controlling Application Execution . . . . . . . . . . . . . . . . . . . . . . 33 Working with the Sprite List . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Generating New Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Tracking Mouse Pointer Location . . . . . . . . . . . . . . . . . . . . . . . 36 Working with the Scratch Toolbar . . . . . . . . . . . . . . . . . . . . . . 37 Switching Between Code Block Groups. . . . . . . . . . . . . . . . . . . 38 Getting Comfortable with the Scripts Area . . . . . . . . . . . . . . . . 38 Keeping Project Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Creating New Sprites Using Scratch’s Paint Editor . . . . . . . . . . . . . 46 Examining the Drawing Canvas . . . . . . . . . . . . . . . . . . . . . . . . 46 Working with the Toolbar and Options Area . . . . . . . . . . . . . . 47 Working with Button Controls . . . . . . . . . . . . . . . . . . . . . . . . . 49 Specifying Color Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Configuring a Sprite’s Rotation Center . . . . . . . . . . . . . . . . . . . 50 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Chapter 3 A Review of the Basic Components of Scratch Projects . . 53 Working with Blocks and Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Three Basic Types of Scratch Blocks . . . . . . . . . . . . . . . . . . . . . . . . 55 Working with Stack Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Working with Hat Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Working with Reporter Blocks . . . . . . . . . . . . . . . . . . . . . . . . . 57 Keeping an Eye Out with Monitors . . . . . . . . . . . . . . . . . . . . . . . . 58 Eight Categories of Scratch Blocks. . . . . . . . . . . . . . . . . . . . . . . . . 60 Moving Objects Around the Drawing Canvas . . . . . . . . . . . . . . 61 Changing Object Appearance. . . . . . . . . . . . . . . . . . . . . . . . . . 62 Making Some Noise. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Drawing Lines and Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Looping, Conditional Logic, and Event Programming . . . . . . . . 65 Sensing Sprite Location and Environmental Input . . . . . . . . . . . 65 Working with Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Storing and Retrieving Data. . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Getting Help with Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 viii Contents Chapter 4 Mr. Wiggly’s Dance—A Quick Scratch Project . . . . . . . . . 73 Programming with Scratch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Creating the Mr. Wiggly’s Dance Application . . . . . . . . . . . . . . . . 75 Step 1: Creating a New Scratch Project . . . . . . . . . . . . . . . . . . . 77 Step 2: Adding a Background to the Stage. . . . . . . . . . . . . . . . 77 Step 3: Adding and Removing Sprites. . . . . . . . . . . . . . . . . . . . 80 Step 4: Adding Mr. Wiggly’s Music. . . . . . . . . . . . . . . . . . . . . . 82 Step 5: Playing the Dance Music. . . . . . . . . . . . . . . . . . . . . . . . 85 Step 6: Making Mr. Wiggly Dance . . . . . . . . . . . . . . . . . . . . . . 87 Step 7: Saving and Executing Your New Scratch Application . . . 90 Distributing Scratch Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Distributing Scratch Applications to Windows Computers . . . . . 92 Distributing Scratch Applications to Mac OS X Computers. . . . . 93 Instructions for Executing Your Application from a CD-ROM. . . 93 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 PART II LEARNING HOW TO WRITE SCRATCH PROGRAMS . . . . . . 95 Chapter 5 Moving Things Around . . . . . . . . . . . . . . . . . . . . . . . . . 97 Working with Motion Code Blocks . . . . . . . . . . . . . . . . . . . . . . . . 98 Moving and Rotating Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Setting Sprite Direction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Repositioning a Sprite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Changing Sprite Coordinates. . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Bouncing Sprites Around the Stage. . . . . . . . . . . . . . . . . . . . . . . 105 Keeping Track of Sprite Coordinates and Direction . . . . . . . . . . . 106 Taking Advantage of Scratch Cards . . . . . . . . . . . . . . . . . . . . . . . 107 Creating the Virtual Scratch Fish Tank. . . . . . . . . . . . . . . . . . . . . 110 Step 1: Creating a New Scratch Project . . . . . . . . . . . . . . . . . . 111 Step 2: Adding a Background to the Stage. . . . . . . . . . . . . . . 111 Step 3: Adding and Removing Sprites. . . . . . . . . . . . . . . . . . . 111 Step 4: Adding a Suitable Audio File to the Stage . . . . . . . . . 112 Step 5: Playing the Audio File . . . . . . . . . . . . . . . . . . . . . . . . 113 Step 6: Animating the Swimming of the Fish . . . . . . . . . . . . . 113 Step 7: Saving and Executing Your New Scratch Application . . 116 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Chapter 6 Sensing Sprite Position and Controlling Environmental Settings . . . . . . . . . . . . . . . . . . . . . . . . 119 Working with Sensing Code Blocks . . . . . . . . . . . . . . . . . . . . . . . 120 Retrieving Mouse Button and Coordinate Status . . . . . . . . . . . . . 121 Determining when Keys Are Pressed . . . . . . . . . . . . . . . . . . . . . . 123 Contents ix Determining when Sprites Collide with Other Objects . . . . . . . . . 124 Determining Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Working with a Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Retrieving Stage and Sprite Data . . . . . . . . . . . . . . . . . . . . . . . . 129 Retrieving Audio Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Code Blocks That Work with Sensor Boards. . . . . . . . . . . . . . . . . 131 Creating the Family Scrapbook Application . . . . . . . . . . . . . . . . . 132 Step 1: Creating a New Scratch Project . . . . . . . . . . . . . . . . . . 132 Step 2: Adding and Removing Sprites and Costumes. . . . . . . . 133 Step 3: Adding a Suitable Audio File to the Stage . . . . . . . . . 134 Step 4: Playing the Audio File . . . . . . . . . . . . . . . . . . . . . . . . 135 Step 5: Displaying the Photographs . . . . . . . . . . . . . . . . . . . . 135 Step 6: Saving and Executing Your New Scratch Application . . 136 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Chapter 7 Storing and Retrieving Data. . . . . . . . . . . . . . . . . . . . . 139 Learning How to Work with Application Data. . . . . . . . . . . . . . . 139 Storing Data in Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Creating Scratch Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Assigning Variables to Sprites and the Stage . . . . . . . . . . . . . 142 Assigning Names to Your Variables . . . . . . . . . . . . . . . . . . . . 143 Understanding Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . 143 Deleting Variables when They Are No Longer Needed. . . . . . . . . 145 Accessing Variables Belonging to Other Sprites . . . . . . . . . . . . . . 145 Working with Variable Monitors. . . . . . . . . . . . . . . . . . . . . . . . . 147 Two Quick Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Developing the Basketball Quiz Project . . . . . . . . . . . . . . . . . . . . 148 Step 1: Creating a New Scratch Project . . . . . . . . . . . . . . . . . . 150 Step 2: Selecting an Appropriate Stage Background . . . . . . . . 150 Step 3: Adding and Removing Sprites. . . . . . . . . . . . . . . . . . . 150 Step 4: Adding Variables Required by the Application . . . . . . 151 Step 5: Adding Scripts to Button Sprites to Collect User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Step 6: Automating the Administration of the Quiz . . . . . . . . 154 Step 7: Saving and Executing Your New Application. . . . . . . . 157 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Chapter 8 Doing a Little Math . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Addition, Subtraction, Multiplication, and Division . . . . . . . . . . . 159 Understanding the Mathematical Order of Precedence . . . . . . . . 160 Generating a Random Number . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Description: