Adventures in Coding - Eva Holland - E-Book

Adventures in Coding E-Book

Eva Holland

0,0
17,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Learn to code the fun way with nine real projects for true beginners Adventures in Coding is written specifically for young people who want to learn how to code, but don't know where to begin. No experience? No problem! This book starts from the very beginning to take you from newbie to app-builder in no time. You'll 'learn by doing' as you build projects designed to help you master fundamental programming skills--and you'll have a great time doing it. These skills form the foundation of any programmer's tool set, and you'll continue to use them as you graduate to other devices and more difficult projects. Each chapter includes a video to help clear up any confusion and make sure you really understand, so you can keep programming your way through every single project without hitting major roadblocks. If you're ready to start designing your own program, this book will help you get started today. More and more kids are learning to code, and many schools offer basic programming classes as part of the regular curriculum. This book is structured like a class, starting with the basics and building skill upon skill, making it both a perfect accompaniment to formal instruction and an ideal guide for self-study. * Learn the basic programming skills you'll use everywhere * Build nine fun projects from super-basic to pretty challenging * Build the skills you need to create bigger and better apps * Watch video tutorials for extra help and explanations How many times have you played with an app only to find yourself wishing it had this or that feature? If you learn how to code, you can be the creator of the next big app! But it all starts with that first small project. Adventures in Coding provides all the information you need, so let's get coding!

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 271

Veröffentlichungsjahr: 2016

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Adventures in Coding

Published byJohn Wiley & Sons, Inc.10475 Crosspoint BoulevardIndianapolis, IN 46256www.wiley.com

Copyright © 2016 by John Wiley & Sons, Inc., Indianapolis, Indiana

Published simultaneously in Canada

ISBN: 978-1-119-23268-1ISBN: 978-1-119-23269-8 (ebk)ISBN: 978-1-119-23275-9 (ebk)

Manufactured in the United States of America

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.

LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEB SITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEB SITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEB SITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.

For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.

Library of Congress Control Number: 2016931381

Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.

This book is dedicated to the brave souls who have inspired in us a sense of adventure, especially David J. Holland, Patricia Minnick, Mary Ellen Holland, and Patrick Minnick.

Publisher’s Acknowledgments

Some of the people who helped bring this book to market include the following:

 

Editorial

Series Creator: Carrie Anne Philbin

Professional Technology & Strategy Director: Barry Pruett

Acquisitions Editor: Aaron Black

Project Editor: Charlotte Kughen

Copy Editor: Kezia Endsley

Technical Editor: Mike Machado

Editorial Manager: Mary Beth Wakefield

 

Marketing

Marketing Manager: Lorna Mein

About the Authors

EVA HOLLAND is an accomplished author, trainer, and co-founder of WatzThis?, a company aimed at teaching technical topics in a fun and approachable manner. In addition to being co-author of this book, Eva is the co-author JavaScript For Kids For Dummies and Coding with JavaScript For Dummies. She enjoys tennis, music, reading, and the outdoors.

CHRIS MINNICK is a prolific published author, trainer, web developer, and co-founder of WatzThis?. Chris loves sharing his knowledge with others and has trained thousands of adults and kids in computer programming. As an author, his published books include JavaScript For Kids For Dummies, Coding with JavaScript For Dummies, Beginning HTML5,CSS3 For Dummies, and Webkit For Dummies. Chris is an avid reader, writer, swimmer, and musician.

Authors’ Acknowledgments

This book is the result of a team effort, not only by your authors, but also by a talented crew of editors and other professionals who are credited on an earlier page.

Thank you to everyone at Wiley, including our Project Editor Charlotte Kughen, Acquisitions Editor Aaron Black, Copy Editor Kezia Endsley, Illustrator Sarah Wright, and Senior Editorial Assistant Cherie Case. A very special thank you to our Technical Editors Gavin Machado and Mike Machado. Thank you to Jay Silver for advising us in the initial stages of the book and for his role in the creation of Scratch.

