An Introduction To Programming Concepts With Scratch

Transcription

An introduction toprogramming concepts withScratch(in 5 hours homework)1

Who computes? ScientistsEngineersBusinessmenSocial scientistsArtistsFBI agentsBrain surgeonsGamersGrandparentsEveryone usesinformation insome way.Computers doinformationprocessing.2

What can we do with Scratch? Learn some computing concepts.Learn some practical algorithms.Use Scratch as computing tool.Have fun with Scratch creating stories, games,art.3

Part I: Looks and Motion Goals:–––Learn Scratch programming environmentLearn the looks and motion menusWrite a sequence of instructions4

Start scratch and let’s go! Click on the cat icon Scratch programmingenvironment comes up We will first do simple things5

Our Scratch environment Stage is at upper right (where actors act andinformation is displayed) Sprite is another name for actor. Instruction menus/panel at left (instructions forthe actors) A script is another name for program or method;a script tells the actor what to do. Programming area in center; here is where weconstruct scripts for the sprites6

We start with the cat spriteWe will learn to use many othersprites later. For now we use thecat as our only actor. We script theBEHAVIOR of our cat.7

The LOOKS menuHas instructions for setting the color,size, and visibility of a sprite.Costumes will be used later.8

The “hello” script (program) Choose the Looks menuClick on the “say hello” lego blockCheck your sprite behavior at the rightThen click “say hello for 2 secs”Your very first Scratch program!9

Try some other looks operations(click on menu items) Change color effect by 25Change color effect by 25 againHideShowChange size by 10Change size by 10 againSet size to 100%10

Let’s write a script to Say “hello” for 2 secondsThen change color by 25Then think “Hmm.” for 4 secondsThen change color by 75Then change size by 200Drag each instruction from the menu to the centerscript area. Connect them into a single block. Editthe parameters to get the numbers we want.11

Our script (program)12

A sequence or block is a simplescript or program The first instruction is done firstThe second instruction is done secondThe last instruction is done last.(if any one instruction is done, then every onof them is done)13

The WAIT instruction WAIT is needed to slow down the acting sowe can see or hear it properly (computersare too fast sometimes) Get the wait instruction from the CONTROLmenu. Ignore the other menu options fornow. Insert a wait in our looks script14

3 second pause between changingcolor and size15

Student exercise: write a script todo the following Double the size of the spriteWait 2 secondsChange the color of the sprite to greenWait 4 secondsChange the whirl effect to 100Say “That’s all folks!”16

The MOTION menuHow to locate and orient a sprite;moving a sprite.17

Position on the stage Using the Looksmenu, shrink ourcat to 25%. Click on theMotion menu. Click to check thebox for x-positionand y-position Drag your cataround and noteits x-y position.18

Exercises: goto instruction In the Motion menu, drag the “goto xy”instruction to the script panel. Edit the coordinates and click to see thesprite’s position A) goto x 200, y 0Where B) goto x -200, y 0does the C) goto x 200, y -100cat go?19

The “glide-to” instruction Drag the “glide-to” instruction into yourscript panel. Edit the coordinate values and click tosee where your sprites goes.20

Exercise Create a script to glide the sprite along thesides of a triangle. The first vertex of thetriangle is (-100, -100). The second vertex is(200, -100). The third vertex is (50, 100). Makesure you complete the triangle. Change the speed of gliding and run again.21

Angles and directionsMaking your sprite gothis way and that way22

Exercise Script Start at ( -100, 100 )Move 200 stepsTurn right 90 degreesMove 200 stepsTurn right 90 degreesMove 200 stepsTurn right 90 degreesMove 200 stepsUse waits ifneeded to makethe motion looksmooth23

Exercise/Homework Script Sprite starts at home base at x -100; y -50Sprite says “I hit the ball” for 2 secondsSprite runs (east) 200 steps to 1st baseSprite changes colorSprite runs 200 steps to 2nd base (north: left turnfrom 1st base) Sprite doubles size Sprite says “I hit a double” for 5 seconds24

