Scratch tm Programming for Teens
|
|
|
- Damian Quinn
- 10 years ago
- Views:
Transcription
1
2 Scratch tm Programmig for Tees Jerry Lee Ford, Jr. Course Techology PTR A part of Cegage Learig Australia. Brazil. Japa. Korea. Mexico. Sigapore. Spai. Uited Kigdom. Uited States
3 Scratch TM Programmig for Tees Jerry Lee Ford, Jr. Publisher ad Geeral Maager, Course Techology PTR: Stacy L. Hiquet Associate Director of Marketig: Sarah Paella Maager of Editorial Services: Heather Talbot Marketig Maager: Mark Hughes Acquisitios Editor: Mitzi Kootz Project Editor: Jey Davidso Techical Reviewer: Parker Hiquet Tee Reviewer: Haah Wittig PTR Editorial Services Coordiator: Eri Johso Iterior Layout Tech: ICC Macmilla Ic. Cover Desiger: Mike Taamachi CD-ROM Producer: Brado Peticuff Idexer: Sharo Shock Proofreader: Gee Reddig 2009 Course Techology, a part of Cegage Learig. ALL RIGHTS RESERVED. No part of this work covered by the copyright herei may be reproduced, trasmitted, stored, or used i ay form or by ay meas graphic, electroic, or mechaical, icludig but ot limited to photocopyig, recordig, scaig, digitizig, tapig, Web distributio, iformatio etworks, or iformatio storage ad retrieval systems, except as permitted uder Sectio 107 or 108 of the 1976 Uited States Copyright Act, without the prior writte permissio of the publisher. For product iformatio ad techology assistace, cotact us at Cegage Learig Customer & Sales Support, For permissio to use material from this text or product, submit all requests olie at cegage.com/permissios Further permissios questios ca be ed to [email protected] Scratch is a project of the Lifelog Kidergarte group at the MIT Media Lab. Scratch, the Scratch logo, ad the Scratch cat are trademarks of the Massachusetts Istitute of Techology. All other trademarks are the property of their respective owers. Library of Cogress Cotrol Number: ISBN-13: ISBN-10: eisbn-10: Course Techology 25 Thomso Place Bosto, MA USA Cegage Learig is a leadig provider of customized learig solutios with office locatios aroud the globe, icludig Sigapore, the Uited Kigdom, Australia, Mexico, Brazil, ad Japa. Locate your local office at: iteratioal.cegage.com/regio Cegage Learig products are represeted i Caada by Nelso Educatio, Ltd. For your lifelog learig solutios, visit courseptr.com Visit our corporate website at cegage.com Prited i Caada
4 To my mother ad father for always beig there, ad to my woderful childre, Alexader, William, ad Molly, ad my beautiful wife, Mary.
5 Ackowledgmets There are a umber of idividuals to whom I owe may thaks for their help ad assistace i the developmet of this book. For starters I eed to thak Mitzi Kootz who served as the book s acquisitios editor. Special thaks also go out to Jey Davidso for servig as the book s project editor. I also wat to thak Parker Hiquet ad Haah Wittig for all the valuable iput ad advice. I additio, I would like to thak everyoe else at Cegage Learig for all their hard work. Special thaks to the Scratch developmet team at the MIT Media Lab for providig such a excellet programmig lagauge ad website.
6 About the Author Jerry Lee Ford, Jr. is a author, educator, ad a IT professioal with over 18 years experiece i iformatio techology, icludig roles as a automatio aalyst, techical maager, techical support aalyst, automatio egieer, ad security aalyst. He is the author of 24 other books ad co-author of two additioal books. His published works iclude AppleScript Studio Programmig for the Absolute Begier, Microsoft Widows PowerShell Programmig for the Absolute Begier, Microsoft Visual Basic 2005 Express Editio Programmig for the Absolute Begier, Microsoft VBScript Professioal Projects, Microsoft Widows Shell Scriptig ad WSH Admiistrator s Guide, Microsoft Widows Shell Script Programmig for the Absolute Begier, Lear JavaScript i a Weeked, Secod Editio, ad Microsoft Widows XP Professioal Admiistrator s Guide.Jerryhas a master s degree i busiess admiistratio from Virgiia Commowealth Uiversity i Richmod, Virgiia, ad he has over five years experiece as a adjuct istructor teachig etworkig courses i iformatio techology.
7 Cotets Itroductio xiv PART I SCRATCH BASICS Chapter 1 Itroducig Scratch Gettig to Kow Scratch Imagie Program Share! Scratch Ucovered Scratch s Buildig Block Approach to Programmig Istallig Scratch Istallig Java o Widows Istallig Scratch o Widows Istallig Scratch o Mac OS X Creatig Your First Scratch Applicatio Creatig a New Scratch Project Chagig Sprite Attributes Addig Code Blocks Savig Your Work Joiig Scratch s Global Commuity Sharig Your Applicatio Projects Registerig with the Scratch Website Keepig I Touch Summary vi
8 Cotets vii Chapter 2 Gettig Comfortable with the Scratch Developmet Eviromet Gettig Comfortable with the Scratch IDE Gettig Familiar with Meu Bar Commads Ruig Scratch Applicatios o the Stage Ruig Applicatios i Presetatio Mode Cotrollig Applicatio Executio Workig with the Sprite List Geeratig New Sprites Trackig Mouse Poiter Locatio Workig with the Scratch Toolbar Switchig Betwee Code Block Groups Gettig Comfortable with the Scripts Area Keepig Project Notes Creatig New Sprites Usig Scratch s Pait Editor Examiig the Drawig Cavas Workig with the Toolbar ad Optios Area Workig with Butto Cotrols Specifyig Color Settigs Cofigurig a Sprite s Rotatio Ceter Summary Chapter 3 A Review of the Basic Compoets of Scratch Projects.. 53 Workig with Blocks ad Stacks Three Basic Types of Scratch Blocks Workig with Stack Blocks Workig with Hat Blocks Workig with Reporter Blocks Keepig a Eye Out with Moitors Eight Categories of Scratch Blocks Movig Objects Aroud the Drawig Cavas Chagig Object Appearace Makig Some Noise Drawig Lies ad Shapes Loopig, Coditioal Logic, ad Evet Programmig Sesig Sprite Locatio ad Evirometal Iput Workig with Numbers Storig ad Retrievig Data Gettig Help with Code Blocks Summary
9 viii Cotets Chapter 4 Mr. Wiggly s Dace A Quick Scratch Project Programmig with Scratch Creatig the Mr. Wiggly s Dace Applicatio Step 1: Creatig a New Scratch Project Step 2: Addig a Backgroud to the Stage Step 3: Addig ad Removig Sprites Step 4: Addig Mr. Wiggly s Music Step 5: Playig the Dace Music Step 6: Makig Mr. Wiggly Dace Step 7: Savig ad Executig Your New Scratch Applicatio Distributig Scratch Projects Distributig Scratch Applicatios to Widows Computers Distributig Scratch Applicatios to Mac OS X Computers Istructios for Executig Your Applicatio from a CD-ROM Summary PART II LEARNING HOW TO WRITE SCRATCH PROGRAMS Chapter 5 Movig Thigs Aroud Workig with Motio Code Blocks Movig ad Rotatig Sprites Settig Sprite Directio Repositioig a Sprite Chagig Sprite Coordiates Boucig Sprites Aroud the Stage Keepig Track of Sprite Coordiates ad Directio Takig Advatage of Scratch Cards Creatig the Virtual Scratch Fish Tak Step 1: Creatig a New Scratch Project Step 2: Addig a Backgroud to the Stage Step 3: Addig ad Removig Sprites Step 4: Addig a Suitable Audio File to the Stage Step 5: Playig the Audio File Step 6: Aimatig the Swimmig of the Fish Step 7: Savig ad Executig Your New Scratch Applicatio Summary Chapter 6 Sesig Sprite Positio ad Cotrollig Evirometal Settigs Workig with Sesig Code Blocks Retrievig Mouse Butto ad Coordiate Status Determiig whe Keys Are Pressed
10 Cotets ix Determiig whe Sprites Collide with Other Objects Determiig Distace Workig with a Timer Retrievig Stage ad Sprite Data Retrievig Audio Data Code Blocks That Work with Sesor Boards Creatig the Family Scrapbook Applicatio Step 1: Creatig a New Scratch Project Step 2: Addig ad Removig Sprites ad Costumes Step 3: Addig a Suitable Audio File to the Stage Step 4: Playig the Audio File Step 5: Displayig the Photographs Step 6: Savig ad Executig Your New Scratch Applicatio Summary Chapter 7 Storig ad Retrievig Data Learig How to Work with Applicatio Data Storig Data i Variables Creatig Scratch Variables Assigig Variables to Sprites ad the Stage Assigig Names to Your Variables Uderstadig Variable Scope Deletig Variables whe They Are No Loger Needed Accessig Variables Belogig to Other Sprites Workig with Variable Moitors Two Quick Examples Developig the Basketball Quiz Project Step 1: Creatig a New Scratch Project Step 2: Selectig a Appropriate Stage Backgroud Step 3: Addig ad Removig Sprites Step 4: Addig Variables Required by the Applicatio Step 5: Addig Scripts to Butto Sprites to Collect User Iput Step 6: Automatig the Admiistratio of the Quiz Step 7: Savig ad Executig Your New Applicatio Summary Chapter 8 Doig a Little Math Additio, Subtractio, Multiplicatio, ad Divisio Uderstadig the Mathematical Order of Precedece Geeratig a Radom Number
11 x Cotets Compariso Operatios Performig Logical Comparisos Roudig Numbers ad Retrievig Remaiders Workig with Built-i Mathematical Fuctios Developig the Number Guessig Game Quiz Project Step 1: Creatig a New Scratch Project Step 2: Addig a Stage Backgroud Step 3: Addig ad Removig Sprites Step 4: Addig Variables Required by the Applicatio Step 5: Addig a Audio File to the Applicatio Step 6: Addig Scripts to Capture Player Iput Step 7: Processig Player Guesses Step 8: Savig ad Executig Your New Scratch Applicatio Summary Chapter 9 Coditioal ad Repetitive Logic Itroducig Scratch Cotrol Blocks Evet Programmig Pausig Script Executio Executig Loops Sedig ad Receivig Broadcasts Coditioal Programmig Logic Nestig Coditioal Cotrol Code Blocks Prevetig Edless Loops Termiatig Script Executio Developig the Ball Chase Game Step 1: Creatig a New Scratch Project Step 2: Addig ad Removig Sprites Step 3: Addig Variables Required by the Applicatio Step 4: Addig a Audio File to the Applicatio Step 5: Addig a Script to Cotrol Ball Movemet Step 6: Addig Scripts That Display Game Over Messages Step 7: Addig Scripts Needed to Cotrol ad Coordiate Game Play Step 8: Savig ad Executig Your Scratch Project Summary Chapter 10 Chagig the Way Sprites Look ad Behave Chagig Sprite Costumes ad Backgrouds Chagig Sprite Costumes Chagig a Stage s Backgroud Costumes
12 Cotets xi Makig Sprites Talk ad Thik Applyig Special Effects to Costumes ad Backgrouds Chagig a Sprite s Size Makig Sprites Appear ad Disappear Determiig What Happes whe Two Sprites Overlap Developig the Crazy Eight Ball Game Step 1: Creatig a New Scratch Project Step 2: Addig ad Removig Sprites Step 3: Addig a Variable Required by the Applicatio Step 4: Addig a Audio File to the Applicatio Step 5: Creatig a Script to Cotrol the Display of the 8 i the Eight Ball Step 6: Addig the Programmig Logic Needed to Cotrol the Eight Ball Step 7: Savig ad Executig Your Scratch Project Summary Chapter 11 Spicig Thigs Up with Souds Playig Souds Play a Drum Playig Musical Notes Cofigurig Audio Volume Settig ad Chagig Tempo Creatig the Family Picture Movie Step 1: Creatig a New Scratch Project Step 2: Addig ad Removig Sprites ad Backgrouds Step 3: Addig a Variable Required by the Applicatio Step 4: Addig a Audio File to the Applicatio Step 5: Developig the Applicatio s Programmig Logic Step 6: Savig ad Executig Your Scratch Project Summary Chapter 12 Drawig Lies ad Shapes Clearig the Stage Area Drawig with the Pe Settig Pe Color Chagig Pe Shade Workig with Differet Size Pes
13 xii Cotets Stampig a Istace of a Costume o the Stage Creatig the Doodle Drawig Applicatio Step 1: Creatig a New Scratch Project Step 2: Addig ad Removig Sprites Step 3: Creatig Scripts Used to Cotrol the Doodle Drawig Applicatio Step 4: Savig ad Executig Your Scratch Project Summary PART III ADVANCED TOPICS Chapter 13 Sharig Your Scratch Projects over the Iteret Ruig Scratch Applicatios o the Iteret Registerig with the Scratch Website Uploadig Your Scratch Applicatios Viewig ad Orgaizig Your Applicatios Olie Ruig Your Applicatio Addig Commets Addig Tags Creatig Galleries Removig Projects Updatig Your Projects Other Scratch Website Features Dowloadig Other People s Projects Summary Chapter 14 Collectig Exteral Iput Usig a Scratch Board Iteractig with the Real World Buyig a Scratch Board Istallig Your Scratch Board Usig the Sesor Block to Iteract with Your Scratch Board Collectig Iput Usig the Slider Cotrol Usig the Butto Cotrol to Iitiate Actio Reactig to Light Respodig to Soud Measurig Electrical Resistace Keepig a Watchful Eye o Sesor Data Summary
14 Cotets xiii Chapter 15 Fidig ad Fixig Program Errors Dealig with Applicatio Errors Uderstadig Sytax Errors Keepig a Eye Out for Logical Errors Trackig Dow Ru-Time Errors Debuggig Your Scratch Applicatios Basic Debuggig Techiques Ruig Your Applicatio i Sigle Steppig Mode Watch Out whe Removig Souds ad Sprites Gettig Help Referrig to Scratch s Olie Help Gettig Help for Idividual Code Blocks Gettig Help from Other Scratch Programmers Summary PART IV APPENDICES Appedix A What s o the Compaio CD? Appedix B What Next? Glossary Idex
15 Itroductio Welcome to Scratch Programmig for Tees! Scratch is a programmig laguage developed by the MIT Media Lab for the purpose of teachig programmig to tees ad other first-time programmers. Scratch is a ew programmig laguage, iitially released i May Scratch supports the developmet of computer games, iteractive stories, graphic artwork ad computer aimatio, ad all sorts of other multimedia projects. Scratch allows ew programmers to create programs by sappig together blocks. Scratch cosists of a programmig laguage made up of differet blocks ad a easy to lear graphical developmet eviromet that icludes a pait applicatio for creatig graphics ad built-i soud editig capabilities. Scratch also comes with huge collectios of sample applicatios as well as graphics ad soud files, all of which you ca use to create your ow Scratch projects. As demostrated i Figure A.1, Scratch programs are made up of graphical blocks, which are sapped together. Scratch blocks resemble puzzle pieces i the way that they sap together. Scratch blocks ca oly be sapped together i ways that make sese, prevetig ew programmers from usig them i ivalid combiatios. I this way, Scratch eforces proper programmig sytax ad esures that ew programmers lear the proper way to assemble ad formulate programmig logic. Scratch s developmet was ispired by the method that hip-hop DJs use to mix ad scratch records to create ew ad uique music. I Scratch, ew programmers xiv
16 Itroductio xv Figure A.1 Script blocks are used as the basis for writig scripts that help brig applicatios to life. are able to create ew applicatio projects that icorporate pre-built code blocks, graphics, ad soud files i all kids of ew combiatios. Scratch lets programmers modify applicatios o the fly, allowig chages to be made eve while Scratch applicatios are ruig. The result is a iteractive, real-time programmig eviromet that ecourages experimetatio ad learig. This book s primary goal is to teach you everythig you eed to kow to lear the basics of computer programmig with Scratch. To help accomplish this goal, this book will emphasize learig by doig through the developmet of a series of fu ad iterestig exercises. Why Scratch? Scratch provides everythig eeded to begi developig computer games, multimedia presetatios, iteractive stories, graphic artwork, ad computer aimatio. Scratch ca be used to play digital music ad soud effects. Scratch s buildig block approach to programmig sets it apart from other programmig laguages. This makes Scratch easier to lear. Ad yet Scratch provides plety of programmig power, allowig you to build very powerful applicatio projects. If you aspire to oe day become a professioal programmer, you will fid that Scratch provides everythig eeded to build a foudatio from which you ca make the trasitio. Scratch also packs all of the programmig power ad puch eeded to satisfy the programmig eeds of most computer ethusiasts ad hobbyists. Who Should Read This Book? Scratch Programmig for Tees is desiged to provide all of the istructio that a first-time programmer requires to quickly get up ad ruig. Previous programmig experiece will certaily be helpful, but it is by o meas a
17 xvi Itroductio requiremet of this book. This book makes o assumptios about your computer backgroud other tha that you are comfortable workig with oe of the operatios systems supported by Scratch. This book provides everythig you eed to get started with Scratch. Before you kow it, you will be creatig all kids of projects, icorporatig graphics, soud, ad aimatio. As you lear how to program with Scratch, you will lear programmig priciples ad techiques that you ca later apply to other programmig laguages. As such, you will be able to apply what you lear about programmig with Scratch to other programmig laguages like Microsoft Visual Basic ad AppleScript. What You Need to Begi Obviously, the first thig you eed is a copy of Scratch. Scratch is available for free dowload at the Scratch website located at You ca also dowload a copy from the CD icluded i the back of the book. You also eed good istructio, which you will fid i this book. I additio to Scratch ad this book, you eed a computer ruig a supported operatig system, which also meets Scratch s miimum system requiremets. Supported Operatig Systems Scratch ca be ru o computers usig either Microsoft or Macitosh operatig systems. Specifically, Scratch ca be istalled o a computer ruig ay of the followig operatig systems. Microsoft Widows 98/ME Microsoft Widows NT/2000 Microsoft Widows XP/Vista Mac OS X Versio 10.3 or higher All of the figures ad examples i this book will be show usig Scratch ruig o computers usig either Microsoft Vista or Mac OS X If you are goig to be workig with Scratch o a differet versio of Widows or Mac OS X, you may otice small differeces i the way thigs look. However, all major Scratch features ad fuctioality should work the same ad you
18 Itroductio xvii should ot have ay problems followig alog with the istructio provided i this book. Note There is o official Liux versio of Scratch curretly available. However, members of the Scratch commuity have created differet Scratch implemetatios for Liux. A example of oe such implemetatio is available at Miimum System Requiremets Scratch does ot impose ay additioal hardware requiremets over ad above those required by the operatig system. However, as Table A.1 shows, Scratch does impose scree resolutio ad disk space requiremets, which must be met for Scratch to ru. To work with Scratch, you must be able to display its graphical iterface, also referred to as its itegrated developmet eviromet or IDE. This iterface requires that the computer s scree resolutio be set to or higher. Aythig less ad part of the iterface will disappear off the scree. Scratch comes packed with all kids of graphics ad audio files that you ca use whe creatig ew Scratch projects. As a result, your computer must have at least a extra 120 MB of hard disk space i order to istall Scratch. Note Oe of the really eat thigs about Scratch is the ability to share Scratch applicatio projects with others o the Iteret at the Scratch website (Scratch.mit.edu). To participate i this experiece, your computer eeds to have Java istalled. Mac OS X comes with Java pre-istalled. However, by default, Microsoft Widows does ot. So, if you are a Widows user ad you have ot yet istalled Java o your computer, you ca do so by visitig Table A.1 Scratch Miimum System Requiremets Requiremet Scree Resolutio Hard Disk Recommeded (16-bit color) 120 MB
19 xviii Itroductio Of course, Scratch s miimum hardware requiremets are just that, miimum requiremets. If your computer s memory ad processor exceed the miimum requiremets of the operatig system, thigs will ru a lot faster ad you will be a lot happier. I additio, you will eed extra hard drive space beyod the 120 MB miimum required to istall Scratch to have a place to store your creatios. Scratch lets you create projects that icorporate the use of soud, both as iput ad output. To take advatage of this feature, your computer will eed both speakers ad a microphoe. How This Book Is Orgaized Scratch Programmig for Tees is orgaized ito four parts. This book was writte with the expectatio that you will read it sequetially, from cover to cover. However, if you have some previous programmig experiece, you may istead wat to jump aroud a bit, focusig o topics that iterest you the most. Part I of this book is made up of four chapters that provide a itroductio to Scratch ad its developmet eviromet. You will also lear about the differet compoets that make up Scratch projects ad the lear how to create ad execute Scratch projects. Part II cosists of eight chapters, each of which is desiged to provide istructio o how to work with differet types of Scratch blocks. You will lear how to use blocks that move thigs aroud, store ad retrieve data, as well as perform math ad coditioal ad repetitive logic. You will also lear how to itegrate soud ad draw lies ad shapes. Part III of this book is made up of three chapters, each of which focuses o a differet advaced topic. These topics iclude learig how to share your Scratch projects with others o the Iteret, how to create Scratch projects that use the Scratch Board, ad how to fid ad fix program errors that prevet your Scratch projects from workig like you wat them to. Part IV is made up of two appedices ad a glossary. The first appedix reviews the list of sample Scratch projects that you will lear how to develop as you make your way through this book. The secod appedix provides a list of websites ad readig materials that you will wat to explore to cotiue learig more about Scratch ad to further your programmig kowledge.
20 Itroductio xix Covetios Used i This Book Oe of the primary objectives of this book is for it to be easy to read ad uderstad. To help support this objective, a umber of simple covetios have bee used throughout the book to highlight critical iformatio ad help emphasize specific poits. These covetios are briefly described below. Italics. Key terms that you will wat to uderstad ad remember are highlighted usig italics the first time that they are istructed. So remember, aytime you see a term i italics, take a extra momet to thik about it ad uderstad its meaig or purpose. Note Notes are used to provide additioal iformatio about a topic, feature, or idea to better help you uderstad its impact or implicatios. Tip Tips are used to poit out programmig shortcuts that will help make you a better ad more efficiet programmer. Cautio Cautios are used to idetify areas where you are likely to ru ito problems ad the provide advice o how to deal with the problem or prevet problems from occurrig, makig you a better, more efficiet, ad much happier programmer.
21 This page itetioally left blak
22 Part I Scratch Basics
23 This page itetioally left blak
24 chapter 1 Itroducig Scratch Scratch is a programmig laguage developed to help youg people betwee the ages of 8 ad 16 lear 21st cetury skills by developig computer programs. The developmet of Scratch was ispired by the scratchig process that DJs use to create ew souds ad music by rubbig old-style viyl records back ad forth o record turtables, creatig a ew ad distictively differet soud out of somethig that already exists. I similar fashio, Scratch applicatio projects mix together graphics ad souds i order to use them i ew ad differet ways. To help get you started with Scratch programmig, this chapter provides a overview of the laguage ad reviews the steps that you eed to follow to get up ad ruig quickly. The major topics covered i this chapter iclude: A review of Scratch s capabilities ad uses Istructio o how to istall Scratch o both Microsoft Widows ad Mac OS X A discussio of the beefits of joiig Scratch s global commuity A demostratio of how to create ad execute your first Scratch applicatio project 3
25 4 Chapter 1 Itroducig Scratch Gettig to Kow Scratch With traditioal computer ad Iteret applicatios, users are limited to workig with applicatios i the way the programmers who developed the applicatios desiged. Scratch turs thigs aroud by lettig users become programmers. Scratch is desiged to meet the eeds of youg people betwee 8 ad 16, helpig to itroduce them to computer techology ad to improve their learig skills while at the same time facilitatig creativity ad persoal expressio. May people regard computer programmig as a mysterious ad complex process that requires advaced techical traiig ad educatio. This is a misperceptio. Programmig laguages like BASIC have bee aroud for decades ad were developed expressly for the purpose of teachig first-time programmers how to program. I recet years, a ew crop of programmig laguages has appeared, specifically geared towards helpig childre ad studets lear to program. Oe of the very best ad ewest of these laguages is Scratch. Scratch is a visual programmig laguage that is made up of a graphic iterface that supports applicatio developmet i which ew projects are created by mixig together images, soud, ad video uder the cotrol of scripts, which specify the applicatio s programmig logic. Scripts are created by sappig blocks together, much i the same way that Lego blocks are sapped together to create all sorts of uique creatios. Each block represets a differet commad or actio that tells the applicatio how to execute. Scratch also provides programmers with access to all kids of media, icludig graphics ad souds as well as tools that ca be used to create ew graphics ad soud files. Note Scratch is also beig istalled o all XO laptops, as part of the Oe Laptop Per Child Project, which is a program desiged to produce ad distribute iexpesive laptop computers to childre i developig coutries aroud the world to help their educatio ad ulock their potetial. Scratch is a iterpreted programmig laguage. This meas that applicatio projects are ot precompiled (tured ito executable code that ca be ru as a stad-aloe applicatio) before their executio. Istead, the code blocks that make up Scratch applicatio projects are iterpreted ad processed each time the applicatio project is executed. Scratch is also a dyamic programmig laguage.
26 Gettig to Kow Scratch 5 It allows chages to be made to applicatio projects eve while the projects are executig. As such, Scratch lets programmers experimet by makig applicatio chages o the fly i order to see what type of effect the chages may have o the applicatio s executio. Imagie Program Share! Scratch s sloga is Imagie Program Share! It is desiged to ecourage tees creativity by providig them with a easy to lear yet powerful programmig eviromet i which they ca uleash the power of their imagiatio. Scratch ecourages ad facilitates the developmet of applicatio projects usig a mixture of media, graphics, soud, ad video i order to create somethig ew. Scratch provides ew programmers with everythig eeded to create ad execute ew applicatio projects. Its programmig laguage is desiged to make it as easy as possible for ew programmers to jump i ad get their feet wet ad to receive immediate feedback o their progress. Scratch promotes a uderstadig of programmig cocepts, icludig coditioal ad iterative logic, evet programmig, the use of variables, mathematics, ad the use of graphics, ad soud effects. By learig to program with Scratch, ew programmers develop a uderstadig ad appreciatio of the desig process, from idea geeratio to program developmet, the testig ad debuggig ad the icorporatio of user feedback. People, especially kids, love to share, as demostrated through the amazig success of websites like YouTube, which allows people to share home video. Sharig is a fudametal part of the Scratch programmig experiece. Scratch applicatio projects ca ot oly be ru o the programmer s desktop but ca also be uploaded to the Scratch website, where they ca be viewed, executed olie, ad commeted o by other Scratch programmers from aroud the world. By postig their Scratch applicatio projects o the Scratch website, kids share their experieces ad lear from oe aother ad gai gratificatio ad cofidece from the experiece. Hit To share a applicatio project, Scratch programmers must provide the source code that makes the applicatio work. There is o way to keep the source code hidde.
27 6 Chapter 1 Itroducig Scratch Scratch Ucovered For your coveiece, a free trial copy of Scratch (versio 1.2.1) is available o this book s compaio CD-ROM. I additio, Scratch ca be dowloaded from the Scratch website located at Ulike may programmig laguages such as Microsoft Visual Basic or C++, Scratch is a ope source project. What this meas is that all of the source code that makes up the Scratch programmig laguage is freely available. I fact, if you wat, you ca dowload a copy of the source code for Scratch at source. Note Scratch was developed usig aother programmig laguage kow as Squeak. Squeak is a crossplatform programmig laguage, meaig that it ca be used to develop applicatios o may differet computer operatig systems. By selectig Squeak as the programmig laguage used to create Scratch, Scratch s developmet team esured that they would be able to create ad execute Scratch o differet operatig systems, icludig Microsoft Widows ad Mac OS X. If you are curious, you ca lear more about Squeak by visitig Examples of other ope source programmig laguages iclude Ruby ad Perl. However, ulike these programmig laguages developed by a commuity of programmers workig together collectively, Scratch was developed as a closed developmet project. This meas that all Scratch developmet is performed by the Lifelog Kidergarte Group at MIT Media Lab. Scratch s Buildig Block Approach to Programmig Scratch is a ew programmig laguage, iitially released i March Scratch is differet from other programmig laguages like Visual Basic i that it does ot support a text-based approach to programmig, as demostrated here: //Excerpt from a Visual Basic applicatio If strcurretactio = "FillCircle" The Dim objcoordiates As Rectagle objcoordiates = _ New Rectagle(Math.Mi(objEd.X, objstart.x), _ Math.Mi(objEd.Y, objstart.y), _ Math.Abs(objEd.X - objstart.x), _ Math.Abs(objEd.Y - objstart.y)) Pick_Color_Ad_Draw("FillCircle", objcoordiates) Ed If
28 Gettig to Kow Scratch 7 Figure 1.1 A example of how programmig logic is outlied i a Scratch applicatio project. I text-based programmig laguages, code statemets are formulated by followig a complex set of sytax rules. Failure to precisely follow these rules whe writig statemets leads to sytax errors that prevet applicatios from ruig. Scratch, o the other had, uses a differet approach. Scratch applicatio projects are built by selectig ad sappig together graphical programmig blocks, as demostrated i Figure 1.1. By usig code blocks i place of complex program text statemets, Scratch sigificatly simplifies applicatio developmet while still makig use of the same basic programmig logic ad cocepts implemeted i other programmig laguages. As Figure 1.1 demostrates, each code block represets a differet commad or actio. Blocks fit together like pieces i a puzzle. You ca oly sap together blocks i ways that make sytactic sese, completely elimiatig sytax errors that proliferate i other programmig laguages. Some code blocks are cofigurable, allowig you to specify thigs like the umber of times a actio should execute, text that is to be displayed, or the color to be used whe displayig somethig o the scree. Despite its use of graphical code blocks, Scratch supports the same basic set of programmig techiques ad costructs as do other traditioal programmig laguages. For example, Scratch supports variables, coditioal ad iterative logic, ad evetdrive programmig. Scratch also supports the maipulatio of graphics ad the itegratio of soud ito applicatio projects.
29 8 Chapter 1 Itroducig Scratch Note Scratch is desiged for teachig first-time programmers how to program. To make the learig experiece as straightforward ad uderstadable as possible, the developers of Scratch have sometimes sacrificed programmig power ad features i favor of simplicity ad ease of learig. The goal of the Scratch developmet team is to promote learig ad ot to develop a programmig laguage capable of deliverig every advaced programmig feature required by professioal programmers. As a result, Scratch lacks some programmig features curretly supported i advaced programmig laguages. Istead, Scratch focuses o fudametal programmig cocepts to provide ew programmers with a foudatio upo which they ca later build, whe ad if they decide to move o to other programmig laguages. Istallig Scratch Before you ca use Scratch, you eed to istall it o your computer. The istallatio process varies, depedig o whether you use Microsoft Widows or Mac OS X. Istructios for istallig Scratch o both of these operatig systems are provided i the sectios that follow. You will fid the istallatio files eeded to istall Scratch o this book s compaio CD-ROM. Alteratively, you ca dowload a copy of Scratch from the Scratch website by executig the followig steps: 1. Go to ad click o the Dowload Scratch Now! lik. 2. The Dowload Scratch page appears. Fill i the optioal form to receive updates about Scratch. 3. Click o the Cotiue to Scratch Dowload butto. The web page show i Figure 1.2 displays. Click o the appropriate lik for your operatig system. The Widows dowload file is provided as a self-extractig executable amed ScratchIstaller.exe. The Mac OS X istallatio file is provided as a Mac OS X disk image file amed MacScratch.dmg. Both of these istallatio files are approximately 30MB i size, so to dowload them you will wat to use a broadbad Iteret coectio. Note There is o official Liux versio of Scratch curretly available. However, a user-adapted versio of Scratch, alog with istructios for istallig it, is available at scratch.html.
30 Istallig Scratch 9 Figure 1.2 Dowloadig either the Mac OS X or Widows versio of Scratch. Istallig Java o Widows While Mac OS X comes with Java already istalled, Widows does ot. Fortuately, istallig Java o Microsoft Widows is both free ad easy. To do so, go to as show i Figure 1.3 ad click o the Free Java Dowload butto. Oce the olie istallatio process begis, you will eed to complete the followig steps to fiish istallig Java: 1. After clickig o the Free Java Dowload butto, you may be prompted by a Widows security widow for permissio to allow the istallatio process to cotiue. If so, click o the Cotiue butto.
31 10 Chapter 1 Itroducig Scratch Figure 1.3 Java is required to view ad execute Scratch projects loaded oto the Scratch website. 2. Next, a widow will appear requestig permissio to begi the istallatio process. Click o the Istall butto to cotiue. 3. Fially, a Java Setup Wizard will appear, requirig that you accept the Java Licese Agreemet. Click o the Accept butto ad the follow the rest of the wizard s istructio to complete the istallatio process. Istallig Scratch o Widows Scratch istalls o Microsoft Widows like ay other Widows applicatio. The followig procedure outlies the steps ivolved i completig Scratch s istall process:
32 Istallig Scratch 11 Figure 1.4 Istallig Scratch o Microsoft Widows. 1. Double-click o the ScratchIstaller.exe file. 2. If prompted for cofirmatio, click o Ru to allow the istallatio process to begi. 3. If a security message displays, click o Allow to give permissio for the istallatio process to cotiue. 4. The Scratch Setup Wizard will the appear, as demostrated i Figure 1.4. Click o Next ad follow the istructios provided by the wizard to complete the istallatio process. 5. Oce the Scratch Setup Wizard has completed the istallatio process, you will eed to click o the Fiish butto to close the wizard. Scratch will the automatically start, as demostrated i Figure 1.5. I additio, a shortcut for Scratch will be added to the Widows desktop. Note I additio to beig able to start Scratch by clickig o its desktop shortcut ico, you ca click o Start > All Programs > the Scratch folder > ad the the Scratch ico.
33 12 Chapter 1 Itroducig Scratch Figure 1.5 Ruig Scratch o Microsoft Widows Vista. Istallig Scratch o Mac OS X To istall Scratch o Mac OS X, double-click o the MacScratch.dmg archive file to ope it. Iside you will see a folder amed Scratch. Drag ad drop the Scratch folder to your Applicatios folder (or to ay other locatio that you wat) to istall it. The cotets of the Scratch folder are show i Figure 1.6. To start Scratch ad begi workig with it, double-click o the Scratch ico, which is represeted as a cartoo image of a cat. Withi a few momets, the Scratch IDE should appear, as show i Figure 1.7.
34 Istallig Scratch 13 Figure 1.6 Istallig Scratch o Mac OS X. Figure 1.7 Ruig Scratch o Mac OS X 10.5.
35 14 Chapter 1 Itroducig Scratch Note The first time you start Scratch, Mac OS X may display a popup dialog widow promptig you for cofirmatio that you wat to ru Scratch, because it is a applicatio dowloaded from the Iteret. Click o the Ope butto to allow Scratch to start. This popup dialog widow will ot display upo subsequet startups. Creatig Your First Scratch Applicatio Scratch applicatio projects are made up of objects called sprites. A sprite is a two-dimesioal bitmap image draw o a trasparet backgroud. Sprites ca be moved aroud ad made to iteract with oe aother. Sprites cosist of three primary compoets, as outlied here: Scripts. Collectios of code blocks that outlie the programmig logic that cotrols the operatio of sprites. Costumes. Images that are used to display the sprite o a area of the Scratch IDE, referred to as the stage. Sprites ca cosist of ay umber of costumes. Souds. Soud effects that are played durig applicatio executio whe certai evets occur or as backgroud audio. A sprite s appearace ca be chaged by assigig it differet costumes. To move a sprite ad cotrol its behavior, you sap together code blocks to create scripts. Sprites ca have ay umber of scripts associated with them. Scripts ca be ru by double-clickig the code blocks that make them up, i which case each block i the script is executed i top-dow order. You ca also set thigs up so that scripts automatically ru whe various evets occur. For example, you ca cofigure script executio to occur whe a sprite is clicked or whe it iteracts with other sprites. Sprites are displayed ad iteract with oe aother o a stage. As such, sprites are ofte referred to as actors. Scratch s stage is located i the upper-right corer of its graphical iterface. Note Sprites ca be selected from a predefied collectio of graphic objects supplied with Scratch. They ca also be copied ad pasted from your hard drive or the Iteret or created usig Scratch s builti Pait Editor.
36 Creatig Your First Scratch Applicatio 15 Creatig a New Scratch Project Now that you are familiar with the basic compoets of sprites, let s sped a few miutes learig how to create your first Scratch applicatio project. All ew Scratch projects automatically cotai a sigle sprite, represetig a image of a kitte. By default, the sprite, amed Sprite1, does ot have ay scripts but does have two costumes ad two souds associated with it. Usig this sprite, let s create a Scratch applicatio project that makes the kitte meow ad say Hello World! whe clicked. The first step i creatig a ew Scratch applicatio is to click o the New butto located at the top of the Scratch IDE. I respose, Scratch will create a ew project, as show i Figure 1.8. As Figure 1.8 shows, the Scratch IDE is orgaized ito a umber of separate compoets. For starters, the code block area cotais code blocks, orgaized ito eight differet collectios. You will use selected code blocks to create a script that makes the kitte talk. Code Block Area Sprite Area Stage Sprite List Figure 1.8 Creatig a ew Scratch applicatio project.
37 16 Chapter 1 Itroducig Scratch To the right of the code block area is the sprite area. Iformatio about the curretly selected sprite is displayed at the top of this area. Just beeath this iformatio are three tabs, which are used to cotrol access to the scripts, costumes, ad souds belogig to the sprite. To the right of the sprite area is the stage, which curretly displays the default costume belogig to Sprite1. Just beeath the stage is the sprite list, which displays a list of all the sprites that make up the applicatio project. Note Chapter 2, Gettig Comfortable with the Scratch Developmet Eviromet, provides a detailed overview of all of the compoets that make up the Scratch IDE. Chagig Sprite Attributes The applicatio project that you are creatig is desiged to work with the default sprite. Rather tha use the sprite s default ame of Sprite1, let s assig it a more descriptive ame. To do so, overtype the text displayed at the top of the sprite area with the word Cat. Oce you chage the ame assiged to the sprite, the ame chage will automatically be reflected i the sprite list. If you look at the etry for the sprite i the sprite list, you should see a picture of the sprite, its ew ame, ad the umber of costumes curretly assiged to the sprite (you ca click o the Costumes tab at the top of the sprite area to view the sprite s costumes). Addig Code Blocks Now that you have chaged the ame of the sprite, it is time to add the code blocks required to make the cat meow ad say Hello World! Let s begi by clickig o the Soud butto located at the top of the code block area. This displays a collectio of code blocks that cotrol the playback of soud effects. Locate the code block labeled play soud ad drag ad drop it oto the sprite area, as show i Figure 1.9. By default, this code block is automatically set up to play a audio file that makes a meow soud. Next, click o the Looks butto located at the top of the code block area. This displays a collectio of code blocks that cotrol the appearace of a sprite. Locate the code block labeled say Hello! for 2 secs ad drag ad drop it oto the sprite area, as show i Figure By default, this code block displays a text strig iside a graphical bubble captio. This code block has two editable fields: a text field ad a umeric field. Sice the
38 Creatig Your First Scratch Applicatio 17 Figure 1.9 Usig a soud block to make the kitte meow. kitte is supposed to display the message Hello world! whe clicked, replace the text Hello! with Hello World!. As previously stated, you ca ru a script at ay time by double-clickig o it. To test this, double-click o oe of the two code blocks that you have added ad the watch the kitte o the stage, ad you ll hear it meow ad display its message. Rather tha havig to double-click o the script to make the kitte do its thig, let s set thigs up so that the kitte automatically meows ad talks wheever you click o it. This is accomplished by clickig o the Cotrol butto located at the top of the code block area ad the draggig ad droppig the cotrol block labeled whe Cat clicked o top of the two buttos you have already added to the sprite s script, as demostrated i Figure The whe Cat clicked block automatically saps i place as you move it toward the top of the script. With this block ow i place, click o the script file ad see
39 18 Chapter 1 Itroducig Scratch Figure 1.10 Usig a looks block to make the kitte say somethig. what happes. As demostrated i Figure 1.12, the kitte respods by meowig ad talkig (displayig "Hello world!" i a text captio bubble). Savig Your Work Okay, ow that you have your ew Scratch applicatio project workig, it is time to save your work. This is doe by clickig o the Save butto located at the top of the Scratch IDE. I respose, the Save Project widow show i Figure 1.13 displays, allowig you to assig a ame to your project ad store it o your computer. Type Hello World i the New Fileame field to ame your applicatio. If you wat, you ca type your ame i the Project Author field ad the eter a short descriptio i the About This Project field ad the click o the OK butto to save your project.
40 Joiig Scratch s Global Commuity 19 Figure 1.11 Usig a cotrol block to cotrol script executio. That s it. At this poit, you have goe through all of the steps ecessary to create, test, modify, execute, ad the save a ew Scratch applicatio project. Now that was t too tough, was it? Before wrappig up this chapter, let s sped a few miutes learig about Scratch s global commuity of users ad how you ca tap i to lear more about Scratch. Joiig Scratch s Global Commuity Scratch is supported by a global commuity of studets, teachers, schools, parets, ad computer ethusiasts ad hobbyists. Scratch is available i may laguages, icludig Eglish, Spaish, Germa, Frech, Italia, Hugaria, Hebrew, Polish, Dutch, Romaia, ad Russia. The Scratch website, located at ad show i Figure 1.14, helps brig together people from aroud the world ad facilitates the developmet of the Scratch commuity.
41 20 Chapter 1 Itroducig Scratch Figure 1.12 Automatig a sprite with a script. Figure 1.13 Savig your ew Scratch applicatio project.
42 Joiig Scratch s Global Commuity 21 Figure 1.14 The Scratch website is the lichpi supportig the growth ad iteractio of the Scratch commuity. The Scratch website provides access to all kids of resources that help Scratch programmers lear more about the laguage. It provides access to olie documetatio ad traiig videos. It also provides access to the help scree packed with documetatio o how to work with Scratch code blocks. Sharig Your Applicatio Projects The Scratch website promotes applicatio project sharig by allowig Scratch programmers to upload their projects ad make them available to ayoe visitig the website. This allows Scratch programmers to show off their work ad to lear from the work of others. I fact, every Scratch project that is uploaded to the website ca be dowloaded ad used as the basis for creatig ew projects. As
43 22 Chapter 1 Itroducig Scratch Figure 1.15 The Scratch website facilitates sharig by promotig Scratch projects ad makig them available for dowload. Figure 1.15 shows, the Scratch website actively promotes Scratch applicatios o its project page ( which meas that you ca expect to see ay Scratch projects that you upload posted there as well. The Scratch website lets members post their uploaded Scratch projects i galleries. You ca post your Scratch projects i differet galleries or create a gallery of your ow ad eve cotrol whether ayoe else is allowed to upload their projects ito it. As Figure 1.16 demostrates, the Scratch website actively promotes member galleries.
44 Joiig Scratch s Global Commuity 23 Figure 1.16 You ca create your ow gallery ad use it to promote your programmig skills. If you decide to create your ow gallery, you ca customize it by assigig it a ame ad a descriptio ad by determiig whether you wat to let ayoe else upload Scratch projects ito it. Registerig with the Scratch Website I order to upload your Scratch projects to the Scratch website, you must sig up for a free accout, which you ca do by clickig o the sig up lik at the top of every page o the Scratch website. Clickig o this lik opes the Create a Accout page, show i Figure The Scratch website gives its members the ability to commet o ay Scratch applicatio project that is uploaded to the website. The website also provides
45 24 Chapter 1 Itroducig Scratch Figure 1.17 Registerig for a free accout o the Scratch website. access to a collectio of forums desiged to host coversatio betwee studets, teachers, ad Scratch ethusiasts from all over the world. Note You will lear more about how to share your Scratch projects whe you get to Chapter 13, Sharig Your Scratch Projects Over the Iteret. Keepig I Touch I additio to facilitatig project sharig ad allowig commets to be posted about projects, the Scratch website hosts a umber of olie forums at scratch.mit.edu/forums/, as show i Figure 1.18.
46 Joiig Scratch s Global Commuity 25 Figure 1.18 Members of the Scratch commuity ca commuicate freely ad discuss ideas usig the forums hosted o the Scratch website. As Figure 1.18 shows, forums have bee set up to address the followig rage of topics: Aoucemets Show ad tell FAQ All About Scratch Educators Advaced Topics
47 26 Chapter 1 Itroducig Scratch Suggestios Troubleshootig These forums provide the ability to lear directly from other Scratch programmers. By readig the discussios that are posted, you ca lear ew programmig techiques ad fid out about problems ecoutered by other programmers ad their solutios. Most importat of all, you ca post questios ad get aswers to those questios. Summary This chapter has provided a overview of the Scratch laguage ad it capabilities. It showed you how to istall Scratch o your computer ad the demostrated how to create your first Scratch applicatio. It also itroduced you to the Scratch website ad explaied the importace of settig up a accout ad becomig a active member of the Scratch commuity.
48 chapter 2 Gettig Comfortable with the Scratch Developmet Eviromet To become a effective Scratch programmer, you eed to become itimately familiar with its itegrated developmet eviromet, or IDE. I this chapter, you will lear about the stage o which applicatios execute ad the sprite list that Scratch uses to display ad orgaize sprites used i your applicatios. You will also lear how to work with editors that create scripts, costumes, ad soud effects. You will also lear all about Scratch s pait program, which you ca use to create your ow custom graphics files. By the time you have completed this chapter, you will have a solid uderstadig of all of the features ad capabilities of the Scratch IDE ad will be ready to begi usig it to create your ow Scratch applicatio projects. A overview of the major topics covered i this chapter icludes: How to work with meu ad toolbar buttos How to add, remove, ad modify the sprites that make up your Scratch applicatios A explaatio of the coordiates system used to cotrol sprite placemets o the stage How to edit ad modify scripts, costumes, ad souds How to create ew sprites usig Scratch s built-i Pait Editor 27
49 28 Chapter 2 Gettig Comfortable Gettig Comfortable with the Scratch IDE Scratch is a graphical programmig laguage. Scratch applicatios are created by executig Scratch projects made up of differet types of media, icludig graphics ad soud, usig scripts made up of differet code blocks. Scratch projects are created usig its IDE. As show i Figure 2.1, Scratch s IDE is composed of umerous compoets. Project Notes Meu Bar Curret Sprite Ifo Toolbar Executio Buttos Stage Mouse Coordiates Sprite List Blocks Palette Scripts Area Presetatio Mode New Sprite Buttos Figure 2.1 The Scratch IDE facilitates the developmet ad executio of Scratch applicatios.
50 Gettig Comfortable with the Scratch IDE 29 Together, all of the compoets idetified i Figure 2.1 provide a robust ad powerful, yet iitiative ad fu, work eviromet, providig everythig eeded to develop Scratch applicatios. The rest of this chapter will offer a detailed overview of each of the major compoets that make up the Scratch IDE. Gettig Familiar with Meu Bar Commads Like most graphic applicatios, the Scratch IDE has a meu bar made up of a collectio of buttos located at the top of the IDE, as show i Figure 2.2. These buttos provide access to commads that allow you to create, ope, ad save Scratch projects as well as share them o the Iteret, udo previous commads, chage the laguage used by the IDE, ad much more. The followig list provides a explaatio of each of the buttos that make up the meu bar. New. Creates a ew Scratch applicatio project. Ope. Opes a existig Scratch applicatio project. Save. Saves the curret Scratch project (with a file extesio of.sb). Save As. Saves the curret Scratch project uder a ew ame. Share! Uploads a copy of the project to the Scratch website ( where it ca be made available for viewig ad dowloaded by other Scratch programmers. Udo. Restores the last script, code block, or sprite deleted from the applicatio project durig the curret workig sessio. Laguage. Lets you specify the laguage to be used by the Scratch IDE. Extras. Displays a popup list from which you ca select oe of the followig commads: Import Project, Start Sigle Steppig, Compress Souds, or Compress Images. Figure 2.2 The meu bar provides easy access to commads that you ca use to create ad save Scratch projects.
51 30 Chapter 2 Gettig Comfortable Wat Help? Displays a page that provides a lik to the Scratch website as well as to the followig set of resources: Referece Materials, Tutorials, or Frequetly Asked Questios. Most of the commads listed above are self-explaatory. However, the last three commads merit additioal explaatio. Whe clicked, the Laguage butto displays a meu of programmig laguages from which you ca select. Depedig o the laguage selected, a complete traslatio may be available. I other cases, oly scripts ad code blocks may be traslated. Tip You ca display a tool tip for ay of the butto cotrols show o the Scratch IDE by movig the mouse poiter over the butto. Whe clicked, the Extras butto displays a meu that has the followig optios. About. Displays a popup widow that provides iformatio about the versio of Scratch beig used. Import Project. This commad imports all of the sprites ad backgrouds, alog with ay related scripts, from the specified project ito the curret project. As such, this commad makes the sharig ad movemet of sprites ad backgrouds betwee Scratch projects a sap. Start Sigle Steppig. This commad tells Scratch to execute a applicatio a step at a time, allowig you to observe the executio flow of code blocks. This commad will be discussed more thoroughly i Chapter 15, Fidig ad Fixig Program Errors. Compress Souds. This commad compresses ay soud files used by the curret applicatio project to reduce the project s size. This is importat because the Scratch website imposes a 10MB limit o the size of Scratch applicatios. Compress Images. Like the Compress Souds commad, this commad compresses ay graphic image files used by the curret applicatio project to reduce the project s size. By compressig the size of your applicatio, you ca sometimes reduce large Scratch projects eough to allow them to upload.
52 Gettig Comfortable with the Scratch IDE 31 The last butto o Scratch s meu bar is the Wat Help? butto. Whe clicked, this butto opes a browser widow that provides access to the followig resources. Gettig Started. Opes the Gettig Started with Scratch PDF user guide. Help Screes. Displays a collectio of help screes that documet the use ad purpose of every Scratch code block. Referece Guide. Opes the Scratch Referece Guide PDF referece file. Visit the Scratch support page. Displays the Scratch support web page located at Ruig Scratch Applicatios o the Stage The stage is the area o the Scratch IDE, located i the upper-right side, as show i Figure 2.3, where your Scratch applicatios execute. The stage provides a place for the sprites that make up your applicatios to iteract with oe aother ad the user. Figure 2.3 The stage provides the cavas upo which sprites are displayed ad iteract with oe aother.
53 32 Chapter 2 Gettig Comfortable Figure 2.4 Sprites are placed o the scree ad moved aroud usig a system of coordiates. The stage is 480 uits wide ad 360 uits high. The stage is mapped out ito a logical grid usig a coordiate system made up of a X-axis ad a Y-axis, as demostrated i Figure 2.4. As you ca see, the X-axis rus from coordiates 240 to 240, ad the Y-axis coordiate rus from coordiates 180 to 180. The middle of the stage has a coordiate locatio of (0, 0). Scratch keeps you iformed of the poiter s locatio wheever it is moved over the stage by displayig its (X, Y) coordiate positio i the mouse x: ad mouse y: fields just beeath the bottom-right side of the stage. The stage ca be assiged oe or more backgrouds, allowig you to chage its appearace durig applicatio executio. By default, all Scratch applicatios are assiged a blak backgroud. You ca add ew backgrouds by clickig o the Stage thumbail, located o the left-had side of the sprite list, ad the clickig o the Backgrouds tab located just above the scripts area. Like sprites, the stage ca be assiged its ow scripts ad soud effects. Tip If you right-click o a ope area o the stage, a popup meu will appear, displayig the followig meu items: Grab scree regio for ew sprite. Makes a copy of a selected portio of the stage ad uses it to create a ew sprite. Save picture of stage. Saves a copy of the stage as a.gif file.
54 Gettig Comfortable with the Scratch IDE 33 Ruig Applicatios i Presetatio Mode As you saw i Chapter 1 whe you created the Hello World project, Scratch rus your applicatios o the stage withi the IDE by default. However, if you click o the Presetatio Mode butto, located just beeath the bottom-left corer of the stage, you ca ru your Scratch applicatio project i Presetatio mode. To see how this works, click o the Ope butto located at the top of the Scratch IDE ad the locate ad ope the Hello World project. Next, click o the Presetatio Mode butto to switch to full-scree mode. Oce i Presetatio scree mode, sigle-click o the sprite represetig the kitte ad watch as your applicatio executes, as demostrated i Figure 2.5. You ca exit Presetatio mode at ay time either by clickig o the Exit Presetatio Mode ico located just above the upper-left side of the stage or by pressig the Escape key. Cotrollig Applicatio Executio Whether ruig your applicatio from the IDE s stage or i Presetatio mode, you ca automatically start ay scripts that begi with the gree flag cotrol block by clickig o the gree flag butto located i the upper-right corer of the IDE, as show i Figure 2.6. This same butto is also available i Presetatio mode. By clickig o the red stop butto located right ext to the gree flag Figure 2.5 Ruig a Scratch applicatio project i Presetatio mode.
55 34 Chapter 2 Gettig Comfortable Figure 2.6 The gree flag ad red stop buttos provide cotrol over script executio. butto, you ca stop the executio of your applicatios ay time you fiish workig with them. Workig with the Sprite List Scratch applicatios are made up of sprites that iteract with oe aother as they move aroud the stage. Each sprite that makes up a Scratch applicatio is displayed as a thumbail i the sprite list area, located o the lower-right portio of the Scratch IDE, as show i Figure 2.7. Although it has o impact o a Scratch applicatio, you ca reorgaize the order i which sprites are displayed i the sprite list by draggig ad droppig thumbails to ay locatio that makes sese to you. I additio to a thumbail, Scratch also displays the ame of each sprite as well as the umber of scripts ad costumes belogig to each sprite. To work with a sprite ad edit its scripts, costumes, ad soud effects, just click o its thumbail. The curretly selected sprite is highlighted by a blue outlie. Oce selected, you ca click o the Scripts, Costumes, ad Souds tabs located at the top of the script area to edit a sprite s scripts, costumes, ad soud effects. Figure 2.7 The sprite list displays a thumbail for each sprite i a applicatio.
56 Gettig Comfortable with the Scratch IDE 35 If you right-click o a sprite s thumbail, the followig list of meu optios is displayed: Show. Ceters a sprite o the stage, placig it o top of all other sprites. Export this sprite. Exports a sprite as a file, makig it available to be imported ito other Scratch projects. Duplicate. Makes a copy of the sprite. Delete. Removes a sprite from the project. Tip You ca also export, duplicate, ad delete sprites by right-clickig o ay sprite o the stage ad the selectig the correspodig meu items that are displayed. The sprite list also displays a thumbail represetig the applicatio project s stage. Whe the stage s thumbail is selected, you ca add scripts to the stage, modify the stage s backgroud by assigig it oe or more graphic files, ad also add souds to the stage. Geeratig New Sprites Scratch makes it easy for you to work with sprites by providig three differet optios for addig them to your applicatios. These optios are accessed through the New Sprite buttos located just below the stage, as show i Figure 2.8. Whe clicked, the Pait New Sprite butto starts Scratch s Pait Editor program. This program provides everythig you eed to draw ew sprites o a trasparet Pait New Sprite Choose New Sprite from File Get Surprise Sprite Figure 2.8 The New Sprite buttos provide access to tools for addig ad creatig ew sprites.
57 36 Chapter 2 Gettig Comfortable Figure 2.9 Scratch supplies easy access to a wide selectio of ready-made sprites. backgroud. You will lear the is ad outs of how to work with the Pait Editor program a little later i this chapter. Whe clicked, the Choose New Sprite from File butto displays the New Sprite widow show i Figure 2.9, providig access to differet collectios of graphic files that you ca add to your Scratch applicatios as sprites. To select ad add a sprite, all you have to do is to drill dow ito oe of Scratch s folders, fid the sprite you wat, ad the click o the OK butto. The sprite that you selected will the appear i the ceter of the stage, ad a thumbail represetig the sprite will be added to the sprite list. The Get Surprise Sprite butto radomly retrieves oe of Scratch s ready-made sprites ad adds it to your applicatio project. It ca be used to geerate all kids of wacky projects. Trackig Mouse Poiter Locatio As you lear how to develop your ow Scratch applicatios, you will eed to keep track of the iitial placemet ad subsequet movemet of sprites o the stage. Scratch assists you i this task by keepig track of mouse-poiter movemet wheever you move the poiter across the stage (see Figure 2.10). You ca use
58 Gettig Comfortable with the Scratch IDE 37 Figure 2.10 The Scratch IDE makes it easy to track the mouse-poiter s locatio whe it moves aroud the stage. Move Duplicate Delete Grow Sprite Shrik Sprite Figure 2.11 The Scratch toolbar provides tools for iteractig with sprites. this iformatio to idetify the coordiates data that you eed to icorporate ito your applicatio code as you develop the programmig logic that drives your Scratch projects. Workig with the Scratch Toolbar Aother importat compoet of the Scratch IDE is the toolbar, show i Figure Figure The toolbar provides access to commads that you ca use to iteract with ad cotrol the sprites that make up your applicatios. The followig list summarizes the fuctioality provided by each of the toolbar s buttos: Move. Allows you to drag ad drop sprites to differet locatios o the stage (default toolbar selectio). Duplicate. Makes a copy of the curretly selected sprite, icludig its scripts, costumes, ad souds, providig a easy way of addig ew sprites to your applicatios. Oce a sprite is duplicated, you ca customize the ew copy of the sprite as you see fit. Delete. Removes a sprite, icludig all its scripts, costumes, ad souds, from the project ad removes its thumbail from the sprite list.
59 38 Chapter 2 Gettig Comfortable Grow Sprite. Icreases a sprite s size, i case its actual size does ot meet the eeds of your applicatio. Shrik Sprite. Decreases a sprite s size, i case its actual size does ot meet the eeds of your applicatio. By default, the Move butto is always selected. However, you may select ay of the other toolbar buttos by clickig o them ad the clickig o the sprite that you wat to work with. Switchig Betwee Code Block Groups Like applicatios created by ay programmig laguage, Scratch applicatios execute program code made up of collectios of code blocks that maipulate sprites ad iteract with the user. Scratch s program code is orgaized ito scripts belogig to sprites. Every sprite i a applicatio ca be assiged oe or more scripts. I additio, the stage ca also execute its ow scripts. As you have already see, the first step i creatig a script is to select the sprite (or the stage) to which the script will belog. This is doe by clickig o the appropriate thumbail i the sprites list. You ca the add code blocks by draggig the blocks from the blocks palette ad droppig them ito the scripts area (whe the Script tab is selected). The blocks palette is orgaized ito two sectios. The top sectio cotais eight butto cotrols, each of which represets a differet category of code block. Each of the buttos is color coded. The curretly selected butto is easily idetified because it is filled i with its assiged color. The left-had edge of the uselected buttos shows the color of the code blocks belogig to its category. For example, Figure 2.12 shows how the blocks palette looks whe the Motio butto has bee selected. Trick You ca right-click (Cotrol-click o Mac OS X) o ay code block ad the select Help from the resultig popup meu to get help iformatio o ay code block. Gettig Comfortable with the Scripts Area The last major part of the Scratch IDE that you eed to become familiar with is the scripts area, which cosists of two major sectios. The Curret Sprite Ifo sectio, located at the top of the scripts area, displays iformatio about the curretly selected sprite. The rest of the scripts area is cotrolled by three tabs, which allow you to add scripts, costumes, ad souds to sprites.
60 Gettig Comfortable with the Scratch IDE 39 Figure 2.12 Each category of code block is desiged to accomplish a related set of tasks. Figure 2.13 Chagig a sprite s ame ad viewig detailed iformatio about the sprite. Examiig Sprite Details The Curret Sprite Ifo sectio displays the ame curretly assiged to the selected sprite, which, as demostrated i Figure 2.13, is Sprite1. You ca chage a sprite s ame by typig over it. The sprite s curret coordiates ad directio are displayed just beeath its ame, ad the sprite s curretly assiged costume is displayed just to the left of its ame.
61 40 Chapter 2 Gettig Comfortable Figure 2.14 Exportig a sprite as a stad-aloe graphic file. Take ote of the blue lie that is displayed o the thumbail i the Curret Sprite Ifo sectio. It shows the sprite s curretly assiged directio. You ca chage the sprite s directio by draggig the outside edge of this lie to a ew directio. If you do ot like the directio that you have set for the sprite, double-click o the sprite to reset it back to its default directio (90-degree agle). You ca export the scripts as a stad-aloe lie by clickig o the Export butto. This opes the Export Sprite widow show i Figure 2.14, allowig you to specify the locatio where you wat to save the sprite, makig it available for use i other Scratch applicatio projects. Just beeath the Export butto is a graphic file represetig a padlock. Clickig o this image toggles the graphic betwee a locked ad ulocked state. Whe set to locked, Scratch prevets the sprite from beig dragged aroud the stage by the user whe the script is ru i Presetatio mode or whe ru from the Scratch website. Just to the left of the sprite s curretly selected costume are three buttos that you ca use to specify the sprite s rotatio style. These three buttos are mutually exclusive, meaig that you ca oly select oe. Table 2.1 idetifies the rotatioal style represeted by each of these buttos.
62 Gettig Comfortable with the Scratch IDE 41 Table 2.1 Sprite Rotatioal Buttos Butto Name Descriptio Ca rotate Oly face left-right Do t rotate Rotates the sprite s costume by 360 degree whe the sprite s directio is chaged. Toggles the directio that the sprite s costume faces from left to right ad vice versa. Maitais the sprite costume s curret directio. Tip To get a better feel of the effect that Scratch s rotatioal buttos have o a sprite, click o each of them ad observe the rotatioal movemet of the sprite costume i the Curret Sprite Ifo sectio. Editig Scripts As you have already see, Scratch scripts are created by draggig code blocks from the blocks palette oto the scripts area (whe the Scripts tab has bee selected). Of course, the code blocks must be added i a maer that makes logical sese, which is what Chapters 5 through 22 are desiged to teach you. Tip As you add ew scripts ad modify existig oes, it is easy to leave the scripts area i a mess. Oe way of dealig with this situatio it to sped a few miutes draggig ad droppig scripts so that they lie up ad are evely spaced. However, a much faster ad easier optio is to right-click o a free area withi the scripts area ad the click o the clea-up optio located i the popup meu that is displayed. I respose, Scratch will realig all of your scripts for you. Addig Costumes A sprite ca have oe or more costumes, allowig it to chage its appearace as a applicatio executes. A sprite must have at least oe costume. For example, Figure 2.15 shows a sprite that has two costumes. Each costume is assiged a uique ame ad umber (displayed just to the left of the costume s image). By default, Scratch oly displays a sprite s first costume. You ca drag ad drop costumes to chage their positio i the list. Whe moved, the umber assiged to the costume is automatically chaged as well. Scratch gives you three differet ways of addig ew costumes to sprites. For starters, you ca click o the Pait butto. This opes the Pait Editor program,
63 42 Chapter 2 Gettig Comfortable Figure 2.15 Importig ad assigig a soud file to a sprite. which you ca use to draw a ew costume. You ca also add a ew costume to a sprite by clickig o the Import butto ad specifyig a image file from a folder o your computer. Lastly, you ca drag ad drop a image file from the Iteret or your desktop oto the scripts area whe the Costumes tab is selected. Note Scratch ca work with differet types of graphic files, icludig GIF, JPG, BMP, ad PNG files. Scratch ca also work with aimated GIF files. A aimated GIF file is a graphic made up of two or more frames, each of which is displayed as a automated sequece whe the GIF file is displayed. Oce added, you ca modify a costume by selectig it ad clickig o the Edit butto, which opes the Pait Editor. You ca also add a ew costume to a sprite by selectig a existig costume ad the clickig o the Copy butto. Oce the copy of the costume has bee added, you ca click o its Edit butto, allowig you to modify it usig the Pait Editor. You ca delete a costume from a sprite by selectig it ad the clickig o the roud Delete butto to the right of the Copy butto. You ca also tur a costume ito a sprite or export it as a stad-aloe costume by right-clickig o it ad selectig the appropriate optio from the popup meu that appears.
64 Gettig Comfortable with the Scratch IDE 43 Note The stage ca be assiged a graphic to be used as a backgroud upo which the applicatio s sprites are displayed. I fact, the stage ca be assiged a series of backgrouds, allowig a applicatio to chage backgrouds durig applicatio executio. To view, edit, ad make a copy of a backgroud, select the stage thumbail located i the sprite list. Whe you do this, the Costumes tab i the scripts area chages to the Backgrouds tab, allowig you to modify ad work with applicatio backgrouds. From here you ca also create ew backgrouds yourself by clickig o the Pait butto. This opes Scratch s Pait Editor program, discussed later i this chapter, allowig you to create ay backgroud you wat. You ca also click o the Import butto to add a exteral graphic file to your applicatio as a backgroud. Addig Soud Effects Just as sprites ca have differet costumes, you ca also assig oe or more souds to them (or to the stage), which ca be played durig applicatio executio, either as backgroud music or oise or as soud effects durig game play. Scratch ca play back MP3 files as well as most WAV, AU, ad AIF audio files. To view the soud files associated with a sprite or to record or import a ew file, select the sprite s thumbail i the sprite list ad the click o the Souds tab i the scripts area. A list of the soud files belogig to the sprite is displayed, as demostrated i Figure Figure 2.16 Addig ad editig soud files.
65 44 Chapter 2 Gettig Comfortable Oce the Souds tab has bee selected, you ca perform ay of the followig actios o ay soud files that belog to the sprite: Chage the ame used to refer to the soud withi the applicatio. Click o the Play butto to liste to the soud. Click o the Stop butto to halt soud playback. Click o the Delete butto to remove the soud from the applicatio project. I additio to iteractig with a sprite s existig soud file, you may add ew soud files by clickig o the Record butto. I respose, the Soud Recorder widow appears, as show i Figure 2.17, allowig you to record ad save a ew soud file. Of course, to record your ow soud files, your computer will eed to have a microphoe. You ca also add ew soud files to your Scratch applicatio by clickig o the Import butto, which opes the Import Soud widow, as show i Figure 2.18, allowig you to select a soud file. Scratch provides access to tos of prerecorded soud files. By default, the Import Soud widow displays a listig of folders cotaiig differet collectios of soud files. Keepig Project Notes Aother importat feature of the Scratch IDE is the ability to add ad update project otes. Scratch allows you to add project otes whe you first save your applicatio project. Oce they are saved, you may update your project s otes at ay time by clickig o the Project Notes ico located i the upper-right corer of the IDE. I respose, the Project Notes widow displays, as demostrated i Figure Figure 2.17 Recordig a ew audio file to be used as part of a Scratch applicatio.
66 Gettig Comfortable with the Scratch IDE 45 Figure 2.18 Importig ad assigig a soud file to a sprite. Figure 2.19 Viewig ad updatig Scratch applicatio project otes.
67 46 Chapter 2 Gettig Comfortable The Project Notes widow operates like a simple Notepad program, allowig you to type i ay text you wat. Tip Use the Project Notes widow to help documet your Scratch applicatios, leavig behid iformatio that explais the applicatio s purpose ad why you desiged it the way you did. If you pla o uploadig your project to the Scratch website, the project otes take o additioal value. Specifically, text saved as otes is displayed o the same web page as your project ad ca therefore provide istructios for ruig your applicatio. Creatig New Sprites Usig Scratch s Pait Editor I additio to usig the sprites supplied with Scratch ad graphics that you acquire from the Iteret, you ca always create your ow sprite usig ay graphic/pait program. Although it does ot have all of the bells ad whistles that applicatios like Corel Pait Shop Pro or Adobe Photoshop have, Scratch s built-i Pait Editor, show i Figure 2.20, offers everythig eeded to draw or modify graphics for use as sprites ad backgrouds. As Figure 2.20 demostrates, Scratch s Pait Editor is divided ito multiple compoets. Thaks to Scratch s cross-platform desig, the Pait Editor looks ad operates idetically o both Microsoft Widows ad Mac OS X. Examiig the Drawig Cavas Liks to the Pait Editor program are located just uder the stage ad withi the Costumes ad Backgrouds tabs located i the scripts area. The Pait Editor program ca be used to create or modify ew sprites, costumes, ad backgrouds. Most of the space o the Pait Editor s widow is dedicated to a drawig cavas. To draw o the cavas, you select differet drawig commads from the toolbar ad the use the mouse to draw o the cavas. You ca work with differet colors ad apply a rage of special effects. If the size of the graphic beig worked o exceeds the available area, scrollbars are eabled o the right-had side ad the bottom of the drawig cavas, allowig you to view all parts of the graphic. You ca also use the Zoom I ad Zoom Out buttos located at the bottom of the Pait Editor widow to temporarily icrease or decrease the magificatio of the drawig cavas.
68 Creatig New Sprites Usig Scratch s Pait Editor 47 Butto Cotrols Drawig Cavas Toolbar Optios Area Curret Color Settigs Slider Cotrols Color Palettes Rotatio Cotrol Zoom Cotrol Figure 2.20 Scratch s built-i Pait Editor program provides everythig eeded to create sprites ad costumes. Workig with the Toolbar ad Optios Area Whe creatig or editig a graphic image o the drawig cavas, the buttos located o the Pait Editor s toolbar provide access to essetial features ad fuctioality. The followig list offers a overview of the fuctioality provided by each toolbar butto: Paitbrush. Allows you to draw freehad o the drawig cavas usig the curret foregroud color ad brush size. Eraser. Allows you to erase selected portios of the drawig cavas usig the curret eraser size. Erased portios of the drawig cavas are retured to a trasparet state.
69 48 Chapter 2 Gettig Comfortable Fill. Allows you to fill i eclosed areas with either a gradiet or a solid color, depedig o the selected optio specified i the optios area. Rectagle. Allows you to draw filled-i or outlied rectagle shapes usig the curret foregroud color. Ellipse. Allows you to draw filled-i or outlied ellipses usig the curret foregroud color. Lie. Allows you to draw straight lies usig the curret foregroud color. Text. Allows you to iclude text as part of a drawig usig the curret fot type ad size. Selectio. Allows you to select a rectagular portio of the drawig cavas ad move it to a differet part of the drawig cavas (cut ad paste). Stamp. Allows you to select a rectagular portio of the drawig cavas ad copy it to differet parts of the drawig cavas (copy ad paste). Eyedropper. Allows you to select the foregroud color. Most of the toolbar buttos accept cofiguratio optios that further refie the fuctioality provided by the butto cotrol. For example, Figure 2.21 shows the four cofiguratio optios that are provided whe the Fill butto has bee selected. These optios set the fill style that is applied ad iclude the applicatio of a solid color ad the use of a horizotal gradiet, vertical gradiet, or radial gradiet. Note A gradiet is a color created by bledig together the foregroud ad backgroud colors. Figure 2.21 The cotet of the optios area chages based o the selected toolbar butto.
70 Creatig New Sprites Usig Scratch s Pait Editor 49 Workig with Butto Cotrols As show i Figure 2.22, Scratch s Pait Editor program icludes a umber of butto cotrols that ca iitiate a assortmet of differet actios. The followig list idetifies each of these buttos ad explais its purpose: Import. Opes a image from a graphic file stored o your computer. Grow. Icreases the size of the drawig cavas, allowig you to focus i o a particular area. Shrik. Decreases the size of the drawig cavas. Rotate couterclockwise. Rotates the drawig cavas couterclockwise. Rotate clockwise. Rotates the Drawig cavas clockwise. Flip horizotally. Flips the drawig cavas horizotally. Flip vertically. Flips the drawig cavas vertically. Clear cavas. Clears ay graphics curretly displayed o the drawig cavas. Udo. Udoes the last actio that you performed i the Pait Editor. Redo. Redoes the last udoe actio. Rotate Clockwise Import Shrik Flip Vertically Clear Cavas Udo Redo Grow Flip Horizotally Rotate Couterclockwise Figure 2.22 The Pait Editor provides access to key fuctioality through various butto cotrols.
71 50 Chapter 2 Gettig Comfortable Specifyig Color Settigs The Pait Editor lets you specify curret color settigs for both foregroud ad backgroud drawig usig the curret Color Settigs cotrol located o the lefthad side of the Pait Editor widow, just uder the optios area. To set the curret foregroud color, click o the top square ad the select a color from oe of the color palettes that are displayed beeath the cotrol. Likewise, you ca set the curret backgroud color by selectig the bottom square ad the selectig a color from oe of the color palettes. Cofigurig a Sprite s Rotatio Ceter Oe fial but very importat Pait Editor feature that you defiitely eed to kow how to use is the Set Rotatio Ceter butto located i the lower-left corer of the Pait Editor widow. Whe clicked, this butto displays a set of cross-hairs o the Pait Editor s drawig cavas, as demostrated i Figure You ca the use drag ad drop to move the cross-hair over the portio of the sprite that you wat to set up as the sprite s rotatioal ceter whe the sprite is rotated o the stage. Figure 2.23 Cross-hairs make it easy to set a sprite s rotatioal ceter.
72 Summary 51 The sprite show i Figure 2.23 is that of a rock that might be used i a space shooter game like Asteroids. I this type of game, the asteroid would move aroud the scree, threateig to destroy the player s ship by collidig with it. To provide a realistic look ad feel, you might wat to tell Scratch to rotate the rock as its moves aroud the scree. By settig up the rock s rotatio poit as the ceter of the sprite, it will appear to rotate or spi aroud its ceter. O the other had, by settigs its rotatio poit to be oe of the edges of the rock, you ca make it rotate i a more wobbly maer. Summary This chapter has itroduced you to the Scratch IDE ad provided a step-by-step overview of all of its major compoets ad fuctioality. You leared how to work with its meu ad toolbar buttos. You leared how to add ad delete sprites as well as how to add scripts, costumes, ad souds to sprites. This chapter explaied the coordiates system used to cotrol the placemet of sprites o the stage. O top of all this, this chapter also provided a overview of Scratch s Pait Editor program ad outlied all of its major features ad fuctioality.
73 This page itetioally left blak
74 chapter 3 A Review of the Basic Compoets of Scratch Projects As you have already see, Scratch applicatio projects are comprised of backgrouds ad sprites. Sprites iteract ad move about the stage uder the programmatic cotrol of scripts made up of code blocks. This chapter will explai the three basic types of code blocks ad how they work together to create scripts. It will also review the eight categories ito which all Scratch s 100-plus code blocks are grouped. Although this chapter does ot offer a i-depth review of each idividual code block, it will provide a series of tables that you ca bookmark ad use as a quick referece whe developig ew Scratch applicatios. A overview of the major topics covered i this chapter icludes: A detailed explaatio of stack blocks, hat blocks, ad reporter blocks A demostratio of how to work with ad cofigure moitors A review of all 100-plus code blocks that make up Scratch scripts A explaatio of how to display help iformatio for idividual code blocks Workig with Blocks ad Stacks To brig the backgrouds ad sprites that make up Scratch applicatios to life, you must create scripts. Scripts are created by draggig ad droppig code blocks from the blocks palette to the scripts area ad sappig them together, creatig 53
75 54 Chapter 3 A Review of the Basic Compoets of Scratch Projects White Idicator Bar Figure 3.1 Use the visual idicator to determie valid coectio poits. stacks. Scripts ca be ru by double-clickig o oe of the code blocks. Scripts ca also be cofigured to automatically execute whe predefied evets occur. You ca drag a code block aroud the scripts area. As demostrated i Figure 3.1, whe you drag a block ear other blocks, a white idicator bar appears to desigate locatios where a valid coectio ca be made. Code blocks ca be sapped to the top ad bottom of stacks or iserted ito the middle of the stack. You ca move code stacks by clickig o their uppermost blocks ad draggig them to a ew locatio. If you drag a block from the middle of a stack, all of the code blocks udereath it are dragged out as well. Tip You ca copy a stack of code blocks from oe sprite to aother by draggig ad droppig the stack oto the thumbail of a sprite located i the sprite list.
76 Three Basic Types of Scratch Blocks 55 Three Basic Types of Scratch Blocks Scratch applicatios are made up of sprites that iteract with oe aother ad the user. Sprites are cotrolled ad aimated by scripts. Sprites ca have ay umber of scripts, each of which is desiged to perform a specific task or actio. Scripts are made up of oe of more Scratch code blocks. I total, there are more tha 100 differet Scratch blocks, each of which is desiged to fulfill a specific purpose. These blocks ca be broadly classified ito three categories, as outlied here: Stack blocks Hat blocks Reporter blocks Workig with Stack Blocks The majority of code blocks provided by Scratch are stack blocks. Stack blocks are code blocks with a otch at the top or a bump at the bottom. The otches ad bumps serve as visual idicators that idetify how the blocks ca be sapped together to create programmig logic. Figure 3.2 shows a example of a typical stack block. The otch o the top idicates that the code block ca be attached to the uderside of aother code block. The bump at the bottom of the code block allows other code blocks to attach to its uderside. Figure 3.3 shows a example of aother stack block. This block will repeatedly execute ay code blocks that you choose to embed iside it for as log as a tested coditio evaluates as true. Note You will lear about the applicatio of repetitive ad coditioal programmig logic i Chapter 9. Figure 3.2 A example of a code block that is used to halt the playback of a audio file. Figure 3.3 This code block allows other stack blocks to be embedded withi it.
77 56 Chapter 3 A Review of the Basic Compoets of Scratch Projects Some stack blocks iclude a iput area iside them that allows you to specify a value by typig i a umber. For example, the stack block show i Figure 3.4 lets you assig the color to be used whe drawig by isertig a color-associated umeric value. To modify the value assiged to a block like the oe show i Figure 3.4, click o the white area withi the code block ad type i a ew value. Some code blocks Editable Text Field Figure 3.4 This code block is used to specify the color to be used whe drawig. Figure 3.5 This code block has a pull-dow meu that you ca use to cofigure how it operates.
78 Three Basic Types of Scratch Blocks 57 let you cofigure them by selectig a value from a pull-dow list, as demostrated i Figure 3.5. Workig with Hat Blocks A hat block is a code block with a rouded or curved top ad a bump at the bottom, visually idicatig that it ca be sapped o top of other stack blocks. Hat blocks provide the ability to create evet-drive scripts. A evet-drive script is oe that automatically executes whe a specified evet occurs. A example of a evet that ca automatically trigger script executio is whe the user clicks o the gree flag butto. Whe this evet occurs, ay scripts that begi with the hat block show i Figure 3.6 are automatically executed. Script executio ca also be triggered whe the user clicks o a sprite. This ca be set up by addig the code block show i Figure 3.7 to the begiig of the script. Note Every sprite i a applicatio ca potetially have its ow scripts. You ca automate the executio of ay or all of the scripts usig hat blocks. I additio to sprites, the stage ca also have scripts. Workig with Reporter Blocks A third type of Scratch code block is a reporter block. A reporter block is a code block that has either rouded or agled sides ad is specifically desiged as a mechaism for providig iput for other code blocks to process. For example, the code block show i Figure 3.8 is a typical reporter block. Figure 3.6 This hat block automatically rus a script whe the user clicks o the gree flag. Figure 3.7 This hat block rus a script wheever the user clicks o the sprite to which this script belogs. Figure 3.8 This code block retrieves a umeric value idicatig a sprite s volume.
79 58 Chapter 3 A Review of the Basic Compoets of Scratch Projects Figure 3.9 You ca provide iput to this code block by either keyig it i or usig a reporter block. Figure 3.10 Agled report blocks pass Boolea data to other code blocks for processig. Figure 3.11 This code block pauses script executio util a specified evet is true. Figure 3.12 This particular combiatio of code blocks will pause script executio util the user presses the spacebar. As you ca see, this reporter block has rouded sides. As such, it ca oly fit ito code blocks like the oe show i Figure 3.9, whose iput area displays a shape with rouded sides. Figure 3.10 shows a example of a reporter block that has agled sides. This particular code block returs a value of true if the user has pressed the spacebar or a false if the spacebar has ot bee pressed. Because it has agled sides, it ca oly be embedded iside code blocks that cotai a iput area whose sides are also agled. Note Boolea is a term used to represet data that has oe of two values, either true or false. To take advatage of a reporter block like the oe show i Figure 3.10, you eed to embed the reporter block ito aother code block that has bee desiged to work with it. For example, Figure 3.11 shows oe such code block. Figure 3.12 demostrates how a reporter blocks looks after beig embedded withi aother code block. Keepig a Eye Out with Moitors You have probably oticed that Scratch displays a small check box just to the left of certai code blocks i the blocks palette, as demostrated i Figure 3.13.
80 Keepig a Eye Out with Moitors 59 Figure 3.13 A example of a code block capable of displayig a moitor o the stage. Figure 3.14 By default, a moitor displays the ame of its associated code block. Figure 3.15 Moitors ca be cofigured to display a large readout. Figure 3.16 Variable moitors also support a display format that icludes a slider bar. The presece of a check box idicates that the code block is capable of displayig a moitor o the stage. A moitor is a small block that displays the value curretly assiged to the code block. To display the moitor, just click o the check box to select it. Whe you do so, a gray block is automatically displayed o the stage, as demostrated i Figure You ca modify the way the moitor looks by right-clickig o it ad selectig Large Readout from the popup meu that appears. As a result, the appearace of the moitor will chage, as demostrated i Figure Tip You ca also toggle betwee moitor formats by double-clickig o the moitor. Variable-based moitors support a third format, which icludes a slider bar, as demostrated i Figure You will lear about variables ad their use i Chapter 7, Storig ad Retrievig Data.
81 60 Chapter 3 A Review of the Basic Compoets of Scratch Projects Eight Categories of Scratch Blocks Scratch provides access to over 100 code blocks. These code blocks are orgaized ito eight categories ad are made available o the blocks palette. Each of these categories of code blocks is described i the followig list: Motio. Code blocks that cotrol sprite placemet, directio, rotatio, ad movemet. Looks. Code blocks that affect sprite ad backgroud appearace ad provide the ability to display text. Soud. Code blocks that cotrol the playback ad volume of musical otes ad audio files. Pe. Code blocks that ca be used to draw usig differet colors ad pe sizes. Cotrol. Code blocks that trigger script executio based o predefied evets, repeatedly execute programmig logic usig loops, ad perform coditioal logic. Sesig. Code blocks that ca be used to determie the locatio of the mouse-poiter, its distace from other sprites, ad whether a sprite is touchig aother sprite. Numbers. Code blocks that perform logical comparisos, roudig, ad other arithmetic operatios. Variables. Code blocks that ca be used to store data used by applicatios whe they execute. You ca view the code blocks belogig to a give category by clickig o oe of the eight labeled butto cotrols at the top of the blocks palette. Note that each category of code block is color coded, makig it easy to distiguish betwee code blocks from differet categories. Each of these categories of code blocks is reviewed i the sectios that follow. This review covers Scratch s etire collectio of code blocks, idicatig which oes support moitors ad providig a brief descriptio of each block s usage.
82 Eight Categories of Scratch Blocks 61 Movig Objects Aroud the Drawig Cavas Motio blocks cotrol a sprite s placemet o the stage. Motio blocks are colored blue. There are motio blocks that let you set the directio a sprite will move ad the other blocks to move them. There are also motios blocks that report o a sprite s locatio ad directio. Table 3.1 outlies all of the code blocks that fit ito this category. Table 3.1 Scratch Motio Blocks Block Moitor Descriptio No Moves a sprite forward or backwards a specified umber of steps. No Rotates a sprite a specified umber of degrees i a clockwise directio. No Rotates a sprite a specified umber of degrees i a couterclockwise directio. No Poits a sprite toward a specified directio (0 = up, 90 = right, = left, 180 = dow). No Poits a sprite toward either the mouse-poiter or a specified sprite. No Moves a sprite to a specified coordiatio locatio o the stage. No Moves a sprite to the locatio of either the mouse-poiter or aother sprite. No Moves a sprite to the specified coordiate positio over a specified umber of secods. No Chages the positio of a sprite o the X-axis by a specified umber of pixels. No Chages a sprite s locatio o the X-axis to a specified value. No Chages the positio of a sprite o the Y-axis by a specified umber of pixels. No Chages a sprite s locatio o the Y-axis to a specified value. No Chages a sprite s directio whe it makes cotact with oe of the edges of the stage. Yes Retrieves a value represetig a sprite s coordiate o the X-axis (betwee ad 240). Yes Retrieves a value represetig a sprite s coordiate o the Y-axis (betwee ad 180). Yes Retrieves a value represetig a sprite s curret directio (0 = up, 90 = right, = left, 180 = dow).
83 62 Chapter 3 A Review of the Basic Compoets of Scratch Projects You will lear more about motio blocks i Chapter 5, Movig Thigs Aroud. Chagig Object Appearace Looks blocks modify sprite ad backgroud appearace ad display text withi popup bubbles. Looks blocks are colored purple. There are looks blocks that let Table 3.2 Scratch Looks Blocks Block Moitor Descriptio No Chages a sprite s costume, modifyig its appearace. No Chages a sprite s costume to the ext costume i the sprite s costume list, jumpig back to the begiig of the list whe the ed of the list is reached. Yes Retrieves a umeric value represetig a sprite s curret costume umber. No Displays a text message i a speech bubble for a specified umber of secods. No No Displays a text message i a speech bubble or removes the display of a speech bubble whe o text is specified. Displays a text message i a thought bubble for a specified umber of secods. No No No No Displays a text message i a thought bubble or removes the display of a thought bubble whe o text is specified. Modifies a sprite s appearace by applyig ad modifyig a special effect (color, fisheye, whirl, pixelate, mosaic, brightess, or ghost) by a specified umeric value. Applies a special effect (color, fisheye, whirl, pixelate, mosaic, brightess, or ghost) to a sprite by a specified umeric value. Restores a sprite to its ormal appearace, removig ay special effects that may have bee applied.
84 Eight Categories of Scratch Blocks 63 Table 3.2 (Cotiued) Block Moitor Descriptio No No Yes No Modifies the size of a sprite by a specified umeric amout. Sets a sprite s size to a percetage of its origial size. Retrieves a percetage value represetig a sprite s curret size whe compared to its origial size. Tells Scratch to display a sprite. No Suppresses the display of a sprite o the stage, prevetig it from iteractig with other sprites. No Places a sprite o top of other sprites, placig it o the top layer ad esurig its display. No Moves a sprite back a specified umber of layers, allowig other sprites to be displayed o top of it. No Alters the stage s appearace by assigig it a differet backgroud. No Chages the stage s backgroud to the ext backgroud i the backgroud list. No Retrieves a umeric value represetig the backgroud umber of the stage s curret backgroud. you modify sprite costumes ad colors. There are also blocks that let you modify a sprite s size ad cotrol whether a sprite is visible o the stage. Table 3.2 outlies all of the code blocks that fit ito this category. You will lear more about looks blocks i Chapter 10, Chagig the Way Sprites Look ad Behave. Makig Some Noise Soud blocks play music ad add soud effects to your Scratch applicatio projects. Soud blocks are colored pik. There are soud blocks that let you play souds ad drum beats, select differet types of istrumets, cotrol playback volume, ad modify tempo. Table 3.3 outlies all of the code blocks that fit ito this category.
85 64 Chapter 3 A Review of the Basic Compoets of Scratch Projects Table 3.3 Scratch Soud Blocks Block Moitor Descriptio No No No No No No No No No Yes No No Yes Plays the specified soud file while allowig the script file i which it is iserted to keep executig. Plays the specified soud file, pausig script executio util the soud file has fiished playig. Halts the playback of ay soud files curretly beig played. Plays a drum soud selected from the block s pull-dow meu a specified umber of secods. Pauses soud playback for a specified umber of beats. Plays a musical ote selected from the block s pull-dow meu a specified umber of beats. Specifies the istrumet to be used whe playig musical otes. Chages a sprite s volume by a specified value. Sets a sprite s soud volume to a specified percetage level. Retrieves a umeric value represetig a sprite s soud volume. Alters a sprite s tempo by a specified umber of beats per miute. Assigs the umber of beats per miute to be used as a sprite s tempo. Retrieves a umeric value represetig a sprite s tempo. You will lear more about soud blocks i Chapter 11, Spicig Thigs Up with Souds. Drawig Lies ad Shapes Pe blocks draw ay combiatio of shapes ad lies usig a virtual pe. Pe blocks are colored mit gree. There are pe blocks that let you eable ad disable drawig, set color ad pe size, ad apply shadig. Table 3.4 outlies all of the code blocks that fit ito this category. You will lear more about pe blocks i Chapter 12, Drawig Lies ad Shapes.
86 Eight Categories of Scratch Blocks 65 Table 3.4 Scratch Pe Blocks Block Moitor Descriptio No No No No No No No No No No No Erases or clears away aythig draw by the pe or stamped from the stage. Places the pe i a dow positio, allowig drawig operatios to occur as the pe is moved aroud the stage. Disables drawig operatios by liftig the pe. Specifies the color to be used whe drawig. Chages the color used whe drawig by a specified amout. Specifies the color to be used whe drawig based o a umeric rage i which 0 is red (at the low ed of the spectrum) ad 100 equals blue (at the high ed of the spectrum). Modifies the shadig used whe drawig by a specified amout. Specifies the shade to be used whe drawig based o a umeric rage i which 0 is the darkest possible shadig ad 100 represets the maximum possible amout of light. Modifies the thickess of the pe based o a umeric icremet. Specifies the thickess or width of the pe used whe drawig. Draws or stamps the image of a sprite oto the stage. Loopig, Coditioal Logic, ad Evet Programmig Cotrol blocks automate the executio of scripts, pause script executio, ad sed messages to other sprites, allowig sprites to sychroize their executio. There are also cotrol blocks that let you set up loops to repeatedly execute collectios of code blocks as well as cotrol blocks that let you coditioally execute other code blocks based o whether or ot a test coditio evaluates as true. Cotrol blocks are colored gold. Table 3.5 outlies all of the code blocks that fit ito this category. You will lear more about cotrol blocks i Chapter 9. Sesig Sprite Locatio ad Evirometal Iput Sesig blocks determie the locatio of the mouse-poiter, its distace from other sprites, ad whether a sprite is touchig aother sprite. Sesig blocks are
87 66 Chapter 3 A Review of the Basic Compoets of Scratch Projects Table 3.5 Scratch Cotrol Blocks Block Moitor Descriptio No No No No No Executes the script to which it has bee attached wheever the IDE s gree flag butto is pressed. Executes the script to which it has bee attached wheever a specified keyboard key is pressed. Executes the script to which it has bee attached wheever the user clicks o the sprite to which the script belogs. Pauses script executio for a specified umber of secods, after which the script resumes its executio. Repeatedly executes all of the code blocks embedded iside it. No Repeats the executio of all the code blocks embedded iside it a specified umber of times. No No No No No Specifies a broadcast message to all sprites without pausig script executio. Seds a broadcast message to all sprites to trigger a predefied actio ad the pauses script executio, waitig util all sprites have completed their assiged actio before allowig the script i which the block resides to cotiue executig. Executes the scripts to which it has bee attached whe a specified broadcast message is received. Repeatedly executes all of the code blocks embedded withi the cotrol for as log as the specified coditio evaluates as true. Executes all of the code blocks embedded withi the cotrol if the specified coditio evaluates as true. No No No Executes all of the code blocks embedded i the top half of the cotrol (betwee the If a Else) if the specified coditio evaluates as true ad executes all of the code blocks embedded i the bottom half of the cotrol (after Else) if the coditio evaluates as beig false. Pauses script executio util a specified coditio becomes true. Repeats all of the code blocks embedded iside it for as log as a tested coditio evaluates as true.
88 Eight Categories of Scratch Blocks 67 Table 3.5 (Cotiued) Block Moitor Descriptio No Halts a script s executio. No Halts the executio of all scripts for all sprites i the applicatio. colored sky blue. There are sesig blocks that ca be used to iteract with Scratch boards, allowig applicatios to detect whe the sesor board s buttos or slider are beig pressed. Table 3.6 outlies all of the code blocks that fit ito this category. Note A Scratch board is a special piece of hardware that you ca purchase from the Scratch website ad attach to your computer. Oce it is attached, you ca use a sesor board to collect ad process eviromet- ad user-provided iput. You will lear how to programmatically iteract with ad cotrol Scratch boards i Chapter 14, Collectig Exteral Iput Usig a Scratch Sesor Board. You will lear more about sesig blocks i Chapter 6, Sesig Sprite Positio ad Cotrollig Evirometal Settigs. Workig with Numbers Numbers blocks perform arithmetic operatios, geerate radom umbers, ad compare umeric values to determie their relatioship to oe aother. Numbers blocks are gree. There are umbers blocks that ca be used to roud umeric values ad to execute a host of mathematical fuctios like determiig absolute value or square root of a umber. Table 3.7 outlies all of the code blocks that fit ito this category. You will lear more about umber blocks i Chapter 8, Doig a Little Math. Storig ad Retrievig Data Variables blocks store ad retrieve umeric values i computer memory. You will eed to use variables to store data as your applicatio executes. For example, if you create a game that challeges the player to try ad guess a radomly
89 68 Chapter 3 A Review of the Basic Compoets of Scratch Projects Table 3.6 Scratch Sesig Blocks Block Moitor Descriptio No No No No No No No No No Retrieves the locatio of the mouse-poiter o the X-axis. Retrieves the locatio of the mouse-poiter o the Y-axis. Retrieves a Boolea value of true or false, depedig o whether a mouse butto is pressed. Retrieves a Boolea value of true or false, depedig o whether a specified key is pressed. Retrieves a Boolea value of true or false, depedig o whether the sprite is touchig a specified sprite, edge, or mouse-poiter as selected from the block s pull-dow meu. Retrieves a Boolea value of true of false, depedig o whether the sprite is touchig a specified color. Retrieves a Boolea value of true of false, depedig o whether the first specified color iside the sprite is touchig the secod specified color o the backgroud or o aother sprite. Retrieves a umeric value represetig a sprite s distace from aother sprite or from the mouse-poiter. Resets the timer back to its default value of zero. Yes Retrieves a umeric value represetig the umber of secods that the timer has ru. No Retrieves the property value (x positio, y positio, directio, customer #, ad size of volume) for the backgroud of a specified sprite. Yes Retrieves a umeric value, from 1 to 100, represetig the volume of the computer s microphoe. Yes Retrieves a Boolea value of true or false whe a soud value of 30 or greater is detected through the computer s microphoe. Yes Retrieves the value beig reported by oe of the sesors o a Scratch board. Yes Retrieves a Boolea value of true or false, depedig o whether a specified sesor is beig pressed. geerated umber, you will eed to use a variable to store ad refer back to this umber. Variables ca be used i cojuctio with coditioal programmig logic to cotrol the executio of other code blocks. Variables ca also be used to cotrol
90 Eight Categories of Scratch Blocks 69 Table 3.7 Scratch Numbers Blocks Block Moitor Descriptio No No No No No No No No No No No No No No Adds two umbers together ad geerates a result. Subtracts oe umber from aother ad returs the result. Multiplies two umbers together ad geerates a result. Divides oe umber ito aother ad returs the result. Geerates a radom umber withi the specified rage. Returs a Boolea value of true or false, depedig o whether oe umber is less tha aother. Returs a Boolea value of true or false, depedig o whether oe umber is equal to aother. Returs a Boolea value of true or false, depedig o whether oe umber is greater tha aother. Returs a Boolea value of true or false, depedig o whether two separately evaluated coditios are both true. Returs a Boolea value of true or false, depedig o whether either of two separately evaluated coditios is true. Reverses the Boolea value from true to false or false to true. Retrieves the remaider portio of a divisio operatio betwee two umbers. Returs the earest iteger value for a specified umber. Returs the result of the selected fuctio (abs, sqrt, si, cos, ta, asi, acos, ata, L, log, E^, ad 10^) whe applied to the specified umber. the repeated executio of code blocks embedded withi code block loops. Variables blocks are colored orage. You ca create ad ame custom variables blocks ad assig them a startig value. You ca also modify their values durig script executio. Other code blocks ca retrieve variable values ad use them as iput. Table 3.8 outlies all of the code blocks that fit ito this category. You will lear more about variables blocks i Chapter 7.
91 70 Chapter 3 A Review of the Basic Compoets of Scratch Projects Table 3.8 Scratch Variables Blocks Block Moitor Descriptio No No Yes Modifies the value assiged to a umeric value stored i a variable by the specified amout. Assigs a value to a umeric variable. Retrieves the value assiged to a variable. Gettig Help with Code Blocks I additio to bookmarkig ad referrig back to the tables provided i this chapter to fid out what a give code block does, you ca view help iformatio for ay Scratch code block by right-clickig o the code block i the blocks palette, as demostrated i Figure Alteratively, you ca right-click o a code block oce it has bee added to the scripts area to access a lik to the block s help file, as demostrated i Figure By clickig o the Help lik i the popup meu that is displayed, you ca display help iformatio for that cotrol. For example, Figure 3.19 shows the help iformatio that is available for the forever code block. As Figure 3.19 shows, the help iformatio that is displayed explais the purpose of the code block ad demostrates its usage. Figure 3.17 Accessig help for a give Scratch code block. Figure 3.18 Accessig help for a Scratch code block that has bee added to the scripts area.
92 Summary 71 Figure 3.19 Displayig the help widow for the forever code block. Summary This chapter provided a quick referece that outlied the purpose ad usage of all of the code blocks provided by Scratch. You may wat to bookmark this chapter to help make it easy to retur to ad take advatage of this iformatio. This chapter explaied the three types of code blocks supported by Scratch ad outlied their relatioship to oe aother. The chapter the provided a explaatio of all 100 plus Scratch code blocks, goig over them category by category. O top of all this, you leared how to work with ad cofigure moitors ad to access help iformatio for idividual code blocks.
93 This page itetioally left blak
94 chapter 4 Mr. Wiggly s Dace A Quick Scratch Project So far, you have bee preseted with a overview of Scratch ad its capabilities ad leared how to work with its IDE. You have also bee give a overview of all of the code blocks that make up the Scratch programmig laguage ad leared the basic steps ivolved i creatig Scratch applicatios. Now that you are more familiar with Scratch ad its key compoets, let s put this ew kowledge to use by creatig a ew Scratch applicatio project, examiig i greater detail the steps ivolved i creatig ad executig Scratch applicatios. The topics covered i this chapter iclude: A review of the programmig cocepts that Scratch ca teach you A detailed overview of how to build Scratch applicatios Learig how to distribute your Scratch programs o CD-ROM Programmig with Scratch As a begier s programmig laguage, Scratch teaches you a umber of critical programmig cocepts that you will be able to later rely o should you decide to make the jump to other more traditioal ad idustrial-stregth programmig laguages like Microsoft Visual Basic, Cþþ, JavaScript, ad 73
95 74 Chapter 4 Mr. Wiggly s Dace A Quick Scratch Project AppleScript. The programmig cocepts that you ca lear from Scratch iclude: Sequetial Processig. This ivolves the processig of applicatio code blocks, i the order that they are laid out, startig at the begiig of a script file ad cotiuig to the ed of the script. Coditioal Programmig Logic. This ivolves the coditioal executio of code blocks based o data collected durig applicatio executio. Use of Variables. This ivolves the storage, retrieval, ad modificatio of data durig applicatio executio. Iterative Processig. This ivolves the repeated executio of code blocks to process large amouts of iformatio or to cotrol the repeated executio of code blocks required to direct the executio of a game or applicatio. Boolea Logic. This ivolves the applicatio of programmig logic that executes based o the aalysis of true/false data provided by Scratch durig program executio. Iterface Desig. This ivolves the developmet of user-friedly ad ituitive applicatio stage layout, makig it easy for users to iteract with applicatios. Program Sychroizatio. This ivolves the passage ad receipt of messages betwee applicatio scripts for the purpose of coordiatig the executio of differet parts of a applicatio. Evet Hadlig. This ivolves the iitiatio of script executio based o the occurrece of predefied evets, such as the pressig of keyboard keys, the pressig of the gree flag key, or the receipt of a sychroizatio message. Applicatio ad Game Developmet. This ivolves the creatio of differet types of computer applicatio projects. Sprite Programmig. This ivolves the use of sprites as the basis for developig graphical programs. Applicatio Troubleshootig. This ivolves the idetificatio, locatio, ad elimiatio of programmig errors, or bugs, that prevet applicatios from executig as they are supposed to.
96 Creatig the Mr. Wiggly s Dace Applicatio 75 You will lear more about each of these programmig cocepts as you make your way through the remaider of this book. Note As powerful ad fu as Scratch is, there are some programmig cocepts that it does ot teach. These cocepts iclude the storage of collectios of data i arrays, the ability to process file iput ad output, the ability to orgaize applicatio code ito procedures, ad the ability to support advaced object-orieted programmig techiques. However, as a first-time programmer, these cocepts ca be challegig to lear, ad by omittig them, the developers of Scratch have produced a streamlied yet powerful learig eviromet, which will prepare you to later make the jump to programmig laguages that support these advaced programmig cocepts. Creatig the Mr. Wiggly s Dace Applicatio The rest of this chapter is dedicated to leadig you through the developmet of the Mr. Wiggly s Dace applicatio. I this Scratch applicatio, a short, roud, ad comical cartooish character amed Mr. Wiggly daces aroud the stage to music, as demostrated i Figure 4.1. Because Mr. Wiggly is bashful, his ski chages color as he daces, as demostrated i Figure 4.2. Although ot immediately obvious whe viewed i black ad white, if you compare the color of Mr. Wiggly i Figures 4.1 ad 4.2, you will otice that he has defiitely begu to blush, betrayig his discomfort at dacig i frot of a audiece. Figure 4.1 Mr. Wiggly practices his dace moves, dacig back ad forth across the stage.
97 76 Chapter 4 Mr. Wiggly s Dace A Quick Scratch Project Figure 4.2 The bashful Mr. Wiggly s ski color chages as he daces. Figure 4.3 Mr. Wiggly pauses at the ed of each dace oly to decide to keep dacig. At the ed of each dace, Mr. Wiggly pauses for a momet to reflect o how thigs are goig before decidig to keep o dacig, as demostrated i Figure 4.3. The Mr. Wiggly s Dace applicatio project will be created by followig a series of steps, as outlied here: 1. Creatig a ew Scratch applicatio project. 2. Addig a project backgroud.
98 Creatig the Mr. Wiggly s Dace Applicatio Addig ad removig sprites to ad from the project. 4. Importig a music file ito the applicatio. 5. Scriptig audio playback. 6. Addig the programmig logic required to make Mr. Wiggly dace. 7. Savig ad executig your work. Sice this book has yet to provide a detailed explaatio of how to work with all of the Scratch code blocks used i this applicatio project, brief explaatios will be provided. You will lear the is ad out of programmig with code blocks i Chapters 5 through 12. As you make your way through each of the steps i this project, try ad keep your focus o the overall process beig followed ad do ot get caught up i the specifics. Later, oce you have fiished reviewig Chapters 5 through 12, you ca always retur ad review this project agai ad clear up ay questios you may have. Step 1: Creatig a New Scratch Project The first step i creatig a Scratch project is to start Scratch. Doig so results i the automatic creatio of a ew Scratch project. New Scratch projects come equipped with a sigle sprite with two costumes represetig a cat. You ca choose to icorporate this sprite ito your applicatio or to remove it. If, o the other had, Scratch has already bee started ad you have bee workig with it for a while, you ca create ad ope a ew Scratch applicatio project by clickig o the New butto located o the Scratch meu bar. I respose, a ew project is opeed i the IDE, as show i Figure 4.4. Step 2: Addig a Backgroud to the Stage With your ew Scratch applicatio project ow created, it is time to get to work. Let s begi by addig a suitable backgroud to the stage that will help set the mood of the applicatio. Backgrouds are associated with the stage, so to add a backgroud to your applicatio, you must click o the blak stage thumbail located i the sprite list. Oce selected, the stage thumbail is highlighted with a blue outlie, as show i Figure 4.5. Oce you have selected the stage thumbail, you ca modify its backgroud by clickig o the Backgrouds tab located at the top of the scripts area. Whe you do so, the curretly assiged stage backgroud is displayed, as show i Figure 4.6.
99 78 Chapter 4 Mr. Wiggly s Dace A Quick Scratch Project Figure 4.4 New Scratch applicatio projects come supplied with a sigle sprite. Figure 4.5 Selected thumbails i the sprite list are highlighted with a blue outlie. To replace the curretly assiged blak backgroud with somethig more iterestig, click o the Import butto. This will ope the Import Backgroud widow. Oce opeed, click o the Idoors folder, select the chalkboard thumbail, as show i Figure 4.7, ad click o the OK butto.
100 Creatig the Mr. Wiggly s Dace Applicatio 79 Figure 4.6 The Backgrouds tab provides the ability to create, import, edit, ad reame backgrouds. Figure 4.7 Importig a ew backgroud ito your Scratch applicatio project. Oce imported, the ew backgroud will be added to the applicatio s curret list of backgroud files, as show i Figure 4.8. As you ca see, the thumbail is automatically assiged a ame ad a umber. Sice this applicatio oly requires oe backgroud, you ca remove the default blak backgroud amed backgroud1 from your project by clickig o the Delete This Costume butto, which is located to the right of the backgroud s picture ad represeted by a roud X butto.
101 80 Chapter 4 Mr. Wiggly s Dace A Quick Scratch Project Figure 4.8 Scratch applicatios ca have ay umber of backgrouds ad ca switch betwee them durig executio. Choose New Sprite from File butto Figure 4.9 Click o the Choose New Sprite from File butto to access a collectio of ready-made sprites. Tip Removig backgrouds, costumes, ad soud files o loger eeded by your Scratch applicatios will reduce their size. This ca be of critical importace should you decide to upload them to the Scratch website. There is a 10MB project size limit at that site. Graphic ad audio files ted to be relatively large, so removig ay that you do ot eed ca have a sigificat impact o the size of your applicatios. Step 3: Addig ad Removig Sprites The ext step i the developmet of this Scratch project is to add a sprite represetig Mr. Wiggly to the project ad to remove the cat sprite, which is ot eeded i this applicatio. To add the sprite represetig Mr. Wiggly, click o the Choose New Sprite from File butto, as show i Figure 4.9. This butto is the middle butto that makes up the collectio of ew sprite buttos, located just beeath the stage ad just above the sprite list. Scratch provides ready access to all kids of sprites, orgaized ito the followig six folders:
102 Creatig the Mr. Wiggly s Dace Applicatio 81 Aimals Fatasy Letters People Thigs Trasportatio The sprite that you wat to use to represet Mr. Wiggly is located i the People folder. Oce clicked, the Choose New Sprite from File butto istructs Scratch to display the New Sprite widow, which provides access to the six folders listed above. Ope the People folder ad the scroll dow util you locate the roudma sprite, as show i Figure Select the roudma sprite by clickig o it ad the click o the OK butto. The New Sprite widow will close ad the ew sprite will be added to the middle of the stage, as show i Figure Figure 4.10 Selectig the sprite that will be used to represet Mr Wiggly.
103 82 Chapter 4 Mr. Wiggly s Dace A Quick Scratch Project Figure 4.11 A thumbail represetig the sprite is also added to the sprite list. Whe cotrasted agaist the stage s backgroud, Mr. Wiggly s default placemet i the middle of the stage makes it look like he is floatig o air. To put thigs ito proper perspective, drag ad drop Mr. Wiggly about oe ich lower dow the stage, so that it looks like he is stadig o the floor. Sice the Mr. Wiggly s Dace applicatio does ot eed the default cat sprite, go ahead ad remove this sprite from the applicatio project by selectig the Delete butto o the Scratch toolbar ad the clickig o the thumbail for the cat located i the sprite list. Tip You ca also remove the cat sprite from the applicatio by right-clickig o its thumbail ad the selectig Delete from the popup meu that is displayed. Step 4: Addig Mr. Wiggly s Music Now that you have take care of the sprites eeded by the applicatio, it is time to import the soud file. To do this, click o the thumbail represetig the stage i the sprite list ad the click o the Souds tab i the scripts area. I respose, Scratch will display all of the soud files belogig to the sprite. By default, every
104 Creatig the Mr. Wiggly s Dace Applicatio 83 Figure 4.12 All sprites supplied by Scratch come equipped with the same soud file. sprite i a Scratch applicatio is assiged a commo soud file amed pop, as show i Figure Scratch provides ready access to all kids of prerecorded audio files. The ame of the soud file that Mr. Wiggly will dace to is Eggs. To add this file to the sprite, click o the Import butto. I respose, Scratch will display the Import Soud widow, which by default cotais eight folders, listed ext, i which Scratch stores its audio files. Aimal Effects Electroic Huma Istrumets Music Loops Percussio Vocals Drill dow ito the Music Loops folder by double-clickig o it. Locate ad click o the Eggs file, as show i Figure Scratch will immediately play the file, so you ca hear what it souds like. Click o the OK butto to import the soud file ito your applicatio project, as demostrated i Figure Note that for each soud file, a umber of pieces of iformatio are displayed. You ca see the ame of the file, the legth of time that it takes to play the file, ad the file s size. Note that the Eggs soud file takes
105 84 Chapter 4 Mr. Wiggly s Dace A Quick Scratch Project Figure 4.13 Importig a soud file ito a Scratch applicatio project. Figure 4.14 You ca add ay umber of soud files to a sprite. 16 secod to play. You will eed to remember this iformatio a little later whe programmig the playback of this soud file. The default pop soud file is ot eeded by this applicatio; therefore, you ca delete it by clickig o the roud Delete This Soud butto located at the bottom-right side of the soud file etry.
106 Creatig the Mr. Wiggly s Dace Applicatio 85 Step 5: Playig the Dace Music It is time to begi puttig together the program code logic required to make your ew applicatio work. I total you will eed to create two scripts for this project: oe for the stage ad aother for the sprite represetig Mr. Wiggly. The script belogig to the stage will be made up of code blocks that are resposible for playig the applicatio s backgroud music. The script belogig to the sprite will cotai the programmig logic required to make Mr. Wiggly dace. The first step i the developmet of the stage s script is to click o the Cotrol butto i the blocks palette ad the to drag ad drop a istace of the whe gree flag clicked block oto the scripts area, as demostrated i Figure This hat code block will automatically execute the script to which it is attached wheever the gree flag butto is clicked. Sice the applicatio s backgroud music is supposed to be played over ad over agai for as log as the applicatio rus, you eed to set up a loop that will repeatedly play the soud file. To set this up, drag ad drop a istace of the forever code block to the scripts area, attachig it to the bottom of the whe gree flag clicked block, as show i Figure Now that you have the loop set up, click o the Soud butto located at the top of the blocks palette ad the drag ad drop a istace of the play soud code block oto the scripts area, embeddig it iside the forever code block. Next, click o the pull-dow meu located o the right-had side of the code block ad select Eggs from the list that appears. At this poit the script that you are developig should look like the example show i Figure Note Scratch automatically populates the play soud code block with a list of all of the soud files that you added previously to the stage, makig it easy for you to access them whe workig with soud code blocks. At this poit you oly eed to add oe last code block to the script to fiish it up. To do so, click o the Cotrol butto located at the top of the blocks palette ad the drag ad drop a istace of the wait secs code block over the scripts area, isertig it iside the forever code block, immediately followig the play soud block, as show i Figure This block is eeded to pause the loop for 16 secods, allowig for the complete playback of the soud file, before the loop repeats ad begis playig it agai.
107 86 Chapter 4 Mr. Wiggly s Dace A Quick Scratch Project Figure 4.15 This block will be used to automatically execute the script wheever the gree flag butto is clicked. Figure 4.16 The forever block will repeat the executio of ay code block that you embed withi it.
108 Creatig the Mr. Wiggly s Dace Applicatio 87 Figure 4.17 Usig the play soud code block to play back the Eggs soud file. Figure 4.18 Pausig loop executio to allow playback of the soud file to complete. Note Now that this script has bee writte, you ca test it out by double-clickig o it. I respose, Scratch will repeatedly play back the soud file. Oce you are coviced that everythig is workig correctly, click o the red Stop Everythig butto to halt the script s executio so that you ca move o to the ext step i the developmet of this applicatio. Note I additio to playig a audio file usig the combiatio of the soud ad cotrol blocks show i Figure 4.18, you ca istead use the code block show here, which does the same thig as these two code blocks. Step 6: Makig Mr. Wiggly Dace Now that you have fiished work o the stage s script, it is time to write the script that makes Mr. Wiggly dace. To do so, click o the thumbail of the sprite represetig Mr. Wiggly (i the sprite area). I respose, Scratch should clear out the script s area ad automatically select the Scripts tab for you so that you ca begi script developmet.
109 88 Chapter 4 Mr. Wiggly s Dace A Quick Scratch Project The first step i the developmet of this is to click o the Cotrol butto i the blocks palette ad the to drag ad drop a istace of the whe gree flag clicked block oto the scripts area, as demostrated i Figure This hat code block will automatically execute the script to which it is attached wheever the gree flag butto is clicked. I this applicatio, Mr. Wiggly is supposed to dace over ad over agai without stoppig (util the user stops ruig the applicatio). To set this up, drag ad drop a istace of the forever code block oto the scripts area ad attach it to the bottom of the whe gree flag clicked block, as show i Figure Next, it is time to add a pair of code statemets that will move Mr. Wiggly 25 steps to the right ad the pause for two secods. This is accomplished by draggig ad droppig the move steps ad wait secs blocks to the scripts area, embeddig them iside the forever code block, as show i Figure Note Figure 4.19 Settig up the script to execute whe the gree flag is clicked. Figure 4.20 Addig a loop to the script to repeat the executio of embedded code blocks. Figure 4.21 Addig the programmig logic that makes Mr. Wiggly dace his first step.
110 Creatig the Mr. Wiggly s Dace Applicatio 89 Figure 4.22 Addig the remaiig code blocks required to complete Mr. Wiggly s dace routie. that by default the move steps block is set to 10. You will eed to replace this with a value of 25. Next, you eed to add a series of move steps ad wait secs code blocks, which, whe executed, will move Mr. Wiggly 25 steps to the right followed by four moves to the left at 25 steps each ad the aother two moves back towards the right. This is accomplished by addig seve sets of a code block, as show i Figure To complete the developmet of this script, you eed to add two looks blocks, as show i Figure The chage effect by code block is used to modify Mr. Wiggly s color each time the loop fiishes its executio, simulatig the feelig of embarrassmet that Mr. Wiggly experieces whe he daces. Lastly, the thik for secs code block is used to display a text message i a popup bubble that shows Mr. Wiggly thikig about ad the decidig to keep dacig.
111 90 Chapter 4 Mr. Wiggly s Dace A Quick Scratch Project Figure 4.23 Modifyig Mr. Wiggly s color ad displayig his thoughts. Step 7: Savig ad Executig Your New Scratch Applicatio At this poit your copy of the Mr. Wiggly s Dace applicatio should be complete. All that remais is for you to save the applicatio ad the to execute it ad see how it looks whe ruig. To save your applicatio, click o the Save butto located o the IDE s meu bar. I respose, Scratch will display the Save Project widow, as demostrated i Figure 4.24, promptig you to specify the ame ad locatio where you wat to store your ew applicatio. I additio, Scratch provides the opportuity to eter your ame as the project author ad to eter otes describig the project i the Project Author ad About This Project text fields. Oce you are doe, click o the OK butto to save your Scratch applicatio project. Oce you have saved your work, ru the applicatio to see how it works. Sice both of the applicatio s scripts are cofigured to execute wheever the gree flag butto is pressed, all you have to do is click o that butto ad sit back ad watch as the bashful Mr. Wiggly daces about the stage for your amusemet.
112 Distributig Scratch Projects 91 Figure 4.24 Savig your copy of the Mr. Wiggly s Dace applicatio project. Distributig Scratch Projects Scratch is a iterpreted programmig laguage. This meas that ulike some programmig laguages, such as Visual Basic ad Cþþ, which compile their applicatios ito a executable file that ca the be ru o other computers without requirig that the programmig laguage be istalled, Scratch applicatios ca oly execute whe ru withi the Scratch IDE (or o the Scratch website at Therefore, if you wat to distribute your Scratch applicatios ad have them execute o someoe else s computer, you must first see to it that Scratch is istalled o the other computer, or you must create a special applicatio distributio CD that icludes Scratch system files required to ru your applicatio whe Scratch has ot bee istalled. Note You ca also share access to your Scratch applicatio projects by postig them o the Scratch website ad poitig your frieds to that website, where they ca view ad ru them usig a Java-eabled web browser. You will lear all about the steps ivolved i sharig your Scratch applicatios this way i Chapter 13, Sharig Your Scratch Projects Over the Iteret.
113 92 Chapter 4 Mr. Wiggly s Dace A Quick Scratch Project Figure 4.25 Bur a copy of the files show i this figure alog with your Scratch applicatio file to create a distributable Widows CD-ROM. Distributig Scratch Applicatios to Widows Computers The files that you eed to bur to your distributio CD-ROM vary, depedig o whether you are workig with Microsoft Widows or Mac OS X. Whe workig with Microsoft Widows, you will eed to bur the followig files idetified i Figure 4.25, as well as a copy of your Scratch applicatio, to a CD-ROM. Each of the files listed ca be foud i the folder i which you istalled Scratch, which o Microsoft Widows is C:\Program Files\Scratch by default. Scratch.exe Scratch.image Scratch.ii ScratchPlugi.dll Mpeg3Plugi.dll Licese.txt Note The reaso for icludig the Licese.txt file, which is Scratch s licese documet, is to esure that ayoe you distribute your CD-ROM to will kow the terms of the licese agreemet. Icludig this file will also keep you out of legal trouble.
114 Distributig Scratch Projects 93 Distributig Scratch Applicatios to Mac OS X Computers If you are workig with Mac OS X ad wat to create a distributio disc to share your creatios with other Mac users who do ot have Scratch istalled o their computers, you may do so by burig a CD-ROM cotaiig your Scratch applicatio projects as well as the followig Scratch system files, all of which are available i Scratch s istallatio folder. Scratch.app Scratch.image Licese.txt Istructios for Executig Your Applicatio from a CD-ROM Oce you have bured a CD-ROM for your Scratch applicatio, you eed to tell your frieds how to execute it, which ca be doe by double-clickig o Screatch.exe (Widows) or Scratch.app (Mac OS X), which will start the Scratch IDE, after which your applicatio ca be accessed by clickig o the IDE s Ope butto. Alteratively, for Widows users, you might wat to cosider addig a batch file for each applicatio that you added to the CD-ROM that whe executed will ru oe of your Scratch applicatios. You ca do this by opeig your preferred text editor (such as Notepad) ad keyig i a sigle statemet usig the followig sytax. Scratch.exe Scratch.image ScratchProject.sb Here, Scratch.exe is the ame of the Scratch executable that starts Scratch. Scratch.image is a required Scratch system file, ad ScratchProject.sb represets the ame of a Scratch applicatio that you have added to the CD-ROM. Note that the.sb file extesio has bee icluded. Oce you have typed i this statemet, save the text file with a fileame that eds with a.bat file extesio (MrWiggly.bat, HelloWorld.bat, etc.). Whe a batch file is added to the CD-ROM alog with all of the files already listed, your frieds ca start your Scratch applicatio by double-clickig o it. Oce double-clicked, the batch file will ope Scratch ad load your Scratch applicatio project ito it, makig it ready for executio.
115 94 Chapter 4 Mr. Wiggly s Dace A Quick Scratch Project Summary This chapter walked you through the developmet of your secod Scratch project. I learig how to create Mr. Wiggly s Dace, you leared the fudametal steps ivolved i creatig ad executig Scratch applicatios. This icluded learig how to chage stage backgrouds ad work with sprites. Although detailed istructio o how to work with differet code blocks ad souds is ot covered util later chapters, you received a quick overview of how to work with a umber of cotrol, motio, looks, ad soud blocks, ad you leared how to import audio files ad sprites ito your Scratch applicatios.
116 Part II Learig How to Write Scratch Programs
117 This page itetioally left blak
118 chapter 5 Movig Thigs Aroud This chapter is the first of eight chapters desiged to teach you how to work with all of the code blocks that make up the Scratch programmig laguage. This chapter s focus is o demostratig how to work with motio code blocks. Usig these blocks, you will be able to create Scratch applicatios that ca move sprites aroud the stage, rotate sprites, poit them i differet directios, chage sprite locatio, detect collisios with the edge of the stage, ad report o a sprite s directio ad coordiates. This chapter also itroduces you to Scratch cards as a meas for learig how to perform differet types of tasks. You will also lear how to create a ew virtual fish tak applicatio. The major topics covered i this chapter iclude: Learig how to move ad rotate sprites Learig how to chage sprite directio ad locatio Learig how to chage sprite locatio ad to detect collisios with the edge of the stage Learig how to retrieve ad report iformatio about a sprite s coordiates ad directio 97
119 98 Chapter 5 Movig Thigs Aroud Workig with Motio Code Blocks To move sprites aroud the stage whe your Scratch applicatios execute, you eed to lear how to work with motio code blocks. As previously stated, motio blocks cotrol sprite placemet, directio, rotatio, ad movemet. I total, Scratch provides access to 16 differet motio blocks, which you ca work with by clickig o the Motio butto located at the top of the blocks palette ad the draggig ad droppig motio blocks oto the scripts area, where you ca cofigure them ad use them i creatig scripts. If you look closely at the various motio code blocks, you will otice that Scratch orgaizes them ito six subgroupigs, each of which is separated by a blak space i the blocks palette. These sub groupigs iclude: Motio blocks that move ad rotate sprites Motio blocks that poit sprites i differet directios or towards differet objects Motio blocks that chage a sprite s locatio ad cotrol whether a sprite jumps to its ew locatio or glides to it Motio blocks that chage a sprite locatio by settig or modifyig the value of its X-axis ad Y-axis coordiates A motio block that cotrols a sprite s movemet whe it touches the edges of the stage Motio blocks that report o a sprite s positio ad directio Examples of how to work with the motio code blocks i each of these subgroups are provided throughout the rest of this chapter. Movig ad Rotatig Sprites Scratch provides access to three motio blocks that move sprites ad rotate them o their axis. These code blocks are show i Figure 5.1. The first of these blocks allows you to specify the umber of steps that a sprite should be moved o the stage (i whatever directio the sprite is curretly poitig). By default, the code block specifies a value of 10. However, you may
120 Movig ad Rotatig Sprites 99 Figure 5.1 These cotrol blocks are desiged to give you cotrol over the relative movemet ad rotatio of sprites. chage this value to suit your eeds. You ca eve eter a egative value to move the sprite i the opposite directio that it is poitig. I additio, you ca drag ad drop ay reporter block you wat ito this code block s etry field whe specifyig a value. The ext two code blocks provide the ability to rotate a sprite o its axis, clockwise ad couterclockwise, as idicated by the directio of the arrow displayed o the blocks. The followig sample script demostrates how to use the first two blocks to move a sprite aroud the stage i a clockwise maer. This script executes wheever the gree flag butto is clicked. Oce this evet has occurred, four pairs of motio code blocks are executed at oe-secod itervals. This applicatio uses the default cat sprite that is supplied as part of every ew Scratch project. To create ad test your ow copy of the applicatio, create a ew Scratch applicatio, click o the thumbail of the cat sprite, drag it to the upperleft corer of the stage, ad shrik it to about 50% of its ormal size ad the assemble the script.
121 100 Chapter 5 Movig Thigs Aroud The first two motio blocks i the script move the sprite 400 steps. Sice the cat, by default, is poited 90 degrees to the left, this will move the sprite from the upper-left corer of the stage to the upper-right corer of the stage. The ext pair of motio blocks moves the sprite dow to the bottom-right corer of the stage. The third pair of motio blocks moves the sprite to the bottom-left corer of the stage, ad the last pair of motio blocks moves it back to the upper-left corer of the stage. Note All of the sprites supplied by Scratch have a predefied rotatio axis. You ca chage the rotatio axis for these sprites ad set the rotatio poit for ew sprites that you create or import ito Scratch by editig the sprite usig Scratch s Pait Editor program ad the specifyig a ew rotatio axis usig the program s Set Rotatio Poit cotrol. A sprite s rotatio is also affected by the selectio of oe of the three rotatio buttos located o the left-had side of the Sprite s ifo area. If you look at the cat sprite s rotatio settig, you will see that the cat sprite is cofigured by default to rotate freely. Figure 5.2 demostrates the movemets of the cat sprite as it moves from corer to corer, clockwise aroud the scree. Figure 5.2 The cat s directio is chaged by 90 degrees immediately after each move, readyig it for its ext move.
122 Settig Sprite Directio 101 If you wat, you ca modify the script to move the sprite aroud the stage i a couterclockwise directio by modifyig it, as demostrated here: Settig Sprite Directio Scratch provides access to two motio blocks that ca be used to poit a sprite i a specified directio or to poit a sprite towards the mouse-poiter or a specified sprite. These code blocks are show i Figure 5.3. The first of these blocks allows you to poit a sprite i a particular directio as specified by the assigmet of a umeric value represetig the umber of degrees that the sprite should be tured. You ca either select a value of 0=up, 90 = right, 90 = left,or180 = dow from the block s drop-dow list or type i a iteger value i the rage of 0 to 360. For example, the followig script demostrates how to rotate a sprite 360 degrees, 90 degrees at a time at oe-secod itervals.
123 102 Chapter 5 Movig Thigs Aroud Figure 5.3 These code blocks ca be used to poit a sprite towards a specified directio or object. Figure 5.4 A example of the four possible directios that the poit i directio code block ca poit a sprite. Figure 5.5 The cat rotates as ecessary to cotiue facig the mouse-poiter. This example uses the default cat sprite. Figure 5.4 shows a example of the four directios that the sprite turs whe the script is executed. Note that for this example to work, you must click o the Ca Rotate butto i the sprite ifo area (allowig the sprite to rotate over a rage of 360 degrees). The secod motio block show i Figure 5.5 lets you poit a sprite towards either the mouse-poiter or aother sprite, as demostrated i the followig script.
124 Repositioig a Sprite 103 I this example, the sprite is cotiuously repositioed so that is poits towards the mouse-poiter. Therefore, wheever the mouse-poiter is moved aroud the stage, the image of the cat follows, as demostrated i Figure 5.5. Note I order for the sprite show i Figure 5.5 to cotiuously repositio itself, the motio block must be embedded withi a cotrol block that sets up a loop, repeatedly executig the motio block, allowig it to react every time the mouse-poiter is moved. Repositioig a Sprite Scratch provides access to three motio blocks that move a sprite to a specified coordiatio locatio o the stage, move a sprite to the locatio curretly occupied by the mouse-poiter or aother sprite, or move a sprite to a specified coordiatio positio over a specified umber of secods. These code blocks are show i Figure 5.6. The first of these three motio blocks allows you to repositio a sprite to ay locatio o the stage by specifyig X-axis ad Y-axis coordiates for the sprite. For example, the followig script demostrates how to repositio a sprite i the middle of the stage, poitig it i a 90-degree directio. Figure 5.6 These code blocks ca be used to move a sprite to a specific locatio.
125 104 Chapter 5 Movig Thigs Aroud Figure 5.7 As this figure demostrates, the sprite automatically moves aroud the stage, followig the mousepoiter. The followig script demostrates how to move a sprite to the locatio o the stage curretly occupied by the mouse-poiter. Figure 5.7 shows a example of the output that is geerated whe this script is ru. If you look closely, you will see that i each of the three examples, the cat sprite remais positioed directly uder the mouse-poiter o matter where it is moved o the stage. This ext script demostrates how to repositio a sprite to a specific locatio o the stage. Istead of simply makig the sprite appear at a specified locatio, as demostrated i the previous two examples, this script repositios the sprite by movig or glidig to its ew positio i a smooth motio.
126 Chagig Sprite Coordiates 105 Chagig Sprite Coordiates Scratch provides four motio blocks that modify the locatio of a sprite o the stage either by assigig it ew coordiates or by chagig the sprite s coordiates by icremetig or decremetig their values. These code blocks are show i Figure 5.8. The followig script demostrates how to move a sprite across the stage i a series of eight steps. Whe first started, the script moves the sprite to the left-had side of the stage, ad the, usig a loop, the sprite is moved by icremetig the value assiged to the X-axis coordiate by 50 ad its Y-axis coordiate by 10 each time the loop repeats itself. As a result, the sprite is repeatedly repositioed ad thus moved across the stage (i a descedig agle over a period of eight secods). Boucig Sprites Aroud the Stage As a sprite is moved aroud the stage, it may evetually come ito cotact with oe of the edges of the stage. Usig the motio block show ext, you ca istruct Scratch to bouce the sprite off of the edge of the stage. Figure 5.8 These code blocks provide the ability to modify a sprite s locatio by chagig its coordiates.
127 106 Chapter 5 Movig Thigs Aroud The followig script demostrates how to use this code block to bouce a sprite aroud the stage: This script reverses the directio that a sprite is travelig wheever it collides with the edge of the stage. If you were to add this script to the cat sprite i a ew applicatio, the cat would move across the stage from side to side util you halted the applicatio s executio. Keepig Track of Sprite Coordiates ad Directio Scratch provides three motio (reporter) blocks that ca be used to retrieve ad display iformatio regardig the value of the sprite s X- ad Y-coordiates as well as the sprite s directio. These code blocks are show i Figure 5.9. Note Scratch s stage coordiate system allows for a coordiate rage of 240 to 240 o its X-axis ad a coordiate rage of 180 to 180 o its Y-axis. To set up a example that demostrates how to work with these reporter blocks, create a ew Scratch applicatio ad add the followig script to the default cat sprite. Whe executed, this script will move the cat sprite aroud the stage to wherever the mouse-poiter is located, boucig it off the edge of the stage whe ecessary. After addig the script, select each of the reporter blocks by clickig o
128 Takig Advatage of Scratch Cards 107 Figure 5.9 These code blocks provide the ability to retrieve ad display a sprite s coordiates ad directio. Figure 5.10 Displayig a sprite s coordiates ad directio. the check box just to the left of each block i the blocks palette. Oce you have doe this, three moitors should be visible o the stage, as demostrated i Figure Oce you have set up the applicatio s moitors, ru the applicatio, move the mouse-poiter aroud the stage, ad keep a eye o the values reported by the moitors. Takig Advatage of Scratch Cards Oe resource available to Scratch programmers is Scratch cards. Scratch cards are PDF files that you ca prit, cut out, glue together, ad the use as a quick referece for performig certai tasks. You ca dowload Scratch cards for free at as show i Figure The frot of each Scratch card idetifies the type of task that the card is desiged to show you how to perform, ad the back of the card provides detailed
129 108 Chapter 5 Movig Thigs Aroud Figure 5.11 Scratch cards serve as quick referece for performig specific types of tasks. istructio o how to perform the task. As of the writig of this book, a doze Scratch cards were available. The PDF file for each of these Scratch cards is descriptively amed to idetify the task that the card teaches you to perform. The list of available Scratch cards icludes: Chage Color Move to a Beat Key Moves Say Somethig Glide Follow the Mouse Dace Twist
130 Takig Advatage of Scratch Cards 109 Iteractive Whirl Aimate It Movig Aimatio Surprise Butto Keep Score Figure 5.12 shows what the PDF file for the Key Moves Scratch card looks like. As you ca see, the left-had side of the Scratch card demostrates the movemet of Figure 5.12 The Key Moves Scratch card demostrates how to move a sprite aroud the stage usig the keyboard arrow keys.
131 110 Chapter 5 Movig Thigs Aroud the sprite, ad the right-had side of the card provides a example of the code blocks eeded to move the sprite i each of the four demostrated directios. I additio, each Scratch card icludes a extra tip that helps you further ehace the task beig performed. Tip There are five Scratch cards that provide iformatio specific to movig sprites aroud the stage. These Scratch cards are briefly described here: Key Moves. Demostrates how to move a sprite aroud the stage usig keyboard keys. Move to a Beat. Demostrates how to create a aimated dace sequece that moves to a drum beat. Movig Aimatio. Demostrates how to aimate the movemets of a sprite usig a alterative series of costumes. Glide. Demostrates how to move a sprite aroud the stage from oe poit to aother i a smooth motio. Follow the Mouse. Demostrates how to script the movemet of a sprite so that it follows the movemet of the mouse-poiter o the stage. Creatig the Virtual Scratch Fish Tak The rest of this chapter is dedicated to leadig you through the developmet of a virtual fish tak applicatio. I this Scratch applicatio, five sprites, represetig a rage of colorful fish ad a small octopus, busily swim aroud the fish tak, represeted by a suitable backgroud, as demostrated i Figure This applicatio will be created by followig a series of steps, as outlied here: 1. Creatig a ew Scratch applicatio project. 2. Addig a stage backgroud. 3. Addig ad removig sprites to ad from the project. 4. Importig a soud file ito the applicatio. 5. Addig the programmig logic required to play a backgroud soud effect. 6. Addig the programmig logic required to aimate fish tak activity. 7. Savig ad executig your work.
132 Creatig the Virtual Scratch Fish Tak 111 Figure 5.13 A example of the virtual fish tak applicatio i actio. Step 1: Creatig a New Scratch Project The first step i creatig this Scratch project is to start Scratch, thereby automatically creatig a ew Scratch applicatio project. Alteratively, if you already have Scratch up ad ruig, you ca create a ew project by clickig o the New butto located o the Scratch meu bar. Step 2: Addig a Backgroud to the Stage Oce you have a ew applicatio project ready to go, let s begi by addig a suitable backgroud to the stage that will give the virtual fish tak a appropriate look ad feel. To set this up, click o the blak thumbail represetig the stage i the sprite list ad the click o the Backgrouds tab located at the top of the scripts area. Next, click o the Import butto, displayig the Import Backgroud widow. Double-click o the Nature folder, scroll dow ad select the uderwater graphic, ad the click o the OK butto. Oce the ew backgroud has bee added, go ahead ad remove the blak stage backgroud from the applicatio. Step 3: Addig ad Removig Sprites The ext step i the developmet of the virtual fish tak applicatio is to add sprites to the applicatio represetig differet marie life. Before doig this, remove the cat sprite from the applicatio, sice it is ot eeded. To do so,
133 112 Chapter 5 Movig Thigs Aroud Table 5.1 Sprite Rotatioal Buttos Sprite Fileame fish2 fish3 fish4 octopus1-a Sprite Applicatio Name Purple Yellow Spotted Squid right-click o its thumbail i the sprites list ad select Delete from the popup meu that appears. Oce you have removed the cat sprite, it is time to add ew sprites eeded by the applicatio. I total, you eed to add five ew sprites. Four of the sprites will represet differet fish, ad the fifth sprite will represet a small octopus. To add the octopus sprite, click o the Choose Sprite from File butto located i the middle of the ew sprite butto cotrols. This will ope the New Sprite widow. Double-click o the Aimals folder, scroll dow ad select the fish1-a sprite, ad the click o the OK butto. Next, click o the sprite s thumbail i the sprites area ad the chage the ame assiged to the sprite to Blue. Usig the same set of steps described above, add the followig list of sprites to the applicatio project, reamig each sprite as idicated i Table 5.1. Oce you have added all five sprites, move the sprites to radom locatios o the stage. Next, chage the directio i which each sprite moves by selectig each sprite ad the chagig it i the sprite ifo area by repositioig the directio of the blue lie displayed o the image of the sprite. Tip To make the virtual fish tak more iterestig, set the fish ad the octopus up so that each moves i a differet directio ad agle. Step 4: Addig a Suitable Audio File to the Stage Now that the applicatio s backgroud ad sprites have bee added, it is time to add a audio file that whe played will give the virtual fish tak a realistic feelig. Specifically, we ll add a audio file that whe played makes bubble souds. To accomplish this task, click o the thumbail represetig the stage i the sprite list ad the click o the Souds tab i the scripts area. Next, click o the Import
134 Creatig the Virtual Scratch Fish Tak 113 butto to display the Import Soud widow. Next, double-click o the Effects folder ad the select the Bubble audio file ad click o OK. Tip To help keep your Scratch applicatio as small as possible, remove the default pop audio file from the backgroud. Step 5: Playig the Audio File Now it is time to add the programmig logic eeded to make your ew applicatio ru. I total you will eed to add six scripts to the project, oe for the stage ad oe for each of the applicatio s five sprites. The script to be added to the stage will be resposible for playig the backgroud soud effect that makes the virtual fish tak soud like a real fish tak. To create it, click o the stage thumbail located i the sprites area ad the select the Scripts tab located at the top of the scripts area. Next, add ad cofigure the followig code blocks exactly as show here: This script cosists of a hat block that will execute wheever the gree flag butto is pressed. Whe this occurs, a loop is set up that repeatedly executes two blocks. The first code block is a soud block that plays the audio file you previously added to the stage. The secod code block pauses script executio for four secods to give Scratch time to fiish playig the audio file, before allowig the loop to repeat ad play it agai. Step 6: Aimatig the Swimmig of the Fish With the programmig logic required to provide the applicatio s backgroud soud effect ow i place, it is time to write the scripts that will aimate the movemet of the fish ad octopus. To set this up, you eed to add a small script
135 114 Chapter 5 Movig Thigs Aroud to each of the sprites that provides the programmig logic required to cotrol the movemet of the sprites as they move (or swim) aroud the fish tak. Scriptig the Movemet of the Blue Fish Let s begi by automatig the movemet of the sprite ame Blue. Dosoby clickig o the sprite s thumbail ad the creatig the followig script for it: As you ca see, this script is set up to begi executig the momet the user clicks o the gree flag butto. It cotais a cotrol block that sets up a loop that repeats the executio of two embedded motio blocks. The first motio block moves the sprite i its curret directio every time the loop repeats. The secod motio block tells Scratch to bouce the sprite off of the edge of the stage whe reached. As a result, the sprite (blue fish) will appear to swim aroud the fish tak from side to side, ad depedig o whether you have adjusted its directio as istructed at the ed of Step 3, it will move up ad dow as well. Scriptig the Movemet of the Purple Fish Next, let s create a script that cotrols the movemet of the purple fish. Rather tha build this script from scratch, let s take a shortcut. With the script for the blue fish curretly displayed o the scripts area, drag ad drop the script oto the thumbail represetig the purple sprite i the sprites list. This adds a exact copy of the script to the purple sprite, which you ca the view ad modify by clickig o the purple sprite s thumbail. To make thigs iterestig, modify the umber of steps that the purple sprite is moved from 1 to 2, as show here:
136 Creatig the Virtual Scratch Fish Tak 115 Other tha movig the purple fish at a little faster pace tha the blue fish, the programmig logic that cotrols both fish is idetical. I fact, the programmig for all of the remaiig fish ad the octopus is idetical, except for variaces i the umber of steps the sprites are moved. Scriptig the Movemet of the Yellow Fish Usig drag ad drop, add a copy of the purple sprite s script to the yellow sprite ad the modify it as show here: As you ca see, the yellow sprite has bee cofigured to move at the same pace as the blue sprite. Scriptig the Movemet of the Spotted Fish Oce agai, usig drag ad drop, add a copy of the yellow sprite s script to the spotted sprite ad the modify it as show here: This time the sprite has bee cofigured so that it moves two steps at a time. Scriptig the Movemet of the Octopus Last but ot least, drag ad drop the script for the spotted sprite oto the sprite represetig the octopus ad the modify it as show here:
137 116 Chapter 5 Movig Thigs Aroud As you ca see, this sprite has bee cofigured to move slower tha ay of the other sprites, at just a half step at a time. Step 7: Savig ad Executig Your New Scratch Applicatio At this poit your copy of the virtual fish tak applicatio should be complete ad should look like the example show i Figure If you have ot doe so yet, save your ew applicatio ad the ru it to see how it looks. To save your applicatio, click o the Save butto located o the Scratch meu bar. This will display the Save Project widow, allowig you to specify the ame of the applicatio, the locatio where you wat to store it, your ame, ad commets documetig the applicatio ad its purpose. Figure 5.14 The completed applicatio cosists of a backgroud, five sprites, ad six scripts.
138 Summary 117 Oce you have saved your applicatio, go ahead ad ru it. Sice all of the scripts i the applicatio are cofigured to execute whe the gree flag butto is pressed, all you have to do is to click o the gree flag butto ad the sit back ad relax as you watch ad liste to your virtual fish tak. Summary This chapter taught you how to work with all 16 motio code blocks. You leared how to move ad rotate sprites, poit sprites i differet directios or towards differet objects, ad chage a sprite s locatio. You also leared how to cotrol whether a sprite jumps to its ew locatio or glides to it, how to chage a sprite s locatio by settig or modifyig the value of its X-axis ad Y-axis coordiates, how to cotrol a sprite s movemet whe it makes cotact with the edge of the stage, ad how to report o a sprite s positio ad directio. You also leared how to work with Scratch cards ad create a virtual fish tak applicatio.
139 This page itetioally left blak
140 chapter 6 Sesig Sprite Positio ad Cotrollig Evirometal Settigs To create may iteractive computer applicatios, you eed the ability to detect whe certai thigs are happeig. For example, i a car racig game, it would be importat to be able to detect whe two cars (sprites) bump ito oe aother, ad i a game that uses predefied keystrokes as iput for cotrollig certai game fuctios, you eed to be able to detect whe those keys have bee pressed. Scratch provides the ability to detect or sese whe thigs happe usig sesig code blocks. This chapter will demostrate how to work with various sesig blocks ad will also guide you through the creatio of a ew Scratch applicatio, the Family Scrapbook. The major topics covered i this chapter iclude learig how to Detect mouse-poiter locatio ad mouse butto status Detect whe keyboard keys are pressed Determie whe a sprite collides with other objects o the stage Keep track of a sprite s distace from other objects ad retrieve differet sprite properties Work with a timer ad detect the loudess of microphoe iput 119
141 120 Chapter 6 Sesig Sprite Positio Workig with Sesig Code Blocks A importat capability eeded by a graphical programmig laguage that works with sprites is the ability to determie whe certai thigs happe. For example, sprite-based applicatios typically eed to kow whe sprites collide with oe aother or whe the user presses certai keystrokes. This type of fuctioality is provided i Scratch by sesig blocks. Sesig blocks also provide the ability to determie the locatio of the mousepoiter ad the ability to determie a sprite s distace from other sprites. Sesig blocks are colored sky blue. I total, Scratch provides access to 15 differet sesig blocks, which you ca work with by clickig o the Sesig butto located at the top of the blocks palette. Scratch orgaizes sesig blocks ito eight sub-groupigs, each of which is separated by a blak space i the blocks palette. These sub-groupigs iclude: Sesig blocks that retrieve ad report o the left mouse butto status ad mouse-poiter coordiates. A sesig block that determies whe specified keyboard keys have bee pressed. Sesig blocks that determie if a sprite has made cotact with the mousepoiter, aother sprite, or the edge of the stage. A sesig block that reports o a sprite s distace from the mouse-poiter or aother sprite. Sesig blocks that provide access to a built-i timer that ca be used to cotrol the timig of applicatio activity. A sesig block that retrieves a property value (X positio, Y positio, directio, costume umber, size, or volume) for the stage or a specified sprite. Sesig blocks that report o how loud audio iput comig from the computer s microphoe is. Sesig blocks that work with a Scratch Board, allowig you to create applicatios that ca detect chages i light ad soud ad work with the Scratch Board s buttos ad slider cotrol.
142 Retrievig Mouse Butto ad Coordiate Status 121 Except for the reset timer code block, all sesig code blocks are reporter blocks, desiged to be embedded iside stack blocks. Examples of how to work with each of the sesig code blocks listed above are provided throughout the rest of this chapter. Retrievig Mouse Butto ad Coordiate Status I may types of applicatios, the mouse-poiter is used to cotrol the movemet of sprites ad to affect the operatio of the applicatio i may other differet ways. The sesig blocks show i Figure 6.1 provide access to data about the operatio of the mouse-poiter. The first of these three code blocks retrieves the locatio of the mouse-poiter as it moves alog the X-axis. As was stated i Chapter 2, Gettig Comfortable with the Scratch Developmet Eviromet, Scratch supports a total rage of 240 to 240. The secod of these code blocks retrieves the locatio of the mousepoiter as it moves alog the Y-axis. Scratch supports a total rage of 180 to 180 o its Y-axis. The third code block is used to retrieve a true/false value that idetifies whe the mouse s butto is beig pressed. The followig script, which is part of a drawig applicatio, demostrates how to work with all three of these sesig code blocks. Figure 6.1 These sesig blocks report o the mouse-poiter s coordiates ad butto status.
143 122 Chapter 6 Sesig Sprite Positio To create the drawig applicatio, create a ew Scratch applicatio project. Remove the cat sprite from it ad the create ad add a ew sprite that cosists of a sigle black dot. Next, select the thumbail represetig the dot ad the add the script show above to it. This applicatio s operatio depeds o the use of a virtual pe object that Scratch makes available to you via pe code blocks, which you will lear about i Chapter 12, Drawig Lies ad Shapes. The overall operatio of the applicatio is cotrolled by the script, which automatically begis executig whe the gree flag butto is clicked. Oce started, two pe blocks are used to set the width of the pe ad the color used by the pe whe drawig. Next, a forever code block has bee added to repeat the executio of all the code blocks embedded withi it. Withi the loop, a if...else code block is used to coditioally cotrol the executio of three additioal statemets. The if...else code block s executio is cotrolled by examiig the value retured by a sesig block that returs a value of true whe the user presses the mouse s left butto ad false if the mouse s left butto is ot beig pressed. Whe the user presses the left mouse butto, the two statemets located at the top of the if...else code block are executed. The first statemet moves thespritetothesamelocatioasthepoiter,adthesecodcodeblock places Scratch s virtual pe i a dow positio, allowig drawig to begi. As aresult,abluelieisdrawaywhereothestagewherethemouse-poiter is moved whe the left mouse butto is beig pressed. The code block located at the bottom of the if...else code block is executed wheever the user releases the left mouse butto, liftig the virtual pe ad haltig ay drawig operatios. Figure 6.2 demostrates the operatio of the drawig applicatio. Figure 6.2 A example of the drawig applicatio i actio.
144 Determiig whe Keys Are Pressed 123 Determiig whe Keys Are Pressed Oe problem with the drawig applicatio is that there is o way to clear the scree ad start over should you make a mistake whe drawig. This ca be easily rectified usig the sesig code block show i Figure 6.3, which retrieves a true or false, depedig o whether a specified key is pressed. To see a example of how to work with this code block, let s modify the previous drawig applicatio by editig the script belogig to the applicatio s sprite, as show here. As you ca see, three ew code blocks have bee added that clear the stage wheever the spacebar is pressed. Figure 6.4 shows a example of the drawig applicatio i operatio. Here, the applicatio is used to draw the ame Lee o the stage. Next, the spacebar is pressed, clearig the stage, after which a image of a tree has bee draw. Figure 6.3 This sesig block ca be used to detect whe the user presses a specified keyboard key. Figure 6.4 This ehaced versio of the drawig applicatio ca be used to draw ad erase.
145 124 Chapter 6 Sesig Sprite Positio Tip I additio to detectig keystrokes usig a sesig code block, you ca also use the cotrol code block show i Figure 6.5. The differece betwee these two code blocks is that the sesig code block ca be used withi a loop to cotiuously determie that a specified keyboard key is beig pressed. The cotrol block, o the other had, oly executes oce whe the specified key is iitially pressed ad is therefore good for iitiatig a idividual actio ad ot for facilitatig the repeated executio of a actio. You will lear more about this code block later i Chapter 9, Coditioal ad Repetitive Logic. Figure 6.5 This code block is used to iitiate a actio wheever a specific keyboard key is pressed. Determiig whe Sprites Collide with Other Objects Oe key programmig requiremet of may computer games is the ability to determie whe a sprite collides with aother sprite, the edge of the scree, or the mouse-poiter. Scratch provides the ability to perform collisio detectio usig the three sesig code blocks show i Figure 6.6. The first code block show i Figure 6.6 ca be used to determie whe a sprite makes cotact with a specified sprite, the edge of the stage, or the mouse-poiter. The list of objects that this code block ca detect is accessible i the block s dropdow list. As a example of how to work with this code block, modify the previous Scratch applicatio by replacig its script with the oe show here. This script demostrates how to determie whe a sprite comes ito cotact with the edge of the stage. This script executes wheever the gree flag butto is clicked ad uses a forever block to set up a loop that repeatedly executes all embedded code blocks. Withi the loop, you ll fid a coditioal if block that executes embedded statemets whe the mouse s left-butto is beig pressed.
146 Determiig whe Sprites Collide with Other Objects 125 Figure 6.6 These sesig blocks ca be used to look for collisios. Figure 6.7 A example of the text that is displayed wheever the sprite makes cotact with the edge of the stage. Whe this is the case, a motio block is used to make the applicatio s sprite follow the mouse-poiter aroud the stage. A secod sesig code block is used withi aother coditioal if code block to detect whe the sprite makes cotact with the edge of the stage. Whe this occurs, a looks code block is executed, displayig a text message i a voice bubble. Figure 6.7 demostrates the output that is displayed whe you reru the applicatio with this ew script ad move the mouse-poiter to oe of the edges of the stage. Next, let s take a look at a example of how to work with the secod sesig block show i Figure 6.7. This code block ca be used to detect whe a sprite makes cotact with a specific color o the stage. To see a workig example of how to work with this code block, create a ew Scratch applicatio ad the create ad add a ew sprite i the shape of a red rectagle (usig the Pait Editor), placig it
147 126 Chapter 6 Sesig Sprite Positio Figure 6.8 This red square will be used to demostrate the ability to detect a collisio with a specific color o the stage. i the middle of the stage. Next, add a secod sprite to the applicatio by clickig o the Choose New Sprite from File butto, opeig the New Sprite widow. Next, drill dow ito the Fatasy folder ad select the drago1-b sprite ad the click o OK. The stage for your ew applicatio should ow look like the example show i Figure 6.8. Next, add the followig script belogig to the sprite represetig the drago. Whe executed, this script plays a audio file wheever the sprite is moved ito cotact with the red square i the ceter of the stage. Note that to correctly set the color specificatio i the sesig block, you must click o the color block embedded withi the cotrol. This displays a small eyedropper graphic that you ca the move to the area o the stage that cotais the color you wat to detect. Click o that color, ad Scratch will automatically chage the code block s color to match the color that you clicked o. At this poit you should have everythig set up ad ready to ru. Go ahead ad ru the applicatio ad the press ad hold the left mouse butto ad move the
148 Determiig Distace 127 mouse-poiter o ad off of the red rectagle i the middle of the stage ad liste for the audio file to be played. Usig the previous code block, you ca set up a applicatio to detect a collisio ay time ay part of a sprite comes ito cotact with a specific color o the stage. I the previous example, this occurs wheever ay part of the drago sprite (head, tail, wigs, flames, etc.) comes ito cotact with the red rectagle sprite. However, if you prefer, you ca use the third sesig code clock show i Figure 6.6 to set up a more specific type of collisio test. Specifically, what this code block does is allow you to specify a color o the sprite that must make cotact with aother color o the stage for a collisio to occur. To get a better uderstadig of the differece betwee this code block ad the previous sesig code block, look at the followig script. The followig script demostrates how to use the secod of these sesig blocks i a script that plays a audio file wheever a specified color withi a sprite comes ito cotact with a specified color o the stage. I this example, the sesig code block has bee replaced. Now, for a collisio to occur, the yellow color o the sprite must come ito cotact with the red color o the stage. If you were to replace the script i the previous applicatio with this script, the the oly time a collisio will occur is whe the yellow flames comig out of the drago s mouth touch the red rectagle sprite, as demostrated i Figure 6.9. Determiig Distace Rather tha detectig whe oe sprite collides with aother sprite, you may wat to detect whe oe sprite comes withi a certai distace of aother sprite or the mouse-poiter. You ca do this usig the sesig code block show i Figure 6.10.
149 128 Chapter 6 Sesig Sprite Positio Figure 6.9 Settig up a more restrictive collisio test. Figure 6.10 This sesig block reports o a sprite s distace from a specified object. To develop a uderstadig of how to work with this code block, modify the previous Scratch applicatio, replacig the drago sprite s script with the script show here. Oe you have replaced the script, ru the applicatio ad the move the mousepoiter aroud the stage. Whe you do, the drago sprite will follow, ad wheever it moves withi 150 steps of the red rectagle sprite, a audio file will be repeatedly played. Workig with a Timer Aother pair of sesig code blocks that you eed to become familiar with is show i Figure These code blocks provide the ability to eable ad work with Scratch s built-i timer. The first code block resets the timer back to its default value of zero, ad the secod code block retrieves a umber specifyig how may secods have passed sice the timer started ruig. Usig Scratch s timer, you ca cotrol the pace
150 Retrievig Stage ad Sprite Data 129 Figure 6.11 These sesig blocks provide the ability to eable ad use a timer withi your Scratch applicatio. of aimatio ad the operatio of your Scratch applicatios. For example, you would eed to use these cotrols to keep track of time whe players are give a certai amout of time i which to make a move. The followig example demostrates how to use both of these timer code blocks to create a script that repeatedly plays a audio file for five secods. Retrievig Stage ad Sprite Data I additio to determiig mouse status, sprite collisios, ad the distace betwee sprites, you ca use the code block show i Figure 6.12 to retrieve sprite ad stage iformatio. This code block provides easy access to a umber of pieces of iformatio, icludig: X positio Y positio Directio Costume umber Size Volume
151 130 Chapter 6 Sesig Sprite Positio Figure 6.12 This sesig block ca be used to retrieve iformatio about a umber of object attributes. As a example of how to work with this code block, take a look at the followig script, which retrieves the X coordiate of a sprite amed Sprite 2 ad plays a audio file wheever that sprite is moved to the right-had side of the stage (betwee coordiates 1 ad 240). Retrievig Audio Data I additio to sesig mouse-poiter ad keyboard data, collisios, distace, ad other stage ad sprite properties ad workig with the timer, Scratch also provides access to a pair of sesig blocks, show i Figure 6.13, that allow you to sese soud iput from the computer s microphoe (if it has oe) ad to use that iput withi your Scratch applicatios. The first of these two sesig blocks retrieves a umber, from 1 to 100, represetig the volume of the computer s microphoe, ad the secod code block retrieves a true/false value, depedig o whether a soud value of 30 or greater is detected through the computer s microphoe. The followig example demostrates how to create a script that plays a audio file amed pop wheever a loud soud is detected through the computer s microphoe.
152 Code Blocks That Work with Sesor Boards 131 Figure 6.13 These sesig blocks are used to report o how loud a soud is beig played. Figure 6.14 Usig moitors to keep track of the loudess of audio playback ad iput. Both of the code blocks show i Figure 6.13 are moitor blocks, so if you wat, you ca display their results o the stage, as demostrated i Figure Code Blocks That Work with Sesor Boards Scratch supplies additioal sesig code blocks, as show i Figure I order to work with these code blocks, you eed a Scratch Board. A Scratch Board is a special piece of hardware that you ca buy from the Scratch website ad the attach to your computer. You ca use the Scratch Board to collect ad process differet evirometal ad user-provided iput. The first of these two blocks retrieves the value reported by oe of the sesors o a Scratch Board. The secod code block retrieves a Boolea value of true or false, depedig o whether a specified sesor is beig pressed. Learig how to work with a Scratch Board is outside of the scope of this chapter. Istead, you will lear
153 132 Chapter 6 Sesig Sprite Positio Figure 6.15 These sesig blocks are used i cojuctio with a Scratch Board. how to programmatically iteract with ad cotrol Scratch Boards i Chapter 14, Collectig Exteral Iput Usig a Scratch Board. Creatig the Family Scrapbook Applicatio The remaider of this chapter will guide you through the developmet of your ext Scratch applicatio, a electroic family scrapbook. I total, this applicatio will cosist of oe sprite, a blak stage, ad three scripts. Oce created, you ca use this applicatio to display ay umber of electroic photographs i a automated photo album that displays pictures at three-secod itervals. Each picture i the applicatio is actually just a costume added to the applicatio s sprite. Figures 6.16 ad 6.17 show how the applicatio looks whe displayig two of the photo book s pictures. The developmet of this applicatio project will be created by followig a series of steps, as outlied here: 1. Creatig a ew Scratch applicatio project. 2. Addig ad removig sprites ad costumes. 3. Importig a soud file ito the applicatio. 4. Addig the programmig logic required to play backgroud music. 5. Addig the programmig logic required to maage the display of photographs. 6. Savig ad executig your work. Step 1: Creatig a New Scratch Project The first step i creatig the Family Scrapbook project is to create a ew Scratch applicatio project. Do so either by opeig Scratch, thereby automatically creatig a ew Scratch applicatio project, or by clickig o the New butto located o the Scratch meu bar.
154 Creatig the Family Scrapbook Applicatio 133 Figure 6.16 A example of oe of the sprite s costumes. Figure 6.17 Aother example of oe of the sprite s costumes. Step 2: Addig ad Removig Sprites ad Costumes This applicatio cosists of a sigle sprite, which will be used to display all of the applicatio s photographs (as costumes). Therefore, the default cat sprite will ot be eeded ad should be removed. After removig the cat sprite, click o the Choose New Sprite from File butto to ope the New Sprite widow. Usig this widow, avigate to the folder cotaiig the electroic image files (photographs) that you pla o displayig, ad the select oe of these files to be used as the applicatio s sprite. Click o the thumbail represetig the ew sprite (i the sprites list) ad the click o the Costumes tab located at the top of the scripts area. Next, click o the
155 134 Chapter 6 Sesig Sprite Positio Figure 6.18 You ca add as may pictures as you wat to the sprite s list of costumes. Import butto, opeig the Import Costume widow. Usig this widow, add aother picture to the applicatio. Repeat this process as may times as ecessary to add all of the image files that you wat to be icluded as part of the family scrapbook, as demostrated i Figure Step 3: Addig a Suitable Audio File to the Stage To make the Family Scrapbook applicatio more ejoyable, let s add a little backgroud music to help set the mood. To add the music file, select the stage thumbail i the sprites list ad the click o the Souds tab located at the top of the scripts area. Next, click o the Import butto to display the Import Soud widow ad the double-click o the Music Loops folder ad the select the GuitarChords1 audio file ad click o OK, addig the soud file to the applicatio project, as show i Figure 6.19.
156 Creatig the Family Scrapbook Applicatio 135 Figure 6.19 Addig backgroud music to be played whe the applicatio executes. Step 4: Playig the Audio File The ext step i the developmet of the applicatio project is to begi addig the programmig logic. I total, you will eed to add three scripts to the project, oe for the stage ad two for the applicatio s sprite. The script to be added to the stage will be resposible for playig the applicatio s backgroud music. To create this script, click o the stage thumbail located i the sprites list ad the select the Scripts tab located at the top of the scripts area. Next, add ad cofigure the followig code blocks exactly as show here. This script maages the repeated playback of the applicatio s audio file for as log as the applicatio is ru. Audio file playback is performed usig a pair of soud blocks, which you will lear about i Chapter 11, Spicig Thigs Up with Souds. Step 5: Displayig the Photographs Now it is time to add the programmig logic that is resposible for displayig all of the photographs that make up the Family Scrapbook. To set this up, you eed to add a small script to the applicatio s sprite that specifies the programmig logic required to automate the display of all of the applicatio s photographs, at threesecod itervals. I additio, you will add a secod script to the applicatio
157 136 Chapter 6 Sesig Sprite Positio that will allow the user to maually cotrol the display of the applicatio s photographs. Scriptig the Operatio of the Family Scrapbook The code blocks that are resposible for automatig the operatio of the scrapbook are show here: This script is automatically executed whe the user clicks o the gree flag butto. Whe this happes, a looks block is executed. This block specifies a specific costume to be displayed whe the applicatio is first started (the first costume i the costume list). Next, a loop is set up that repeatedly executes the two statemets embedded withi it. The first code block located iside the loop pauses the script s executio for three secods, after which a secod looks block is used to switch the sprite s costume to the ext costume i the sprite s costume list. Allowig for the Maual Operatio of the Family Scrapbook If the user prefers, rather tha viewig photographs i the Family Scrapbook as a automated slideshow, the cotets of the scrapbook ca be maually browsed by clickig o the applicatio s sprite, which causes the ext costume (photograph) to be displayed. To provide the user with this maual optio, add the followig script to the applicatio s sprite. Step 6: Savig ad Executig Your New Scratch Applicatio Okay, assumig that you have bee followig alog ad creatig your copy of the Family Scrapbook applicatio as you made your way through this chapter, the your copy of the Family Scrapbook applicatio should look somethig like the example show i Figure 6.20.
158 Summary 137 Figure 6.20 The completed applicatio cosists of a blak stage ad a sigle sprite with 11 costumes ad two scripts. So, if you have ot doe so yet, save your ew applicatio by clickig o the Save butto located o the Scratch meu bar. This will display the Save Project widow, allowig you to ame the applicatio ad specify the locatio where you wat to store it. Oce saved, switch to Presetatio mode, click o the gree flag butto, ad kick back ad ejoy listeig to ad watchig your ew applicatio. Alteratively, start clickig o the applicatio s sprite ad go through the cotets of the Family Scrapbook at your ow pace. Summary This chapter has provided a review of all of the Scratch sesig code blocks (except for the oes that work with Scratch Boards). You leared how to detect collisios, idetify whe the left mouse butto or a keyboard key is pressed, ad eve to determie whe a sprite comes ito cotact with differet colors o the
159 138 Chapter 6 Sesig Sprite Positio stage. You leared how to work with the timer as a meas of cotrollig applicatio activity. This chapter also showed you how to retrieve differet property values belogig to sprites ad the stage ad to detect the loudess of microphoe iput. Use of the iformatio preseted i this chapter is key to the developmet of iteractive Scratch applicatios ad games.
160 chapter 7 Storig ad Retrievig Data All computer applicatios require some sort of data with which to work as they execute. This is true of eve the simplest applicatios. The data processed by a applicatio may be embedded withi it. Data may also be radomly geerated or collected from the user as the applicatio executes. I order to work with ad maipulate data, programmers eed the ability to store, retrieve, ad modify data whe a applicatio rus. Withi Scratch applicatios, data is maaged usig variables. The goal of this chapter is to teach you everythig you eed to kow to begi developig Scratch applicatios that ca collect, store, ad process applicatio data. The major topics covered i this chapter iclude: How to create local ad global variables How to use variables as a meas of storig ad retrievig data How to delete variables that are o loger eeded How to view data stored i local variables belogig to other sprites Learig How to Work with Applicatio Data Like all computer programs, Scratch applicatios eed to be able to process ad store data. Data is ay type of iformatio that your Scratch applicatios collect, process, ad store whe executig. Data ca also be collected whe the user 139
161 140 Chapter 7 Storig ad Retrievig Data Figure 7.1 A example of text embedded withi a looks code block. iteracts with the applicatio usig the keyboard or mouse. Data may be geerated by your applicatios such as whe you create a Scratch project that geerates ad the uses radom umbers (covered i Chapter 8). Data may also be hard-coded withi your Scratch applicatio projects. For example, the code block show i Figure 7.1 ca be used to store ad display a text strig withi a script. Whe executed, a script cotaiig this looks code block will display the hardcoded text strig iside a voice bubble. Like most programmig laguages, Scratch lets you work with a umber of differet types of data. Each of these differet types of data, listed ext, is hadled differetly by Scratch. Strig Boolea Iteger Real A strig is a piece of text data that you hard code withi Scratch applicatios usig differet types of looks code blocks, which you will lear how to work with i Chapter 10, Chagig the Way Sprites Look ad Behave. Boolea data is data that is automatically geerated by Scratch whe you work with differet types of umbers code blocks (which you will lear about i Chapter 8). A Boolea value represets data that has a assiged value of either True or False. For example, ay time you compare oe umeric value agaist aother to see if they are equal, Scratch returs a Boolea value. Based o the result of that aalysis, you ca alter the way your Scratch applicatios execute usig cotrol blocks, which are covered i Chapter 9, Coditioal ad Repetitive Logic. A iteger is a umeric value that does ot iclude a decimal poit (sometimes referred to as a whole umber). Scratch lets you eter iteger values as iput ito umerous differet types of code blocks. It also allows you to store umeric data iside variables, allowig you to store, retrieve, ad maipulate the data as
162 Storig Data i Variables 141 ecessary durig applicatio executio. A real umber is a umber that icludes a decimal umber. Scratch hadles differet types of data differetly. For example, strig data ca oly be displayed by embeddig it withi looks code blocks. Iteger ad real data ca also be embedded withi code blocks ad displayed i moitors. I additio, iteger ad real data ca be added, subtracted, ad maipulated i all the differet ways that you would to be able to maipulate umeric data. Scratch also allows you to use itegers ad real umbers iterchageably. Note Idustrial stregth programmig laguages Microsoft C++ ad Visual Basic support a much wider rage of data types. However, they all support the same basic types of data that Scratch does. Storig Data i Variables As has already bee stated, you ca embed umeric data iside differet types of code blocks, usig it to cotrol the operatio of scripts. You ca also store umeric data collected whe your applicatios execute usig variables. I Scratch, variables allow you to store, retrieve, ad modify umeric data. Note Scratch caot store strig or Boolea data i variables. Creatig Scratch Variables I order to store, modify, ad retrieve data i a Scratch applicatio, you eed to create variables. I order to work with variables withi your Scratch applicatios, you must first defie ad add them to your applicatio projects. This is doe by clickig o the Variables butto located at the top of the blocks palette ad the clickig o the Make a Variable butto, as show i Figure 7.2. Oce this butto has bee clicked, Scratch displays the widow show i Figure 7.3, allowig you to assig a ame to the variable. Figure 7.2 Creatig ad deletig Scratch variables.
163 142 Chapter 7 Storig ad Retrievig Data Figure 7.3 Assigig a ame to a ew Scratch variable. Icremet a Variable s Value Assig a Variable s Value Retrieve a Variable s Value Figure 7.4 Scratch creates three ew code blocks for each variable that you create. Figure 7.5 Every ew variable supports a moitor that displays its value. The ame that you assig will be used to create ad add three ew code blocks to your Scratch project, as show i Figure 7.4. I additio, a moitor showig the variable s value is automatically displayed o the stage, as demostrated i Figure 7.5. Usig the three code blocks created for every variable, you ca assig a iitial value to the variable, chage its value while your applicatio is ruig, ad display a moitor o the stage, which shows the variable s value. Assigig Variables to Sprites ad the Stage Variables i Scratch applicatios belog to the sprites i which they are defied (or to the stage). Therefore, it is importat that whe addig ew variables to your applicatio, you select the thumbail for the sprite (or stage) where the variable belogs. For example, variables that eed to be accessed by differet
164 Storig Data i Variables 143 scripts belogig to differet sprites may best be added to the stage, whereas a variable eeded oly by a specific sprite should be added to that sprite. Assigig Names to Your Variables Ulike may programmig laguages, Scratch is very flexible whe it comes to amig variables. You ca make variable ames as log or as short as you wat. Variable ames ca iclude: Letters Numbers Special characters Blak spaces Because Scratch creates a edless supply of code blocks for each ew variable that you defie, it elimiates ay cocers about case-sesitivity, makig thigs a lot easier to work with. Tip Make your variable ames as descriptive as possible. This will help make your scripts selfdocumetig. Although Scratch variable ames ca be extremely log, it s a good idea to limit their legth to a maximum of 30 characters. This provides you with plety of room to create descriptive, maageable variable ames. Uderstadig Variable Scope Oe very importat cocept that you eed to uderstad whe workig with variables is variable scope. A variable s scope idetifies the locatio withi a applicatio where the variable s value ca be modified. Scratch supports two levels of variable scope, as outlied here: Local. Variables that ca be modified oly by scripts belogig to the sprite i which the variable is defied. Global. Variables that ca be modified by ay script i a applicatio. Note Although local variables ca oly be modified by scripts belogig to the sprite i which they are defied, their assiged values ca be retrieved (ot modified) by scripts belogig to other sprites usig sesig code blocks, as demostrated a little later i this chapter.
165 144 Chapter 7 Storig ad Retrievig Data Figure 7.6 Creatig a local variable amed Couter. Creatig Local Variables Local variables ca be modified oly withi the sprite i which they are defied. The followig procedure outlies the steps ivolved i creatig a local variable. 1. Select the sprite (or stage) to which the variable is to be added. 2. Click o the Variables butto located at the top of the blocks palette. 3. Click o the Make a Variable butto. 4. Eter the ame you wat to assig to the variable ad the select the For This Sprite Oly optio, as demostrated i Figure 7.6. Sice a local variable ca oly be modified withi the sprite to which it has bee added, it caot be modified by scripts belogig to other sprites. If you eed a variable that ca be accessed by ay script withi a applicatio, create a global variable as discussed i the ext sectio. Creatig Global Variables Ulike local variables, a global variable s value ca be modified by ay script withi the applicatio where it has bee defied. You use the exact same procedure to create a global variable as you do whe creatig a local variable, the oly differece beig that you eed to leave the default For All Sprites optio selected whe amig your variable, as demostrated i Figure 7.7. Tip It is cosidered a good programmig practice to restrict the scope of all variables to local wheever possible. This helps to make your applicatios easier to maitai ad elimiates the possibility that you might accidetally modify the variable s value usig scripts belogig to other sprites.
166 Accessig Variables Belogig to Other Sprites 145 Figure 7.7 Creatig a global variable amed Total Score. Figure 7.8 Deletig a variable that is o loger eeded. Deletig Variables whe They Are No Loger Needed Over time, you may fid yourself makig umerous chages to your Scratch projects. As you do, you may fid that certai variables are o loger eeded by your applicatios. If this is the case, you ca clea up your applicatios by deletig these variables from your projects. Doig so is very easy: First, make sure that ay refereces to the variable withi the applicatio s scripts have bee removed ad the click o the Delete a Variable butto, as demostrated i Figure 7.8, ad select the variable that you wat to delete. I respose, Scratch will delete the variable from the sprite to which it was added. Cautio If you delete a variable from a sprite without first removig refereces to the variable i the sprite s scripts, Scratch will delete the variable but will also leave i place ay code blocks i the applicatio s scripts that referece that variable. As a result, thigs will ot work properly. Accessig Variables Belogig to Other Sprites Although data stored i local variables ca oly be chaged by scripts belogig to the sprite to which the variables have bee assiged, Scratch does allow scripts belogig to other sprites to view data stored i variables belogig to other sprites. To view data stored i aother sprite s local variables, you eed to use the sesig block show i Figure 7.9.
167 146 Chapter 7 Storig ad Retrievig Data Figure 7.9 Usig this code block, you ca create a script that ca view data stored i aother sprite s local variables. Figure 7.10 Specifyig the ame of the sprite whose variable you wat to access. Figure 7.11 Selectig the variable whose data you wat to access. This code block lets oe sprite retrieve aother sprite s X positio, Y positio, directio, costume umber, size, ad volume. It also lets you retrieve values assiged to aother sprite s variable. As demostrated i Figure 7.10, you ca click o the code block s right-had pull-dow meu, ad it will display a listig made up of the stage ad all of the sprites i the Scratch applicatio. After selectig the stage or a sprite, you ca use the drop-dow meu located o the left-had side of the code block to select ad retrieve iformatio for ay of the specified items that are listed. A gray horizotal divider bar located at the bottom of the resultig list deotes the sprite s list of variables, separatig the list from other available data, as demostrated i Figure Usig this code block, you ca retrieve data stored i ay sprite s local variables. However, all you ca do is read the value assiged to those variables; you caot modify them. The oly variables that ca be remotely modified are global variables.
168 Two Quick Examples 147 Figure 7.12 Cofigurig the upper ad lower limits of a variable s slider cotrol. Workig with Variable Moitors As you leared back i Chapter 3, A Review of the Basic Compoets of Scratch Projects, Scratch supports the display of moitors for may of its code blocks, icludig variable code blocks. I additio to beig able to display a variable s value i either ormal or large readout, variable moitors also support a third slider bar moitor format. To display a slider bar for ay variable, eable the display of the variable s moitor ad the right-click o the resultig moitor ad select the Slider optio from the popup meu that is displayed. Sliders have a small roud hadle o them that you ca drag to modify the value assiged to a variable. By default, you ca use the slider to assig a value i the rage of 1 to 100 to its variable, although you ca assig ay value to the variable by keyig it ito the code block s iput field. If you eed to, Scratch will let you chage a slider bar s rage by right-clickig o it ad selectig the Set Slider Mi ad Max optio from the popup meu that is displayed. Whe you do this, the widow show i Figure 7.12 displays, allowig you to specify ay rage you wat. Two Quick Examples To help you become more comfortable with workig with variables, let s look at two quick examples. I the first example, show ext, a script has bee created that whe executed will display the value assiged to a variable amed Couter. Remember, by default every variable that you create has a reporter block with a associated moitor, which Scratch displays by default o the stage.
169 148 Chapter 7 Storig ad Retrievig Data Note To set up ad ru this example, you must create a ew applicatio ad add a variable amed Couter to it ad the add the script to the applicatio s default script. This script has bee set up to execute wheever the gree flag butto is pressed. It uses a cotrol block to set up a loop that repeats the executio of two embedded code blocks a total of 10 times. Each time the loop executes, the value assiged to a variable amed Couter is icreased by 1. The ext statemet pauses the loop for oe secod before allowig it to cotiue ruig. By default, Scratch assigs a default value of zero to all ew variables, which is why the first time you ru the previous script, it couts from 1 to 10. However, if you ru it agai, you will otice that it will cout from 11 to 20. If you wat, you ca chage this behavior by explicitly assigig a iitial value to the Couter variable, as demostrated i the followig example: Here, the value of Couter has bee set to 0 through the additio of a ew variable block at the begiig of the script, immediately after its hat code block. As a result, o matter how may times this script executes, it always couts from 1to10. Developig the Basketball Quiz Project The rest of this chapter is devoted to guidig you through the developmet of your ext Scratch applicatio, the NBA Trivia Quiz. This applicatio makes extesive use of variables to store ad retrieve player iput ad to keep track of the player s quiz results. I total, the applicatio is made up of a backgroud, six sprites, ad six scripts. Whe executed, this applicatio presets the user with a electroic quiz made up of five questios, desiged to evaluate the user s kowledge of NBA trivia.
170 Developig the Basketball Quiz Project 149 Figure 7.13 shows a example of how the game looks whe first started. To begi game play, the user must click o the sprite represetig the game s hostess, at which poit she will begi admiisterig the quiz. Figure 7.14 provides a example of how the hostess iteracts with the user whe admiisterig the quiz. The hostess provides the user with immediate feedback after each questio is aswered, lettig the user kow if the aswer was correct or icorrect. I additio, the user s score is automatically tabulated after each aswer is evaluated ad displayed i a moitor located at the lower right-had side of the stage. Figure 7.13 The NBA Trivia Quiz presets the user with a series of multiple choice questios. Figure 7.14 The user aswers questios by clickig o buttos labeled A, B, C, ad D located o the right-had side of the stage.
171 150 Chapter 7 Storig ad Retrievig Data The developmet of this applicatio project will be created by followig a series of steps, as outlied here: 1. Creatig a ew Scratch applicatio project. 2. Addig a backgroud to the stage. 3. Addig ad removig sprites ad costumes. 4. Addig variables eeded by the applicatio. 5. Addig scripts to each butto sprite to collect user aswers. 6. Addig the programmig logic required to admiister the quiz. 7. Savig ad executig your work. Step 1: Creatig a New Scratch Project The first step i creatig the NBA Trivia Quiz applicatio is to create a ew Scratch applicatio project. Do so by startig Scratch, thereby automatically creatig a ew Scratch applicatio project or, if Scratch is already ruig, by clickig o the New butto located o the Scratch meu bar. Step 2: Selectig a Appropriate Stage Backgroud Oce you have created your ew Scratch project, it is time to get to work. Let s begi by addig a appropriate backgroud to the stage. To do so, click o the blak stage thumbail located i the sprite list. Oce selected, modify its backgroud by clickig o the Backgrouds tab located at the top of the scripts area. To add a ew backgroud to the applicatio, click o the Import butto. Whe the Import Backgroud widow opes, click o the Idoors folder ad the select the basketball-court thumbail ad click o the OK butto. Sice this applicatio oly requires oe backgroud, you ca remove the default blak backgroud amed backgroud1 from your project by clickig o its Delete This Costume butto. Step 3: Addig ad Removig Sprites This applicatio cosists of a umber of sprites, represetig a hostess who is resposible for admiisterig the quiz, four buttos o which the user must click
172 Developig the Basketball Quiz Project 151 whe aswerig quiz questios, ad a graphic cotaiig a welcomig text message. Before addig ay sprites, go ahead ad remove the cat sprite from the applicatio, sice it will ot be eeded. To add the sprite represetig the game s hostess, click o the Choose New Sprite from File butto to ope the New Sprite widow. Drill dow i to the People folder ad the select the girl3-stadig sprite ad click o the OK butto. Elarge the sprite ad repositio it as demostrated i Figures 7.13 ad7.14.whileyouareatit,chagetheameassigedtothespritetosay host. Next, click o the Choose New Sprite from File butto ad the drill dow ito the Thigs folder ad select the butto sprite ad click o the widow s OK butto. Oce the butto sprite has bee added, select it i the sprites list, click o the Costumes tab located at the top of the scripts area, ad the click otheeditbutto.thiswillopethespriteithepaiteditorprogram. Click o the Text butto located o the Pait Editor s toolbar ad the specify ComicSas as the fot type ad 18 as the fot size, type a uppercase letter A oto the middle of the butto sprite, ad the click o OK. Next, reame the sprite A. Usig the same series of steps outlied i the previous paragraph, add three additioal istaces of the butto sprite to the applicatio, amig them B, C, ad D. Oce added, alig all four of the butto sprites alog the right-had side of the stage, as demostrated i Figures 7.13 ad At this poit, you oly have oe last sprite to add. This sprite will eed to be created from scratch. To do so, click o the Pait New Sprite butto ad the after specifyig ComicSas as the fot type ad 18 as the fot size, type Welcome to the NBA trivia quiz! as demostrated i Figure 7.15 ad click o the OK butto. Oce added, repositio this ew sprite at the top of the stage, as show i Figures 7.13 ad Step 4: Addig Variables Required by the Applicatio I order to execute, this applicatio eeds three variables as show i Figure To add these three variables to the applicatio, click o the Variables butto located at the top of the blocks palette ad the click o the Make a Variable butto three times to create three global variables amed Aswer, Clicked, ad Score.
173 152 Chapter 7 Storig ad Retrievig Data Figure 7.15 Creatig a ew sprite eeded by the NBA Trivia Quiz. Figure 7.16 The NBA Trivia Quiz requires the additio of three global variables. The variable amed Aswer will be used to keep track of the user s aswers to each quiz questio. The variable amed Clicked will be used to cotrol applicatio executio, makig sure that the script used to admiister the quiz pauses ad waits each time the user is prompted to aswer a ew questio. The variable amed Score will be used to keep track of the user s score (grade).
174 Developig the Basketball Quiz Project 153 By default, Scratch will display moitors o the stage for all three of these variables. However, the game oly eeds to display the Score moitor. Therefore, you should clear the moitor check boxes for the Aswer ad Clicked variables. Also, the moitor for the Score variable eeds to be moved to the lower righthad corer of the stage. Step 5: Addig Scripts to Butto Sprites to Collect User Iput The programmig logic that cotrols the overall admiistratio of the quiz will be added to the host sprite, which is resposible for displayig quiz questios, collectig user aswers, ad the gradig the results. I order to aswer quiz questios, the user must click o oe of the four sprite butto (A, B, C, or D) whe prompted by the hostess. Each of these four sprites has a small script belogig to it, which sets two variables whe it is clicked. Below is the script that is executed whe the A sprite is clicked. As you ca see, this script begis with a hat block that executes wheever the A butto is clicked. Whe this happes, the valued assiged to the Clicked variable is set to 1, ad the value assiged to the Aswer variable is also set to 1. The Clicked variable is used i the applicatio to keep track of whe the user aswers a questio. This variable s value is set to 1 whe the A sprite is clicked, idicatig that the user has submitted a aswer. Oce the aswer has bee evaluated by a script belogig to the host sprite, the value of Clicked is set back to 0, makig the applicatio ready to process a ew questio. The Aswer variable is used to idetify which butto has bee clicked. Assigig a value of 1 to this variable idicates that the A sprite has bee clicked. The programmig eeded by the B sprite is show ext. As you ca see, it is almost idetical to the code assiged to the A sprite, with the value assiged to
175 154 Chapter 7 Storig ad Retrievig Data the Clicked variable beig set to 1 whe the butto is clicked. Note that the value assiged to the Aswer variable is 2, idicatig that the secod butto (the B sprite) has bee clicked. The code blocks that make up the C sprite scripts are show ext. As you ca see, the third code block is used to idetify whe it is clicked. As you have probably aticipated, the code blocks that make up the script for the D sprite, show ext, assig a value of 4 to the Aswer variable. Step 6: Automatig the Admiistratio of the Quiz At this poit, you should have added scripts to each of the butto sprites that idicate whe they have bee clicked ad uiquely idetify which of the four buttos was selected. Now it is time to create the two scripts belogig to the host sprite. The first script, show ext, is resposible for startig the applicatio ad gettig the applicatio ready to admiister the quiz.
176 Developig the Basketball Quiz Project 155 As you ca see, this script has bee set up to execute whe the user clicks o the gree flag butto. Whe this happes, the values assiged to all three of the script s variables are set to 0 (settig the score to zero, idicatig that oe of the buttos have bee clicked, ad that o aswer has bee specified). Next, two looks code block are used to display istructios, welcomig the user ad the istructig her to click o the hostess whe ready to begi takig the quiz.
177 156 Chapter 7 Storig ad Retrievig Data The host sprite s secod script, show here, is resposible for the overall admiistratio of the quiz. As you ca see, it is pretty big ad is made up of may differet types of code blocks, some of which you have ot leared about yet. As such, oly a high-level overview of the script will be provided i this chapter. Oce you have read Chapters 9 ad 10, you may wat to retur ad review this script agai. This script begis with a hat code block that executes whe the user clicks o the host sprite. Next, the script s executio is paused for oe secod, ad the a looks block is used to display a text message, presetig the user with the quiz s first questio. The ext code block, which cotais a pair of embedded code blocks, pauses script executio ad waits util the value assiged to the Clicked variable is set to 1 (which will occur oly whe the user specifies a aswer by clickig o oe of the four butto sprites).
178 Developig the Basketball Quiz Project 157 The value assiged to Clicked is the reset to 0, makig the variable ready for the ext quiz questio. Next, a cotrol code block is used to evaluate the user s aswer to the quiz questio. This is accomplished by checkig to see if the player clicked o the A sprite, as idicated by a value of 1 beig assiged to Clicked. If this is the case, the user s score is icreased by 20, ad a looks block is used to display a text message iformig the user that her aswer was correct. If this is ot the case, the user is otified that the aswer provided was icorrect. The ext four quiz questios are admiistered usig programmig logic that is idetical to that used to admiister the first questio, the oly differece beig that a differet questio is preseted, ad a differet aswer is required. Fially, oce the last quiz questio has bee processed, the script s executio is paused for two secods, after which the user s grade (the value assiged to Score) is evaluated to see if it is greater tha 60, i which case the hostess aouces that the user has passed the quiz. If this is ot the case, the hostess aouces that the user has failed. Either way, a three-secod pause esues, after which the values assiged to all three variables are reset to their default startig value of 0, to make the quiz ready for the ext perso. Fially, oe last cotrol block is executed, esurig that all scripts withi the applicatio termiate their executio. Step 7: Savig ad Executig Your New Applicatio At this poit, you have all the iformatio eeded to create your ow copy of the NBA Trivia Quiz. Assumig that you have bee followig alog ad creatig your copy of the applicatio as you made your way through this chapter, the your applicatio project should look somethig like the example showifigure7.17. If you have ot doe so, go ahead ad save your ew applicatio. Oce saved, switch to Presetatio mode ad start the NBA Trivia Quiz. As you test your ew applicatio, make sure that the feedback beig provided by the hostess after each aswer is correct. I additio, keep a eye o the Score moitor ad make sure that the game is correctly tabulatig your grade.
179 158 Chapter 7 Storig ad Retrievig Data Figure 7.17 The completed applicatio cosists of a stage backgroud, six sprites, ad six scripts. Summary I this chapter you leared how to create variables ad use them to store ad retrieve umeric data. This icluded learig how to create both local ad global variables ad how to use them withi Scratch projects to cotrol the applicatio executio. You also leared how to set up ad cofigure variable moitors ad to chage variable values usig a slider cotrol. This chapter also showed you how to delete variables whe you o loger eed them.
180 chapter 8 Doig a Little Math Scratch provides robust support for performig mathematical calculatios. This gives you the ability to develop applicatios that ca maipulate umeric data i a variety of ways. Scratch provides this support through umbers code blocks. Numbers code blocks are reporter blocks ad therefore ca oly be used i cojuctio with stack code blocks. This chapter will provide a thorough review of each of these code blocks ad will also show you how to create a ew Scratch applicatio, the Number Guessig game. The major topics covered i this chapter iclude: Learig how to add, subtract, multiply, ad divide programmatically Learig how to geerate radom umbers usig ay rage you specify Istructio o how to perform differet types of umeric comparisos Learig how to perform a umber of built-i mathematical operatios Additio, Subtractio, Multiplicatio, ad Divisio Like all moder programmig laguages, Scratch provides programmers with the ability to add, subtract, multiply, ad divide umeric data. This capability is offered through the code blocks show i Figure
181 160 Chapter 8 Doig a Little Math Figure 8.1 These code blocks provide Scratch programmers with the ability to perform arithmetic calculatios. The use of these code blocks is quite ituitive, with each code block clearly idetifyig its usage. These code blocks ca be embedded withi ay Scratch code block that accepts umeric iput. For example, the followig script demostrates how to use these code blocks to modify the value assiged to a variable amed Cout. Here, the script begis by assigig a iitial value of 10 to Cout. Next, four sets of code blocks are executed. Each set cosists of oe stack block ad two reporter blocks. The first set of statemets sets the value of Cout equal to the value curretly assiged to Cout plus 5, makig Cout equal to 15. The secod set of code blocks sets Cout equal to the value curretly assiged to Cout mius 5, makig Cout equal to 10. The third set of code blocks sets Cout equal to the curret value of Cout times 5, makig Cout equal to 50. Lastly, the last set of code blocks chages the value of Cout to 10 by dividig its curret value by 5. Uderstadig the Mathematical Order of Precedece As is the case with all programmig laguages, Scratch allows you to strig together differet combiatios of umbers code blocks i order to create more complicated umeric calculatios. For example, take a look at the followig script. Here, a small script has bee created that evaluates a umeric expressio ad assigs the result to a variable amed Total. This equatio was created by embeddig a series of umbers code blocks withi oe aother. Specifically, the
182 Geeratig a Radom Number 161 Figure 8.2 Creatig complex formulas by assemblig differet combiatios of code blocks. equatio was created by embeddig the code blocks show i Figure 8.2 ito oe aother. As show i Figure 8.2, the equatio was assembled by embeddig the divisio code block ito a variable block. Next, the additio code block was embedded withi the left-had side of the divisio code block. Fially, a multiplicatio code block ad a subtractio code block are embedded withi the iput fields of the additio code block. Like all programmig laguages, Scratch evaluates the compoets of mathematical expressios by followig a specific order, referred to as the order of precedece. Specifically, Scratch evaluates a expressio usig a top-dow approach. Whe applied to the example show i Figures 8.2, Scratch evaluates it as follows: 1. First, it calculates the value of the two top code blocks. Therefore, 4 is multiplied by 5, yieldig a value of 20, ad 2 is subtracted from 4, yieldig a value of 2. At this stage, the expressio has bee evaluated as show here. 20 þ 2/2 2. Next, the expressio located i the secod level code bock (the additio block) is evaluated. Therefore, 20 is added to 2, yieldig a value of 22. At this stage, the expressio has bee evaluated as show here. 22 / 2 3. Fially, the lowest level code block is evaluated, dividig 22 by 2 ad resultig i a fial value of 11. Geeratig a Radom Number Some applicatios, such as computer games, require a elemet of radomess or chace. For example, a game that eeds to simulate the rollig of dice eeds to be able to create a pair of radom umbers i the rage of 1 to 6. Scratch provides the capability through the code block show i Figure 8.3.
183 162 Chapter 8 Doig a Little Math Figure 8.3 By default, this code block is cofigured to geerate a umber i the rage of 1 to 10. This code block provides a meas of geeratig radom iteger (whole) umbers usig ay specified rage of umbers. The default rage is 1 to 10, but you may chage the iput fields to suit your eeds. If eeded, you ca geerate egative umbers. I additio to hard codig a umeric rage ito the cotrol, you ca substitute variable blocks by draggig ad droppig them ito either or both of this code block s iput fields. To develop a uderstadig of how this code block works, look at the followig example: Here, a script has bee created that begis by assigig a variable amed Cout a startig value of 0. Next, the variable s value is chaged by assigig it a radomly selected value i the rage of 1 to 5. A loop is the set up to repeat the executio of two embedded code blocks. The loop is desiged to repeat a specified umber of times ad is set up by default to execute 10 times. However, by draggig ad droppig a istace of the Cout variable block ito the loop s iput field, the umber of times that the loop executes is radomly determied, depedig o the radomly assiged value of Cout. Note Each time the loop executes, it plays a audio file that souds like a cat meowig. I order to give the audio file time to fiish playig, a cotrol block was added to pause script executio for oe secod. To see this script i actio, create a ew Scratch applicatio ad add the script to the default Cat sprite. Compariso Operatios I order to work with umbers, you ofte eed to mathematically maipulate them as demostrated i the previous sectio. Doig so will ultimately leave you with a result. Typically, you will wat to do somethig with this result oce it has
184 Compariso Operatios 163 Figure 8.4 These code blocks provide the ability to compare ay two umeric values. bee calculated. For a simple applicatio, all you may eed to do is display its value. However, more ofte tha ot, you are goig to ed up usig it to guide the executio of your applicatio i some maer. For example, suppose you wat to create a umber guessig game that automatically geerates a radom umber ad the challeges the player to try to guess it. Oce the radom umber is geerated ad stored i a variable, the player eeds to be prompted to try to guess it (perhaps by clickig o oe of 10 buttos with umbers prited o them). Oce the player s guess is captured, the applicatio eeds to compare the player s guess agaist the value of the variable that stores the game s radom umber to determie whether the player s guess is correct. To facilitate this type of compariso operatio, Scratch provides access to the three code blocks show i Figure 8.4. The first ad last code blocks show i Figure 8.4 allow you to compare oe value agaist a rage of values. The first code block checks to see if the umeric value specified i its first iput field is less tha the value specified i its secod iput field. The third code block does the opposite, checkig to see if the umeric value specified i its first iput field is greater tha the value specified i its secod iput field. The middle code block is used to determie if two values are equal. To develop a better uderstadig of how to work with each of these three code blocks, let s look at a few examples. I the first example, show below, a script has bee created that executes wheever the gree flag butto is clicked. Whe this happes, the value Cout is set equal to 10. Next, a umbers block is embedded withi a cotrol block to set up a coditioal test that evaluates the value assiged to Cout ad to execute the code block embedded withi the cotrol block i the evet that the tested coditio (Cout equals 10) is true. Sice this is the case, a text strig of Hello! is displayed i a speech bubble. Note To prove that the embedded umbers code block is workig as it is supposed to, you could chage the value assiged to Cout to somethig other tha 10 ad ru the example agai. Sice the value assiged to Cout o loger equals 10, the tested coditio would evaluate as false, ad the text message would ot display.
185 164 Chapter 8 Doig a Little Math I this ext example, the umbers code block that tests for greater tha coditios is used. Agai, a script has bee set up to execute wheever the gree flag butto is clicked. The value assiged to Cout is the set to 1, ad a cotrol block is used to set up a loop that rus forever (util you provide a meas for stoppig its executio). A umber of code blocks are embedded withi the loop. The first block plays a audio file, ad the secod block pauses script executio for oe secod to allow Scratch time to fiish playig the file. Aother cotrol block is the used to set up a coditioal test that evaluates the value assiged to Cout to see if it is greater tha 2, ad if it is, aother cotrol block is used to termiate the script s executio. If the value assiged to Cout is ot greater tha 2, the the last code block located at the bottom of the loop is executed, icremetig the value of Cout by 1. The loop the repeats ad executes agai. The first time the loop rus, the value assiged to Cout is 1. The loop must iterate two times before the value of Cout is set to 3, resultig i the termiatio of the script s executio. Because of this, the audio file will play three times. I this fial example, show ext, the umbers code block that tests for less tha coditios is used. Like the last two examples, this script is set up to execute wheever the gree flag butto is clicked. Whe this happes, the value of Cout is set to 1. Next, a loop is set up that repeatedly executes as log as the value of Cout is less tha 15. Each time this test evaluates as true, three embedded code blocks are executed. The first code block moves the sprite 25 steps. The ext code block icremets the value assiged to Cout by 1, ad the last code block pauses script executio for oe secod.
186 Compariso Operatios 165 The way this script is writte, its loop will execute 14 times ad will stop executig whe the value of Cout fially reaches 15. Trick While Scratch oly supplies you with three code blocks for performig coditioal tests (equality, greater tha, ad less tha), most programmig laguages support three additioal types of coditioal tests, allowig you to perform the followig compariso operatios: Greater tha or equal to Less tha or equal to Not equal to Although Scratch does ot provide equivalet code blocks, you ca easily set up equivalet compariso tests by combiig the three code blocks just discussed with Scratch s logical compariso code blocks, as show i Figure 8.5. The first combiatio of code blocks show i Figure 8.5 creates a test that determies if the value assiged to a variable amed Total is less tha or equal to 10. This example is made up of five code blocks----two variable blocks, two umbers code blocks used to perform less tha ad equality comparisos, ad aother umbers block, which is used to tie everythig together. The secod combiatio of code blocks show i Figure 8.5 is very similar ad is desiged to create a test that checks to see if the value assiged to Total is greater tha or equal to 5. The last example is made up of three code blocks ad is used to evaluate the values assiged to Total to determie to see if it is ot equal to 3. You will lear more about code blocks that support logical comparisos i the ext sectio. Figure 8.5 Creatig customized logical comparisos.
187 166 Chapter 8 Doig a Little Math Performig Logical Comparisos I additio to code blocks desiged to perform mathematical ad compariso operatios, Scratch also provides access to three code blocks that support logical compariso operatios. These code blocks are show i Figure 8.6. The first code block is used to test two differet sets of values to determie if both are true. The secod code block is used to test two differet sets of values to determie if at least oe is true. Ad the last code block lets you evaluate two values to determie if the tested coditio is false (ot true). To help you better uderstad how to work with all three of these code blocks, let s review a few examples. The first example, show ext, is a script that executes wheever the gree flag butto is clicked. Whe this occurs, the value assiged to the variable Cout is set to 50. Next, a cotrol code block is used to aalyze the value assiged to Cout. If the value of Cout is less tha 100 ad also greater tha 10, the the ed statemet embedded withi the cotrol block is executed. However, if both tested coditios evaluate as false, the embedded code block is ot executed. Note Scratch is very flexible i its support for umbers blocks. For example, if you prefer, you could swap the order i which the two embedded umbers blocks occur (e.g., checkig to see that Cout is greater tha 10 before checkig to make sure that Cout is also less tha 100), ad the results would be the same. This ext example is very similar to the previous example, except that istead of esurig that both tested coditios evaluate as beig true, the script has bee Figure 8.6 Usig these code blocks, you ca perform more complex compariso operatios.
188 Roudig Numbers ad Retrievig Remaiders 167 modified so that oly oe of the tested coditios has to be evaluated as true i order for the embedded code block to be executed. This fial example shows a script that performs a egative test, checkig to see if two values are ot equal istead of checkig to see if they are equal. As a result, if the value assiged to Cout is ot equal to 50, which it is ot, the code block embedded withi the cotrol block is executed. Roudig Numbers ad Retrievig Remaiders The ext set of umbers code blocks, show i Figure 8.7, provides the ability to retrieve the remaider portio of ay divisio operatio ad lets you roud ay decimal umber to the earest whole umber. The first code block show i Figure 8.7 returs the remaider portio of a divisio operatio, also referred to as a modulus, as demostrated i the followig example, which divides 10 by 3 ad the assigs the modulus (a value of 1) to a variable amed Remaider. Figure 8.7 These code blocks retrieve remaiders ad roud umbers.
189 168 Chapter 8 Doig a Little Math The secod code block show i Figure 8.7 returs the rouded value for a specified umeric value, rouded to the earest whole umber, as demostrated i the followig examples, which retur values of 4 ad 5, respectively. Workig with Built-i Mathematical Fuctios I additio to all of the mathematical operatios that you ca put together usig the umbers code blocks previously discussed i this chapter, Scratch provides oe additioal multi purpose code block, as show i Figure 8.8. This code block is desiged to perform ay of 12 differet mathematical fuctios, which ca be selected from the code block s drop-dow list. The fuctios that this code block ca perform are outlied i the followig list: abs. Returs the absolute, o-egative value of a umber. sqrt. Returs the square root of a umber. si. Returs a value represetig the sie of a agle. cos. Returs a value represetig the cosie of a agle. ta. Returs a value represetig the taget of a agle. asi. Returs the arc sie for the specified umeric value. acos. Returs the arc cosie for the specified umeric value. ata. Returs the arc taget for the specific umeric value. l. Returs the iverse of the atural expoet of a specified value (i.e., the opposite of e^). log. Returs the atural log of a umber. Figure 8.8 This code block ca assist you i settig up extremely complex calculatios.
190 Developig the Number Guessig Game Quiz Project 169 e^. Returs the atural expoet of a specified value. 10^. Returs the value of a umber raised to the 10th power. These code blocks ca be real time savers whe developig applicatios that require the use of ay of the mathematical fuctios supported by the code block, savig you the trouble of implemetig the uderlyig programmig logic yourself to retrieve similar results. As a result, ot oly will you sped less time workig o the developmet of your applicatio, but the programmig logic that you have to develop will be simplified ad easier to maitai, sice this code block ca do most of the heavy liftig for you. To specify which fuctio you wat to work with, all you have to do is select it from the code block s drop-dow list. For example, the followig examples demostrate the use of two differet fuctios provided by this code block: This example cosists of two sets of code blocks. The first set of code blocks returs the absolute value of 4.4, which is 4.4, ad assigs that value to a variable amed Result. The secod set of blocks returs the square root of 9, which is 3, ad assigs that value to a variable amed Result. Developig the Number Guessig Game Quiz Project The remaider of this chapter is focused o the developmet of your ext Scratch applicatio, the Number Guessig game. This applicatio will make use of umbers code blocks to geerate radom umbers for the player to guess ad to compare the player s guesses agaist the game s radomly geerated umber. I total, the applicatio is made up of a backgroud, 11 sprites, ad 12 scripts. Whe ru, the game will challege the player to guess a radomly geerated umber i the rage of 0 to 9 i as few guesses as possible. Figure 8.9 shows a example of how the game looks whe first started. To eter a guess, the player must click o oe of the butto sprites located at the bottom of the stage. The cat provides immediate feedback after each guess, as demostrated i Figure 8.10.
191 170 Chapter 8 Doig a Little Math Figure 8.9 The Number Guessig game is moderated by the Cat sprite. Figure 8.10 The cat lets the player kow whe guesses are too high or too low. Figure 8.11 The player guessed the secret umber i five guesses. Figure 8.11 shows how the game looks oce the player fially maages to guess the game s secret radom umber. The game automatically geerates a ew radom umber at the ed of each game, i order to ready the game to be played agai. The developmet of
192 Developig the Number Guessig Game Quiz Project 171 this applicatio project will be created by followig a series of steps, as outlied here: 1. Creatig a ew Scratch applicatio project. 2. Addig a backgroud to the stage. 3. Addig ad removig sprites. 4. Addig variables eeded by the applicatio. 5. Addig a audio file to the applicatio. 6. Addig scripts to each butto to collect player guesses. 7. Addig the programmig logic required to process player guesses. 8. Savig ad executig your work. Step 1: Creatig a New Scratch Project The first step i the developmet of the Number Guessig game is to create a ew Scratch applicatio project. To do so, start Scratch, automatically creatig a ew Scratch project or, if Scratch is already ruig, click o the New butto located o the Scratch meu bar. Step 2: Addig a Stage Backgroud The ext step i the developmet of the Number Guessig game is to add a backgroud to the stage. To do so, click o the blak stage thumbail located i the sprite list ad the chage its backgroud by clickig o the Backgrouds tab located at the top of the scripts area. Next, click o the Import butto ad whe the Import Backgroud widow opes, click o the Outdoors folder. The select the brick-wall1 thumbail ad click o the OK butto. Sice the applicatio oly eeds oe backgroud, remove the default blak backgroud, amed backgroud1, from your project by clickig o the Delete This Costume butto. Step 3: Addig ad Removig Sprites The Number Guessig game is comprised of the default Cat sprite plus 10 butto sprites ad a variable moitor, as show i Figure 8.12.
193 172 Chapter 8 Doig a Little Math Figure 8.12 A overview of the differet parts of the Number Guessig game. To add the first of the sprites represetig the 10 iput buttos, click o the Choose New Sprite from File butto to ope the New Sprite widow. Drill dow i to the Letters folder ad the the Keys folder to select the 0 sprite. The click o the OK butto. Place the sprite i the lower-left corer of the stage, as show i Figure Followig this same process, add sprites 1 through 9 to the bottom of the stage as well. At this poit, all that is left i the desig of the applicatio s user iterface is the display ad repositioig of the moitor, which you will do i the ext step. Step 4: Addig Variables Required by the Applicatio I order to execute, the Number Guessig game requires three variables, as show i Figure To add these variables to the applicatio, click o the Figure 8.13 The Number Guessig game requires three variables.
194 Developig the Number Guessig Game Quiz Project 173 Variables butto located at the top of the blocks palette ad the click o the Make a Variable butto three times to defie variables amed Guess, No Of Guesses, ad RadomNo. The variable amed Guess will be used to store the most recet guess made by the player. The variable amed No Of Guesses will be used to keep track of the umber of guesses made by the player durig each game. The variable amed RadomNo will be used to store the game s radomly geerated secret umber. Oce added, clear the check box cotrols belogig to the Guess ad No Of Guesses variables to prevet their moitors from beig displayed. Lastly, drag ad drop the moitor for the No Of Guesses variable to the middle right-had side of the stage. Step 5: Addig a Audio File to the Applicatio The Number Guessig game makes use of two audio files that are played as soud effects whe the player makes icorrect ad correct guesses. The audio file played whe the player eters a missed guess is the default pop file, which is automatically icluded as part of each of the butto sprites used i the applicatio. The secod audio file is the Fairydust file, which is played wheever the player maages to correctly guess the mystery umber. To add the Fairydust audio file, select the Cat sprite thumbail i the sprite list ad the click o the Souds tab located at the top of the scripts area. Next, click o the Import butto to display the Import Soud widow, ad the doubleclick o the Electroic folder, select the Fairydust file, ad click o OK. Step 6: Addig Scripts to Capture Player Iput The programmig logic that drives the Number Guessig applicatio is divided ito a series of scripts belogig to the applicatio s sprites. Specifically, small scripts must be added to each of the butto sprites to capture ad save player guesses. I additio, two scripts must be added to the Cat sprite. These two scripts, which are resposible for startig the game ad processig player guesses, will be covered i Step 7. To begi work o each of the scripts belogig to the butto sprites, select the sprite represetig the 0 butto ad the add the followig code blocks to it:
195 174 Chapter 8 Doig a Little Math The script begis with a hat block that executes wheever the sprite is clicked (whe the player clicks o it as a guess). Whe this occurs, the secod code block i the script seds a Player has guessed broadcast message to the other sprites as a sigal that the player has submitted a guess. The Player has guessed must be typed ito the cotrol block exactly as show. A third code block is the used to assig a value to the Guess variable, recordig the player s guess. Note that i this example, settig Guess to 0 idicates that the player has submitted a guess of 0. The last code block i the script plays the default pop audio file, which lets the player kow that the guess has bee processed. Note A broadcast message is a message exchaged betwee sprites that sigals whe a evet of some type has occurred withi a applicatio. Broadcast messages are geerated by ad received usig various cotrol code blocks, which you will lear all about i Chapter 10, Chagig the Way Sprites Look ad Behave. For ow, all you eed to kow is that this applicatio uses broadcast messages i order to coordiate activity ad keep track of what is occurrig withi the game. The scripts that eed to be added to the rest of the butto sprites are almost idetical to the script that you just added. The oly differece is that you eed to modify the value that is set i the third code block to properly reflect which butto sprite each script belogs to. The easiest way to add these scripts to the other ie butto sprites is to drag ad drop a istace of the first script oto each of the ie other sprites ad the to select each sprite, oe at a time, ad modify the value of the third code block accordigly. Step 7: Processig Player Guesses Oce scripts have bee added to all 10 of the butto sprites, it is time to create the two scripts belogig to the Cat sprite. The first of these scripts is show ext ad is resposible for iitializig the game ad gettig it ready to play. This script is executed whe the player clicks o the gree flag butto. It begis by assigig a iitial value of 0 to No Of Guesses ad the assigs a radomly geerated value i the rage of 0 to 9 to a variable amed RadomNo. Lastly, it
196 Developig the Number Guessig Game Quiz Project 175 displays a pair of messages that iform the player that the cat is thikig of a umber ad challeges the player to try to guess it. The secod ad fial script to be added to the Cat sprite is show ext. This script is automatically executed wheever the Player has guessed broadcast message is received. This happes whe the player clicks o oe of the 10 butto sprites. First, the script modifies the value assiged to No Of Guesses by icreasig it by 1. This allows the applicatio to keep track of the umber of guesses that the player has made i the curret game. The rest of the script is made up of code blocks embedded withi a cotrol block. The cotrol block begis by evaluatig the value assiged to the Guess variable to see if it is equal to the value assiged to the RadomNo variable. If this is the case, a series of code blocks embedded withi the upper portio of the cotrol block are executed. If this is ot the case, code blocks embedded i the bottom of the cotrol block are executed. The code statemets located i the upper half of the cotrol block, which execute whe the player eters a correct guess, perform the followig actios: Play the Fairydust audio file that was added to the Cat sprite back i Step 5 Notify the player that the game has bee wo Pause script executio for oe secod Reset the value of No Of Guesses to 0
197 176 Chapter 8 Doig a Little Math Select a ew radom umber for the game Challege the player to play agai If, o the other had, the player eters a icorrect guess, the code blocks embedded at the bottom of the script are executed. These code blocks are orgaized ito two separate cotrol blocks. The first cotrol block evaluates the value assiged to Guess to see if it is less tha RadomNo, ad if it is, a message is displayed that iforms the player that the guess was too low. The secod cotrol block determies if the value assiged to Guess is less tha RadomNo, ad if it is, a message is displayed that iforms the player that the guess was too high. Step 8: Savig ad Executig Your New Scratch Applicatio At this poit, you ow have all the iformatio that you eed to create your ow copy of the Number Guessig game. If you have ot already doe so, save your ew Scratch project. Oce saved, switch to Presetatio mode, ru the game, ad put it through its paces. Remember to begi game play by clickig o the gree flag butto ad followig the istructios provided by the Cat sprite. Summary This chapter provided a thorough overview of Scratch umbers code blocks ad demostrated their usage. This icluded learig how to perform mathematical calculatios ad geerate radom umbers, as well as how to perform umeric comparisos. You leared how to perform differet types of logical comparisos ad to combie code blocks that execute logical ad compariso operatios to carry out advaced compariso operatios. O top of all this, you leared how to perform a host of advaced mathematical operatios like roudig umbers ad executig differet arithmetic fuctios. You also leared how to create aother Scratch applicatio, the Number Guessig game.
198 chapter 9 Coditioal ad Repetitive Logic To create a script, you must kow how to work with cotrol code blocks. All of Scratch s hat blocks are cotrol blocks. Cotrol blocks also provide the capability to implemet loops ad coditioal programmig logic, which are the buildig blocks of advaced ad complex applicatios. Cotrol blocks ca pause ad halt script executio. Cotrol blocks also provide the capability to sed ad receive broadcast messages, providig you with a meas of coordiatig applicatio activity. The major topics covered i this chapter iclude: How to use cotrol blocks to iitiate script executio How to pause ad halt script executio How to set up differet types of loops ad implemet coditioal programmig logic How to sed ad receive broadcast messages betwee sprites Itroducig Scratch Cotrol Blocks Scratch cotrol blocks provide programmers with may differet capabilities, all of which are geared aroud cotrollig script executio. Without cotrol blocks, scripts would ot be able to execute. Nor would they be able to pause, loop, or 177
199 178 Chapter 9 Coditioal ad Repetitive Logic execute coditioal logic whe evaluatig data. Through cotrol blocks, Scratch ca perform all of the actios listed here: Evet programmig Pause script executio Create loops Sed ad receive broadcast messages Execute coditioal logic Halt script executio You have already see cotrol blocks i actio i every script preseted i the first eight chapters of this book. Now it is time to lear more about these powerful code blocks ad the programmig features they provide. Evet Programmig Cotrol blocks ca iitiate script executio, which is critical to the executio of Scratch applicatios. This is accomplished with hat blocks, icludig those show i Figure 9.1. As you have see i may examples i this book, the first code block show i Figure 9.1 iitiates a script s executio wheever the gree flag butto is clicked, ad it is the most commo meas of startig a applicatio s executio. For example, if you were to add the followig script to ay sprite or backgroud i a Scratch applicatio, it would automatically play a specified audio file (provided that file has bee imported). Figure 9.1 Hat blocks automate the executio of scripts.
200 Pausig Script Executio 179 The secod code block show i Figure 9.1 iitiates a script s executio wheever a specified keyboard key is pressed. The key that is used as the trigger is selected by clickig o the code block s drop-dow list ad makig a selectio of oe of the followig keystrokes: Up, dow, right, ad left arrow keys The spacebar a z 0 9 For example, the followig script demostrates how to move a sprite by 50 steps wheever the keyboard s spacebar is pressed: The third code block show i Figure 9.1 iitiates script executio wheever the sprite to which it belogs is clicked. The followig script demostrates how to use this code block to automate the display of text i a speech bubble wheever the sprite to which it has bee added is clicked: Note Scratch provides a fourth hat cotrol block, which is covered later i this chapter. This code block is used to iitiate script executio whe broadcast messages are received. Pausig Script Executio Oce started, scripts execute without pause util they are doe. However, sometimes you eed to temporarily pause a script s executio for a specified period. The code block that you eed to use i this type of situatio is show i Figure 9.2. Figure 9.2 Usig this cotrol block, you ca pause script executio for as log as ecessary.
201 180 Chapter 9 Coditioal ad Repetitive Logic This code block adds brief pauses to your Scratch applicatios. For example, you might wat to pause a script s executio for a secod or two after the player scores a poit. This brief pause would allow the player a momet to review the score ad to get ready for the ext poit. Aother reaso for pausig a script s executio is to help maage the playback of audio files, as demostrated i the followig example: Here, you see a script that plays two audio files. I order to allow the first audio file time to play back, the script is paused for two secods, after which executio resumes, ad the secod audio file is played. If you were to remove the cotrol block that pauses the script from this example, both audio files would play simultaeously, iterferig with oe aother. Tip It you wat to cotiuously play a audio file without pausig a script s executio, cosider puttig the code statemets that are resposible for audio file playback i their ow script ad addig that script to the stage. Note The cotrol block show i Figure 9.3 also pauses script executio, waitig util a specified coditio becomes true. This code block is covered a little later i this chapter, whe coditioal programmig logic is discussed. Figure 9.3 This code block provides aother way of coditioally pausig script executio. Executig Loops Most computer applicatios ad games are iteractive, meaig that they respod to user iput ad react accordigly. I doig so, it is ofte ecessary to execute collectios of code statemets repeatedly. For example, a arcade-style computer game might require the cotiuous playback of backgroud music ad soud effects. This would require the repeated executio of programmig
202 Executig Loops 181 logic required to maage soud playback for as log as the game was played. To maage this type of iteractio, you eed to add loops to your applicatios. I Scratch, a loop is a collectio of oe or more code blocks embedded with a cotrol block that are repeatedly executed. Without loops, programmers would have to create extremely large scripts filled with repeated series of duplicate statemets to perform certai tasks. For example, to create a Scratch applicatio that bouces the Cat sprite up ad dow four times without a loop, would you have to add a script like the oe show ext to the sprite. The script begis by positioig the sprite at the bottom ceter of the stage. Two sets of motio blocks are eeded to bouce the sprite oe time. So to bouce the sprite up ad dow four times, these two code block have to be repeated four times. Suppose you wated to make the sprite bouce 10, 100, or 1,000 times. Clearly, this is a situatio where a loop is eeded. Scratch supplies access to two code blocks that you ca use to set up loops, as show i Figure 9.4. Note Scratch also supplies two additioal cotrol blocks that offer the capability to coditioally execute loops. These two code blocks will be discussed a little later i this chapter whe coditioal logic is covered. Figure 9.4 Usig these code blocks, you ca create loops that repeat the executio of ay code blocks you embedded withi them.
203 182 Chapter 9 Coditioal ad Repetitive Logic The first of the two code blocks show i Figure 9.4 ca be set up as a loop that executes forever, which really meas that the loop repeatedly executes util the script i which it resides is halted. For example, the followig script uses this code block to set up a loop that bouces a sprite over ad over agai, util the Stop Everythig butto is clicked: The first statemet moves the sprite to the bottom ceter of the stage. The two statemets withi the loop bouce the sprite, i a glidig motio, up ad dow from the bottom to the middle of the stage. Note I Scratch, there are two ways to force a immediate termiatio of a script. First, you ca halt a script by stoppig the executio of the applicatio by clickig o the red Stop Everythig butto. However, this optio ca ofte be a bit of overkill. As a less extreme optio, Scratch offers a cotrol block that allows you to halt a idividual script s executio. There is also a cotrol block that you ca use to halt the executio of all scripts withi a applicatio. Both of these cotrol blocks are reviewed a little later i this chapter. Rather tha repeatig the executio of a loop forever, you ca use the secod code block show i Figure 9.4 to set up a loop that executes a predetermied umber of times. For example, the followig script demostrates how to bouce a sprite up ad dow a total of 10 times. Obviously, the fewer code blocks you use whe developig scripts, the more streamlied ad easier to support your applicatios will be. Loops make programmig a lot easier ad provide a tool that you ca use to repeat the executio of ay umber of code statemets with as little fuss as possible.
204 Sedig ad Receivig Broadcasts 183 Sedig ad Receivig Broadcasts Because Scratch applicatios ca be made up of may differet sprites, each of which may cosist of may differet scripts, coordiatig the activity of all the differet parts of the applicatio ca be challegig. By providig access to the three code blocks show i Figure 9.5, Scratch offers the ability to sed ad receive broadcast messages as a meas of coordiatig script executio. Usig the first two code blocks show i Figure 9.5, you ca pass messages to ay script withi a applicatio that begis with the hat code block show at the bottom of Figure 9.5. For example, the followig script demostrates how to sed a broadcast message of jump to all sprites withi the applicatio: To specify the message set by the cotrol code block, all you have to do is click o the block s drop-dow list ad the either select a previously typed message or create a ew message by clickig o New ad the typig i the message. This particular code block seds its message ad the allows the script i which it is embedded to cotiue executig. Alteratively, the followig script ot oly seds a broadcast message but also waits util every script i the applicatio, which has bee set up to execute whe the message is set, has fiished executig: Usig the hat block, you ca set up a script to execute wheever a specified message is received. Figure 9.5 Broadcast messages provide the capability for oe script to otify other scripts that a evet has occurred.
205 184 Chapter 9 Coditioal ad Repetitive Logic Note Usig the three previous scripts, you could create a ew applicatio made up of two butto cotrols ad the default Cat sprite. By assigig the first script to the first butto sprite, the secod script to the secod butto sprite, ad the third script to the Cat sprite, you ca make the Cat sprite jump up ad dow o the stage ay time you click o oe of the butto sprites. Coditioal Programmig Logic The ext set of cotrol code blocks provided by Scratch is show i Figure 9.6. These code blocks allow you to apply coditioal programmig logic to your scripts. Usig these code blocks, you ca aalyze data withi your applicatios ad make decisios based o this aalysis, resultig i the coditioal executio of collectios of code blocks. The key cocept to uderstad whe workig with these types of code blocks is that coditioal logic ivolves a evaluatio as to whether a coditio is true or ot. If the coditio beig aalyzed is true, the the code blocks embedded withi the cotrol block are executed. However, if the coditio beig aalyzed proves false, the embedded code blocks are ot executed. The followig script demostrates how to use the first code block show i Figure 9.6 to set up a loop i which executio is cotrolled by a coditioal test. Each time the loop repeats, it checks the value assiged to a variable amed Couter to see if it is equal to 0. If it is, the loop executes, plays a audio file, pauses for two secods, ad the checks to see if it should execute agai. Figure 9.6 These five code blocks let you coditioally execute collectios of code blocks.
206 Coditioal Programmig Logic 185 This ext example demostrates how to coditioally execute the playback of a audio file. Whe executed, this script examies the value assiged to a variable amed Couter to see if it is equal to 0, ad if it is, the audio file is played. Sometimes you may wat to execute either of two sets of code blocks based o the results of a tested coditio. This ca be accomplished usig the third code block show i Figure 9.6. Here, a coditioal test is performed that checks to see if the directio that a sprite is facig is 90 degrees. If it is, the directio that the sprite is poitig is reversed. If you ru the script repeatedly, the directio that the sprite is poitig is cotiuously reversed. This ext example demostrates how to use a cotrol block that pauses script executio ad waits for a specified coditio to become true. Here, a script has bee set up that, oce ru, checks o the value assiged to Couter to see if it is greater tha 5. If it is, a audio file is played. If Couter is ot greater tha 5, the the script pauses its executio, waitig util the value of Couter exceeds 5 before fiishig its executio. Fially, the last example demostrates how to work with the last of the cotrol blocks show i Figure 9.6. Here, a loop is set up to execute repeatedly util the value assiged to Couter is set equal to 3, at which time the loops will stop ruig. Each time the loop rus, it moves, or bouces, its associated sprite up ad dow o the stage.
207 186 Chapter 9 Coditioal ad Repetitive Logic Nestig Coditioal Cotrol Code Blocks As powerful as the cotrol blocks are that facilitate coditioal executio, they are limited to aalyzig a sigle coditio at a time. To develop more complex programmig logic, you ca embed oe cotrol block withi aother, as demostrated i the followig example: Here, oe cotrol block has bee embedded withi aother cotrol block to further aalyze the value assiged to Couter. If ecessary, you ca embed cotrol blocks may levels deep. However, the deeper you go, the more difficult your scripts will be to uderstad ad maitai. Prevetig Edless Loops Loops are extremely powerful tools, providig the capability to perform repetitive tasks with ease. However, if you are ot careful whe settig them up, you ca accidetally set up a edless loop. A edless loop is a loop that, because of a logical error o the programmer s part, ever eds. For example, you might wat to set up a loop that plays a audio file five times. But suppose whe settig up the loop you made a mistake that preveted the loop from ever termiatig, as show here. Here, the itetio was to set up a loop that would execute five times. The loop has bee set up to execute for as log as the value assiged to Couter is
208 Termiatig Script Executio 187 less tha 5. Couter is assiged a iitial value of 1, ad its value is supposed to be icremeted by 1 each time the loop executes. However, istead of icremetig the value of Couter by 1 at the ed of the loop, the value of Couter is decremeted by a value of 1. As a result, the loop ever termiates, forever repeatig the playback of the audio file. To prevet edless loops from occurrig, you eed to take extra care whe settig up loops ad test your scripts thoroughly whe developig your applicatios. Termiatig Script Executio The last two cotrol blocks offered by Scratch are show i Figure 9.7. These code blocks programmatically halt script executio withi your Scratch applicatios. Usig the first of these two cotrol blocks, you ca halt the executio of the scripts i which the code block is placed, as demostrated i the followig example: Here, the script checks to see if the value assiged to a variable amed Couter is equal to 3, ad if it is, a audio file is played. If Couter is ot equal to 3, the a differet audio file is played, ad the script s executio is halted. Haltig a script this way forces its immediate termiatio, eve if the script cotais additioal code blocks that have ot bee executed. Usig the secod cotrol block show i Figure 9.7, you ca ot oly halt the executio of the curret script, but you ca also halt the executio of every script i the applicatio. For example, the followig script executes a loop three times ad the halts the executio of every script i the applicatio i which it resides. Figure 9.7 Usig these code blocks, you ca halt the executio of ay or all scripts withi a applicatio.
209 188 Chapter 9 Coditioal ad Repetitive Logic Developig the Ball Chase Game The rest of this chapter is dedicated to teachig you how to create your ext Scratch applicatio, the Ball Chase game. This applicatio makes heavy use of differet cotrol blocks to cotrol the movemet of the ball ad the cat that chases it aroud the stage. I total, the applicatio is made up of four sprites ad ie scripts. The object of the game is to try to prevet the cat from catchig the ball as it chases it aroud the stage. If you ca keep the ball out of the cat s reach for 30 secods, you wi. Figure 9.8 shows how the game looks whe first started. To play, all you have to do is move the mouse-poiter aroud the stage, ad the ball will automatically follow. If the cat maages to catch the ball before 30 secods is up, the game eds, as demostrated i Figure 9.9. Figure 9.8 The object of the game is to prevet the cat from catchig the ball. Figure 9.9 The game eds if the cat catches the ball.
210 Developig the Ball Chase Game 189 Figure 9.10 The player wis if the ball ca be kept away from the cat for 30 secods. Figure 9.10 shows how the game looks whe the player successfully maages to evade the cat for the etire 30 secods. The developmet of this applicatio will be accomplished by followig a series of steps, as outlied here: 1. Creatig a ew Scratch project. 2. Addig ad removig sprites. 3. Addig variables eeded by the applicatio. 4. Addig a audio file to the applicatio. 5. Addig a script to cotrol ball movemet. 6. Addig scripts that display game over messages. 7. Addig the scripts required to cotrol ad coordiate game play. 8. Savig ad executig your work. Step 1: Creatig a New Scratch Project The first step i the developmet of the Ball Chase game is to create a ew Scratch project. To do so, start Scratch, automatically creatig a ew Scratch project, or if Scratch is already ruig, click o the New butto located o the Scratch meu bar. Step 2: Addig ad Removig Sprites The Ball Chase game is made up of the default Cat sprite plus three other sprites ad a variable moitor, as show i Figure 9.11.
211 190 Chapter 9 Coditioal ad Repetitive Logic Figure 9.11 A overview of the differet parts of the Ball Chase game. Sice the default Cat sprite is ot eeded i this applicatio, go ahead ad remove it. I its place you eed to add a differet sprite, represetig a top-dow view of a differet Cat sprite. To add this sprite, click o the Choose New Sprite from File butto. Whe the New Sprite widow opes, drill dow i to the Aimals folder ad the select the cat2 sprite ad click o the OK butto. By default, the sprite is placed i the middle of the stage ad faces i a 90-degree directio. Leave this sprite i its default locatio i the middle of the stage, chage its directio to 0, ad the chage its assiged ame to Cat. Next, add the Ball sprite by clickig o the Choose New Sprite from File butto, drillig dow i to the Thigs folder, selectig the beachball1 sprite, ad the clickig o the OK butto. Place the Ball sprite o the top ceter of the stage ad chage its assiged amed to Ball. To add the applicatio s remaiig two sprites, which will be othig more tha text strigs saved as sprites, you must create them, which you ca do usig Scratch s built-i Pait Editor program. Both of these sprites cosist of text messages. For the first of these two sprites, ope the Pait Editor by clickig o the Pait New Sprite butto. Whe the Pait Editor program opes, specify a fot type of ComicSas, a fot color of red, ad a fot size of 18. Type Game Over, press the Eter key, type You lose! ito the Pait Editor, ad the click o the OK butto. Usig this same process, create a secod sprite that says Game Over! You wi. Chage the ames assiged to these two sprites to LosigMsg ad WiigMsg, respectively.
212 Developig the Ball Chase Game 191 Figure 9.12 The Ball Chase game requires oe variable. Step 3: Addig Variables Required by the Applicatio To execute, the Ball Chase game requires oe variable as show i Figure To add this variable to the applicatio, click o the Variables butto located at the top of the blocks palette ad the click o the Make a Variable butto to defie a variable amed Elapsed Time. This variable will be used to display the amout of time remaiig i the game. Make sure that you leave the check box for this variable selected ad that you repositio the variable s correspodig moitor to the upper right-had corer of the stage. Step 4: Addig a Audio File to the Applicatio The Ball Chase game makes use of oe soud effect, which simulates the meowig of the cat as its chases the ball aroud the stage. To add this audio file, select the Cat sprite thumbail i the sprite list ad the click o the Souds tab located at the top of the scripts area. Next, click o the Import butto to display the Import Soud widow, double-click o the Aimal folder, select the Meow audio file, ad fially click o the OK butto to fiish addig the audio file to the sprite. Step 5: Addig a Script to Cotrol Ball Movemet The objective of the game is to try to keep the ball out of the reach of the cat for 30 secods. The followig script, which should be added to the Ball sprite, is resposible for cotrollig the movemet of the ball o the stage. This script begis with a hat block. Next, a motio block is used to positio the ball i the upper middle portio of the stage. A looks block is the used to move
213 192 Chapter 9 Coditioal ad Repetitive Logic the sprite back oe layer, esurig that if the Ball sprite ecouters the Cat sprite, the Ball sprite will be displayed uder the Cat sprite istead of o top of it. (You will lear about looks blocks i Chapter 10, Chagig the Way Sprites Look ad Behave. ) The rest of the script cosists of a loop that repeatedly executes aother motio block. The motio block is resposible for movig the Ball sprite aroud the stage to where the mouse-poiter is. Step 6: Addig Scripts That Display Game Over Messages You will add the script that is resposible for makig the cat chase the ball aroud the stage i the ext sectio. Before doig so, add the followig pair of scripts to the WiigMsg sprite. These scripts are resposible for displayig ad hidig the game s wiig message. The first of the two scripts show above is resposible for hidig the display of the sprite to which it has bee added. The secod script, o the other had, is resposible for displayig the sprite wheever a broadcast message of You wi is received. Note that this script icludes a looks block that pushes the sprite to the frot of ay other sprites that it may happe to overlap. This esures that the message is completely visible oce displayed. Oce you have created ad added these two scripts to the WiigMsg sprite, drag ad drop both of them oto the LosigMsg sprite ad the edit the secod script so that it executes wheever a broadcast message of You lose is received. Step 7: Addig Scripts Needed to Cotrol ad Coordiate Game Play To wrap up your work o the Ball Chase game, you eed to add four scripts to the Cat sprite. The first of these scripts is show ext ad is resposible for esurig the cat chases the ball aroud the stage.
214 Developig the Ball Chase Game 193 This script begis by movig the Cat sprite to the ceter of the stage ad poitig it i its default upward directio. Next, it pauses for oe secod ad the eters ito a loop, which repeatedly executes the embedded code blocks. The first of these three code blocks poits the cat sprite towards the Ball sprite. The secod code block pauses the loop s executio for.15 secods, after which the third block moves the Cat sprite 66 steps i the directio of the Ball sprite. Note The reaso for imposig the.15 secod delay i the script s loop is to slow dow thigs eough to give the player a chace to keep the ball from the cat. If the little extra delay were removed from the loop, the speed at which the cat moves would easily overcome eve the fastest player. The secod of the four scripts to be added to the Cat sprite is show ext. This script is set to execute whe the player starts the game by clickig o the gree flag butto. The script begis by settig the value at which the audio is played to 50% of the level of the computer s curret soud level. The rest of the script is cotrolled by a loop that repeatedly rus two embedded code blocks. The first code block pauses script executio for five secods. The secod code block plays the Meow audio file. The result is that the cat will meow every five secods as it chases the ball aroud the stage. The third script to be added to the Cat sprite is resposible for haltig the executio of all scripts i the applicatio i the evet that the cat maages to touch the sprite durig game play. The code blocks that make up this script are show here:
215 194 Chapter 9 Coditioal ad Repetitive Logic This script is executed whe the player starts the game by clickig o the gree flag butto. The script s overall executio is cotrolled by a loop. Withi the loop, a coditioal test is performed that checks to see if the Cat sprite has made cotact with the Ball sprite. If this is the case, a broadcast message of You lose is set. Oce this message has bee received ad processed by the other scripts i the applicatio, the last code block i the loop is executed, haltig all script executio. The last script to be added to the Cat sprite is show ext. This script is resposible for keepig track of time as the applicatio executes ad for haltig game play after 30 secods, should the player maage to keep the cat at bay for that log. Whe started, this script begis by resettig Scratch s iteral timer ad the assigig the curret value of the timer (0.0) to a variable amed Elapsed Time. The rest of the script is cotrolled by a loop. Each time the loop executes, it updates the value assiged to the Elapsed Time variable to reflect the timer s curret value. Next, a check is made to see if the timer s value has exceeded 30 secods, ad if it has, a broadcast message of You wi is set. Oce processed by the other scripts i the applicatio, the executio of all scripts i the applicatio is halted. If, o the other had, the timer s value is less tha 30 secods, the loop simply executes agai. Accordigly, if the cat does ot maage to catch the ball withi 30 secods, thus edig the game, the fourth script will ed the game ad declare the player to be the wier.
216 Summary 195 Step 8: Savig ad Executig Your Scratch Project All right! Assumig you have followed alog closely with each of the steps preseted i this chapter, your copy of the Ball Chase game should be ready for testig. If you have ot doe so yet, save your ew Scratch project. Oce saved, switch over to Presetatio mode ad execute the game. Remember that game play begis whe you click o the gree flag butto ad that your object is to keep the ball out of the cat s reach for 30 secods. Summary This chapter provided a overview of all of Scratch s cotrol blocks. You leared how to use Scratch hat blocks ad to pause ad halt script executio. This chapter also showed you how to set up differet types of loops ad to work with all five of Scratch s cotrol blocks that support coditioal programmig logic. You also leared how to cotrol ad coordiate script activity by sedig ad receivig broadcast messages betwee sprites.
217 This page itetioally left blak
218 chapter 10 Chagig the Way Sprites Look ad Behave By its very ature, Scratch leds itself to the developmet of graphical applicatios that ivolve the maipulatio of sprites. This icludes takig actios that affect the appearace ad behavior of both sprites ad the stage backgroud. Sprite ad backgroud appearace ad behavior ca be cotrolled usig looks code blocks. Looks code blocks ca be used to affect sprite appearace through the applicatio of special effects, to make sprites visible or ivisible as applicatios execute, ad eve to chage sprite costumes ad stage backgrouds. This chapter offers a i-depth overview of all of Scratch s looks code blocks ad will guide you through the creatio of your ext Scratch project, the Crazy Eight Ball game. The major topics covered i this chapter iclude: Learig how to programmatically chage a sprite s costume Learig how to display text i speech ad thought bubbles Discoverig how to apply a rage of special graphical effects to sprites Learig how to chage a sprite s size Makig sprites appear ad disappear durig applicatio executio Specifyig how sprites that overlap oe aother should be displayed 197
219 198 Chapter 10 Chagig the Way Sprites Look ad Behave Chagig Sprite Costumes ad Backgrouds Depedig o whether you have selected a sprite s thumbail or the stage thumbail i the sprite list, several differet code blocks are displayed whe you look at Scratch s looks blocks i the blocks palette. For starters, the first three code blocks are differet, as show i Figure Both sets of code blocks have similar tasks, with oe set focusig o workig with sprite costumes while the other set is focused o workig with the stage s backgroud. Chagig Sprite Costumes Every sprite that is added to a Scratch applicatio is capable of chagig its appearace by chagig its costume. Sprites ca be assiged ay umber of costumes ad switch betwee them at ay time. To add a costume to a sprite, all you have to do is select the sprite s thumbail, click o the Costumes tab located at the top of the scripts area, ad the click o the Import butto. This opes a widow that allows you to locate ad select a graphic file to be used as a ew costume for the sprite. Every costume that is added to a sprite is automatically assiged a umber ad a ame (based o the graphic s fileame). The first costume i the costume list represets the sprite whe the applicatio is started. However, usig drag ad drop, you ca rearrage the order i which costumes are listed. I additio, usig the first looks block show i Figure 10.1, you ca programmatically replace a sprite s curret costume by specifyig the ame of a differet costume. For example, the followig script demostrates how to use this code block i a loop to repeatedly chage a sprite s costume 10 times at half-secod itervals. The result is the geeratio of aimatio that makes it look like the bat is flyig. Figure 10.1 The code blocks o the left are displayed whe you are workig with a sprite, ad the code blocks o the right are displayed whe you are workig with the stage.
220 Chagig Sprite Costumes ad Backgrouds 199 To chage the costume of the sprite to which this script is added, select the costume s ame from the looks block s drop-dow list. The block s drop-dow list is automatically populated with a list of all of the costumes that have bee added to the sprite. The costumes listed i the previous example refer to two costumes represetig differet views of a bat, as show i Figure 10.2, ad are supplied as part of a collectio of graphic files that ships with Scratch. Costume umbers are automatically assiged by Scratch as you import ew costumes ito a sprite. The first costume assiged to a sprite is give a costume umber of 1. Each successive costume is assiged a higher umber, as demostrated i Figure Usig the secod looks block show o the left-had side of Figure 10.1, you ca chage a sprite s costume to the ext costume i the costume list. For example, the followig script automatically chages a sprite s costume wheever the sprite is clicked. Whe executed, the script chages the sprite s costume to the ext costume i the list. By clickig o the sprite repeatedly, you cotiue chagig the sprite s Figure 10.2 Bat costumes.
221 200 Chapter 10 Chagig the Way Sprites Look ad Behave Costume # 1 Costume # 2 Costume # 3 Figure 10.3 Three costumes have bee added to a sprite, each of which depicts a slightly differet versio of a blue dog. These costumes are umbered 1, 2, ad 3 ad are amed dog2-a, dog2-b, ad dog2-c, respectively. Figure 10.4 Scratch loops back to the begiig of the sprite s costume list as ecessary to fulfill additioal costume switches. costume. Oce the last costume i the costume list has bee displayed, Scratch will go back to the top of the costume list ad start over, as depicted i Figure The last looks block show at bottom left of Figure 10.1 ca be used to display a moitor that presets a sprite costume umber o the stage. Alteratively, you ca use this code block as iput to ay code block that accepts umeric iput. Chagig a Stage s Backgroud Costumes The looks code blocks o the right-had side of Figure 10.1 are used to chage the stage s backgroud ad work idetically to their couterparts that deal with
222 Makig Sprites Talk ad Thik 201 costumes. For example, the followig script demostrates how to radomly set the stage s backgroud to oe of three optios. Note that i additio to chagig the stage s backgroud twice, this example also plays oe of three audio files, depedig o which of the three backgrouds is radomly selected. Makig Sprites Talk ad Thik The followig set of looks code blocks, show i Figure 10.5, is applicable oly to sprites ad ca used to display text i speech ad thought bubbles, makig a sprite look like it is talkig or thikig. Figure 10.6 provides examples of how speech ad thought bubbles look. The first two code blocks are used to display text i speech bubbles. The differece betwee these two code blocks is that the first code block displays its text Figure 10.5 Usig these code blocks, you ca display text i both speech ad thought bubbles. Figure 10.6 Speech ad thought bubbles resemble callouts used to display captios i cartoos foud i may popular ewspaper comic strips.
223 202 Chapter 10 Chagig the Way Sprites Look ad Behave for a specified umber of secods, ad the secod code block permaetly displays its text (util the text is overridde by aother speech or thought bubble). For example, the followig script could be used to display the text Hello! for two secods i a speech bubble. Tip Ay text displayed usig the secod ad fourth code blocks show i Figure 10.5 do ot automatically go away. However, you ca clear out the text displayed i a speech or thought bubble by executig a speech or thought code block with o text typed i it. Similarly, the followig script demostrates how to display a text message of Hmm... i a thought bubble. Applyig Special Effects to Costumes ad Backgrouds The ext three looks code blocks, show i Figure 10.7, apply to both sprites ad the stage ad ca be used to apply ad clear differet graphical special effects. The first ad secod code blocks show i Figure 10.7 select ad the apply oe of the followig special effects to a sprite s costume or to the stage s backgroud. Color. Modifies the costume or backgroud s color. Fisheye. Magifies a portio of a costume or backgroud. Whirl. Twists ad distorts a portio of a costume or backgroud. Figure 10.7 These code blocks allow you to set ad clear differet graphics effects o sprites.
224 Applyig Special Effects to Costumes ad Backgrouds 203 Pixelate. Displays a sprite or backgroud at a lower resolutio tha the resolutio at which the image was created. Mosaic. Creates a image made up of repeated istaces of a sprite or backgroud. Brightess. Modifies a image by icreasig or decreasig its itesity of light. Ghost. Fades the appearace of a costume or backgroud to make it look trasparet. A example of each of these graphic effects o a sprite is show i Figure To develop a better uderstadig of how to work with these two code blocks, let s look at a couple of examples. I this first example, a sprite s appearace is chaged by executig a loop four times. Each time the loop executes, it applies the ghost effect to the sprite to which it belogs. Note that the value specified i the iput field for the code block i the previous script is 25, which represets a percetage value. As such, for each of the four times that the loop repeats, the sprite fades away util at the ed of the last executio of the loop, the sprite completely disappears. Figure 10.8 A demostratio of how special effects affect a sprite.
225 204 Chapter 10 Chagig the Way Sprites Look ad Behave This secod example applies the whirl special effect to its sprite. Specifically, it begis by clearig ay previous whirl effect that may have bee applied to the sprite. The, over a period of four secods, it slowly modifies the appearace of the sprite by applyig a icreased applicatio of the whirl effect. A oe-secod pause the esues, ad the sprite is retured to its origial state. The last looks code block restores a costume or backgroud back to its origial appearace regardless of how may differet graphical effects may have bee applied to it. For example, the followig statemet demostrates how to restore a costume or backgroud s appearace whe the gree flag butto is pressed. Chagig a Sprite s Size The ext three looks code block, show i Figure 10.9, apply oly to sprites. They allow you to chage a sprite s size. The first code block modifies a sprite s size by specifyig a relative value. Usig this code block, as demostrated ext, you ca slowly icrease a sprite s size ad the reduce its size just as quickly. Figure 10.9 With these code blocks, you ca modify a sprite s size.
226 Makig Sprites Appear ad Disappear 205 The secod code block show i Figure 10.9 lets you set a sprite s size to a specific percetage of its curret size (larger or smaller). For example, the followig script begis by doublig the size of a sprite. It the pauses for a secod ad reduces the sprite to 50% of its origial size. After aother brief pause, the sprite is restored to its origial size. Makig Sprites Appear ad Disappear The ext two looks code blocks, show i Figure 10.10, apply oly to sprites. As the text displayed o the blocks idicates, they programmatically display or hide a sprite. Sice they do ot accept ay iput, these two code blocks are very easy to work with. For example, the followig script ca be added to ay sprite to make it disappear ad the reappear after a oe-secod pause. Figure With these two code blocks, you ca cotrol whe sprites appear o the stage.
227 206 Chapter 10 Chagig the Way Sprites Look ad Behave Determiig What Happes whe Two Sprites Overlap The last two Scratch looks code blocks, show i Figure 10.11, specify what happes whe all or part of a sprite is covered by aother sprite. I Scratch, each sprite that you add to a applicatio is assiged to a layer. For example, suppose you create a applicatio with multiple sprites. Whe you add the first sprite to the applicatio, it is placed at the topmost layer. Whe you add the applicatio s secod sprite, it gets added to the top layer, ad the previous sprite gets moved back oe layer. Each additioal sprite starts off o the top layer ad stays there util you either add aother ew sprite or util you click o oe of the sprites that was previously added, which moves the selected sprite back to the topmost layer. By default, the first sprite would be placed o the top layer. The secod sprite added to the applicatio would be placed o the secod layer, ad the third sprite would be placed o the third layer. Uderstadig the layer o which a sprite has bee placed is importat because the sprite s layer assigmet determies whether it remais o top or is displayed udereath aother sprite whe they overlap oe aother. Sprites at higher levels remai o top of sprites at lower levels. Note To better uderstad the importace of levels, cosider what happes whe you place five pieces of paper o top of oe aother o a desk. The piece of paper sittig o top (at the top layer) is visible, ad your view of the other pieces of paper is obstructed. Now, reach ito the middle of the stack of paper, pull out a sheet, ad place it o top of all the other pages. By alterig the page s layer positio, you have ow made it visible. I additio to cotrollig what happes to sprites by addig them to applicatios i a specific order, cotrollig their layer positio, you ca use the code blocks show i Figure to programmatically cotrol a sprite layer locatio. For example, usig the first code block, you ca move a sprite to the top layer, esurig that it remais visible at all times o the stage, eve whe other sprites come ito cotact with it. Figure With these code blocks, you ca determie what happes whe two sprites overlap.
228 Developig the Crazy Eight Ball Game 207 As a example of how to work with both of these code blocks, revisit the Ball Chase game that was preseted i Chapter 9, where both of these two code blocks were used to esure that ed of game messages were displayed o top of all other sprites. I additio, the applicatio also used these blocks to esure that the cat overlaps the ball whe it catches it. Developig the Crazy Eight Ball Game Now it is time to tur your attetio to the developmet of a ew Scratch applicatio, the Crazy Eight Ball game. This game simulates the operatio of a crazy eight ball fortue-tellig toy. As you work o the developmet of this game, you will get additioal experiece with differet looks code blocks. I total, the applicatio is made up of three sprites ad three scripts. Figure shows how the game looks whe first started. To play the game, thik of a questio ad the click o the image of the cat located i the ceter of the eight ball. Oce clicked, the image of the cat is replaced with a 8, as demostrated i Figure 10.13, ad over the ext four secods, the souds of bubbles ca be heard. Figure To play, you must ask questios that ca be aswered with yes/o-style aswers. Figure It takes a few momets for the crazy eight ball to come up with a aswer.
229 208 Chapter 10 Chagig the Way Sprites Look ad Behave Figure The crazy eight ball has decided ot to aswer the player s questio. The crazy eight ball displays ay of five radomly selected aswers i respose to player questios. The rage of aswers supported by the game icludes: Maybe! No! Yes! Ask a differet questio! Maybe...but the maybe ot! Figure shows how the game looks oce it has fially decided o a aswer to the player s questio. The developmet of this applicatio project will be created by followig a series of steps, as outlied here: 1. Creatig a ew Scratch applicatio project. 2. Addig ad removig sprites. 3. Addig the variable eeded by the applicatio. 4. Addig a audio file to the applicatio. 5. Addig a script to cotrol the display of the 8 i the eight ball. 6. Addig the programmig logic required to operate the eight ball. 7. Savig ad executig your work.
230 Developig the Crazy Eight Ball Game 209 Step 1: Creatig a New Scratch Project Begi the creatio of the Crazy Eight Ball game by creatig a ew Scratch project. The easiest way is to start Scratch, which automatically creates a ew applicatio project. Alteratively, if Scratch is already ope, create a ew applicatio by clickig o the New butto located o the Scratch meu bar. Step 2: Addig ad Removig Sprites The Crazy Eight Ball game cosists of three sprites ad three scripts, as show i Figure The first sprite that you eed to add to the game is that of a empty eight ball. The secod sprite is that of a umber 8. You will fid copies of graphics for both of these sprites located o this book s compaio CD-ROM. You ca add these sprites to your ew Scratch applicatio by clickig o the Choose New Sprite from File butto ad the selectig these files. Alteratively, you ca create them yourself by clickig o the Pait New Sprite butto ad the usig the Pait Editor program. Oce added to the stage, repositio these two sprites so that the eight ball is cetered i the middle of the stage ad the umber is cetered i the middle of the eight ball. The applicatio s third sprite is that of a cat s face. You ca create this sprite by usig the Pait Editor program to edit the applicatio s default sprite, removig the Cat sprite s body, leavig just its face i place. Oce modified, click o the Figure A overview of the differet compoets that make up the Crazy Eight Ball game.
231 210 Chapter 10 Chagig the Way Sprites Look ad Behave Figure The Crazy Eight Ball game requires oe variable. Grow Sprite butto located o Scratch s toolbar ad the click o the image of the Cat sprite 12 times to icrease the size of the cat s face. Next, repositio the Cat sprite, movig it oto the ceter of the eight ball so that it overlaps the Cat sprite. At this poit, the overall desig of the Crazy Eight Ball game is complete. Before movig o to the ext step, reame these three sprites Cat, EightBall, ad Number, as show i Figure Step 3: Addig a Variable Required by the Applicatio I order to execute, the Crazy Eight Ball game requires the defiitio of the variable show i Figure To add this variable, click o the Variables butto located at the top of the blocks palette, click o the Make a Variable butto, ad create a ew variable amed RadomNo. This variable will be used to store a radomly geerated umber that the game will use whe geeratig aswers to player questios. Step 4: Addig a Audio File to the Applicatio The Crazy Eight Ball game makes use of a sigle soud effect, which souds like bubbles beig blow i water. This soud is played for four secods precedig the display of the eight ball s aswer. The audio file that is played must be added to the Cat sprite. To add this soud file, select the Cat sprite thumbail i the sprite list ad the click o the Souds tab located at the top of the scripts area. Next, click o the Import butto to display the Import Soud widow, doubleclick o the Effects folder, select the Bubbles audio file, ad the click o OK. Step 5: Creatig a Script to Cotrol the Display of the 8 i the Eight Ball Of the applicatio s three scripts, two belog to the Number sprite. These scripts, show ext, are automatically executed based o the receipt of broadcast messages.
232 Developig the Crazy Eight Ball Game 211 Specifically, whe a message of Show 8 is received, the Eight sprite is made visible. Whe the message Hide 8 is received, the Eight sprite is hidde. The receipt of these messages serves as triggers, which cotrol whe the Eight sprite is visible (which oly occurs whe the eight ball is i the process of preparig to geerate a aswer). As you ca see, these two scripts each use a looks code block to cotrol sprite visibility. Sice the game begis by displayig oly the image of the Cat sprite, go ahead ad click o the secod script belogig to the Eight sprite, hidig it from view. Step 6: Addig the Programmig Logic Needed to Cotrol the Eight Ball The last script i the applicatio, show ext, belogs to the Cat sprite. It is executed wheever the player thiks of a questio ad clicks o the Cat sprite for a aswer. Oce started, the script begis by assigig a radom umber i the rage of 1 to 5 to the RadomNo variable. Next, a looks code block is executed, hidig the Cat sprite ad the the broadcast message Show 8 is set. This message will trigger the
233 212 Chapter 10 Chagig the Way Sprites Look ad Behave executio of a script belogig to the Eight sprite. Next, the Bubbles audio file is played, ad the script s executio is paused for four secods, allowig Scratch time to fiish playig the audio file. Oce the four secods is up, a secod broadcast message of Hide 8 is set, triggerig the hidig of the Eight sprite. Next, the Cat sprite is redisplayed o the stage ad the value assiged to RadomNo is aalyzed. Depedig o the value assiged to RadomNo, oe of five differet text messages is displayed i a speech bubble. After two secods, the bubble is closed, ad the game waits o the player to ask aother questio. Step 7: Savig ad Executig Your Scratch Project At this poit, you have all of the iformatio you eed to create your ow copy of the Crazy Eight Ball game. As log as you followed alog carefully with the istructios provided i this chapter, you should ot ru ito ay problems. If you have ot doe so yet, save your ew Scratch applicatio project ad the switch over to Presetatio mode ad test it. Summary I this chapter, you leared how to work with Scratch s looks code blocks. This icluded learig how to switch betwee sprite costumes ad differet stage backgrouds ad how to apply a rage of special effects to sprites ad backgrouds. You leared how to display text i speech ad thought bubbles, cotrol the size of sprites, ad programmatically cotrol sprite visibility. You also leared about the importace of uderstadig layerig ad how it affects the display of sprites. This chapter also guided you through the creatio of the Crazy Eight Ball game.
234 chapter 11 Spicig Thigs Up with Souds May differet types of applicatios, especially computer games, rely o soud as a meas of coveyig meaig ad excitemet. Through the additio of backgroud music ad soud effects, applicatios ca really come alive, providig users with a deeper ad more meaigful experiece. I Scratch, soud effects ad music are itegrated ito applicatios usig soud blocks. This chapter will teach you how to work with all of Scratch s soud blocks ad demostrate how to icorporate audio files, drum otes, ad musical otes ito your applicatios. O top of all this, you will lear how to create a ew applicatio called the Family Picture Movie, which demostrates how to create a slideshow complete with accompayig backgroud music. The major topics covered i this chapter iclude learig how to Cotrol the playback of audio files Play drum beats ad pause drum play Set ad cotrol the volume at which audio files, otes, ad musical istrumets are played Set ad chage the tempo of drum ad ote play 213
235 214 Chapter 11 Spicig Thigs Up with Souds Playig Souds To add the playback of music ad soud effects to your applicatios, you eed to lear how to use the soud code blocks show i Figure These code blocks provide everythig you eed to play or stop the playback of MP3 ad wave files i your Scratch applicatios. Note A wave file is a type of file desiged for storig a audio bit stream o persoal computers. Wave files have a.wav file extesio. A MP3 file is a audio file that utilizes advaced compressio techology while retaiig high audio quality. The first two code blocks show i Figure 11.1 let you play ay MP3 or wave file that you add to your Scratch project. The third code block lets you stop the playback of all of the audio files belogig to a sprite. I order to play a audio file, you must first add it to a sprite or to the stage, which you ca do by selectig thestageoraspritefromthespritelist,clickigothesoudstablocatioat the top of the scripts area, the clickig o the Import butto. Oce the file is imported, you ca play the audio file usig a script belogig to the stage or sprite, as demostrated here. I Figure 11.2, a audio file amed meow is played whe the gree flag butto is pressed. I order to play the audio file, you must select it from the code block s drop-dow list. The drop-dow list is automatically populated by Scratch with all the audio files that have bee added to the sprite to which the script belogs. The soud code block used i the previous script allows the script to which it has bee added to cotiue ruig. If the script cotaiig the soud block Figure 11.1 These code blocks cotrol audio file playback. Figure 11.2 The meow audio file is played whe the gree flag butto is clicked.
236 Playig Souds 215 has additioal code left to be executed, the playback of the soud will be cut short whe the script cotiues executig. This was ot a problem i the previous example because the soud block was the last code block i the script. For situatios where you wat to pause script executio to allow time for the etire audio file to fiish playig, you have two choices. First, you ca add a cotrol block to the script immediately followig the soud block that pauses script executio for a specified umber of secods (the umber of secods eeded to play the audio file). Better yet, you ca use the secod code block show i Figure 11.1 as demostrated i the followig script: The soud code block used i this example plays a audio file that has bee previously added to your Scratch applicatio, pausig script executio util the audio file has fiished playig. Oce playback is complete, the rest of the script is permitted to fiish its executio. Tip If you wat to add the repeated playback of backgroud music or soud effects to a applicatio, create a script specifically for this purpose. This keeps the programmig logic eeded to play the audio file separate from other scripts ad elimiates the eed to pause other scripts executio to support audio playback. Depedig o what your applicatios are desiged to do, there may be times whe you wat to stop the playback of audio files belogig to a sprite or the stage. This ca be achieved usig the third code block show i Figure 11.1, as demostrated i the followig example: Here, the playback of ay audio files belogig to the sprite is immediately halted whe the spacebar is pressed.
237 216 Chapter 11 Spicig Thigs Up with Souds Note I additio to playig ay of the audio files supplied with Scratch, you ca import exteral audio files, both MP3 ad wave, ito ay sprite. If your computer has a microphoe, you ca record your ow audio files by selectig a sprite or the stage, clickig o the Souds tab located at the top of the scripts area, ad the clickig o the Record butto. This opes the Soud Recorder program show i Figure To record a custom soud, just click o the red Record butto, ad whe you are doe, click o OK. Oce doe, your ew audio file will be displayed o the Souds tab immediately available to your applicatio. Figure 11.3 Scratch makes it easy to record your ow custom audio files. Play a Drum Usig the two code blocks show i Figure 11.4, you ca add the playig of a drum to your Scratch applicatio ad, whe ecessary, pause drum play for a specified umber of beats. The first code block show i Figure 11.4 plays a drum soud for a specified umber of beats. This code block lets you choose from amog 46 differet types of drums, each of which is easily selected by clickig o the code block s dropdow list, as demostrated i Figure The secod code block show i Figure 11.4 lets you mometarily pause drum play for a specified umber of beats. Usig both of the code blocks, you ca play a wide assortmet of drums withi your applicatios. Figure 11.4 These code blocks let you cotrol the playig of a drum withi your applicatios.
238 Playig Musical Notes 217 Figure 11.5 This code block supports the playig of 46 differet types of drum souds, umbered from 35 to 81. I this example, the first soud block plays a drum beat for five beats usig a Acoustic Sare. The secod soud block rests for.5 beats, ad the third code block uses a Ope Triagle to play a drum for.5 beats. Playig Musical Notes I additio to playig audio files ad differet types of drum beats, Scratch lets you play musical otes with various istrumets usig the soud code blocks show i Figure The first code block plays a ote for a particular umber of beats. You ca specify a ote either by typig it ito the code block s first iput box or by clickig o the drop-dow list located iside the code block s iput field, which displays a graphic represetatio of a piao keyboard. Usig this keyboard, you ca select a ote by clickig o oe of the keyboard keys, as demostrated i Figure The rage of available otes is from 0 to 127, with 60 represetig the middle C ote. The secod code block show i Figure 11.6 specifies the istrumet to be used ad is desiged to be used i cojuctio with the first cotrol block. It supports a total of 128 differet istrumets, umbered 1 to 128. You ca select a
239 218 Chapter 11 Spicig Thigs Up with Souds Figure 11.6 These soud blocks let you play otes usig musical istrumets. Figure 11.7 Selectig a ote is as easy as clickig o a piao key. Figure 11.8 Selectig the istrumet you wat play withi your Scratch applicatio. istrumet by keyig its umber ito the block s iput field or by selectig a istrumet from the block s drop-dow list, as demostrated i Figure The followig script demostrates how to use both of the code blocks show i Figure 11.6 to play a C ote followed by a D ote usig a harpsichord. Each ote is played for.5 beats.
240 Cofigurig Audio Volume 219 Cofigurig Audio Volume Rather tha playig audio files, drum beats, ad musical otes at whatever volume the computer is set to, you ca use the soud code blocks show i Figure 11.9 to chage or set the volume at which audio files, drum beats, ad musical otes are played. The first code block show i Figure 11.9 is used to chage the volume of soud playback for a idividual sprite. Usig this code block, you ca chage a sprite s volume by a specified percetage, with 0 beig o volume ad 100 beig the maximum volume. The secod code block lets you assig a specific value to a sprite i the rage of 0 to 100. Usig the third code block, you ca retrieve a sprite s volume ad optioally display this value i a moitor o the stage. Note Volume is set idividually for each sprite i a applicatio. Therefore, you ca assig differet volume levels to each sprite i your applicatio. A example of how to work with the first of these cotrol blocks is provided here: Here, a audio file amed meow is played at the computer s default volume level. Next, the volume settig for the sprite to which the script has bee added is Figure 11.9 Usig these code blocks, you ca take cotrol of the volume of music ad soud effects played by ay sprite i your applicatio.
241 220 Chapter 11 Spicig Thigs Up with Souds reduced by 80%. The meow file is the played a secod time, this time much quieter. I this ext example, the sprite s volume is set to 10 percet of its default volume level, after which a audio file amed meow is played. Note The third code block show i Figure 11.9 ca be used to retrieve a sprite s curret volume level. I additio, by selectig its check box, you ca eable a moitor that displays the volume level of the sprite o the stage. Settig ad Chagig Tempo The last three looks blocks provided by Scratch are show i Figure Usig these blocks you ca set, chage, ad report o the tempo at which drum beats ad musical otes are played. The first code block show i Figure chages the tempo used to play a drum or ote. Tempo is a measuremet of the speed, i beats per miute, at which a drum or ote is played. The larger the tempo value, the faster the drum or ote is played. The secod code block lets you set the tempo used to play a drum or ote to a specific umber of beats per secod. Usig the third code block, you ca retrieve a sprite s curretly assiged tempo ad optioally display this value i a moitor o the stage. The followig script demostrates how to set ad modify a sprite s tempo whe playig musical otes: Figure These code blocks allow you to modify ad report o the tempo used by a sprite to play beats ad otes.
242 Creatig the Family Picture Movie 221 Here, the tempo used to play otes is set to 60 beats per miute, ad the, after a oe-secod pause, a C ote is played five times i a row, each time for a half a beat. After aother oe-secod pause, the sprite s tempo is slowed dow by 20 beats per miute, ad aother C ote is played five times. Creatig the Family Picture Movie The rest of this chapter is dedicated to showig you how to develop your ext applicatio project, the Family Picture Movie. The developmet of this applicatio provides the opportuity to work further with differet soud blocks, cotrollig soud volume, playback, ad playback termiatio. I total, the applicatio will be made up of 8 sprites ad 13 scripts. Figure shows how the applicatio looks whe iitially started. To ru the applicatio ad view its picture show, all you have to do is click o the gree flag butto. Oce clicked, the applicatio begis a aimatio sequece that couts dow from five ad the starts displayig a series of pictures represetig the cotets of the movie, as demostrated i Figure Backgroud music is played to help set a friedly toe as the pictures are displayed. The Family Picture Movie is capable of displayig ay umber of pictures. Oce the movie eds, credits are displayed, as show i Figure Figure The applicatio begis by displayig a series of umbers, from 5 to 1, o a orage radar scree.
243 222 Chapter 11 Spicig Thigs Up with Souds Figure As the movie plays, a series of pictures is displayed at three-secod itervals. Figure Credits are displayed at the ed of the movie. The developmet of this project will be created by followig a series of steps, as outlied here: 1. Creatig a ew Scratch project. 2. Addig ad removig sprites ad backgrouds. 3. Addig the variable eeded by the applicatio. 4. Addig a audio file to the applicatio. 5. Addig the programmig logic to cotrol applicatio executio. 6. Savig ad executig your work. Step 1: Creatig a New Scratch Project To begi the developmet of the Family Picture Movie, you must create a ew Scratch project. If Scratch is ot already ruig, start it up, ad you will be ready to go. Otherwise, if you already have Scratch ope, click o the New butto located o the Scratch meu bar, ad a ew project will be created for you.
244 Creatig the Family Picture Movie 223 Figure A overview of the differet compoets that make up the Family Picture Movie applicatio. Step 2: Addig ad Removig Sprites ad Backgrouds The Family Picture Movie is made up of 8 sprites ad 13 scripts, as show i Figure The applicatio cosists of two separate backgrouds: Couter, which is displayed whe the applicatio is first started ad begis its coutdow, ad the default blak stage backgroud. A copy of the Couter backgroud ca be foud o this book s compaio CD. To add it, click o the Stage thumbail located o the sprite list ad the click o the Backgrouds tab located at the top of the scripts area. Next, click o the Import butto ad use the Import Backgroud widow to locate ad select the Couter backgroud file. Sice the Couter backgroud is goig to be used as the applicatio s iitial backgroud, drag ad drop its thumbail from the bottom of the list of backgroud files to the top positio. I additio to the backgroud, the Family Picture Movie makes use of a umber of sprites. As show i Figure 11.15, the first of these sprites is a black lie. You ca create this sprite yourself usig the Pait Editor program, or you ca import the Lie sprite located o this book s compaio CD. To add this sprite, click o the Choose New Sprite from File butto, opeig the New Sprite widow, ad the locate ad import the sprite. Oce the sprite is added, you eed to positio it exactly as show i Figure
245 224 Chapter 11 Spicig Thigs Up with Souds Note If you elect to create your ow versio of the Lie sprite, you will eed to set the rotatio ceter for the sprite as show i Figure Figure Assigig a rotatio ceter to the Lie sprite. Next, you eed to add five sprites represetig umbers displayed durig the applicatio s opeig aimatio sequece. To add the first of these five umbers, click o the Choose New Sprite from File butto ad the drill dow ito the Letters folder followed by the Stoe folder. Next, select the 5 sprite ad click o the OK butto. As you will see, the sprite is colored black ad white. However, it is supposed to be red ad yellow. To fix this, you eed to edit the sprite ad chage its colors. To do this, select the thumbail represetig the sprite ad the click o the Costumes tab located at the top of the scripts area. Next, select the sprite s thumbail ad click o its Edit butto, opeig it i the Pait Editor program. Usig the Fill tool located i the Pait Editor s toolbar, modify the black portios of the sprite ad make them red. The modify all of the white portios of the sprite, makig them yellow. Usig the steps outlied above, add the 4, 3, 2, ad 1 sprites to the applicatio, editig each oe so that they are red ad yellow. Oce the iitial aimatio sequece has fiished, the Family Picture Movie begis displayig a series of graphics pictures. To add the first of these pictures, click o the Choose New Sprite from File butto ad the add ay graphic files that you wat. If you do ot have a suitable graphic file hady, you ca use the
246 Creatig the Family Picture Movie 225 Pics file located o this book s compaio CD. The rest of the pictures show i the applicatio will be displayed by chagig this sprite s costume. To add additioal costumes to the sprite, select the sprite, click o the Costumes tab located at the top of the scripts area, ad the click o the Import butto, opeig the Import Costume widow. If you do ot have ay suitable pictures to be used as backgrouds, you ca import the backgroud files provided o this book s compaio CD. These backgroud files have ames like IM The last sprite to be added to the applicatio is a graphic file that displays the applicatio s credits. You ca create ad add your ow sprite usig the Pait Editor program, or you ca import the Credits sprite located o this book s compaio CD. Oce this sprite has bee added, the stage should be filled with differet sprites. However, of all of these sprites, oly the Lie sprite eeds to remai visible. To temporarily remove each of the remaiig sprites from view, select each sprite oe at a time, click o the Looks butto located at the top of the blocks palette, ad the double-click o the Hide code block. By the time you are doe, the stage should look like the example show i Figure Step 3: Addig a Variable Required by the Applicatio I order to execute, the Family Picture Movie requires that you defie a sigle variable. To add this variable, click o the Variables butto located at the top of the blocks palette, click o the Make a Variable butto, ad the create a ew variable amed Couter, as show i Figure The applicatio will use the variable to cotrol the executio of the applicatio s opeig coutdow sequece, coordiatig the display of the umbers used durig the coutdow process. Step 4: Addig a Audio File to the Applicatio As it executes, the Family Picture Movie plays backgroud music to set the mood for the applicatio. The script resposible for playig this music belogs to the Pics sprite. To add this audio file to the Pics sprite, select the sprite s thumbail Figure The Family Picture Movie uses oe variable to help cotrol the opeig aimatio sequece.
247 226 Chapter 11 Spicig Thigs Up with Souds i the sprite list ad the click o the Souds tab located at the top of the scripts area. Next, click o the Import butto to display the Import Soud widow, double-click o the Music Loops folder, select the GuitarChords2 audio file, ad the click o OK. Step 5: Developig the Applicatio s Programmig Logic The programmig logic that drives the executio of the Family Picture Movie is orgaized ito 13 separate scripts, assiged to each of the applicatio s sprites ad to its backgroud. The overall executio of all of this applicatio s scripts is coordiated through the use of broadcast messages ad through the use of cotrol blocks that moitor the value assiged to the applicatio s variable, executig oly whe the variable reaches a predefied value. Settig Up the Opeig Aimatio Sequece The Family Picture Movie begis ruig whe the player clicks o the gree flag butto. Whe this occurs, a umber of the scripts withi the applicatio begi executig. Oe of these scripts is resposible for maagig the aimated sequece that plays whe the applicatio first begis executig. This script, show ext, must be added to the Lie sprite.
248 Creatig the Family Picture Movie 227 As you ca see, this script begis by settig the directio of the Lie sprite ad the makes it visible. Next, the Couter variable is assiged a startig value of 6, after which a loop is set up to execute five times. Withi this loop, a secod loop executes 36 times (for a total of 360 degrees), rotatig the Lie sprite by 10 degrees ad pausig.005 secod after each tur. The value assiged to Couter is the decremeted by a value of 1. By the time the outer loop has executed five times, five other applicatio scripts, moitorig the value assiged to Couter, are executed. Each of these five scripts is resposible for displayig a umber o the stage. The ed result is a aimated sequece that emulates the coutdow that is ofte displayed at the begiig of old movie reels. Oce the coutdow has bee completed, a secod loop executes, rotatig the Lie sprite oe fial time aroud the ceter of the stage. Oce the last loop has fiished, the value of Couter is reset to 6 ad poited back to its iitial directio. A oe-secod pause the esues, ad the Lie sprite is hidde. Lastly, a cotrol block is used to sed a broadcast message of Start Movie. This broadcast message will be used to trigger the executio of two scripts belogig to the Pics sprite, which is resposible for displayig the pictures that make up the applicatio s picture show. Displayig the Numeric Coutdow As the previous script executes, it modifies the value assiged to the Couter variable, chagig its value from 6 to 1, oe umber at a time. Each of the five sprites represetig the umbers displayed durig the opeig aimatio sequece is displayed by scripts belogig to those sprites. The scripts belogig to each sprite are early idetical. The followig script belogs to the Sprite5 sprite: As you ca see, it starts executig whe the player clicks o the gree flag butto, which begis by makig sure that the sprite is hidde from view. The script the goes ito a loop that waits util the value of Couter is set to 5. Oce this occurs,
249 228 Chapter 11 Spicig Thigs Up with Souds the script displays the sprite for 1.6 secods ad the hides it agai. After creatig this script, drag ad drop a istace of it oto the Sprite4, Sprite3, Sprite2, ad Sprite1 sprites ad the modify the scripts belogig to each sprite by chagig the value that is looked for to 4, 3, 2, ad 1, respectively. Switchig Costumes ad Playig Backgroud Music As has bee previously stated, the applicatio displays differet pictures by chagig costumes. I additio, backgroud music is played to help set the mood as the picture show begis. Two separate scripts, belogig to the Pics sprite, are resposible for maagig the switchig of costumes ad the playig of the applicatio s audio file. Both of the scripts are automatically executed whe the Start Movie broadcast message is received. The first of these two scripts, show ext, maages costume switches. It begis by displayig a default costume of IM000327, which is the displayed o the stage. Next a loop is set up that pauses three secods ad the switches the sprite s costume to the ext costume i the list. The secod script, show ext, begis by sedig out its broadcast message of Clear backgroud ad the sets the sprite s value to half its curret level. Next, a loop is set up that executes 10 times. Each time the loop executes, a audio file amed GuitarChords2 is played. At the ed of its teth executio, the loop halts, ad the Pics sprite is hidde. The script eds by sedig out a broadcast message of Show Credits.
250 Creatig the Family Picture Movie 229 Note The Show Credits broadcast message is used as a trigger that executes a script belogig to the Credits sprite. Displayig the Closig Credits The Credits sprite has two scripts, as show ext. The first script executes whe the gree flag butto is pressed ad is resposible for removig the display of the sprite from the stage. The secod script is automatically executed whe the Show Credits broadcast message is received. It displays the Credits sprite, waits three secods, ad the hides the sprite, leavig the stage blak. The script eds by executig a cotrol block that halts the executio of the applicatio s scripts. Switchig Backgrouds The last two scripts belog to the stage. These scripts are show ext. The first script executes whe the gree flag butto is clicked. Its job is to switch the stage s backgroud to Couter, readyig the applicatio to begi its five-secod coutdow sequece. The secod of the stage s scripts automatically executes whe the Clear Backgroud broadcast message is received. Oce executed, it switches the stage back to the default Clear backgroud.
251 230 Chapter 11 Spicig Thigs Up with Souds Step 6: Savig ad Executig Your Scratch Project Assumig you have followed alog carefully with the istructios that have bee provided, your copy of the Family Picture Movie should be ready for testig. If you have ot already saved your ew applicatio, do so ow. Whe you are ready, click o the gree flag butto to ru the applicatio ad watch the movie. I the evet that you ru ito ay problems, go back ad recheck your work agaist the istructios outlied i this chapter. Summary The additio of soud playback is fudametal to the operatio of may Scratch applicatios. I Scratch, soud effects ad music playback are cotrolled through differet soud code blocks. Usig these code blocks, you ca covey additioal meaig ad ehace excitemet whe your applicatios ru. This chapter provided istructio o how to work with all of Scratch s soud blocks ad to use them to play audio files, drum otes, ad musical otes. You also leared how to chage the tempo at which drums ad otes are played; cotrol the volumes at which audio files, otes, ad drum beats are played; ad select differet types of drums ad istrumets to be played.
252 chapter 12 Drawig Lies ad Shapes I additio to displayig sprites with differet costumes ad differet stage backgrouds, Scratch also draws custom lies, shapes, ad other graphics usig pe code blocks. Usig a virtualized pe, these blocks allow you to set the color, width, ad shade used i drawig operatios. This chapter reveals how to work with all of Scratch s pe blocks ad will ed by demostratig how to use them to create a pait drawig applicatio. The major topics covered i this chapter iclude learig how to: Draw usig Scratch s virtual pe Set the color used whe drawig Set pe shade ad size Stamp a copy of a costume o the stage Clear the stage of ay drawig operatios Clearig the Stage Area The first of Scratch s pe code blocks, show i Figure 12.1, is desiged to let you clear out ay drawig operatios that you may have made o the stage. 231
253 232 Chapter 12 Drawig Lies ad Shapes Figure 12.1 This pe block is used to clear out ay drawig operatios that you may have made o the stage. Aythig you draw or stamp o the stage s curret costume does ot actually chage the costume. Therefore, whe you clear out ay drawig, the costume that makes up the backgroud remais uchaged. The followig script demostrates how easy it is to use this code block: By addig a script like this to a Scratch applicatio, you ca reset the stage back to its origial state (erasig ay drawig made to the stage). Drawig with the Pe Withi Scratch applicatios, drawig is performed usig a virtual pe. This pe works very much like a real pe. Whe placed i a dow positio, it ca be used to draw o the stage. Whe placed i a up positio, drawig ceases. I order to draw or stop drawig, you must be able to programmatically cotrol the pe s up ad dow positios, which you ca do usig the code blocks show i Figure Usig the first code block, you ca easily create a simple drawig applicatio. To create this applicatio, start a ew Scratch project ad the delete the default cat sprite ad replace it with a ew sprite made up of a small black dot (easily created usig the Pait Editor program). Oce you have created your ew applicatio as described above, select its sprite ad add the followig script to it: Figure 12.2 Usig these pe blocks, you ca cotrol whe the pe ca be used to draw.
254 Drawig with the Pe 233 Whe executed, this script clears the stage ad the places Scratch s virtual pe i a dow positio, eablig drawig to occur (wheever the sprite to which the script belogs is moved). Next, a loop is set up that uses a motio block to make the sprite follow the poiter aroud the stage. As a result, wheever you move the mouse aroud the stage, the sprite follows, ad a lie is draw. Oce you create ad ru your ow copy of this applicatio, it should become immediately clear that you do ot have eough cotrol over the pe. Specifically, you caot cotrol whe ad whe ot to draw. This situatio is easily remedied by modifyig the script so that you ca place the pe i a dow or up positio based o the status of the mousepoiter s left-mouse butto, as show ext. Figure 12.3 shows a example of a picture draw o the stage usig this modified versio of the applicatio. By beig able to cotrol whe the pe is i a dow positio, you ca produce a precise drawig. Figure 12.3 A quick little doodle created usig a small drawig applicatio.
255 234 Chapter 12 Drawig Lies ad Shapes Settig Pe Color I additio to beig able to clear the stage ad cotrol whe the pe is up or dow, Scratch also specifies the color that is used i drawig operatios usig ay of the three pe code blocks show i Figure The first code block show i Figure 12.4 lets you set the color to be used whe drawig by allowig you to click o the color swatch located i its iput field. Whe the swatch is clicked, Scratch respods by displayig a color palette, as show i Figure You ca select the color you wat either by clickig o the color show withi the color palette or by movig the poiter, which ow looks like a dropper, over ay color curretly displayed aywhere o the Scratch IDE ad clickig i it. Oce specified, the color you selected is displayed i the code block s iput area. The followig script demostrates how to use this code block to specify the color you wat to use. Here, the stage is cleared, ad the pe s color is set to red. Otherwise, the applicatio operates o differetly tha before. Figure 12.4 The code blocks let you cotrol the color used whe drawig. Figure 12.5 Select a color by clickig aywhere o the color palette.
256 Settig Pe Color 235 Scratch also lets you specify the color to be used whe drawig by specifyig a umber. For example, the followig list idetifies umbers that you ca use to specify a rage of commoly used colors. 0 = red 20 = orage 35 = yellow 70 = gree 130 = blue 150 = purple 175 = pik By experimetig with other umbers, you idetify a host of differet colors. For example, usig the secod code block show i Figure 12.4, you chage the color used whe drawig, chagig it relative to its curretly assiged value.
257 236 Chapter 12 Drawig Lies ad Shapes Here, the pe block has bee added to the begiig of the script s loop. Each time the loop repeats, it chages the pe s curret color assigmet by a value of 10. The result is that a raibow effect is applied as you draw, with the color chagig across a full spectrum supported by Scratch as you move the mouse ad draw o the stage. Usig the third code block show i Figure 12.4, you ca specify the color to be used whe drawig usig its associated umeric value. For example, you could modify the applicatio s script to draw usig red with this code block by passig it a value of 0, as demostrated here. Chagig Pe Shade I additio to selectig color, Scratch also allows you to select the level of shadig applied whe drawig. The rage of values supported by the pe shade is 1 to 100, as demostrated i Figure By default, Scratch applies a shadig value of 50 whe drawig colors. A shade value of 0 results i a black color. A shade value of 100 results i white. Scratch lets you specify the level of shadig to be applied whe drawig usig either of the pe code blocks show i Figure Figure 12.6 Shadig affects the applicatio of light to a color.
258 Chagig Pe Shade 237 Figure 12.7 You ca chage the value used to apply shadig by varyig its curret value or by settig a etirely ew value. As a example of how to work with the first code block show i Figure 12.7, let s modify the drawig example agai as show here. Here, the shadig level has bee icreased by a value of 10. Rather tha chage the shadig level relative to its curret value, you ca use the secod code block show i Figure 12.7 to specify a shade level, as demostrated i the followig script:
259 238 Chapter 12 Drawig Lies ad Shapes Workig with Differet Size Pes I additio to settig color ad shadig values, Scratch also lets you chage the size of the pe. This ca be accomplished usig either of the two pe code blocks show i Figure By default, Scratch draws usig a pe size of 1. You ca chage the pe size relative to its curret size usig the first code block, as demostrated i the followig script: Here, the size of the pe used i the drawig applicatio i icreased by 1, makig it twice its default size. If you prefer, you ca simply assig a specific pe size usig the secod code block, as demostrated here. Figure 12.8 Scratch supports a ulimited umber of pe sizes.
260 Stampig a Istace of a Costume o the Stage 239 Figure 12.9 A example of a drawig made usig a pe size of 10. I this example, the drawig applicatio has bee modified to use a pe that s size has bee icreased to 10. Figure 12.9 shows a example of a simple drawig created usig the applicatio with this pe size. Stampig a Istace of a Costume o the Stage I additio to all of the pe code blocks demostrated so far, Scratch provides oe last block, show i Figure 12.10, which allows you to capture a sprite s costume ad use it to stamp copies of the sprite o the stage. As a example of how to work with this code block, create a ew Scratch applicatio, remove the default cat sprite from it, ad the add a copy of the crab1-a sprite to it. You will fid this sprite i Scratch s Aimals folder. Oce added, shrik the sprite dow to about a third of its default size ad the add the followig script to it. Figure This code block lets you use a sprite s costume as the basis for creatig a stamp.
261 240 Chapter 12 Drawig Lies ad Shapes Figure Decoratig the stage usig a sprite as the basis for geeratig stamps. Whe executed, this script clears the stage of ay previous drawig, which also icludes stamps, moves the sprite to the upper-left corer of the stage, ad sets its directio. Next, a loop is executed four times, stampig the image of the sprite four times as it is moved aroud the stage. Figure shows how the stage will look oce the script has fiished executig. Creatig the Doodle Drawig Applicatio At this poit you have completed your review of all of Scratch s code blocks ad have leared how to put them all to work. Now it is time to work o the chapter s applicatio project, the Doodle Drawig applicatio. This pait-like applicatio expads o the examples you have bee workig o throughout this chapter, makig extesive use of the pe code blocks ad allowig you to draw by selectig from a rage of predefied colors. The applicatio allows you to draw usig a rage of differet pe sizes. There is also a Clear feature that lets you start over ay time you wat so that you ca begi workig o a ew drawig. I total, the Doodle Drawig applicatio is made up of 12 sprites ad 3 scripts. Figure shows how the game looks whe first started. Figure Drawigs are made by holdig dow the left mouse butto ad movig the mouse-poiter aroud the stage.
262 Creatig the Doodle Drawig Applicatio 241 Figure You ca use ay of 10 colors ad 9 differet pe sizes whe drawig. To create a drawig, click o oe of buttos show o the left-had side of the stage to pick a color, the hold dow the mouse s left butto, ad move the mouse-poiter aroud the stage. If you wat, you ca use differet-sized lies whe drawig by pressig keyboard keys 1 through 9. Pressig the 1 key results i a thi lie, whereas pressig the 9 key results i a lie that is approximately a quarter-of-a-ich thick. If you make a mistake or wat to start over, you ca do so at ay time by clickig o the Clear butto located at the lower-left side of the stage. Figure shows the Doodle Drawig applicatio i actio. Here, the applicatio has bee used to draw a sowma, complete with a blue hat ad red scarf. The developmet of this applicatio project will be created by followig a series of steps, as outlied here: 1. Creatig a ew Scratch applicatio project. 2. Addig ad removig sprites. 3. Developig the applicatio programmig logic. 4. Savig ad executig your work. Step 1: Creatig a New Scratch Project To begi work o the Doodle Drawig applicatio, you eed to create a ew Scratch project. If Scratch is already ruig, click o the New butto located o the Scratch meu bar. Otherwise, start Scratch up, ad it will automatically create a ew applicatio for you to work o.
263 242 Chapter 12 Drawig Lies ad Shapes Step 2: Addig ad Removig Sprites The Doodle Drawig applicatio is made up of 12 sprites ad 3 scripts, as show i Figure This applicatio does ot eed the default cat sprite, so you should begi by removig that sprite from the applicatio. The first 10 sprites that you eed to add to the applicatio represet the applicatio butto cotrols. To add the first of these cotrols, click o the Choose New Sprite from File butto ad drill dow i to the Thigs folder where the New Sprite widow appears. Next, locate ad select the butto sprite ad the click o OK. Oce it has bee added to the stage, drag ad drop this sprite to the upper-left corer of the stage, click o the Costumes tab located at the top of the scripts area, ad the click o the sprite s Edit butto. Usig the Fill tool feature located o the Pait Editor s toolbar, chage the etire surface of the sprite to red. This will take a umber of differet clicks because the sprite has may shaded areas ad caot therefore be filled with red i a sigle click. Oce you have completed this task, click o the Pait Editor s OK butto ad the reame the sprite Red. Figure A overview of the differet parts of the Doodle Drawig applicatio.
264 Creatig the Doodle Drawig Applicatio 243 Now that the first of the 10 butto sprites has bee created, thigs will go a lot faster. Right-click o the Red sprite ad select Duplicate from the popup meu that appears. Reame the ew sprite Orage ad the click o the Edit butto located i the Costumes tab. Usig the Fill tool cotrol, make the butto orage ad the click o OK. Now, repositio the Orage sprite so that it lies up just uder the Red sprite. Usig the steps outlied i this paragraph, create eight more buttos for the followig colors. Yellow Gree LightBlue NavyBlue Purple Pik Black White Next, you eed to add a small sprite i the shape of a black dot to the applicatio. To do so, click o the Pait New Sprite butto ad the whe the Pait Editor appears, click oce o its cavas to make a black dot ad the click o the OK butto. Reame this sprite Drawig Poit. Now,add thelastspriteusigthesamestepsyouusedtoaddtheapplicatio sfirst butto. Oce added, click o the Edit butto located o the Costumes tab ad usig the Text tool feature located o the Pait Editor s toolbar, add the word Clear o top of the butto (usig the ComicSas fot with a fot size of 14). Click o the OK butto whe you are doe ad reame the sprite Clear ad the repositio the sprite so that it appears as the fial butto o the lower-right side of the stage. The default blak backgroud will be used i this applicatio to provide it with white space o which to draw. Assumig that you have created all of the sprites as istructed above, you should be ready to begi the codig process.
265 244 Chapter 12 Drawig Lies ad Shapes Step 3: Creatig Scripts Used to Cotrol the Doodle Drawig Applicatio Most of the Doodle Drawig applicatio s programmig logic resides withi a sigle script belogig to the Drawig Poit sprite. This script is resposible for all drawig operatios, icludig determiig which color ad what size pe the user wats to use. The remaiig logic revolves aroud the clearig of the stage, which is hadled by two small scripts, oe belogig to the Clear sprite ad the other to the stage. Developig the Drawig Poit Sprite s Programmig Logic The programmig logic that cotrols the overall executio of all drawig withi the Doodle Drawig applicatio belogs to a script that must be added to the Drawig Poit sprite. Do ot let the legth of the code deceive you; the programmig logic is really very simple. To help make thigs easy to follow, the script will be developed i three parts. For the first part, create ad add the followig script to the Drawig Poit sprite: As you ca see, the script executes whe the gree flag butto is pressed. It starts by settig a default pe size of 4 ad a default color of black. Next, a loop is set up that will be used to maage the executio of all of the remaiig code blocks. The first set of code blocks to be embedded withi the loop is already preset. It cosists of a cotrol block that checks to see if the left mouse butto is beig pressed, ad if it is, the Drawig Poit sprite is moved to the mouse-poiter, the pe is placed i a dow positio, ad the Drawig Poit sprite is displayed. If the
266 Creatig the Doodle Drawig Applicatio 245 left mouse butto is ot beig pressed, the the pe is placed i a up positio ad the Drawig Poit sprite is hidde from view. The programmig logic outlied above is resposible for the overall maagemet of the drawig process ad is i fact all that is eeded to create a simple drawig applicatio. If you wat, you ca switch to Presetatio mode ad ru the applicatio ad use it to draw. Of course, as curretly writte, the applicatio oly allows the user to draw usig a color of black ad a pe size of 4. To ehace the applicatio so that the user ca select differet colors by clickig o oe of the color buttos located o the left-had side of the stage, add the followig code block to the ed of the script, placig it iside ad at the bottom of the script s loop. As you ca see, the code blocks show previously are orgaized usig 10 separate coditioal code blocks, each of which checks to see if the Drawig Poit sprite has bee moved over oe of the 10 color buttos. (I order for the sprite to be moved over oe of the buttos, the Drawig Poit sprite must be visible, which occurs oly whe the left mouse butto is pressed.) If it has, the the pe s color is chaged to reflect the butto the user has clicked.
267 246 Chapter 12 Drawig Lies ad Shapes Note The applicatio oly switches color whe the Drawig Poit sprite is moved over a color butto ad the left mouse butto is clicked. The Drawig Poit must be visible for this to work, ad this is the case oly whe the left mouse butto is beig pressed. Therefore, to select a color, the user must click o the color. Simply movig the mouse over a color will ot select it. I additio to allowig the user to choose a color by clickig o oe of the applicatio s 10 color butto cotrols, the applicatio also allows the user to chage pe size by clickig o keyboard keys 1 through 9. To eable support for differet pe sizes, add the followig code block to the script, iside ad at the bottom of the script s loop. As you ca see, these code blocks are orgaized usig ie separate coditioal cotrol blocks, each of which moitors the keyboard lookig for a specific key to be pressed ad chagig pe size accordigly. Clearig the Stage I additio to facilitatig drawig usig differet colors ad pe sizes, the Doodle Drawig applicatio also allows the user to clear the stage at ay time to ready it for a ew drawig. The programmig logic that allows the user to clear
268 Summary 247 the stage to start a ew drawig is maaged by the Clear sprite i cojuctio with the stage. The process of clearig the stage is iitiated wheever the user clicks o the Clear sprite. Whe this happes, the followig script, which eeds to be added to the Clear sprite, is executed. As you ca see, all that this script does is sed a broadcast message of Clear, idicatig that the user wats to clear the stage. This broadcast message serves as a trigger that iitiates the executio of the followig script, which must be added to the stage: As you ca see, this script is very straightforward. It executes a pe code block that clears off the stage wheever the Clear broadcast message is received. Step 4: Savig ad Executig Your Scratch Project All right! You ow have all of the iformatio eeded to create ad execute the Doodle Drawig applicatio. Assumig that you followed alog carefully with all of the istructios that were provided, you should be ready to test your ew applicatio. If you have ot already doe so, save your ew Scratch applicatio project adtheswitchovertopresetatiomodeadclickothegreeflagbutto. As you work with the Doodle Drawig applicatio, be sure to click o every oe of its buttos to make sure the pe switches its color whe drawig. Also, experimet with each of the applicatio s lie sizes to esure they are workig properly. Summary This chapter s focus was o teachig you how to work with Scratch s virtual pe to draw all kids of differet lies, shapes, ad graphics. You leared how to eable ad disable drawig by cotrollig the pe s up ad dow positio. You
269 248 Chapter 12 Drawig Lies ad Shapes leared how to modify the color ad pe width ad cotrol the level of shadig that is applied. You also leared how to capture a sprite s costume, use it to stamp its image o the stage, ad clear off ay drawig operatios from the stage. Fially, through the developmet of the Doodle Drawig applicatio, you got to put all of this ew iformatio to practical use.
270 Part III Advaced Topics
271 This page itetioally left blak
272 chapter 13 Sharig Your Scratch Projects over the Iteret Scratch s sloga is Imagie, Program, Share. As the sloga implies, sharig is a big part of Scratch. The Scratch website is specifically desiged to facilitate sharig ad to promote the developmet of a large global commuity of Scratch programmers. By sharig ideas ad projects with other Scratch programmers, you ot oly help others to lear but you icrease your ow kowledge ad experiece as well. This chapter will teach you everythig you eed to kow about how to upload, maage, ad share your Scratch applicatios o the Scratch website, helpig you to become a active member of Scratch s global commuity. The major topics covered i this chapter iclude learig how to: Register a ew accout at the Scratch website Upload your Scratch applicatios Delete applicatios that you have uploaded Post commets ad add tags to your uploaded applicatios Create galleries i which you ca store ad orgaize your applicatios Ruig Scratch Applicatios o the Iteret Scratch is all about learig ad sharig. The Scratch website ( scratch.mit.edu) is specifically desiged to facilitate both of these objectives, makig it easy for you to upload ad ru your Scratch applicatios olie ad 251
273 252 Chapter 13 Sharig Your Scratch Projects over the Iteret Figure 13.1 Determiig if your browser supports Java so that it ca ru Scratch applicatios. to ru ad dowload applicatios submitted by members of the global Scratch commuity. Note At the time this book was writte, over 125,000 Scratch projects had already bee posted o the Scratch website, providig a wealth of examples that you ca dowload, study, ad lear. To view ad ru Scratch applicatios o the Scratch website, you eed to use a web browser that supports Java. As a easy way to determie if Java is istalled o your browser, visit the Scratch website ad click o oe of the may available Scratch projects. If the applicatio opes, the Java is istalled ad workig correctly. However, if you see results similar to those show i Figure 13.1, Java is ot istalled. If you determie that you eed to istall Java, you ca do so for free by visitig clickig o the Free Java Dowload butto, ad followig the istructios that are provided. Registerig with the Scratch Website I order to upload your Scratch applicatios to the Scratch website, you must first register for a free Scratch accout. To do so, go to sigup as show i Figure 13.2 ad fill out the required form.
274 Registerig with the Scratch Website 253 Figure 13.2 You must register with the Scratch website before you ca upload your applicatios. Figure 13.3 Oce registered, you ca upload applicatios ad create galleries i which to store your applicatios. Note If you are over 18 years of age, you will also be prompted to supply your address. Oce you have fiished fillig out the required iformatio, click o the Sig Up butto. A ew accout will the be created for you, ad you will be logged ito the website, as demostrated i Figure 13.3.
275 254 Chapter 13 Sharig Your Scratch Projects over the Iteret Oce you have created a ew accout, you ca begi uploadig your Scratch applicatios. At the time this book was writte, the Scratch website placed a 10MB limit o the size of applicatio projects that could be uploaded. The purpose of this restrictio is to esure that plety of space is made available to all members of the Scratch commuity ad to help esure that upload ad dowload times are kept to a reasoable level. Oce they are uploaded, you ca maage your uploaded Scratch applicatios by loggig i to the Scratch website usig your ew accout. There is oe importat poit you eed to kow: Oce uploaded to the Scratch website, there is o way to restrict or keep private ay of your Scratch applicatios. Everythig uploaded is made available to ayoe who visits the website. Uploadig Your Scratch Applicatios The first step i sharig a Scratch applicatio is to click o the Share! butto located at the top of the Scratch IDE, displayig the widow show i Figure Begi by keyig i your accout ame ad password ad the provide a ame for your project. Next, eter ay otes that you thik other Scratch programmers visitig the Scratch website will eed to kow to work with your applicatio. Figure 13.4 You ca provide detailed iformatio about your applicatios whe uploadig them.
276 Uploadig Your Scratch Applicatios 255 Note I additio to providig istructios about how to work with your applicatio, you should also use the Project Notes area to ackowledge the source of ay audio or graphic files that you use i your applicatio. Scratch also supports a optioal taggig feature that you ca use to help other Scratch programmers locate your applicatios whe searchig the Scratch website. By default, Scratch lets you select ay of six predefied tags coverig the followig categories. Aimatio Art Game Music Simulatio Story I additio, you ca also create as may as four custom tags by supplyig keywords that you thik best describe your applicatio ad its purpose. Oce you have fiished fillig out this widow, click o the OK butto, ad the upload process will begi. Note Note the optio located at the bottom of the Upload to Scratch Server widow. This optio is automatically selected by default. It istructs Scratch to compress ay soud ad image files that make up your applicatio before uploadig them to the Scratch website. Compressig audio ad image files durig upload has o effect o the files stored o your computer. This is i direct cotrast to the compress souds ad compress images commads provided by the Extras butto o the Scratch IDE. These two commads compress ay audio ad graphic files used i your applicatio. Oce it is compressed, you caot ucompress a soud or graphic file, so you should pla o maitaiig a origial copy of your media files someplace for safekeepig. Give the ability to automatically compress souds ad images o the fly whe uploadig your Scratch applicatios, there is very little eed for the commads provided o the Extras butto. Oce a upload is started, a dialog widow similar to the oe show i Figure 13.5 is displayed, allowig you to track the progress of the upload process.
277 256 Chapter 13 Sharig Your Scratch Projects over the Iteret Figure 13.5 Scratch keeps you abreast of what is happeig as it uploads your applicatios. Figure 13.6 You ca click o the scratch.mit.edu lik to lauch your browser ad view your uploaded applicatios. Figure 13.7 The uploaded applicatio is visible ad ready to ru olie. Oe the upload process has completed, the dialog widow show i Figure 13.6 will be displayed. If you wat, you ca click o the blue scratch.mit.edu lik located i the middle of the dialog widow to automatically ope your browser ad log yourself ito the home page of the Scratch website, where you will fid your uploaded applicatio waitig o you, as demostrated i Figure 13.7.
278 Viewig ad Orgaizig Your Applicatios Olie 257 Viewig ad Orgaizig Your Applicatios Olie Ay Scratch applicatio projects that you upload to the Scratch website are stored o your home page o the website, as demostrated i Figure From here you ca ru your applicatio, post commets for it, add additioal tags, ad create galleries ito which to orgaize your applicatios. You ca also delete ay projects that you have uploaded ad view commets posted by other members of the Scratch commuity. Ruig Your Applicatio Oce they are uploaded, you ca view ad execute your applicatios olie by clickig o them. This opes the applicatio ad makes it ready for executio, as demostrated i Figure Oce it is opeed, you ca iteract with ad ru your applicatio i exactly the same maer as you did whe ruig it o your local computer. For example, the gree flag ad red Stop Everythig buttos are both clearly visible i the upper-right corer of the olie stage. Oce they are started, you ca iteract with Scratch applicatios usig the mouse ad keyboard as well. Addig Commets You ca share additioal iformatio about your Scratch applicatio by postig commets. To do so, scroll dow the scree as demostrated i Figure to expose the Add a Commet etry field. Figure 13.8 Oce logged oto the Scratch website, you ca view, execute, ad maage all your applicatios.
279 258 Chapter 13 Sharig Your Scratch Projects over the Iteret Figure 13.9 Your olie applicatio ca be ru i exactly the same way you ru it o your computer. Figure Addig commets to your Scratch applicatio.
280 Viewig ad Orgaizig Your Applicatios Olie 259 Figure Viewig the commets posted about your applicatio. You ca eter ay text that you wat ito this field ad the click o the Add butto to post your commets. Oce posted, your commets, as well as ay commets that other members of the Scratch commuity post about your applicatio, are visible. For example, Figure demostrates how commets look oce posted. As you ca see, commets are posted at the bottom of the web page, as is the accout ame of the idividuals who post them. Addig Tags I additio to addig tags to your applicatio projects whe uploadig them, you ca also add them olie. As demostrated i Figure 13.12, tags are displayed to the right of your applicatio oce it has bee opeed. You ca add ew tags, oe at a time, by keyig them i to the Add Tags field ad the clickig o the Add butto. You ca also delete ay tag that you o loger cosider useful by clickig o the [x] characters located just to the right of the tag.
281 260 Chapter 13 Sharig Your Scratch Projects over the Iteret Figure Addig tags to your applicatios makes them easier to fid. Creatig Galleries As you begi to upload your Scratch applicatios, you may fid it helpful to orgaize them ito differet galleries. A gallery is a collectio of Scratch applicatios. Typically, most Scratch programmers group their applicatios ito related collectios. For example, you may create oe gallery to orgaize your games ad a separate gallery for your other applicatios. To create a gallery, go to your home page ad scroll dow ad click o the Create lik located i the Galleries sectio o the left-had side of the web page. This will display the Create New Gallery page, as show i Figure To create a gallery, you provide it with a ame ad descriptio, ad you specify who ca add projects to it. You choices of who ca add projects to your gallery iclude: Oly Me My Frieds Everyoe You ca access your gallery by clickig o its lik, which automatically adds a Galleries area to the bottom-left side of the page, as demostrated i Figure
282 Viewig ad Orgaizig Your Applicatios Olie 261 Figure Creatig a ew gallery where you ca store your Scratch applicatios. Figure You ca access your ew gallery by clickig o the lik at the bottom of the web page. You ca add a Scratch applicatio to oe of your galleries by opeig the applicatio ad the clickig o the Add to a Gallery lik located just below the stage area. Whe you do this, the web page expads to iclude a Where Do You Wat to Add sectio, as demostrated i Figure
283 262 Chapter 13 Sharig Your Scratch Projects over the Iteret Figure Addig a applicatio to a gallery. This sectio displays a list of all your galleries. To add the applicatio to a gallery, select the check box cotrol to the left of the gallery s ame. You ca display a listig of all of the applicatios stored i your gallery by opeig the gallery. For example, the gallery show i Figure curretly has a sigle applicatio stored i it. I additio to viewig your ow gallery, you ca browse ay gallery o the Scratch website by clickig o the Galleries butto located at the top of ay Scratch web page. I respose, a list of galleries is displayed. By default, 10 galleries are displayed at a time, ad you ca avigate through the etire list usig the avigatio cotrols located o the right-had side of the page, as demostrated i Figure The most recetly created galleries are displayed first. However, by clickig o the buttos located ear the top of the page, you ca display galleries based o which oes have the most projects, or you ca view featured galleries.
284 Viewig ad Orgaizig Your Applicatios Olie 263 Figure Maagig your gallery. Figure Explorig applicatio galleries.
285 264 Chapter 13 Sharig Your Scratch Projects over the Iteret Removig Projects If you decide that you wat to remove ay of the applicatios you have uploaded to the Scratch website, you may do so by displayig your list of projects, selectig oe or more usig the applicatio s check box cotrol (located just udereath it), ad the clickig o the Delete Selected Project butto, as show i Figure Updatig Your Projects If after uploadig oe of your Scratch applicatios to the Scratch website you decide to make chages to it that you would like to share, you may do so by simply uploadig it agai, usig the exact same ame that you used to upload it the first time. If you wat to keep the origial copy of the applicatio itact o the Scratch website, the you will eed to assig a differet ame to the updated versio of your applicatio before you upload it. Other Scratch Website Features The Scratch website supports may other features related to the sharig of Scratch applicatio projects that have ot bee discussed i this chapter but Figure Deletig a applicatio that you have uploaded to the Scratch website.
286 Dowloadig Other People s Projects 265 which you may wat to ivestigate ad lear more about. For example, you ca chage your persoal profile iformatio by uploadig a picture to represet who you are. You ca also chage your password. As you browse the website, you ca add applicatios that you really like to a favorites list, makig them easy to retur to ad fid agai. You ca delete your galleries by clickig o the Delete This Gallery butto whe viewig oe of your galleries. You ca add projects that you have uploaded ito galleries by opeig the gallery that you wat to place the applicatio ito ad the clickig o the Add My Project butto. This displays a list of your projects, allowig you to select which oes you wat to move ito the curret gallery. You ca eve upload a custom graphic from your desktop to be used to represet your gallery. Dowloadig Other People s Projects I additio to allowig you to upload ad share your Scratch applicatio projects with Scratch programmers from aroud the world, the Scratch website also offers access to all of the applicatio projects that other programmers have uploaded. As such, you have istat access to a virtually ulimited umber of Scratch applicatios, all of which you ca view, ru, ad if you wat, dowload. Oce it is dowloaded, you ca study the applicatio ad see how it works. If you have ideas for makig it better, you ca use it as the basis for creatig your ow versio of the applicatio. Dowloadig a Scratch applicatio project is easy. First, locate ad ope the applicatio that you wat to dowload, ad the look for its dowload lik, located i the upper-right corer of the web page, as demostrated i Figure Oce you click o a Scratch project s Dowload lik, a File Dowload widow is displayed, askig you what you wat to do. Your choices are to ope a copy of the applicatio ito Scratch o your local computer or to dowload the applicatio as a file to your computer, allowig to you ope ad work with it later. Note If you elect to ope a applicatio project usig Scratch, you ca still save a copy of it o your computer usig Scratch s Save As butto. If you elect to dowload the applicatio project as a file, the file that is dowloaded ca the be easily idetified by its ame, the familiar Scratch cat ico, ad its.sb file extesio.
287 266 Chapter 13 Sharig Your Scratch Projects over the Iteret Figure Dowloadig a applicatio from the Scratch website. Summary This chapter provided istructio o how to upload your Scratch applicatios to the Scratch website. Doig so allows you to share your work with other members of the Scratch global commuity. The Scratch website places thousads of applicatios at your figertips, allowig you to ot oly ru them but to dowload them ad see how they work. Through the exchage of applicatio projects, you ca become a much more kowledgable ad effective programmer, leveragig ot oly your ow work but also the work ad ideas of others.
288 chapter 14 Collectig Exteral Iput Usig a Scratch Board I additio to usig Scratch to develop all kids of games ad applicatios ad iteractig with those games ad applicatios usig the mouse ad keyboard, Scratch is also desiged to iteract with a special piece of hardware kow as a Scratch Board. Usig a Scratch Board, you ca create applicatios that are capable of sesig ad collectig real-world iput. Scratch Boards come with a umber of built-i cotrols, icludig a slider, a butto, ad four pairs of alligator clips, as well as two sesors that allow it to capture light ad soud data. This chapter will teach you everythig you eed to kow to work with a Scratch Board, icludig how to istall ad programmatically iteract with it. The major topics covered i this chapter iclude: Learig how to purchase a Scratch Board Dowloadig ad istallig Scratch Board software Usig sesig code blocks to programmatically iteract with a Scratch Board Keepig a eye o Scratch Board data usig differet types of moitors 267
289 268 Chapter 14 Collectig Exteral Iput Usig a Scratch Board Iteractig with the Real World A Scratch Board is a specialized piece of hardware, show i Figure 14.1, which you ca purchase directly from the Scratch website ad attach to your computer via a USB coectio. Oce a Scratch Board is coected to your computer, your Scratch applicatios ca begi collectig, processig, ad respodig to differet types of real-world data, collected by the Scratch Board s built-i set of sesors ad cotrols. Scratch Boards come equipped with a umber of cotrols ad sesors; their fuctios are outlied here: Slider. Detects the curret positio of the Scratch Board s slider cotrol. Light Sesor. Detects the amout of light that is curretly visible through the Scratch Board s light sesor. Butto. Returs a value of true or false, depedig o whether the Scratch Board s butto is beig pressed. Soud Sesor. Detects the loudess of souds through the Scratch Board s soud sesor. Alligator Clips. Provides a measuremet of the electrical resistace i a circuit. Alligator Clips Light Sesor Slider Butto Soud Sesor Figure 14.1 Scratch Boards allow your applicatios to icorporate exteral iput ito your applicatios.
290 Istallig Your Scratch Board 269 The rest of this chapter is dedicated to teachig you how to istall ad iteract with a Scratch Board. I doig so, you will be able to icorporate a whole ew rage of iput ito your applicatios, usig for example variatios of light ad soud to cotrol the executio. Tip I additio to the iformatio provided i this chapter, you ca lear more about Scratch Boards by visitig Amog the items you will fid o this web page is a lik to a small Gettig Started with Scratch Boards PDF maual, which provides a umber of excellet example scripts that demostrate how to iteract with ad use iput collected ad reported by Scratch Boards. Buyig a Scratch Board Scratch Boards ca oly be purchased from the Scratch website. At the time this book was writte, the price of a Scratch Board was $25, alog with a additioal $5 charge for shippig ad hadlig. To verify the curret price of a Scratch Board, visit To order a Scratch Board, go to ad fill out the required form. Istallig Your Scratch Board Istallig a Scratch Board o your computer is a relatively quick ad easy process ad begis with dowloadig the software driver. Two differet types of software driver dowloads are available, oe for Microsoft Widows ad oe for Mac OS X. To dowload the drivers for your computer, go to pages/scratchboardsetup ad click o oe of the followig liks (Widows Vista users ca skip this step because your computer should automatically istall the eeded software driver): Widows XP (ad older) Driver Mac OS X Driver Oce you have dowloaded the appropriate software driver for your computer, you eed to istall it. O Microsoft Widows this meas extractig the istallatio program from the Zip file, double-clickig o it, ad the followig the istructios that are provided.
291 270 Chapter 14 Collectig Exteral Iput Usig a Scratch Board For Mac OS X users, istallig Scratch s software drivers ivolves opeig the file that is dowloaded ad the double-clickig o the.dmg file that is stored iside. This displays a.pkg program, which whe double-clicked executes the driver istallatio process. Click o Cotiue to begi the istallatio process ad the follow the istructios that are preseted. Oce you have istalled the software driver o your computer, coect the USB portio of the cable that came with your Scratch Board to your computer s USB port ad the coect the serial portio of the cable to your Scratch Board. At this poit your Scratch Board should be ready to use. Usig the Sesor Block to Iteract with Your Scratch Board I order to programmatically iteract with a Scratch Board, you eed to work with the two sesig code blocks show i Figure The first code block show i Figure 14.2 returs a rage of data, from 1 to 100, for the selected Scratch Board sesor. I additio, you ca select this code block s check box to eable the display of a moitor o the stage, allowig you to keep track of the data that the sesor is returig. This code block works with the slider, light, soud, ad all four of the resistace cotrols (alligator clips). The secod code block show i Figure 14.2 returs a value of true or false, depedig o whether the Scratch Board s butto cotrol has bee pressed or oe of the resistace cotrols has bee used to establish a electrical coectio (the alligator clips are coected to oe aother). Examples of how to work with both of these sesig code blocks to receive data collected by each of the Scratch Board s sesors ad cotrols are provided throughout the rest of this book. Collectig Iput Usig the Slider Cotrol I order to work with the Scratch Board s slider cotrol, you must use the first sesig code block show i Figure This meas draggig ad droppig a Figure 14.2 Access to a Scratch Board is provided through these two sesig code blocks.
292 Usig the Sesor Block to Iteract with Your Scratch Board 271 Figure 14.3 The speed at which the fa spis will be cotrolled by the Scratch Board s slider cotrol. istace of the code block ito aother cotrol, where it ca be used to provide iput; the select Slider from the list of choices displayed i the cotrol s dropdow list. As a example of how to work with the cotrol, let s create a ew Scratch applicatio that emulates a virtual fa. To do this, you will create a ew applicatio ad the import the sprite show i Figure 14.3 ito it. You will fid a copy of this spite o the book s compaio CD-ROM. You will also eed to remove the default Cat sprite. Oce added to your applicatio, select the sprite ad the add the followig script to it. As you ca see, this script places the sprite represetig a fa i the middle of the stage ad the uses a loop to retrieve a cotiuous feed of data from the Scratch Board s slider cotrol. Usig this data collected from the Scratch Board as iput, a motio block is used to rotate the sprite. The sprite has bee set up so that its rotatioal ceter is directly i the ceter of the black circle i the middle of the sprite. Movig the slider by a small amout will make the fa begi to slowly spi. Movig the slider cotrol by a larger amout will icrease the speed at which the fa spis. Usig a similar approach, you ca use a Scratch Board as a iput device for all kids of Scratch applicatios. For example, you might use it as a meas of cotrollig a paddle i a Breakout-style game or to cotrol the assigmet of data to a variable, which i tur is used to cotrol a applicatio s operatio. The possibilities are edless.
293 272 Chapter 14 Collectig Exteral Iput Usig a Scratch Board Usig the Butto Cotrol to Iitiate Actio I order to work with the Scratch Board s butto cotrol, you must use the first sesig code block show i Figure Usig this code block, you ca determie whether the Scratch Board s butto cotrol is beig pressed. As a example, let s create aother applicatio. Begi by removig the default Cat sprite ad the click o the Choose New Sprite from File butto, drill dow ito the Thigs folder, select the basketball sprite, ad click o OK. Oce it is added, select the basketball sprite ad add the followig script to it: As you ca see, this script begis by positioig the sprite at the ceter of the stage. It the starts a loop to repeatedly execute a coditioal code block that checks to see if the Scratch Board s butto is beig pressed. If this is the case, the statemets located iside the coditio code block are executed. As a result, the image of the basketball is made to bouce. Figure 14.4 depicts how the basketball looks as it begis its upward bouce. The basketball will repeatedly bouce for as log as the Scratch Board s butto is beig pressed ad will stop boucig as soo as the butto is released. Usig the previous example as a startig poit, you should be able to use a Scratch Board s butto cotrol as a iput device for all kids of Scratch applicatios. For example, you might use it i place of the mouse butto as a meas of cotrollig whe to shoot a missile i a Space Ivaders-style game. Reactig to Light I additio to the slider ad butto cotrol, you ca retrieve iput from the light sesor located o your Scratch Board to provide iput to your applicatios. You Figure 14.4 Usig the Scratch Board s butto to cotrol the boucig of a virtual basketball.
294 Usig the Sesor Block to Iteract with Your Scratch Board 273 ca use either of Scratch s two sesig code blocks to iteract with the light sesor. To get a better feel of how to work with the light sesor, let s modify the previous applicatio so that it respods to a chage i light i place of the Scratch Board s butto cotrol. To do so, modify the applicatio s script as show here. As you ca see, the script has bee redesiged so that it oly bouces the basketball whe the Scratch Board s light sesor returs a value of 0 (total darkess). To test out the executio of this script, place your had over the Scratch Board so that it blocks out the light. Whe you do, the basketball should start boucig. Remove your had so that the Scratch Board ca detect some light, ad the basketball will stop boucig. Usig this example as a startig poit, you could create a Scratch applicatio that performs a certai task oly whe the lights have bee tured off or o. You might also use your Scratch Board as the basis for creatig a alarm clock that awakes you whe the su comes up. Respodig to Soud I additio to providig your applicatio with data based o the amout of light it is able to detect, your Scratch Board ca also detect variatios i the loudess of souds. For example, you could easily modify the script belogig to the applicatio that you have bee experimetig with to work with soud i place of light. As redesiged, the script will ow bouce the basketball oly whe the Scratch Board detects a relatively loud oise i the room. The soud sesor returs a rage of umbers from 1 to 100, where 0 represets total silece, ad 100 represets maximum volume. To see how this chage affects your applicatio,
295 274 Chapter 14 Collectig Exteral Iput Usig a Scratch Board start your applicatio ad make a little oise. If the basketball does ot move, make aother oise, this time a little louder. Keep goig util you make a oise that is loud eough to trigger the boucig of the basketball. Note The sesig code block show i the precedig example operates much like the sesig code block show here. Ulike this code block, which reports o the loudess of the computer s microphoe, the sesig code block used i the example retrieves its data directly from the Scratch Board s microphoe. Usig soud as a trigger for script executio, you could, for example, create ad execute a applicatio that plays a alarm wheever it detects someoe i your room, warig him that his presece has bee detected, thus creatig your ow virtual watch dog. Measurig Electrical Resistace I additio to workig with the Scratch Board s slider, butto, light sesor, ad soud sesor, the Scratch Board also comes equipped with four sets of alligator clips, which you ca attach to the bottom of the Scratch Board. Each set of alligator clips represets a idividual sesor, which you ca use to provide your applicatios with iput based o the stregth of the electrical resistace i ay circuit you set up. As a example of how you might work with a alligator clip, let s modify the script for the applicatio that you have bee experimetig with, as show here: With this modified script ow i place, you must touch both eds of the alligator clips together i order to make the basketball bouce. To test how well differet materials coduct electricity, you could attach both eds of the alligator clips to differet objects to see if eough curret passes through to make the basketball
296 Usig the Sesor Block to Iteract with Your Scratch Board 275 bouce. With access to four separate sets of alligator clips, you ca create all sorts of differet tests ad eve ru them all at oce. Keepig a Watchful Eye o Sesor Data Scratch allows you to display idividual moitors for each of the differet types of sesor cotrols supported by either of the two sesig code blocks that work with the Scratch Board. To do so, click o the Sesig butto located at the top of the blocks palette, the click o the drop-dow list located i the sesig code block you pla o workig with ad select the sesor that you wat to keep a eye o. Next, select the check box located just to the left of the code block. A moitor for the selected Scratch Board sesor will the appear o the stage. If you wat to display additioal moitors, you may do so by selectig the code block s drop-dow list agai to select a differet sesor. You will have to select the block s check box agai. Usig this approach, you ca display a moitor for as may of the Scratch Board s sesors as you wat, as demostrated i Figure To disable the display of ay moitor that you eable, you must perform the procedure outlied above i reverse order to clear out the check box for each sesor. A quicker ad easier way of keepig a eye o the data beig supplied by multiple sesors is to eable the display of the Scratch Board Watcher, as show i Figure To eable the display of the Scratch Board Watcher, right-click o the sesig code block that you pla to use ad select Show Scratch Board Watcher from the Figure 14.5 Displayig idividual moitors to report o differet Scratch Board sesors.
297 276 Chapter 14 Collectig Exteral Iput Usig a Scratch Board Figure 14.6 The Scratch Board Watcher lets you keep track of all of the data beig supplied by your Scratch Board. popup meu that is displayed. Whe you are doe with the Scratch Board Watcher, you ca remove it from the stage by right-clickig o it ad selectig Hide from the popup meu. Summary I this chapter, you leared all about Scratch Boards. This icluded learig how to purchase ad istall them. You leared how to programmatically iteract with them usig sesig code blocks ad saw examples of how to work with all of the Scratch Board s cotrols ad sesors. These examples icluded the creatio of scripts that ca react to chages i light ad soud level as well as to butto presses, slider bar movemet, ad chages i electrical curret. This chapter also demostrated how to work with differet moitors that allow you to keep track of the data beig collected ad reported by your Scratch Board.
298 chapter 15 Fidig ad Fixig Program Errors Compared to most programmig laguages, Scratch is less proe to may types of programmig errors, ofte referred to as bugs. As a programmer, your job is to seek out ad remove all of the programmig bugs from your applicatios ad to esure that they operate as they are supposed to. That s where this chapter comes i. By the time you are doe readig it, you will have a solid uderstadig of the types of errors that Scratch is susceptible to ad the basic steps ivolved i trackig dow ad fixig them. I additio, you will lear about differet resources that you ca tur to i order to get help. The major topics covered i this chapter iclude: Uderstadig the differeces betwee sytax, logical, ad ru-time errors Learig how to ru applicatios i sigle steppig mode Accessig code block help Gettig help from the Scratch global commuity Dealig with Applicatio Errors Program errors, sometimes referred to as bugs, are a programmer s umber oe problem. Errors ca occur for a umber of differet reasos ad ca cause your applicatios to misbehave or eve prevet them from executig at all. As your projects ievitably get larger ad more complex, the possibility ad frequecy of errors also icrease. That s just the way it is. 277
299 278 Chapter 15 Fidig ad Fixig Program Errors The goal of this chapter is to help you gai a uderstadig of the differet types of errors that you will ru ito ad provide you with guidace o how to go about locatig ad elimiatig them from your applicatios. Some errors are easy to fid, especially i small scripts, while others ca be quite challegig to locate ad ofte ca oly be foud through itese testig ad debuggig. Fortuately, there are steps that you ca take to reduce the umber of errors that occur i your applicatios. For starters, take a little extra time to pla out the desig of your applicatios rather tha makig thigs up as you go alog. Aother importat step is to create your applicatio scripts a few code blocks at a time, frequetly testig as you go alog, rather tha waitig util your etire applicatio has bee built to see how thigs work. I additio, you should set aside a little extra time at the ed of the developmet process just for testig your applicatios ad makig sure that they ot oly meet your expectatios but do so without geeratig ay errors. I additio to the programmig practices discussed above, there are a umber of other steps that you ca take to make sure your Scratch applicatios work like you wat them to. These steps iclude: Takig a little extra time to carefully desig ad lay out your applicatio s iterface Esurig that you provide clear istructios o how to properly work with your applicatio Creatig descriptive ames for all applicatio variables Reamig all the sprites, souds, ad costumes used i your applicatio to make them more ituitive to work with Breakig dow programmig logic ito a umber of maageable small scripts as opposed to a few really large oes Ufortuately, o matter how much you try, you ca ever totally avoid all of the differet types of errors that Scratch applicatios are susceptible to. Broadly speakig, most programmig laguages are susceptible to the followig types of errors: Sytax errors Logical errors Ru-time errors Each of these three types of errors is discussed i the sectios that follow.
300 Dealig with Applicatio Errors 279 Uderstadig Sytax Errors Oe of the thigs that makes Scratch uique amog programmig laguages is the way it prevets sytax errors. A sytax error is a error that occurs whe a programmer fails to write code statemets i a maer that follows the sytax rules specified by the programmig laguage. Scratch code blocks are desiged to fit together i logical ways like pieces i a puzzle. Scratch oly allows you to sap together blocks i ways that make sytactic sese. As a result, Scratch elimiates sytax errors that proliferate i other programmig laguages. Keepig a Eye Out for Logical Errors Oe category of errors you eed to worry about regardless of the programmig laguage you are workig with is logical errors. A logical error is a error that occurs because of a mistake o your part i the implemetatio of the programmig logic you applied to solvig a problem or performig task. For example, suppose you had a applicatio that eeded to add two umbers together, but whe you assembled the programmig logic you accidetally subtracted oe umber from aother. As a result, your applicatio will ot ru correctly. From Scratch s perspective, everythig would be fie, sice there was techically o problem with the logic you implemeted. As soo as you see that the results tallied by the applicatio are ot correct, you should immediately suspect that you have a logical error to debug. As aother example of a logical error, cosider the followig pair of scripts, which belog to a applicatio that uses the default Cat sprite to display text messages that are supposed to cout from 1 to 5.
301 280 Chapter 15 Fidig ad Fixig Program Errors Both scripts begi their executio whe the gree flag butto is clicked. Whe this happes, the first script assigs a startig value of 0 to a variable amed Couter ad the goes ito a loop that has bee set up to wait util the value of Couter is equal to 5. Whe this occurs, the sprite is made to display a message, ad the all script activity withi the sprite is halted. The secod script is resposible for makig the sprite cout from 1 to 5, icremetig the value of Couter each time the sprite says a umber. If you were to ru this example, you would see that as it is curretly writte, it has a logical error. Specifically, the secod script loop was accidetally set up to ru four times istead of five times. As a result, the sprite oly couts from 1 to 4, ad sice the value of Couter ever reaches 5, the first script gets stuck i its loop. Oly by fixig the loop i the secod script (so that it executes five times) ca this logical error be fixed. The best way to idetify logical errors is to take a little extra time to carefully pla out the desig of your applicatios ad to test them extesively, esurig that they ru exactly as you expect them to. If, despite your best efforts, a logical error maages to make its way ito your program logic, all hope is ot lost. Usig the debuggig techiques discussed later i this chapter, you should be able to track dow ad elimiate all of the errors from your Scratch applicatios. Trackig Dow Ru-Time Errors A third category of errors that plagues all programmig laguages, icludig Scratch, is ru-time errors. A ru-time error is a error that occurs whe a Scratch script attempts to perform a illegal actio. Scratch automatically idetifies rutime errors whe they occur by surroudig the script where the error occurred with a red outlie. Depedig o how your applicatios are desiged, it is etirely possible that you might be able to ru them over ad over agai without ever executig the script i the applicatio where a ru-time error lies. This is why it is so importat that you thoroughly test the executio of every script i your applicatios. Failure to do so leaves you ope to ru-time errors. As a example of what a ru-time error looks like whe reported by Scratch, take a look at the followig script.
302 Debuggig Your Scratch Applicatios 281 Here, Scratch has flagged the script as havig a ru-time error. The reaso for the error resides i the Variable code block. As you ca see, it icludes a embedded Numbers block that attempts to divide 10 by 0. However, the divisio of 10 by 0 is a illegal actio i all moder programmig laguages, icludig Scratch. The ufortuate thig about ru-time errors is that if you do ot idetify ad elimiate them durig applicatio developmet, you ca bet that your users will fid them for you, which is the last thig ay programmer wats to happe. Debuggig Your Scratch Applicatios No matter how carefully you pla out your Scratch scripts, somewhere alog the lie you are goig to ru ito errors. As previously demostrated, Scratch helps you locate ad idetify scripts that cotai ru-time errors, ad while your Scratch applicatios are ot subject to sytax errors, logical errors ca be particularly difficult to track dow ad idetify. Fortuately, there are a umber of debuggig techiques that you ca employ to help you track dow ad elimiate problems withi your applicatio s scripts. Basic Debuggig Techiques Oe of the challeges i debuggig a Scratch applicatio is to idetify whe thigs are happeig. Scratch helps simplify this challege a bit by highlightig scripts whe they execute. However, the exact activity occurrig withi a give script ca be hard to idetify. This makes it difficult to determie if thigs are occurrig i both the order ad maer that you ited for them to. Makig a Little Noise Oce way of figurig out what is happeig withi a applicatio is to embed code blocks iside your scripts for the purpose of otifyig you whe thigs occur. For example, usig a soud block you could play a ote every time a particular variable is updated durig the executio of your script. Usig this soud as a meas of keepig track of updates, you could verify that a variable s value is beig properly set whe testig the applicatio. If durig testig you do ot hear the soud played, the you kow that somethig is wrog. If the variable that you are watchig is modified i more tha oe place withi a script, or if it ca be modified by differet scripts, you might wat to play differet otes at each locatio where variable modificatios occur. The by simply keepig
303 282 Chapter 15 Fidig ad Fixig Program Errors your ears ope whe testig the executio of your applicatio, you may be able to track dow the script or area where the problem lies. Display Iformative Messages Of course, you do ot have to work with soud blocks. If you prefer to, you ca work with looks blocks istead. Looks blocks provide the added beefit of beig able to display text, which you ca use as marker withi script executio to let you idetify exactly whe certai parts of a script are executig. For example, you might begi each script with a looks block that displays a text message aoucig that the script is executig ad ed each script by displayig a closig message. You might embed additioal looks blocks at key locatios withi your script to otify whe specific thigs happe. If, for example, whe testig a applicatio, a particular text message is ot displayed whe you expect it to be, the you will kow where to begi lookig for the source of the problem. Tip If you thik that a variable is ot beig set correctly durig script executio, you ca eable the display of a moitor so that you ca keep your eye o the variable s assiged value whe testig your applicatio. However, if your applicatio utilizes a larger umber of variables, displayig lots of moitors ca get i the way of thigs. As a alterative, you ca keep a eye o the value of a variable by displayig it iside a looks blocks, as demostrated i Figure Figure 15.1 Usig a looks block to report o a variable s assiged value. Although ot obvious because of the shape of some looks blocks iput fields, you ca use them as show above to display a variable s value. Slowig Thigs Dow Because of the speed at which thigs ted to happe i may applicatios, it ca be difficult to keep track of what is goig o. If you are usig looks blocks to display helpful text messages, you ca slow thigs dow by pausig script executio for a specified umber of secods. Alteratively, you ca also slow thigs dow by usig the cotrol block show i Figure 15.2 to pause script executio. By temporarily haltig a script, you ca give yourself time to check o variable values to see if they have bee correctly set ad poke aroud ad look at the
304 Debuggig Your Scratch Applicatios 283 Figure 15.2 You ca use this code block to slow dow script executio. Figure 15.3 Testig a script by breakig it dow ito smaller parts. activity of other scripts. This is especially helpful i applicatios made up of multiple scripts ad scripts where broadcast messages ad variables are used to coordiate the executio of script activity. Testig Idividual Scripts Whe testig your Scratch applicatios, it is importat that you make sure that every script gets executed. Otherwise, you may miss out o fidig a potetial problem. To make sure this happes, take time to test all of the fuctioality ad features of your applicatios. Oe easy way to do this is to double-click o every script i your applicatio ad observe the effects of its executio. Breakig Thigs Dow ito Smaller Pieces Really large scripts ca be challegig to test because of their size ad iheret complexity. Oe easy way of gettig aroud this challege is to break these scripts dow ito smaller parts whe idividually testig them. As a example of how you might do this, take a look at Figure 15.3.
305 284 Chapter 15 Fidig ad Fixig Program Errors By breakig dow a script like the oe i Figure 15.3 ito multiple parts, you ca double-click o each part ad examie its effects o your applicatio. Should somethig uexpected occur, you will kow exactly which part of your script to focus o to fid the source of a applicatio s problem. Makig Liberal Use of Moitors Aother importat source of iformatio at your disposal that you ca use whe debuggig your applicatio is code block moitors. By temporarily eablig the display of moitors whe testig your applicatios, you ca keep track of key data used by your applicatios. Oce you are doe testig, you ca disable the display of ay moitors that you do ot eed to display as part of the ormal operatio of the applicatio. Ruig Your Applicatio i Sigle Steppig Mode I additio to all of the debuggig techiques discussed above, Scratch provides oe additioal debuggig tool, kow as sigle steppig. Whe you ru a applicatio usig sigle steppig mode, Scratch slows dow the speed at which your applicatio executes, makig it easier for you to moitor executio flow. Normally the Scratch IDE highlights a etire script with a white outlie whe it executes. But whe ru i sigle steppig mode, Scratch also highlights idividual code blocks as they execute. As your applicatios execute i sigle steppig mode, you ca moitor their executio flow to determie if thigs are executig i the proper order. Note You ca cotrol the speed at which your applicatio executes by pressig the Shift key ad leftclickig o the Extras butto, the clickig o Set Sigle Steppig from the popup meu that appears. This displays a list of optios to cotrol sigle steppig executio speed. These choices iclude: Turbo Speed Normal Flash blocks (fast) Flash blocks (slow)
306 Debuggig Your Scratch Applicatios 285 To develop a better uderstadig of how sigle steppig works, cosider the followig series of examples, which demostrate what you ca expect to see whe ruig a applicatio i sigle steppig mode. To tur o sigle steppig mode, click o the Extras butto located at the top of the IDE ad the click o the Start Sigle Steppig optio from the popup meu that appears. Oce sigle steppig has bee eabled, go ahead ad start ruig your applicatio. As the applicatio executes, two thigs become immediately obvious: Thigs are occurrig more slowly, ad i additio to highlightig each script with a white outlie as it executes, Scratch ow highlights idividual code blocks as they execute. Below is a example of a script that has begu executig, as idicated by the white outlie that surrouds the script. Withi the script, you ca see that the secod code block is the code block that is curretly executig because Scratch has highlighted it usig a yellow color. Normally, Scratch rus scripts so quickly that it would ot be practical to try to moitor the executio of idividual code blocks. However, sigle steppig slows thigs dow eough to let you do so. For example, as show below, you ca clearly see that the fourth code block is ow beig executed. If you have a moitor for the Couter variable displayed o the stage, you would be able to cofirm that the code block has correctly modified the variable value.
307 286 Chapter 15 Fidig ad Fixig Program Errors Withi a few momets, the script eters ito a loop ad begis the repeated executio of two code blocks. Below you ca see how the script looks whe the first of these two code blocks is executed. As you ca see, this code block rotates its sprite by 10 degrees. You should be able to observe this movemet by watchig the sprite o the stage. After a brief pause, the secod of the two code blocks i the loop executes, as show here.
308 Watch Out whe Removig Souds ad Sprites 287 Scratch cotiues to highlight code blocks oe at a time for as log as the script executes, givig you the opportuity to validate that the script is executig exactly as you expect it to ad that variables are beig modified as you wat. If while moitorig script activity you see somethig happe that youdootexpect,youcahaltapplicatioexecutio,kowigexactlywhere the problem lies. As your applicatio executes i sigle steppig mode, you ca also keep a eye o variable values, esurig that they are beig properly set ad modified as you expect them to. You ca also switch betwee sprites ad observe other scripts, which will also be executig i sigle steppig mode. Hit Although sigle steppig is a very helpful debuggig tool, it lacks may of the features that are usually icluded i debuggig tools provided by most moder programmig laguages. For example, it lacks the ability to set breakpoits, which pause executio whe certai code statemets are reached, givig programmers the ability to access a applicatio s status before allowig the applicatio to cotiue its executio. Still, sigle steppig serves its purpose well, ad whe combied with the debuggig techiques covered i this chapter, it should be more tha sufficiet to help you track ad fix ay applicatio bug. Watch Out whe Removig Souds ad Sprites Ulike may programmig laguages, Scratch is extremely forgivig whe it comes to what i may programmig laguages would be cosidered a major error. For example, let s say you created a script that played a audio file amed
309 288 Chapter 15 Fidig ad Fixig Program Errors meow, as demostrated below, ad you later decided to remove the audio file from your applicatio but forgot to remove the soud block i the script. It would certaily be logical to expect that whe you ra your applicatio, a error would occur. But this will ot be the case. Rather tha prevetig applicatio executio ad highlightig the error, Scratch overlooks the problem ad rus your applicatio ayway. Whe it comes time to play the missig audio file, scratch just igores the problem. This behavior ca be a double-edged sword, because o the oe had your applicatio still rus. However, uless you carefully test the executio of your applicatio after deletig the soud file, you may ot discover the error, ad the overall quality of your applicatio will suffer. Scratch is just as forgivig whe it comes to the maagemet of sprite costumes. Suppose, for example, that you added a costume amed bat1-a to a sprite ad the used the followig script to switch its costume: If sometime dow the road you decided to modify your applicatio by removig the costume from the sprite, Scratch would ot flag the oversight as a error ad would istead allow your applicatio to ru, igorig the costume switch error whe it came across it. Agai, this type of behavior is a double-edged sword ad ca oly be overcome by careful modificatio ad retestig of your Scratch applicatios ay time you decide to chage or remove a soud, costume, or backgroud. Gettig Help The developmet of good debuggig skills is a absolute requiremet for ay serious programmer. However, o matter how good you may be at debuggig, there are goig to be times whe you may eed additioal help i fidig the aswer to a particular problem or challege. Fortuately, there are a umber of resources that you ca tur to for assistace, both withi Scratch ad olie, as discussed i the followig sectios.
310 Gettig Help 289 Referrig to Scratch s Olie Help Oe source of help that you ca tur to with the click of a butto is the Scratch Help web page, which you ca access by clickig o the Wat Help? butto located at the top of the Scratch IDE. Whe clicked, Scratch opes your default browser ad loads the web page show i Figure O this web page you will fid liks to a umber of helpful resources, icludig liks that let you ope Scratch s Gettig Started ad Referece Guide PDF mauals as well as its support page. The support page cotais additioal liks to olie videos, Scratch Cards, ad other iformatio. Also available o the web page is a lik labeled Help Screes, which whe clicked displays a listig of help screes, as show i Figure 15.5, each of which is desiged to teach you how to work with a idividual Scratch code block. The help screes are orgaized by category. Usig liks provides at the top of the web page, you ca jump to specific categories of help screes. Gettig Help for Idividual Code Blocks A eve faster way of accessig Scratch help screes is to view them oe at a time o a as-eeded basis without havig to go through the Iteret to view Figure 15.4 Olie help is just a sigle click away.
311 290 Chapter 15 Fidig ad Fixig Program Errors Figure 15.5 Usig the Help Scree liks, you ca quickly view help iformatio for all of Scratch s code blocks. Figure 15.6 A example of a typical help scree. them. To view the help scree for a idividual code block, right-click o the code block ad the click o the help optio that appears i the resultig popup meu. For example, Figure 15.6 shows the help scree for oe of the sesig blocks.
312 Gettig Help 291 I this particular example, the help scree demostrates the code block s usage ad provides a example that further demostrates the effect of usig the code block. I additio, more iformatio is provided at the bottom of the help scree that shows all of the code block s available optios. Gettig Help from Other Scratch Programmers I additio to the documetatio made available to you through Scratch s help screes, the Scratch website also sposors a collectio of forums that brig together Scratch programmers from aroud the world. These forums facilitate the free exchage of ideas ad provide you with the opportuity to seek out help ad advice from fellow Scratch programmers. As show i Figure 15.7, you ca access these forums by goig to Tip If all else fails ad you simply caot fid a aswer to a particular problem, you ca try sedig a to the Scratch developers by goig to ad fillig i the form that is provided. Whe doig so, provide as much iformatio as possible about your problem ad the steps that you have take i tryig to fix it. Figure 15.7 The forums are orgaized ito a umber of high-level categories, icludig a forum dedicated to discussig troubleshootig.
313 292 Chapter 15 Fidig ad Fixig Program Errors Figure 15.8 Scratch forums provide the ability to iteract with ad lear from other Scratch programmers. By postig your questios to the appropriate forum, you ca tap ito the expertise ad experiece of other Scratch programmers. Ofte, you ca fid a aswer to your problem without havig to post a questio at all. Aswers ca ofte be foud i threads already posted by other Scratch programmers. Figure 15.8 shows a example of types of discussios you will fid whe you visit the Scratch website s forums. Summary This chapter taught you about the differet types of errors to which Scratch applicatios are susceptible ad examied a umber of differet ways i which pesky applicatio bugs ca be tracked dow ad elimiated. This icluded learig how to ru your applicatio i steppig mode so that you ca moitor the executio of the logical flow withi your applicatio while also keepig a watchful eye o variable values. You also leared how to access help from differet sources, icludig the forums sposored o the Scratch website, where you ca receive help from Scratch programmers aroud the world.
314 Part IV Appedices
315 This page itetioally left blak
316 appedix a What s o the Compaio CD? As you cotiue to lear more about Scratch ad improve your programmig skills, it helps to have access to a good collectio of source code that you ca referece. This book has provided you with umerous sample Scratch applicatio projects. By studyig these projects, you ca lear a lot about how to program. You ca also use this book to fid workig examples of how to perform differet types of tasks ad use them as the basis for creatig ew Scratch applicatio projects. This will ot oly save you time, but it will also keep you from havig to re-ivet the wheel ad let you keep your focus o tacklig ew programmig challeges. If you have bee faithfully re-creatig all of the Scratch applicatio projects preseted i this book, the you already have access to such a collectio of sample projects. However, if you skipped aroud a bit, the you may have missed a few sample projects. You will be happy to kow that all of the sample Scratch projects covered i this book are available at your figertips o this book s compaio CD. Scratch Project Source Code You will fid copies of the source file for all of the Scratch projects developed i this book o the compaio CD. You will also fid copies of ay custom graphics ad audio files required to build projects. Table A.1 provides a complete list of each of the Scratch project source code files that you will fid o the CD. 295
317 296 Appedix A What s o the Compaio CD? Table A.1 Scratch Projects Available o the Compaio CD Chapter Chapter 1 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 File Name Hello World.sb My. Wiggly s Dace.sb Fish Tak.sb Family Scrapbook.sb Basketball Quiz.sb NumberGuess.sb Ball Chase.sb Crazy Eight Ball.sb Family Picture Movie.sb Doodle.sb You will also fid each of these projects published o the Scratch website at i the Scratch Programmig for Tees gallery. Note I additio to all of the sample applicatios listed i Table A.1, you will also fid a bous applicatio amed Scratch Pog o the book s CD-ROM. You will ot fid this applicatio o the Scratch Programmig for Tees gallery at the Scratch website. Scratch Istallatio Files for Microsoft Widows ad Mac OS X I additio to all of this book s sample projects, you will also fid the istallatio files eeded to istall Scratch versio o either Microsoft Widows or Mac OS X o the book s compaio CD-ROM.
318 appedix b What Next? Learig how to become a good programmer takes time ad effort. It meas puttig i the hours ecessary to lear the fudametal techiques ivolved i developig computer applicatio projects. Scratch provides a excellet platform for gettig started. It provides a friedly ad fu eviromet i which to lear. A good uderstadig of Scratch programmig will prepare you to make the jump to other programmig laguages like Visual Basic, AppleScript, Cþþ, ad so o. Learig Scratch requires commitmet. By makig your way through to the ed of this book, you have demostrated this commitmet. Although this book has certaily taught you a lot about Scratch ad programmig i geeral, there is still much more to be leared. To become a world-class programmer, you eed to cotiue your programmig educatio. You eed to cotiue to experimet ad lear as much as you ca about Scratch. Do ot thik of this book as the ed of your Scratch programmig educatio. Istead, thik of it as the begiig. Over the comig weeks ad moths, you should cotiue developig ew Scratch projects. You should also keep a eye o the differet forums hosted o the Scratch website to lear from the experieces of others. Better yet, cosider becomig a active member of the Scratch commuity. 297
319 298 Appedix B What Next? To help you further your uderstadig of Scratch ad to become a better programmer, this appedix provides a list of websites ad supplemetal readig materials that you ca tur to as you cotiue to develop ad hoe your programmig skills. Locatig Scratch Resources Olie As you would expect, there is a awful lot of helpful iformatio o the Iteret about Scratch. By frequetig the websites discussed i the sectios that follow, you ca keep abreast of the latest happeigs i the Scratch commuity while also keepig your Scratch programmig kowledge ad skills up to date. The Scratch Website The most iformative ad helpful Scratch website is the Scratch site developed ad maitaied by MIT located at as show i Figure B.1. This site is packed with helpful iformatio, icludig documetatio, video tutorials, ad forums where you ca go to iteract with ad lear from other Scratch programmers from aroud the world. Best of all, this site provides istat access to tos of Scratch projects, all of which you ca dowload, experimet with, ad lear from. Figure B.1 The official home page of the Scratch programmig laguage.
320 Locatig Scratch Resources Olie 299 Figure B.2 Scratch is developed by the Lifelog Kidergarte Group at MIT. The Lifelog Kidergarte Website Aother website that is certaily worth visitig is the Lifelog Kidergarte MIT Media Lab site located at as show i Figure B.2. This site icludes iformatio about Scratch, icludig liks to various papers about Scratch. The Wikipedia Scratch Page Aother excellet source of Scratch iformatio is the Wikipedia Scratch page located at as show i Figure B.3. Here you ca fid iformatio o Scratch, its origis ad creator, as well as its developmet eviromet ad website. I additio, you will also fid plety of liks to papers about Scratch. The Programmig Page at the Thorburg Ceter Website If your operatig system of choice is Liux, you will be pleased to kow that a official Liux versio of Scratch is i the works. I the meatime, if you caot wait, you ca dowload a free user implemetatio of Scratch at tcpdpodcast.org/scratch.html, as show i Figure B.4.
321 300 Appedix B What Next? Figure B.3 The Scratch page located at Figure B.4 Dowloadig a free copy of Scratch for Liux. You will fid all of the istructios you eed to dowload ad istall Scratch o Liux at this site. Although it does ot support Scratch s Presetatio mode, this Scratch implemetatio provides most of the programmig features curretly available i the Widows ad Mac OS X versios of Scratch.
322 Recommeded Readig 301 Figure B.5 You ca dowload free sprite ad soud files from the Scratch Resources website. The Scratch Resources Website Aother useful website dedicated to Scratch is the Scratch Resources website located at as show i Figure B.5. This sites provides free access to a growig collectio of sprite ad soud files, which you are ivited to dowload for free, provided you referece the Scratch Resources website i your Scratch project s credits. I additio to sprite ad soud files, you will also fid video tutorials desiged to help you lear more about programmig with Scratch. Recommeded Readig I additio to the websites previously discussed, you ca lear a lot about Scratch by reviewig documetatio available o the web. This documetatio is available electroically. A brief descriptio of some particularly useful documets, icludig their locatios, is listed here. Gettig Started with Scratch. This 14-page PDF file provides a step-by-step guide to Scratch, demostratig its basic operatio ad may of its capabilities. This documet ca be dowloaded from the Support page at the Scratch website (
323 302 Appedix B What Next? Scratch Referece Guide. This 17-page PDF file provides detailed iformatio about the Scratch graphical iterface, its Pait Editor program, ad a detailed overview of each of the Scratch blocks. This documet ca be dowloaded from the Support page at the Scratch website ( Gettig Started with Scratch Boards. This ie-page PDF file provides a overview of Scratch Boards ad detailed explaatios of how to work with its may differet features. It also provides troubleshootig advice. This documet ca be dowloaded from the Scratch Board page at the Scratch website ( A Itroductio to Scratch. This olie book is available as a Wikibook through At the time of writig, the Wikibook was still a work i progress. However, it was already well uderway ad cotaied a growig collectio of programmig iformatio about Scratch. This documet ca be read olie at Wikibooks by visitig
324 Glossary Actor. A term used to refer to sprites ad the role they play as they iteract with oe aother o the stage. Aimated GIF. A graphic is made up of two or more frames, each of which is displayed as a automated sequece whe the GIF file is displayed. Boolea. A term used to represet data that has either of two values, true or false. Brightess. The applicatio or restrictio of the itesity of light i a graphic image. Broadcast Message. A electroic message set betwee sprites as a meas of coordiatig applicatio activity. Code Block. A graphical commad used i the creatio of a script. Collisio. A evet that occurs wheever two sprites come ito cotact with oe aother o the stage. Compressio. The process of reducig the size of soud ad graphics files i order to reduce the overall size of Scratch applicatios. Coditioal Logic. The process of executig sets of code blocks based o whether or ot a tested coditio proves true. Costumes. Images that are used to represet a sprite o the stage. 303
325 304 Glossary Data. A piece of iformatio collected, stored, modified, ad processed durig applicatio executio. Debugger. A program or utility that ca be used to execute a applicatio withi a special eviromet that allows programmers to slow ad moitor the executio of a applicatio s script as it rus. Decimal. A floatig poit or real umber. Edless Loop. A loop that does ot have a meas for termiatig its executio. Evet Hadlig. The process of iitiatig script executio based o the occurrece of predefied evets, such as a mouse click, the pressig of a keyboard key, or the clickig of a sprite. Fisheye. A graphic effect that ca be applied to a sprite or backgroud i order to magify a portio of its image. Ghost. A graphic effect that fades the appearace of a costume or backgroud, makig it look trasparet. Global Variable. A variable that ca be modified by ay script i a applicatio. Gradiet. A color created by bledig together the foregroud ad backgroud colors. Hat Block. A code block that creates evet-drive scripts. IDE (Itegrated Developmet Eviromet). A graphical applicatio developmet eviromet desiged to facilitate program developmet. Iteger. A absolute or whole umber that does ot have a decimal poit. Java. A popular web-based programmig laguage that is a prerequisite for executig a Scratch applicatio o the Scratch website. Local Variable. A variable that ca be modified oly by scripts belogig to the sprite i which the variable is defied. Logical Error. A error created by a mistake made by the programmer whe developig the logic implemeted by a script. Looks Blocks. Code blocks that affect sprite ad backgroud appearace ad display text. Loop. A collectio of oe or more code blocks that are repeatedly executed. Moitor. A small block that displays the value curretly assiged to the code block. Mosaic. A special graphic effect that creates a image made up of repeated istaces of a sprite or backgroud.
326 Glossary 305 Motio Blocks. Code blocks that cotrol sprite placemet, directio, rotatio, ad movemet. MP3. A audio file that utilizes advaced compressio techology while retaiig high audio quality. Nest. The process of embeddig oe set of code blocks withi aother set of code blocks. Numbers Blocks. Code blocks that perform mathematical operatios, logical comparisos, roudig, ad other arithmetic operatios. Order of Precedece. The set of rules that is followed whe evaluatig a umeric expressio. Pait Editor. A Scratch program that supports the creatio of graphics files to be used as the basis for creatig ad modifyig sprites ad backgrouds. Pe. A virtualized drawig tool that ca be used to draw o the stage. Pe Blocks. Code blocks that ca be used to draw usig differet colors ad pe sizes. Pixelate. A special graphic effect that displays a sprite or backgroud at a lower resolutio tha the resolutio at which it was created. Project. A collectio of sprites, scripts, backgrouds, ad souds that is used as the basis for creatig Scratch applicatios. Real Number. A umber that icludes a decimal umber. Reporter Block. A code block that has either rouded or agled sides ad is specifically desiged as a mechaism for providig iput for other code blocks to process. Rotatio Ceter. The poit o a sprite that remais i positio whe a sprite is rotated. Ru-time Error. A applicatio error that occurs whe a applicatio attempts to perform a illegal actio. Scope. A term that refers to the area withi a applicatio where a variable s value ca be accessed ad modified. Scratch Board. A special piece of hardware that you ca buy from the Scratch website ad attach to your computer i order to collect ad process evirometal ad user-provided iput. Scratch Cards. PDF files that you ca prit ad use as a quick referece for performig certai tasks.
327 306 Glossary Script. A collectio of code blocks that outlies the programmig logic that iflueces the operatio of a sprite. Sesig Blocks. Code blocks that ca be used to determie the locatio of the mouse-poiter, its distace from other sprites, ad whether a sprite is touchig aother sprite. Soud Blocks. Code blocks that cotrol the playback ad volume of musical otes ad audio files. Sprite. A two-dimesioal image draw o a trasparet backgroud that ca be moved aroud the stage. You ca chage its appearace usig differet costumes. Squeak. A cross-platform programmig laguage used to develop Scratch. Stack Blocks. Code blocks with a otch at the top or a bump at the bottom that ca be sapped together with other bocks to defie a script s programmig logic. Stacks. Aother term for a script. Stage. The backgroud area o the Scratch IDE upo which sprites are displayed durig applicatio executio. Strig. A set of characters that ca be displayed withi thought ad speech bubbles. Tempo. A measuremet of the speed, i beats per miute, at which a drum or ote is played. Troubleshootig. The idetificatio, locatio, ad elimiatio of programmig errors, or bugs, that prevet applicatios from executig properly. Variable. A locatio i memory where a idividual piece of data is stored. Variable Scope. Idetifies the locatio withi a applicatio where the variable s value ca be modified. Variables Blocks. Code blocks that ca be used to store data used by applicatios whe they execute. Wave. A file with a.wav extesio that supports the storage ad playback of audio files. Whirl. A special graphic effect that twists ad distorts a portio of a costume or backgroud.
328 INDEX A About butto, 30 abs fuctio, 168 accouts, Scratch registratio, acos fuctio, 168 actios, iitiatig, 272 actors. See sprites additio operatios, Adobe Photoshop program, 46 Advaced topics forum, 25 advatages of Scratch, 4 5 AIF files, 43 aligmet, 41 All About Scratch forum, 25 alligator clips, 268 aimated GIF files, 43 aimatio sequece, Aimatio tags, 255 aoucemets, 25 applicatios distributig, executig from CD-ROM, 93 executio, 34 ruig, 257 o Iteret, 251 i presetatio mode, 33 troubleshootig, 74 uploadig, arithmetic operatios, 67 Art tags, 255 asi fuctio, 168 Asteroids, 50 ata fuctio, 168 AU files, 43 audio files. See soud files automatio, 17 18, 20 B backgrouds addig to stage area, 32 Dace applicatio example, Family Picture Movie example, Fish Tak applicatio example, 111 Number Guessig Game project example, 171 copyig, 43 deletig, 79 editig, 43 importig, 78 special effects, switchig, 229 viewig, 43 Ball Chase game example ball movemet cotrol, coordiate game play scripts, game over messages, 192 ew project creatio, 189 project preview, savig ad executig ew project, 195 soud files, 191 sprites, addig ad removig, variables, addig required by applicatio, 191 Basketball Quiz Project example addig ad removig sprites, addig variables required by applicatio, automatig admiistratio of, ew project creatio,
329 308 Idex Basketball Quiz Project example (cotiued) project preview, savig ad executig ew applicatio, 157 scripts, addig to butto sprites, blocks. See also code blocks categories, 60 color-coded, 60 cotrol, 65, hat, 57 help files, 70, if...else, 122 looks, moitors, 59 motio, 61 62, 98 movig, 54 umbers, 67 pe, 64 poit i directio, 102 reporter, sesig, 65 67, , 123, 270 soud, stack, variables, Boolea data, 140 boolea logic, 74 broadcast messages, 174, 183 bubble captios, 16 butto cotrols (Pait Editor), buttos About, 30 Choose New Sprite from File, 36 Compress Images, 31 Compress Souds, Cotrol, 17 Copy, 43 Delete, 35, 38, Duplicate, 35, 38 Edit, 43 Export, 40 Export this sprite, 35 Extras, 30 Get Surprise Sprite, 37 gree flag, 34 Grow Sprite, 38 Help Screes, 31 Import, 30, 42, 78 Laguage, 30 Looks, 16 Motio, 38 Move, 37 New, 15, 29, 77 New Sprites, Ope, 29 Pait, 42 Pait Editor, Pait New Sprite, 35 Play, 44 Presetatio Mode, 33 Record, 44 red stop, 34 Referece Guide, 31 Save, 18, 29, 90 Save As, 29 Scratch toolbar, Share!, 29 Show, 35 Shrik Sprite, 38 Soud, 16 sprite rotatioal, 41 Start Sigle Steppig, 30 Stop, 44 tooltips, 30 Udo, 30 Visit the Scratch support page butto, 31 Wat Help?, Zoom (Pait Editor program), 47 C case-sesitivity, variable ames, 143 cat image ico represetatio, 12 CD-ROM, executig applicatios from, 93 CD-ROM (Scratch Programmig for Tees) free trial versio, 6 Scratch istallatio files, 8 ceterig sprites, 35 Choose New Sprite from File butto, 36 Clear cavas butto (Pait Editor), 50 code blocks. See also blocks addig, ew project creatio, cofigurable, 7 draggig from blocks palette to scripts area, 41 overview, 7 switchig betwee, 38 collisio detectio, color settigs Pait Editor program, 50 pe,
330 Idex 309 color special effects, 202 commets, 257, 259 compariso operatios, Compress Images butto, 31 Compress Souds butto, coditioal logic, 65, 68, 74, cotrol blocks, 65, Cotrol butto, 17 coordiate system, stage, 31 coordiates, sprite, 105 Copy butto, 43 copyig backgrouds, 43 costumes, 43 sprites, 35, 38 stage area portios, 33 Corel Pait Shop Pro project, 46 cos fuctio, 168 costumes addig, 42 43, 198 chagig, 199 copyig, 43 defied, 12 deletig, 43 editig, 43 umberig, 199 order of, 198 special effects, switchig, 228 viewig, 16 coutdow, Crazy Eight Ball Game example eight ball display, ew project creatio, 209 programmig logic, project preview, savig ad executig ew project, 212 soud files, 210 sprites, addig ad removig, variables, addig required by applicatio, 210 cross-hairs, D Dace applicatio example backgrouds, addig to stage area, dace music, playig, ew sprite creatio, 77 project preview, savig ad executig ew Scratch applicatio, 90 soud files, importig, sprites, addig ad removig, data Boolea, 140 data types, how data is collected, itegers, 140 real umbers, 141 storig i variables, 141 strig, 140 debuggig. See also errors basic techiques, iformative messages, 282 moitors, liberal use of, 284 sigle steppig, testig idividual scripts, 283 updates, trackig, 281 Delete butto, 35, 38, deletig backgrouds, 79 costumes, 43 sprites, 35, 38 variables, 145 developmet, Scratch, 3 directio, sprite, disappearig/reappearig sprites, 205 distace, determiig, distributio, applicatios, divisio operatios, Doodle Drawig project example clearig the stage area, ew project creatio, 241 programmig logic, project preview, savig ad executig ew project, 247 sprites, addig ad removig, dowloadig projects, 265 Scratch, 8 drawig cavas (Pait Editor program), 47 drawig lies ad shapes. See pe drum souds, Duplicate butto, 35, 38 E Edit butto, 43 editig backgrouds, 43
331 310 Idex editig (cotiued) costumes, 43 scripts, 41 Educators forum, 25 electrical resistace, Ellipse toolbar butto (Pait Editor), 48 edless loops, equal to compariso operator, 165 equal to operator, 165 Eraser toolbar butto (Pait Editor), 48 errors. See also debuggig logical, programmig practices, ru-time, sytax, 279 evet hadlig, 74 evet programmig, 65, executio loop, pausig, Export butto, 40 Export this sprite butto, 35 exportig sprites, 35 Extras butto, 30 Eyedropper toolbar butto (Pait Editor), 48 F Family Picture Movie example ew project creatio, 222 programmig logic, project preview, savig ad executig ew project, 230 soud files, sprites ad backgrouds, addig ad removig, variables, addig required by applicatio, 225 FAQ forum, 25 Fill toolbar butto (Pait Editor), 48 Fish Tak applicatio example aimatig ad swimmig of fish, backgrouds, addig to stage area, 111 ew project creatio, 111 project preview, 110 savig ad executig ew project, soud files, addig, 112 soud files, playig, 113 sprites, addig ad removig, fisheye special effects, Flip horizotally butto (Pait Editor), 50 Flip vertically butto (Pait Editor), 50 Follow the Mouse Scratch card, 110 forums help files, list of, free trial copy, 6 full-scree mode, 33 G galleries, 22 23, Game tags, 255 Get Surprise Sprite butto, 37 Gettig Start butto, 31 ghost effects, 203 GIF files, 43 Glide Scratch card, 110 global commuity of Scratch, 19, 21 global variables, greater tha compariso operator, 165 gree flag butto, 34 Grow butto (Pait Editor), 49 Grow Sprite butto, 38 H hat blocks, 57 Hello World! applicatio, 15 help files. See also resources blocks, 70 forums, Gettig Started butto, 31 Help Screes butto, 31 for idividual code blocks, Referece Guide butto, 31 Scratch Help web page, 289 Visit the Scratch support page butto, 31 Wat Help? butto, Help Screes butto, 31 I icos, cat image represetatio, 12 IDE (itegrated developmet eviromet), if...else block, 122 images. See costumes Imagie-Program-Share! sloga, 5 Import butto, 42, 49, 78 Import Project butto, 30 importig backgrouds, 78 soud files, 44, iformative messages, 282
332 Idex 311 istallig Java, 9 10 Scratch o Mac OS X, o Widows, Scratch Boards, itegers, 140 iterface desig, 74 Iteret, ruig applicatios o, 251 iterative processig, 74 J Java, istallig o Widows, 9 10 K key, determiig whe pressed, 123 Key Moves Scratch card, 110 L Laguage butto, 30 laguage support, 19 laptops, Oe Laptop Per Child project, 4 less tha operator, 165 levels, overlappig sprites, 206 licesig agreemets, Java istallatio o Widows, 10 light, reactig to, light sesors, 268 Lie toolbar butto (Pait Editor), 48 lies ad shapes, drawig. See pes l fuctio, 168 local variables, lockig/ulockig sprites, 41 log fuctio, 168 logic, Scratch applicatio project example, 7 logical comparisos, logical errors, looks blocks, Looks butto, 16 loopig, 65 edless loops, executio, variables blocks ad, 69 M Mac OS X distributig Scratch applicatios to, 93 scratch istallatio, mathematical calculatios abs fuctio, 168 acos fuctio, 168 additio operatios, asi fuctio, 168 ata fuctio, 168 built-i, compariso operatios, cos fuctio, 168 divisio operatios, l fuctio, 168 log fuctio, 168 logical comparisos, multiplicatio operatios, order of precedece, radom umber geeratio, rouded umbers, si fuctio, 168 sqrt fuctio, 168 subtractio operatios, ta fuctio, 168 member galleries, meu bar buttos, MIT Media Lab, 6 moitors appearace of, chagig, 59 defied, 59 displayig, 59 liberal use of, 284 togglig betwee, 59 variable-based, 59, 147 mosaic effects, 203 motio blocks, 61 62, 98 Motio butto, 38 mouse poiter locatio retrievig mouse butto ad coordiate status, trackig, 37 Move butto, 37 Move to a Beat Scratch card, 110 movig blocks, 54 sprites, 37, Movig Aimatio Scratch card, 110 MP3 files, 43 Mr. Wiggly s Dace applicatio example. See Dace applicatio example multiplicatio operatios, music. See soud files Music tags, 255 musical otes,
333 312 Idex N amig projects, 18 sprites, 16, 39 variables, 143 estig coditioal cotrol blocks, 186 New butto, 15, 29, 77 ew project creatio Ball Chase game example, 189 Basketball Quiz Project example, 150 Crazy Eight Ball Game example, 209 Dace applicatio example, 77 Doodle Drawig project example, 241 Family Picture Movie example, 222 Fish Tak applicatio example, 111 Number Guessig Game project example, 171 savig ad executig Ball Chase game example, 195 Basketball Quiz Project example, 157 Crazy Eight Ball Game example, 212 Doodle Drawig project example, 247 Family Picture Movie example, 230 Fish Tak applicatio example, Number Guessig Game project example, 176 Scrapbook applicatio example, New Sprites butto, ot equal to operator, 165 otes addig ad updatig, 45 text saved as, 46 Number Guessig Game project example backgrouds, addig, 171 ew project creatio, 171 player guesses, processig, project preview, savig ad executig ew project creatio, 176 scripts, addig, soud files, 173 sprites, addig ad removig, variables, addig required by applicatio, umbers blocks, 67 umeric coutdow, O Oe Laptop Per Child project, 4 Ope butto (meu bar), 29 order of precedece, order of sprites, 34 overlappig sprites, P Pait butto, 42 Pait Editor program butto cotrols, color settigs, 50 drawig cavas, 47 overview, 46 Set Rotatio Ceter butto, 50 startig, 35, 42 toolbar buttos, 48 Zoom buttos, 47 Pait New Sprite butto, 35 Paitbrush toolbar butto (Pait Editor), 48 pe blocks, 64 pixelate effects, 203 Play butto, 44 playback, souds, 16 poit i directio block, 102 Presetatio Mode butto, 33 presetatio mode, ruig applicatios i, 33 program sychroizatio, 74 programmig logic Crazy Eight Ball Game example, Doodle Drawig project example, Family Picture Movie example, project otes addig ad updatig, 45 text saved as, 46 projects creatig ew, dowloadig, 265 amig, 18 removig, 264 updatig, 264 R radom umber geeratio, 67 68, real umbers, 141 Record butto, 44 recordig soud files, 44 Rectagle toolbar butto (Pait Editor), 48 red stop butto, 34 Redo butto (Pait Editor), 50 Referece Guide butto, 31 registerig Scratch, websites, removig projects, 264 reporter blocks, 57 58
334 Idex 313 repositioig sprites, resources. See also help files forums, global commuity of Scratch, 19, 21 Wat Help? butto, Rotate clockwise butto (Pait Editor), 50 Rotate couterclockwise butto (Pait Editor), 50 rotatio clockwise, 99 sprite, sprite rotatioal buttos, 41 roudig umbers, Ruby programmig laguage, 6 ruig applicatios, 257 ru-time errors, S Save As butto, 29 Save butto, 18, 29, 90 Save Project widow, 18 savig how to save projects, stage area copies, 33 Scrapbook applicatio example addig ad removig sprites ad costumes, addig soud files to stage, 134 ew project creatio, 132 savig ad executig ew project applicatio, Scratch advatages of, 4 5 buildig block approach to programmig, 6 7 developmet, 3 dowloadig, 8 free trial copy of, 6 istallig o Mac OS X, o Widows, user-adapted versio, 8 website, 6, 8, Scratch Boards, 131 cotrols ad sesors, 268 istallig, purchasig, 269 Scratch Board Watcher, Scratch cards, scripts aligmet, 41 defied, 12 editig, 41 executio pausig, termiatig, 187 ruig, 14, 17 spacig evely, 41 Selectio toolbar butto (Pait Editor), 48 sesig blocks, 65 67, , 123, 270 sequetial processig, 74 Set Rotatio Ceter butto (Pait Editor), 50 Setup Wizard (Scratch), shadig levels, shapes ad lies, drawig. See pes Share! butto, 29 shared projects, 251 Scratch sloga, 5 sharig your applicatio projects, show ad tell topic forum, 25 Show butto, 35 Shrik butto (Pait Editor), 50 Shrik Sprite butto, 38 Simulatio tags, 255 si fuctio, 168 sigle steppig, sites. See websites size pe, sprite, 38, sliders, 147, 268, sloga, 5 soud blocks, Soud butto, 16 soud files addig, Fish Tak applicatio example, 112 Scrapbook applicatio example, 134 AIF files, 43 AU file, 43 Ball Chase game example, 191 cautious removal, Crazy Eight Ball Game example, 210 Family Picture Movie example, importig, 44, importig ad assigig to sprites, 42 MP3 files, 43 musical otes, Number Guessig Game project example, 173 playback,
335 314 Idex soud files (cotiued) playig, 44 Fish Tak applicatio example, 113 Scrapbook applicatio example, 135 recordig, 44 removig from applicatio, 44 respodig to, retrievig audio data, stoppig playback, 44 tempo, viewig list of, volume cofiguratio, WAV file, 43 Soud Recorder widow, soud sesors, 268 souds Compress Souds butto, playback cotrol, 16 as sprite compoets, 12 Space Ivaders, 272 special effects addig to costumes ad backgrouds, brightess, 203 color, 202 fisheye, 202 ghost, 203 mosaic, 203 pixelate, 203 whirl, 202 sprites addig ad removig Ball Chase game example, Basketball Quiz Project example, Crazy Eight Ball Game example, Dace applicatio example, Doodle Drawig project example, Family Picture Movie example, Fish Tak applicatio example, Number Guessig Game project example, appearace of, chagig, 16 attributes of, chagig, 16 automatig, 17 18, 20 blue lie idicator, 40 boucig aroud stage, ceterig, 35 chagig size of, 38 collisio detectio, compoets of, 12 coordiate ad directio display, coordiates ad directio, keepig track of, coordiates, chagig, 105 copyig, 35, 38 costumes, viewig, 16 defied, 12 deletig, 35, 38 directio, disappearig/reappearig, 205 distace, determiig, exportig, 35 geeratig ew, lockig/ulockig, 41 movig, 37 movig ad rotatig, amig, 16, 39 New Sprites butto, 35 order of, reorgaizig, 34 overlappig, repositioig, retrievig stage ad sprite data, rotatio ceter cofiguratio, rotatioal buttos, 41 selectig, 36, 81 size, sprite list, uses for, 14 sqrt fuctio, 168 Squeak programmig laguage, 6 stack blocks, stacks, stage area appearace of, chagig, backgrouds, addig ew, 32 Dace applicatio example, Fish Tak applicatio example, 111 clearig, , coordiate systems, 31 copyig selected portio of, 33 defied, 12 full-scree mode, 33 mouse poiter locatio, trackig, 37 retrievig stage ad sprite data, ruig applicatios o, savig copies of, 33 thumbails, 32 Stamp toolbar butto (Pait Editor), 48
336 Idex 315 stamps, 239 Start Sigle Steppig butto, 30 Stop butto, 44 Story tags, 255 strig data, 140 subtractio operatios, suggestios forum, 26 sytax errors, 279 T tags addig, 259 list of, 255 ta fuctio, 168 tempo, termiatig script executio, 187 testig, 283 text cotrols, Text toolbar butto (Pait Editor), 48 thumbails, stage area, 32 timer cotrols, togglig, betwee moitors, 59 toolbars Pait Editor program, 48 Scratch, tooltips, 30 trial copy, 6 troubleshootig applicatios, 74 forums for, 26 U Udo butto, 30, 50 updates, trackig, 281 uploadig applicatios, user iput collectio, user-adapted versio of Scratch, 8 V variable-based moitors, 59, 147 variables accessig variables belogig to other sprites, addig required by applicatio Ball Chase game example, 191 Crazy Eight Gall Game example, 210 Family Picture Movie example, 225 Number Guessig Game project example, assigig to sprites, creatig, 141 deletig, 145 examples of, global, local, amig, 143 storig data i, 141 use of, 74 variable scope, variables blocks, Visit the Scratch support page butto, 31 volume, soud files, W Wat Help? butto, WAV files, 43 websites registerig, Scratch, 6, 8, Squeak, 6 whirl special effects, Widows distributig applicatios to, 92 Java istallatio, 9 10 Scratch istallatio, X X-axis repositioig sprites, 103 stage coordiates, 31 Y Y-axis repositioig sprites, 103 stage coordiates, 31 YouTube website, 5 Z Zoom buttos (Pait Editor program), 47
337 Licese Agreemet/Notice of Limited Warraty By opeig the sealed disc cotaier i this book, you agree to the followig terms ad coditios. If, upo readig the followig licese agreemet ad otice of limited warraty, you caot agree to the terms ad coditios set forth, retur the uused book with uopeed disc to the place where you purchased it for a refud. Licese The eclosed software is copyrighted by the copyright holder(s) idicated o the software disc. You are licesed to copy the software oto a sigle computer for use by a sigle user ad to a backup disc. You may ot reproduce, make copies, or distribute copies or ret or lease the software i whole or i part, except with writte permissio of the copyright holder(s). You may trasfer the eclosed disc oly together with this licese, ad oly if you destroy all other copies of the software ad the trasferee agrees to the terms of the licese. You may ot decompile, reverse assemble, or reverse egieer the software. Notice of Limited Warraty The eclosed disc is warrated by Course Techology to be free of physical defects i materials ad workmaship for a period of sixty (60) days from ed user s purchase of the book/disc combiatio. Durig the sixty-day term of the limited warraty, Course Techology will provide a replacemet disc upo the retur of a defective disc. Limited Liability THE SOLE REMEDY FOR BREACH OF THIS LIMITED WARRANTY SHALL CONSIST ENTIRELY OF REPLACEMENT OF THE DEFECTIVE DISC. IN NO EVENT SHALL COURSE TECHNOLOGY OR THE AUTHOR BE LIABLE FOR ANY OTHER DAMAGES, INCLUDING LOSS OR CORRUPTION OF DATA, CHANGES IN THE FUNCTIONAL CHARACTERISTICS OF THE HARDWARE OR OPERATING SYSTEM, DELETERIOUS INTERACTION WITH OTHER SOFTWARE, OR ANY OTHER SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES THAT MAY ARISE, EVEN IF COURSE TECHNOLOGY AND/OR THE AUTHOR HAS PREVIOUSLY BEEN NOTIFIED THAT THE POSSIBILITY OF SUCH DAMAGES EXISTS. Disclaimer of Warraties COURSE TECHNOLOGY AND THE AUTHOR SPECIFICALLY DISCLAIM ANY AND ALL OTHER WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING WARRANTIES OF MERCHANTABILITY, SUITABILITY TO A PARTICULAR TASK OR PURPOSE, OR FREEDOM FROM ERRORS. SOME STATES DO NOT ALLOW FOR EXCLUSION OF IMPLIED WARRANTIES OR LIMITATION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THESE LIMITATIONS MIGHT NOT APPLY TO YOU. Other This Agreemet is govered by the laws of the State of Massachusetts without regard to choice of law priciples. The Uited Covetio of Cotracts for the Iteratioal Sale of Goods is specifically disclaimed. This Agreemet costitutes the etire agreemet betwee you ad Course Techology regardig use of the software.
BaanERP. BaanERP Windows Client Installation Guide
BaaERP A publicatio of: Baa Developmet B.V. P.O.Box 143 3770 AC Bareveld The Netherlads Prited i the Netherlads Baa Developmet B.V. 1999. All rights reserved. The iformatio i this documet is subject to
Desktop Management. Desktop Management Tools
Desktop Maagemet 9 Desktop Maagemet Tools Mac OS X icludes three desktop maagemet tools that you might fid helpful to work more efficietly ad productively: u Stacks puts expadable folders i the Dock. Clickig
Domain 1: Identifying Cause of and Resolving Desktop Application Issues Identifying and Resolving New Software Installation Issues
Maual Widows 7 Eterprise Desktop Support Techicia (70-685) 1-800-418-6789 Domai 1: Idetifyig Cause of ad Resolvig Desktop Applicatio Issues Idetifyig ad Resolvig New Software Istallatio Issues This sectio
(VCP-310) 1-800-418-6789
Maual VMware Lesso 1: Uderstadig the VMware Product Lie I this lesso, you will first lear what virtualizatio is. Next, you ll explore the products offered by VMware that provide virtualizatio services.
ODBC. Getting Started With Sage Timberline Office ODBC
ODBC Gettig Started With Sage Timberlie Office ODBC NOTICE This documet ad the Sage Timberlie Office software may be used oly i accordace with the accompayig Sage Timberlie Office Ed User Licese Agreemet.
Configuring Additional Active Directory Server Roles
Maual Upgradig your MCSE o Server 2003 to Server 2008 (70-649) 1-800-418-6789 Cofigurig Additioal Active Directory Server Roles Active Directory Lightweight Directory Services Backgroud ad Cofiguratio
Professional Networking
Professioal Networkig 1. Lear from people who ve bee where you are. Oe of your best resources for etworkig is alumi from your school. They ve take the classes you have take, they have bee o the job market
Domain 1: Configuring Domain Name System (DNS) for Active Directory
Maual Widows Domai 1: Cofigurig Domai Name System (DNS) for Active Directory Cofigure zoes I Domai Name System (DNS), a DNS amespace ca be divided ito zoes. The zoes store ame iformatio about oe or more
Ideate, Inc. Training Solutions to Give you the Leading Edge
Ideate, Ic. Traiig News 2014v1 Ideate, Ic. Traiig Solutios to Give you the Leadig Edge New Packages For All Your Traiig Needs! Bill Johso Seior MEP - Applicatio Specialist Revit MEP Fudametals Ad More!
Domain 1: Designing a SQL Server Instance and a Database Solution
Maual SQL Server 2008 Desig, Optimize ad Maitai (70-450) 1-800-418-6789 Domai 1: Desigig a SQL Server Istace ad a Database Solutio Desigig for CPU, Memory ad Storage Capacity Requiremets Whe desigig a
Baan Service Master Data Management
Baa Service Master Data Maagemet Module Procedure UP069A US Documetiformatio Documet Documet code : UP069A US Documet group : User Documetatio Documet title : Master Data Maagemet Applicatio/Package :
PUBLIC RELATIONS PROJECT 2016
PUBLIC RELATIONS PROJECT 2016 The purpose of the Public Relatios Project is to provide a opportuity for the chapter members to demostrate the kowledge ad skills eeded i plaig, orgaizig, implemetig ad evaluatig
A guide to School Employees' Well-Being
A guide to School Employees' Well-Beig Backgroud The public school systems i the Uited States employ more tha 6.7 millio people. This large workforce is charged with oe of the atio s critical tasks to
CS100: Introduction to Computer Science
Review: History of Computers CS100: Itroductio to Computer Sciece Maiframes Miicomputers Lecture 2: Data Storage -- Bits, their storage ad mai memory Persoal Computers & Workstatios Review: The Role of
Flood Emergency Response Plan
Flood Emergecy Respose Pla This reprit is made available for iformatioal purposes oly i support of the isurace relatioship betwee FM Global ad its cliets. This iformatio does ot chage or supplemet policy
CCH Accountants Starter Pack
CCH Accoutats Starter Pack We may be a bit smaller, but fudametally we re o differet to ay other accoutig practice. Util ow, smaller firms have faced a stark choice: Buy cheaply, kowig that the practice
E-Plex Enterprise Access Control System
Eterprise Access Cotrol System Egieered for Flexibility Modular Solutio The Eterprise Access Cotrol System is a modular solutio for maagig access poits. Employig a variety of hardware optios, system maagemet
Engineering Data Management
BaaERP 5.0c Maufacturig Egieerig Data Maagemet Module Procedure UP128A US Documetiformatio Documet Documet code : UP128A US Documet group : User Documetatio Documet title : Egieerig Data Maagemet Applicatio/Package
Serial ATA PCI Host Adapter AEC-6290/6295
Serial ATA PCI Host Adapter AEC-6290/6295 User s Maual Versio:1.0 Copyright 2003 ACARD Techology Corp. Release: April 2003 Copyright ad Trademarks The iformatio of the product i this maual is subject to
iprox sensors iprox inductive sensors iprox programming tools ProxView programming software iprox the world s most versatile proximity sensor
iprox sesors iprox iductive sesors iprox programmig tools ProxView programmig software iprox the world s most versatile proximity sesor The world s most versatile proximity sesor Eato s iproxe is syoymous
Agenda. Outsourcing and Globalization in Software Development. Outsourcing. Outsourcing here to stay. Outsourcing Alternatives
Outsourcig ad Globalizatio i Software Developmet Jacques Crocker UW CSE Alumi 2003 [email protected] Ageda Itroductio The Outsourcig Pheomeo Leadig Offshore Projects Maagig Customers Offshore Developmet
CREATIVE MARKETING PROJECT 2016
CREATIVE MARKETING PROJECT 2016 The Creative Marketig Project is a chapter project that develops i chapter members a aalytical ad creative approach to the marketig process, actively egages chapter members
TruStore: The storage. system that grows with you. Machine Tools / Power Tools Laser Technology / Electronics Medical Technology
TruStore: The storage system that grows with you Machie Tools / Power Tools Laser Techology / Electroics Medical Techology Everythig from a sigle source. Cotets Everythig from a sigle source. 2 TruStore
How to use what you OWN to reduce what you OWE
How to use what you OWN to reduce what you OWE Maulife Oe A Overview Most Caadias maage their fiaces by doig two thigs: 1. Depositig their icome ad other short-term assets ito chequig ad savigs accouts.
WindWise Education. 2 nd. T ransforming the Energy of Wind into Powerful Minds. editi. A Curriculum for Grades 6 12
WidWise Educatio T rasformig the Eergy of Wid ito Powerful Mids A Curriculum for Grades 6 12 Notice Except for educatioal use by a idividual teacher i a classroom settig this work may ot be reproduced
Bio-Plex Manager Software
Multiplex Suspesio Array Bio-Plex Maager Software Extract Kowledge Faster Move Your Research Forward Bio-Rad cotiues to iovate where it matters most. With Bio-Plex Maager 5.0 software, we offer valuable
CCH CRM Books Online Software Fee Protection Consultancy Advice Lines CPD Books Online Software Fee Protection Consultancy Advice Lines CPD
Books Olie Software Fee Fee Protectio Cosultacy Advice Advice Lies Lies CPD CPD facig today s challeges As a accoutacy practice, maagig relatioships with our cliets has to be at the heart of everythig
client communication
CCH Portal cliet commuicatio facig today s challeges Like most accoutacy practices, we ow use email for most cliet commuicatio. It s quick ad easy, but we do worry about the security of sesitive data.
One Goal. 18-Months. Unlimited Opportunities.
18 fast-track 18-Moth BACHELOR S DEGREE completio PROGRAMS Oe Goal. 18-Moths. Ulimited Opportuities. www.ortheaster.edu/cps Fast-Track Your Bachelor s Degree ad Career Goals Complete your bachelor s degree
Hypergeometric Distributions
7.4 Hypergeometric Distributios Whe choosig the startig lie-up for a game, a coach obviously has to choose a differet player for each positio. Similarly, whe a uio elects delegates for a covetio or you
The Importance of Media in the Classroom
01-TilestoVol09.qxd 8/25/03 3:47 PM Page 1 1 The Importace of Media i the Classroom As teachers, we have a wealth of iformatio from which to choose for our classrooms. We ca ow brig history ito the classroom
IT Support. 020 8269 6878 n www.premierchoiceinternet.com n [email protected]. 30 Day FREE Trial. IT Support from 8p/user
IT Support IT Support Premier Choice Iteret has bee providig reliable, proactive & affordable IT Support solutios to compaies based i Lodo ad the South East of Eglad sice 2002. Our goal is to provide our
Conversion Instructions:
Coversio Istructios: QMS magicolor 2 DeskLaser to QMS magicolor 2 CX 1800502-001A Trademarks QMS, the QMS logo, ad magicolor are registered trademarks of QMS, Ic., registered i the Uited States Patet ad
AGC s SUPERVISORY TRAINING PROGRAM
AGC s SUPERVISORY TRAINING PROGRAM Learig Today...Leadig Tomorrow The Kowledge ad Skills Every Costructio Supervisor Must Have to be Effective The Associated Geeral Cotractors of America s Supervisory
Modified Line Search Method for Global Optimization
Modified Lie Search Method for Global Optimizatio Cria Grosa ad Ajith Abraham Ceter of Excellece for Quatifiable Quality of Service Norwegia Uiversity of Sciece ad Techology Trodheim, Norway {cria, ajith}@q2s.tu.o
GOOD PRACTICE CHECKLIST FOR INTERPRETERS WORKING WITH DOMESTIC VIOLENCE SITUATIONS
GOOD PRACTICE CHECKLIST FOR INTERPRETERS WORKING WITH DOMESTIC VIOLENCE SITUATIONS I the sprig of 2008, Stadig Together agaist Domestic Violece carried out a piece of collaborative work o domestic violece
Confidence Intervals for One Mean
Chapter 420 Cofidece Itervals for Oe Mea Itroductio This routie calculates the sample size ecessary to achieve a specified distace from the mea to the cofidece limit(s) at a stated cofidece level for a
The Forgotten Middle. research readiness results. Executive Summary
The Forgotte Middle Esurig that All Studets Are o Target for College ad Career Readiess before High School Executive Summary Today, college readiess also meas career readiess. While ot every high school
Conclusions. Chapter 9
Chapter 9 Coclusios You have reached the fial chapter of this book o Microsoft s DirectX. At this poit you should have a good uderstadig of DirectX 11 from graphics to iput ad audio as well as basic, yet
Domain 1 Components of the Cisco Unified Communications Architecture
Maual CCNA Domai 1 Compoets of the Cisco Uified Commuicatios Architecture Uified Commuicatios (UC) Eviromet Cisco has itroduced what they call the Uified Commuicatios Eviromet which is used to separate
SECTION 1.5 : SUMMATION NOTATION + WORK WITH SEQUENCES
SECTION 1.5 : SUMMATION NOTATION + WORK WITH SEQUENCES Read Sectio 1.5 (pages 5 9) Overview I Sectio 1.5 we lear to work with summatio otatio ad formulas. We will also itroduce a brief overview of sequeces,
Analyzing Longitudinal Data from Complex Surveys Using SUDAAN
Aalyzig Logitudial Data from Complex Surveys Usig SUDAAN Darryl Creel Statistics ad Epidemiology, RTI Iteratioal, 312 Trotter Farm Drive, Rockville, MD, 20850 Abstract SUDAAN: Software for the Statistical
Radio Dispatch Systems
Radio Dispatch Systems ZETRON DISPATCH SOLUTIONS: AT THE CENTER OF YOUR CRITICAL OPERATIONS Your dispatch system is the ceterpoit through which your key operatios are coordiated ad cotrolled. That s why
Digital Enterprise Unit. White Paper. Web Analytics Measurement for Responsive Websites
Digital Eterprise Uit White Paper Web Aalytics Measuremet for Resposive Websites About the Authors Vishal Machewad Vishal Machewad has over 13 years of experiece i sales ad marketig, havig worked as a
Lesson Plans for Teachers
Lesso Plas for Teachers Kidergarte - Grade 3 Copyright 2012 by Little Pickle Press LLC Table of Cotets Quite uique! Streeeetch ad grooow... Overview 3 Activity Set 1 What Is a Brai ad What Does It Do?
INDEPENDENT BUSINESS PLAN EVENT 2016
INDEPENDENT BUSINESS PLAN EVENT 2016 The Idepedet Busiess Pla Evet ivolves the developmet of a comprehesive proposal to start a ew busiess. Ay type of busiess may be used. The Idepedet Busiess Pla Evet
Chapter 10 Computer Design Basics
Logic ad Computer Desig Fudametals Chapter 10 Computer Desig Basics Part 1 Datapaths Charles Kime & Thomas Kamiski 2004 Pearso Educatio, Ic. Terms of Use (Hyperliks are active i View Show mode) Overview
facing today s challenges As an accountancy practice, managing relationships with our clients has to be at the heart of everything we do.
CCH CRM cliet relatios facig today s challeges As a accoutacy practice, maagig relatioships with our cliets has to be at the heart of everythig we do. That s why our CRM system ca t be a bolt-o extra it
Neolane Leads. Neolane v6.1
Neolae Leads Neolae v6.1 This documet, ad the software it describes, are provided subject to a Licese Agreemet ad may ot be used or copied outside of the provisios of the Licese Agreemet. No part of this
InventoryControl. The Complete Inventory Tracking Solution for Small Businesses
IvetoryCotrol The Complete Ivetory Trackig Solutio for Small Busiesses Regular Logo 4C Productivity Solutios for Small Busiesses Logo Outlie Get i cotrol of your ivetory with Wasp Ivetory Cotrol the complete
Setting Up a Contract Action Network
CONTRACT ACTION NETWORK Settig Up a Cotract Actio Network This is a guide for local uio reps who wat to set up a iteral actio etwork i their worksites. This etwork cosists of: The local uio represetative,
Measures of Spread and Boxplots Discrete Math, Section 9.4
Measures of Spread ad Boxplots Discrete Math, Sectio 9.4 We start with a example: Example 1: Comparig Mea ad Media Compute the mea ad media of each data set: S 1 = {4, 6, 8, 10, 1, 14, 16} S = {4, 7, 9,
HP Asset Manager. Software version: 5.20. Service Asset and Configuration Management
HP Asset Maager Software versio: 5.20 Service Asset ad Cofiguratio Maagemet Documet Release Date: 01 October 2009 Software Release Date: October 2009 Legal Notices Copyright Notices Copyright 1994-2009
Neolane Reporting. Neolane v6.1
Neolae Reportig Neolae v6.1 This documet, ad the software it describes, are provided subject to a Licese Agreemet ad may ot be used or copied outside of the provisios of the Licese Agreemet. No part of
Authentication - Access Control Default Security Active Directory Trusted Authentication Guest User or Anonymous (un-authenticated) Logging Out
FME Server Security Table of Cotets FME Server Autheticatio - Access Cotrol Default Security Active Directory Trusted Autheticatio Guest User or Aoymous (u-autheticated) Loggig Out Authorizatio - Roles
ContactPro Desktop for Multi-Media Contact Center
CotactPro Desktop for Multi-Media Cotact Ceter CCT CotactPro (CP) is the perfect solutio for the aget desktop i a Avaya multimedia call ceter eviromet. CotactPro empowers agets to efficietly serve customers
The Big Picture: An Introduction to Data Warehousing
Chapter 1 The Big Picture: A Itroductio to Data Warehousig Itroductio I 1977, Jimmy Carter was Presidet of the Uited States, Star Wars hit the big scree, ad Apple Computer, Ic. itroduced the world to the
In nite Sequences. Dr. Philippe B. Laval Kennesaw State University. October 9, 2008
I ite Sequeces Dr. Philippe B. Laval Keesaw State Uiversity October 9, 2008 Abstract This had out is a itroductio to i ite sequeces. mai de itios ad presets some elemetary results. It gives the I ite Sequeces
Dragon NaturallySpeaking. Version 11.5. User Guide
Drago NaturallySpeakig Versio 11.5 User Guide Drago User Guide, Versio 11.5 Nuace Commuicatios, Ic. has patets or pedig patet applicatios coverig the subject matter cotaied i this documet. The furishig
Did you know that houses with CCTV are 90% less likely to be burgled? Yale now offer a range of Easy Fit CCTV systems
Easy Fit CCTV Rage Did you kow that houses with CCTV are 90% less likely to be burgled? Yale ow offer a rage of Easy Fit CCTV systems Protect your premises wherever you are with our ew rage of CCTV systems,
STUDENTS PARTICIPATION IN ONLINE LEARNING IN BUSINESS COURSES AT UNIVERSITAS TERBUKA, INDONESIA. Maya Maria, Universitas Terbuka, Indonesia
STUDENTS PARTICIPATION IN ONLINE LEARNING IN BUSINESS COURSES AT UNIVERSITAS TERBUKA, INDONESIA Maya Maria, Uiversitas Terbuka, Idoesia Co-author: Amiuddi Zuhairi, Uiversitas Terbuka, Idoesia Kuria Edah
Pre-Suit Collection Strategies
Pre-Suit Collectio Strategies Writte by Charles PT Phoeix How to Decide Whether to Pursue Collectio Calculatig the Value of Collectio As with ay busiess litigatio, all factors associated with the process
Week 3 Conditional probabilities, Bayes formula, WEEK 3 page 1 Expected value of a random variable
Week 3 Coditioal probabilities, Bayes formula, WEEK 3 page 1 Expected value of a radom variable We recall our discussio of 5 card poker hads. Example 13 : a) What is the probability of evet A that a 5
Making training work for your business
Makig traiig work for your busiess Itegratig core skills of laguage, literacy ad umeracy ito geeral workplace traiig makes sese. The iformatio i this pamphlet will help you pla for ad build a successful
BaanERP 5.0c. EDI User Guide
BaaERP 5.0c A publicatio of: Baa Developmet B.V. P.O.Box 143 3770 AC Bareveld The Netherlads Prited i the Netherlads Baa Developmet B.V. 1999. All rights reserved. The iformatio i this documet is subject
Security Functions and Purposes of Network Devices and Technologies (SY0-301) 1-800-418-6789. Firewalls. Audiobooks
Maual Security+ Domai 1 Network Security Every etwork is uique, ad architecturally defied physically by its equipmet ad coectios, ad logically through the applicatios, services, ad idustries it serves.
INVESTMENT PERFORMANCE COUNCIL (IPC) Guidance Statement on Calculation Methodology
Adoptio Date: 4 March 2004 Effective Date: 1 Jue 2004 Retroactive Applicatio: No Public Commet Period: Aug Nov 2002 INVESTMENT PERFORMANCE COUNCIL (IPC) Preface Guidace Statemet o Calculatio Methodology
OfficePACS. Digital Imaging
OfficePACS Digital Imagig About Stryker Stryker is a multi-billio dollar compay specializig i orthopaedics We are oe of the largest compaies i the worldwide orthopaedic market We are a Fortue 500 & BusiessWeek
G r a d e. 2 M a t h e M a t i c s. statistics and Probability
G r a d e 2 M a t h e M a t i c s statistics ad Probability Grade 2: Statistics (Data Aalysis) (2.SP.1, 2.SP.2) edurig uderstadigs: data ca be collected ad orgaized i a variety of ways. data ca be used
2014 Menu of Agency Support Services 17 TOP OF MIND TOUCH POINTS
2014 Meu of Agecy Support Services 17 TOP OF MIND TOUCH POINTS Table of Cotets Turig a moolie customer ito a multi-lie customer icreases retetio by 7x! ORGANIC GROWTH Policy Reewal Appoitmet Calls.4 Life
Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.
This documet was writte ad copyrighted by Paul Dawkis. Use of this documet ad its olie versio is govered by the Terms ad Coditios of Use located at http://tutorial.math.lamar.edu/terms.asp. The olie versio
RELEASE GUIDE. VirusScan Enterprise VERSION 7.0
RELEASE GUIDE VirusSca Eterprise VERSION 7.0 COPYRIGHT 2003 Networks Associates Techology, Ic. All Rights Reserved. No part of this publicatio may be reproduced, trasmitted, trascribed, stored i a retrieval
leasing Solutions We make your Business our Business
if you d like to discover how Bp paribas leasig Solutios Ca help you to achieve your goals please get i touch leasig Solutios We make your Busiess our Busiess We look forward to hearig from you you ca
Now here is the important step
LINEST i Excel The Excel spreadsheet fuctio "liest" is a complete liear least squares curve fittig routie that produces ucertaity estimates for the fit values. There are two ways to access the "liest"
6. p o s I T I v e r e I n f o r c e M e n T
6. p o s I T I v e r e I f o r c e M e T The way positive reiforcemet is carried out is more importat tha the amout. B.F. Skier We all eed positive reiforcemet. Whether or ot we are cosciously aware of
3G Security VoIP Wi-Fi IP Telephony Routing/Switching Unified Communications. NetVanta. Business Networking Solutions
3G Security VoIP Wi-Fi IP Telephoy Routig/Switchig Uified Commuicatios NetVata Busiess Networkig Solutios Opportuity to lower Total Cost of Owership ad improve Retur o Ivestmet The ADTRAN Advatage ADTRAN
Handling. Collection Calls
Hadlig the Collectio Calls We do everythig we ca to stop collectio calls; however, i the early part of our represetatio, you ca expect some of these calls to cotiue. We uderstad that the first few moths
auction a guide to buying at Residential
Residetial a guide to buyig at auctio Allsop is the market leader for residetial ad commercial auctios i the UK Aually sells approximately 1 billio of property at auctio i the UK Holds at least seve residetial
The Canadian Council of Professional Engineers
The Caadia Coucil of Professioal Egieers Providig leadership which advaces the quality of life through the creative, resposible ad progressive applicatio of egieerig priciples i a global cotext Egieerig
Building Enterprise Applications with Windows Presentation Foundation and the Model View ViewModel Pattern
Buildig Eterprise Applicatios with Widows Presetatio Foudatio ad the Model View ViewModel Patter Raffaele Garofalo Published with the authorizatio of Microsoft Corporatio by: O Reilly Media, Ic. 1005 Gravestei
Equalizer Installation and Administration Guide
Equalizer Istallatio ad Admiistratio Guide Versio 7.2.3 Jauary 2007 Coyote Poit Systems, Ic. 675 North First Street Suite 975 Sa Jose, Califoria 95112 Copyright 1997-2007 Coyote Poit Systems, Ic. All Rights
WHERE CHANGE IS POSSIBLE
WHERE CHANGE IS POSSIBLE gree workspaces AT TIDES, WE HAVE BEEN WORKING WITH PEOPLE LIKE YOU FOR MORE THAN 30 YEARS TO MAKE THE WORLD A BETTER PLACE. Our missio is to parter with philathropists, foudatios,
Agency Relationship Optimizer
Decideware Developmet Agecy Relatioship Optimizer The Leadig Software Solutio for Cliet-Agecy Relatioship Maagemet supplier performace experts scorecards.deploymet.service decide ware Sa Fracisco Sydey
QUADRO tech. PST Flightdeck. Put your PST Migration on autopilot
QUADRO tech PST Flightdeck Put your PST Migratio o autopilot Put your PST Migratio o Autopilot A moder aircraft hardly remids its pilots of the early days of air traffic. It is desiged to eable flyig as
The Official Joomla! Book. Second Edition
The Official Joomla! Book Secod Editio Joomla! Press Visit iformit.com/joomlapress for a complete list of available publicatios. The missio of Joomla! Press is to ehace the Joomla! experiece by providig
CS100: Introduction to Computer Science
I-class Exercise: CS100: Itroductio to Computer Sciece What is a flip-flop? What are the properties of flip-flops? Draw a simple flip-flop circuit? Lecture 3: Data Storage -- Mass storage & represetig
This publication was written by the staff of the College Information Services office
This publicatio was writte by the staff of the College Iformatio Services office ad desiged by the Registrar s Office Academic Publicatios Uit. Special ackowledgemet for the desig ad productio of the Gradebook
Domain 1 - Describe Cisco VoIP Implementations
Maual ONT (642-8) 1-800-418-6789 Domai 1 - Describe Cisco VoIP Implemetatios Advatages of VoIP Over Traditioal Switches Voice over IP etworks have may advatages over traditioal circuit switched voice etworks.
NBX Business Telephone Guide
NBX Busiess Telephoe Guide Versio 2.0 Also icludes sectios o: APX Voice Messagig NBX NetSet DSS/BLF Adjuct http://www.3com.com/ Part No. 3C10171 Rev. D Published December 1999 3Com Corporatio 5400 Bayfrot
PUBLIC RELATIONS PROJECT 2015
PUBLIC RELATIONS PROJECT 2015 Supported by MARKETING The purpose of the Public Relatios Project is to provide a opportuity for the chapter members to demostrate the kowledge ad skills eeded i plaig, orgaizig,