Thank you also to our agent, Carole Jelen of Waterside Productions, as well as to our friends and families.

Adventures in Coding

Contents

Cover

Title Page

About the Authors

Introduction

What Is Scratch?

Who Should Read This Book?

What You Will Learn

How This Book Is Structured

What You Need to Use This Book

Conventions

Companion Website

Contact Us

Adventure 1: Scratching the Surface

Coding Is Everywhere

Speaking the Language of Machines

Knowing Your Coding Lingo

Writing Your First Scratch Program

Learning the Scratch Environment

Further Adventures in Coding

Adventure 2: Where in the World Is Scratch?

Setting the Stage

Understanding Coordinates on the Stage

Knowing Your Directions

Moving a Sprite

Creating Random Scratch Art

Further Adventures in Coding

Adventure 3: Using Control Blocks

Understanding Code Nesting

Branching Programs in Scratch

Making Comments in Scratch

Looping in Scratch

Building the Fly Catcher Game

Further Adventures in Coding

Adventure 4: Using Sensing Blocks

Learning the Sensing Blocks

Getting and Using Text Input

Detecting Key Presses

Watching for Mouse Moves

Using Timing

Sensing Touching and Distance

Building the Apple Patrol Game

Further Adventures in Coding

Adventure 5: Using Event Blocks

Understanding the Role of Events

Working with Key Press Events

Using Backdrop Change Events

Implementing Sensing and Timing Events

Getting Your Message Across

Putting on the Big Event

Further Adventures in Coding

Adventure 6: Variables and Lists

Understanding Variable Blocks

Making Lists

The Ultimate Party Host

Further Adventures in Coding

Adventure 7: Using Operators in Scratch

Saying Hello to the Operators

Doing the Math

Coding Logically

Operating on Text

Understanding Other Operations

Make a Math Practice Game

Further Adventures in Coding

Adventure 8: Using Graphics and the Pen

Creating Art with the Paint Editor

Use the Pen to Make a Skywriting Plane

Further Adventures in Coding

Adventure 9: Building Your Own Blocks

Making Your Own Blocks

Borrowing Blocks with the Backpack

Putting on a Fashion Show

Further Adventures in Coding

Adventure 10: Making and Using Sounds and Music

Using Sounds

The Sound Library

Using the Sound Editor

Editing Sounds

Recording Sounds

Importing Sounds

Forming the Scratch Jazz Band

Further Adventures in Coding

Adventure 11: Exploring the Scratch Universe

Visiting scratch.mit.edu

Sharing Your Projects

The Scratch Community Rules

Remixing Projects

Interfacing with the Real World

Sensing with the PicoBoard

Further Adventures in Coding

Appendix A: Installing the Scratch Offline Editor

Installing the Scratch Offline Editor on Windows

Installing the Scratch Offline Editor to Your Mac Operating System

Other Resources

Appendix B: Testing Your Programs

Practice Proper Planning

Ask Other People to Test

Look for Possible Invalid Input

Use Comments Often

Test Early and Often

Keep Track of Tests and What Breaks Things

Use Custom Blocks

Use Sliders for Numbers

Keep Learning

Keep Practicing

Glossary

End User License Agreement

Guide

Cover

Table of Contents

Begin Reading

Pages

iii

iv

v

vi

vii

viii

ix

x

xi

xii

xiii

xiv

1

2

3

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

263

264

265

266

267

268

269

270

271

272

273

274

275

277

278

279

280

281

282

283

285

286

287

Introduction

ARE YOU A fearless adventurer? Do you like to set sail on new ventures and learn new skills? Do you want to learn how to use technology to turn your ideas into reality? Are you curious about computer programming, but aren’t sure where to start? If your answer to these questions is a confident “yes!” then this book is for you!

Like people, computers can be talked to in a variety of languages. Computer programming, or coding, is a way for people to talk to computers. Many computer languages are similar to one another, so once you know one programming language, it’s much easier to learn another one.

Adventures in Coding introduces you to the world of programming, using Scratch.

What Is Scratch?