End of Part I: OutcomesStudent should be able to control the looks of a spriteand its location on the stage. Students should beable to construct sequences of instructionscontrolling looks and location on the stage.25

Part 2: Repetition and Variation Goals:–––Learn how to program with repetitionLearn how to change costumesLearn to controll execution (behavior)using special keys26

The sneezing cat script We repeat 20 timesSay “AAAHHHH”for a short timeThen grow thecat 5 bigger27

Exercise: bring the cat sizedown to normalRepeat 20 timesBriefly say“CCHHEEEW”Then shrink thecat’s size by 5Repeat N timesand repeatforever are in theControl menu28

Exercise: Have the cat do a flip byrotating 20 degrees 18 times. Repeat from the Control menu: set thenumber of repetitions to 18 Rotate from the Motion menu: set thesize of each small rotation to 20 degrees29

Changing costumesAnother way to change theappearance of a sprite.30

Making a new costumeto animate the bat1) Get bat2-a sprite fromthe “sprite box”.2) Click on sprite3) Click on “Costumes”4) Click on “Import”5) Now get bat-2b spritefrom the sprite box31

Use a loop to make the bat fly!1) When and repeat from Control menu and 2) next costume fromLooks menu.32

Changing coordinates We can randomly set the location of the batso it will “flutter”. Set X to a random number Set Y to a random number Move the bat to location (X,Y) Of course, the bat should remain on stage!33

Using pick randomClick the stop sign at the stage upper right to stop the forever loop.Do we need a wait in the loop? Try it to see the change in flying.34

Controlling the bat’s direction Let’s create multiple scripts.Click space bar for random movesClick right arrow to move rightClick left arrow to move leftThe bat will behave differently dependingupon which key is typed! (So, could a gamer catch the bat?)35

Multiple interactive bat flight3 differentbehaviorsfor the bat36

Adding a background Click on theStage icon atlower right Click onBackgrounds Click on bat icon(Sprite1) Click on Scripts FLY AGAIN!37

Homework/Exercise: Add a behavior (script) to move the bat upwith the up arrow. Add a behavior to move the bat down withthe down arrow.38

End of Part 2: outcomesStudent should be able to write a programcontrolled by a loop, and execute a script byclicking special keys. The student should alsoknow how to use “costumes” to change theappearance of a sprite.39

Part 3: Input/Output, Variables,Simple computing Goals:–––Learn how the user and the program cancommunicateLearn how to compute with formulasComputing an average, perimeter of ageometric figure, etc40

The SENSING menuGetting information from the useror some other machine41

Asking the age of the user Program wants age of user (maybe toset a level of difficulty in a game) Program asks user for age User types in age Program stores the answer in a variablenamed “answer”42

What is your age? Depends on who you areFor one person it’s 12For another person it’s 27For yet another, it’s 19IT’S VARIABLE!43

Use the SENSING menu1) Ask the user for age; 2) user types in age;3) result stored in “answer”44

User types “13” and the scriptstores the answer“answer”is aSpecialScratchvariable45

OUTPUT: Giving the userinformation with say answerSay is in Looks menu;drag “answer” fromSensing menu; click46

Using the Variables menuWe can save an answer in our ownnamed variable;Then we can program with many dataitems using many variables47

Making an “age variable”1) Click Variables menu; 2) click “Make a variable”2) Type in “age” and click OKDo you want todisplay for allsprites or just one?48

We have an “age variable”Operations for “age”Displaying variable “age”49

We can join text and a number tosay thingsFrom LooksFrom OperatorsFrom SensingMake the joinBuild this instruction fromthe parts above it.50

Storing a value in a variable; thensaying it to the user1) Set from Variables menu; 2) Say from Looks menu; 3) Join from theOperators menu joins your text with the “answer”51

Using variables in programs A script might have to remember informationHow far is the cat from the dog?How fast is the rocket moving?Where did the user click on the stage?What is my score in the game?What is the user’s skill level?52

Computing C A B (1 of 3) Make 3 variables, A, B, and C Type in the values of A and B Set the value of C to the sum of A and B53

Computing C A B (2 of 3) Make 3 variables, A, B, and C Type in the values of A and B Set the value of C to the sum of A and B54