Scratch is for anyone new to coding. It’s a great place to begin your lifelong adventures in the world of computer programming. Scratch introduces programming concepts in a fun and approachable way. Using simple drag-and-drop features, you can create real computer programs while learning the fundamentals of coding.

Who Should Read This Book?

Adventures in Coding is a great starting point for any young person who is interested in learning how to create games, apps, and art on a computer.

What You Will Learn

Adventures in Coding introduces and guides you through the world of coding using Scratch. You learn the ins and outs of the Scratch universe—from learning about the features of the Project Editor, to connecting with fellow “Scratchers” and sharing projects.

Adventures in Coding teaches you how to create fun games, animate characters, build interactive projects, and more!

How This Book Is Structured

Each chapter of this book is an adventure of its own. With each adventure, you learn a new aspect of Scratch while building on what you already learned. Each adventure finishes with a completed project.

What You Need to Use This Book

The Scratch programming interface lives on the web. All you need to get started and complete the adventures in this book is a computer with a web browser (such as Chrome, Safari, Firefox, or Internet Explorer) and an Internet connection. No experience is expected or necessary. There is nothing you need to purchase or install. Scratch is always free, to anyone!

Conventions

Throughout the book, there are some special boxes to guide and support you. They use the following key:

These boxes explain new concepts or terms.

These boxes provide tips to make your life easier.

These boxes include important things to watch out for.

These boxes further explain the inner workings of programs.

These boxes provide explanations or additional information about the topic at hand.

These boxes point you to videos on the companion website.

You will also find two sets of sidebars in the book. Challenge sidebars give you extra suggestions for expanding on the projects in the book. Digging into the Code sidebars further explain how some of the more complex programs work.

Companion Website

To download the videos mentioned in this book, visit the companion website at www.wiley.com/go/adventuresincoding.

Contact Us

Your authors, Chris and Eva, would love to hear about your progress in coding. You can reach out to us with questions, or to show us a cool project you’ve created, by visiting www.watzthis.com or emailing us at [email protected].

PROGRAMMING COMPUTERS IS a lot of fun. It’s also a skill that many people see as mysterious and even magical. This chapter unmasks programming to show you just how easy it can be to start your own coding adventure.

Coding Is Everywhere

Computer programming, also known as coding, is how people tell computers what to do. What sorts of things can you do after you learn to program? For starters, you can write your own computer games, create modifications (or “mods”) for existing games, program robots to do your bidding, create beautiful computer art and animations, and instruct your computer to play songs! The best part is that the whole time you’re doing all these fun things, you’re learning a valuable skill that is in sky-high demand!

Coding is a common name for computer programming. When you code, you’re using a computer language to tell computers what to do.

Can you think of other things that computers can do? Think of all the things that programmers can tell computers to do. There are hundreds, or thousands, of things. Think about all the things you see computers do every day—and not just the fun things. Computer programs are used to create new medicines, design buildings, do complex mathematics, control cars, and so much more.

This is the amazing world in which computer programmers live; we get to solve interesting problems every day and do things that other people see as magic.

Speaking the Language of Machines

All sorts of different people are programmers. Programmers come from different places and countries, with different experiences and different training. They speak different languages, have many different interests, and program for different reasons. What they have in common is that they’ve learned to speak at least one language that is understood by computers.

A programmer is a person who writes computer programs.

Computers don’t speak the same languages that people do. People speak languages such as English, French, Spanish, Portuguese, Japanese, and many others. Computers speak machine language. Machine language is a difficult-to-read (for us) language that uses numbers to provide instructions to computers.

If machine language were the only way people could talk to computers, coding would be difficult. Fortunately, people have invented languages, called programming languages, which make it easier for people to talk to computers. Here are some examples of programming languages:

JavaScript

BASIC

Perl

PHP

Python

Java

Visual Basic

C

C++

Scratch

These languages all have one thing in common: They take words and symbols that people understand and translate them into words and symbols that computers understand.

A programming language is a language used for giving instructions to computers.