Computing C A B (3 of 3) Make 3 variables, A, B, and C Type in the values of A and B Set the value of C to the sum of A and BDrag operatorfrom Operators menuFrom Variables menudrag variables A and Binto the slots55

Algorithm for computing the averageof two numbers Let the first number be A; say A 12And the second number be B; say B 15To compute the averageSo the average of 12 and 15 56

Programming the average A is the first “variable”: A B is the second “variable”: B V is the third variable: V Take the value of A, add the value of B, dividethis sum by 2, then set the value of V to thisanswer. V (A B)/2 (how to do it in Scratch?)57

Script from Variables andOperators menusDrag the variable names into the slots of the operatorsScript notyetexecutedA formula from theOperators menu58

After clicking to execute scriptThe variable V now stores the computed average of A and B59

Exercise Put a Wait 5 secs in the script between thetwo set operations for variable V Execute the script and observe the changes inthe value of V60

ExerciseChange the values of A and B by editing thescript (keep the Wait);Input the age of grandchild and age ofgrandparent;Click on your script to compute the average ofthe new A and B61

Homework/Exercise:average program Modify the average programAsk the user for a value of AAsk the user for a value of BCompute V as the averageReport the average to the userRun the program for different As and Bs62

HW Exercises Average 3 numbers; say 12, 33, and 2163

Computing perimeter of arectangle: algorithm Ask the user for length LAsk the user for width WCompute P (L W)*2Report P to the userW 10exampleL 15P (15 10)*2 5064

Review:Getting length from userDrag from Sensing menu Ask “What’s the length” and wait Set length to answerCreate this variableDrag from sensingmenu65

Review: Reporting the value of avariable to the userLooks menuYou type this in Say join “length “ length for 2 secOperators menuDrag length variablefrom Variables menu66

Review: Program with user inputand output67

HW Exercise: create the Scratchprogram we designed Ask the user for length LAsk the user for width WCompute P (L W)*2Report P to the userShow your program to your Grandparent.68

End of Part 3: outcomesStudent should be able to write a program toask the user for data, compute using thedata, and report the answer to the user.Students should be able to compute usingcommon formulas.69

Part 4: working with soundsand sprite communication

Part 4 goalsLearn how sprites can communicateby sending messagesLearn how to play sounds and toinput your own voice

Communicating sprites: takingturns by message passingGet cat and dog spritesSuppose cat speaks firstWhen finished, the cat can send a message to the dogto speakWhen the dog is finished, the dog can send a messageto the cat to speak

First behavior for the catAfter saying hello, cat sends amessage to dog (actually to allsprites)

Dog is activated by hidog1messageDog send message back to cat(and any other sprites)

Cat’s turn again after hicat2message from the dog

Dog can have 2nd touchingbehavior

Sprites can have many behaviorsActivated by clicking green flagActivated by clicking the spriteActivated by a key being pressedActivated by a message from another spriteActivated by touching another sprite

Using soundsSound library;Speaking in the mike;Importing music

Composing with drum beats

Jodi jumps on the drumStory teller scriptJodi’s script

Recording from a mike (or an iPodor other music player)1. Plug in microphone2. Pick the speaker sprite3. Click “Sounds”4. Click “Record”5. Click red button and speak6. Click square button to stop7. Click triangle to hear it8. Click “OK” if it’s OK9. Use name of sound in a play-soundinstruction

The Friends Story (play it)

Variations in storiesHave several scenesScenes change with messages or variablesMake characters moveMake characters hide or showBehavior can change with variables, touching,mousing.Background music for each scene

Learning from examples1.2.3.4.06/02/10Click on the FILE tabClick on the OPEN tabClick on the Examples tabChoose an example: for example,try STORIES and MAD LIB

End of Part 4: outcomesStudents should be able to programby passing messages. Studentsshould be able to program theirsprites to make sounds.

Jun 02, 2010 · 6 Our Scratch environment Stage is at upper right (where actors act and information is displayed) Sprite is another name for actor. Instruction menus/panel at left (instructions for the actors) A script is another name fo