The examples in this book use Scratch. Scratch is a language that was invented at the Massachusetts Institute of Technology (MIT). It was designed to be easy for beginners to learn while using (and teaching) all of the most important things that programmers need to know.

Knowing Your Coding Lingo

You already know some of the lingo of coding. You know that “coding” is just another name for “computer programming,” and you know that people who do computer programming—or coding—are called computer programmers (or coders).

Programming languages, like human languages, are made up of different parts. In English, we have nouns, verbs, adjectives, pronouns, and other parts of speech, not to mention punctuation, and they form sentences and paragraphs. In programming languages, you combine different statements (also known as commands) to make computer programs, which are also known as applications (or apps).

A command is an instruction, written in a programming language, that tells a computer to do a task.

An application is a set of programming commands that follow each other in a particular order to accomplish tasks. Application is another name for a computer program.

Scratch, and certain other programming languages, use the term script. Script is just another name for a program.

A script is another term for a computer program that is smaller and more limited than an application.

There are a lot of specialized words in coding that all have very specific meanings, and you’ll find that sometimes there are many different words for the same thing. For this reason, we’ve included a glossary at the back of this book where you can look up or remind yourself of the meaning of terms you’re not familiar with.

One of the greatest things about Scratch is that it’s easy to dive right into! To get started, you don’t need to learn a lot of new concepts or vocabulary. So enough talk! Let’s begin!

Writing Your First Scratch Program

When we were growing up, kids didn’t care about learning a specific style of dance. Instead of trying to learn complicated dance moves like the cha-cha or the hustle, we ran around like lunatics, jumping off of things, and we sometimes got hurt in the process. Your first Scratch program will be a simulator of an old-fashioned punk rock mosh pit.

Figure 1-1 shows what the finished product will look like. If you imagine the two characters in the figure bouncing off the walls and off each other while a drumbeat plays, you’ll have a good idea of the program you’ll be learning to make.

Figure 1-1 Your first Scratch program

Joining Scratch

In order to create, save, and share your programs on the Scratch website, you need to use your favorite web browser to visit http://scratch.mit.edu, where you can sign up for a free account. When you go to the website, you see a screen that looks similar to Figure 1-2.

Figure 1-2 The Scratch website

Follow these steps to create your free account:

Click the Join Scratch link in the upper-right corner or center of the screen.

The Join Scratch window opens.

Type a username into the field labeled Choose a Scratch Username.

Your username is how Scratch will know you and how other users will see you when you start to share programs. Be creative! Choosing a username can be fun! For everyone’s safety, a good username shouldn’t reveal any personal information, such as your full name, age, gender, or address. Try personalizing your username by including the name of your favorite sports team or musical group.

Choose a password and enter it into the Password and Confirm Password fields.

When creating your password, avoid using information that other people are likely to know, such as your address or birthday. Your password should be something you can remember, but it should also contain numbers or punctuation to make it more secure.

Click Next.

You see the second screen of the signup form.

Enter your birthday, gender, and country and then click Next.

When you’re asked for an email address, enter your email address in both the Email Address and Confirm Email Address fields and click Next.

Scratch sends a confirmation email to the address you provided.

Click the OK Let’s Go! button.

Check your email. When you get the email from Scratch, click the link in it to confirm your account.

Now you’re ready to go. The next section tells you how to start coding!

Meeting Scratch the Cat

After you’ve joined Scratch and you’re ready to start coding, click the Create tab in the top menu of the screen. When the new page loads, you see the scratch Project Editor, which looks like Figure 1-3.

Figure 1-3 The Scratch Project Editor

Don’t worry too much about what the things on this screen do. We’ll be talking in detail about each part later. For now, let’s build something!

See that cool cat in the middle of the screen? Her name is Scratch the Cat. Every new Scratch program starts with her sitting right there, waiting for instructions from you. The area where she lives is called the Stage. This is where all the action of your program takes place.

Below the Stage is an area called the Sprite Pane. The Sprite Pane shows small images of each of the characters (also known as sprites) in your program.

To the right of the Stage is a rectangle containing differently shaped blocks. This is called the Block Palette. Think of it like a painter’s palette, where a painter selects the colors that she wants to paint with before combining those colors to make a painting on a canvas.

To the right of Block Palette is the Scripts Area. This is your canvas, where you put together the blocks selected from the Block Palette to make your sprites do things.

Moving Scratch Around

To get a better idea of how all the pieces fit together, follow these steps to make Scratch the Cat do something:

Find the block that says

move 10 steps

. This is the

move ()

block. It looks like

Figure 1-4

.

Click the

move ()

block and drag it into the Scripts Area. Your Scripts Area should now look like

Figure 1-5

.

Double-click the

move ()

block and watch what Scratch the Cat does. Did you see it? She moved slightly to the right.

Click the number 10 inside the

move

block to highlight it and change the 10 to 20.

Double-click the block. Now Scratch moves twice as far as she did before.

Try changing the value in the

move ()

block to an even larger number and see what happens.

Figure 1-4 The move block

Figure 1-5 The Scripts Area, with one block

Connecting Blocks

Notice that the move block looks a bit like a puzzle piece. It works a bit like a puzzle piece too! Any time you see a block shaped like this, you know that you can attach it to another block.

Follow these steps to put some blocks together to make Scratch the Cat do something more complicated:

Find the turn clockwise block in the Block Palette. Figure 1-6 shows what the block looks like.

Notice that it has the same shape as the move block.

Drag the

turn clockwise

block to the Stage and snap it to the bottom of the

move

block. When the blocks are connected, they look as shown in

Figure 1-7

.

Figure 1-6 The turn clockwise block

Figure 1-7 Your first connected blocks

Double-click the combination of blocks and watch what happens on the Stage. The instructions on the first block happen, and then the instructions on the second block happen!

By right-clicking the combination of blocks and selecting Duplicate, from the menu you can create an exact copy of the block combination. Try it out! Then snap the two combinations of blocks together so that they look like Figure 1-8.

Figure 1-8 Duplicating blocks

Now click the whole thing and watch what Scratch the Cat does.

Looping Movements

If you want to make Scratch the Cat do this turning and moving thing over and over, you could keep making as many duplicates as you want of these same blocks, or you could create what’s called a loop.

A loop is a block that causes the commands contained within it to repeat one or more times.

Use the following steps to create a loop:

Separate the second two blocks from the first two by clicking the second

move

block and dragging downward.

Right-click the second set of blocks and then select Delete from the context menu to remove the block set from the Stage.

Click the word Control in the color-coded menu in the Block Palette. Then find the

forever

block (see

Figure 1-9

) inside the Control Block Palette.

Drag the

forever

block to the Stage and place it over the

move

and

turn

blocks so that it snaps around them, as shown in

Figure 1-10

.

When snapping blocks together and inside of each other, be sure they’re completely fitted into place and are not overlapping; otherwise, your script will not run.

Double-click the combination of blocks and watch what happens!

Scratch the Cat keeps moving and turning forever … or until you click the red Stop Sign shown in Figure 1-11.

Click the Stop Sign to end the loop.

There are other ways to stop a loop. You learn all those tricks in Adventure 3, “Using Control Blocks”!

Figure 1-9 The forever block

Figure 1-10 Wrapping the forever block around other blocks

Figure 1-11 The Stop Sign

Starting at the Green Flag

Next to the Stop Sign, you see a Green Flag. This is also known as the Run button. You can press it to start all the action in a program, rather than double-clicking on the blocks.

To enable the Green Flag for your program, follow these steps:

In the Block Palette, click Events, as shown in

Figure 1-12

.

Remember that in Scratch, a script is a series of connected commands that cause a sprite to do some task.

Drag the

when green flag clicked

block to the Scripts Area and snap it onto the top of your existing script. Your script should now look like

Figure 1-13

.

Click the Green Flag above the Stage to see your program run.

Scratch the Cat starts running in circles.

When you’re done watching Scratch the Cat run in circles, click the Stop Sign.

Figure 1-12 Selecting the Events Block Palette

Figure 1-13 Adding the Green Flag event to your program

Bouncing Off the Walls

Moving in circles is cool, but it’s time to add some new instructions so Scratch can roam a bit more. Use the following steps to tell Scratch to do more things:

Click the

turn clockwise

block and drag it out of the Scripts Area and back into the Block Palette. It disappears from your script, which now looks like

Figure 1-14

.

Click the Green Flag to run the program.

Scratch runs right off the screen and keeps on running forever!

Click the Stop Sign.

Click Motion in the Block Palette and drag the

if on edge, bounce

block to the Stage. Snap it underneath the

move

block, as shown in

Figure 1-15

.

Click the Green Flag to see what Scratch does now.

She runs around, bouncing off the walls, until you click the Stop Sign.

Figure 1-14 The program after you’ve removed the turn clockwise block

Figure 1-15 Adding an if on edge, bounce block

That’s more like it! You’re getting closer to having a mosh pit, but it’s not really a proper mosh pit unless there are other dancers. In the next section, you add a second character to the dance floor.

Creating a Sprite

Look for the Sprite Pane at the bottom of the screen. At the top of it, you see New Sprite: and then some icons. Scratch has numerous built-in characters besides Scratch the Cat that you can use in your programs. You can also create your own characters by uploading a graphic or even by taking a picture!

Click the Choose Sprite from Library icon, which is the first icon to the right of New Sprite.

Browse through the Sprite Library and find a sprite you like.

When you find the sprite you want to use, click it, and then click the OK button at the bottom of the Sprite Library.

Your new sprite is added to the Stage and to the Sprite Pane.

After you add your second sprite, you see that the Scripts Area becomes blank. That’s because each sprite has its own Scripts Area that is visible when that sprite is selected. Because you just added this sprite, it doesn’t yet have any scripts tied to it.

Press the Green Flag. You see that your first sprite bounces around the screen while your new one just sits there.

To animate your new sprite, follow these steps:

In the Sprite Pane, click Scratch the Cat.

You should now see her script appear in the Scripts Pane.

Click the first block in the Scripts Area (the

when green flag clicked

block) and drag it to the Sprite Pane, dropping it on top of your new sprite.

Click on your second sprite in the Sprite Pane.

You see that the scripts from the first sprite have been copied over to the new sprite!

Click the Green Flag.

Now, both characters are bouncing around like maniacs. Fun!

Handling Collisions on the Dance Floor

You may have noticed a strange thing about how your two sprites interact. In real dancing, when two people bump into each other, a collision happens and they bounce away from each other. It’s time to make these sprites collide and bounce!

Select one of your sprites in the Sprite Pane. Because they have the same script now, it doesn’t matter which sprite you choose. You’re going to add the collision script to one of the sprites and then copy it to the other.

Click the Control Block Palette to open it.

Look for the

if … then

block. It looks like

Figure 1-16

.

Drag the

if … then

block to the Stage so that it’s snapped to the bottom of the

if on edge, bounce

block, as shown in

Figure 1-17

.

Figure 1-16 The if … then block

Figure 1-17 Adding an if … then block to the script

Next, you’re going to use a new type of block—a Sensing block—to detect whether the sprites are touching.

Click the Sensing Block Palette.

Drag the touching () block into the hexagon-shaped empty spot in the if … then block.

The empty spot expands to fit the touching block, and the new block snaps into place, as shown in Figure 1-18.

Notice that the touching () block has a drop-down menu inside of it.

Click the drop-down menu inside the touching () block and select the name of the other character on the Stage.

Now, whenever this sprite is touching the other sprite, it does the actions inside of the if … then block.

Click the Motion Block Palette to open it.

Drag the

turn clockwise

block from the Motion Block Palette and snap it inside of the

if … then

block.

Drag the

move

block to the Scripts Area and snap it to the bottom of the

turn clockwise

block.