Introduction to the Scratch Programming Environment

26
INTRODUCTION TO THE SCRATCH PROGRAMMING ENVIRONMENT

description

Introduction to the Scratch Programming Environment. What is Scratch?. Developed by the “life-long kindergarten” group at MIT Simple, “code-free” style that lets you write programs by visually combining ‘blocks’ Built-in graphics and sound Share Projects online - PowerPoint PPT Presentation

Transcript of Introduction to the Scratch Programming Environment

Page 1: Introduction to the Scratch Programming Environment

INTRODUCTION TO THE SCRATCH PROGRAMMING ENVIRONMENT

Page 2: Introduction to the Scratch Programming Environment

What is Scratch? Developed by the “life-long

kindergarten” group at MIT Simple, “code-free” style that lets you

write programs by visually combining ‘blocks’

Built-in graphics and sound Share Projects online Made for beginners, but ‘expert-level’

programs are possible

Page 3: Introduction to the Scratch Programming Environment

Main Scratch Window

Performance Area = Stage (background) + Sprites (characters)Pick a Sprite to

program

Scripts tab: add commands for Sprite1’s actions

Script Inventory (block palette): drag new commands from here to the scripts tab

Block Types (more to follow)

Start (optional) and Stop Buttons

Page 4: Introduction to the Scratch Programming Environment

Block Shapes

Trigger: Statement Ending Statement Boolean (true/false) value Numeric or Text value

Page 5: Introduction to the Scratch Programming Environment

Block Types Blocks are sorted into categories:

Motion: move a sprite Looks: change costume/color Sound: play instrument, song Pen: draw lines on stage Control: have sprite make ‘decisions’ Sensing: See/hear/touch objects Operators: mathematics and text Variables: give sprites memory

Page 6: Introduction to the Scratch Programming Environment

Motion Blocks

Page 7: Introduction to the Scratch Programming Environment

Looks Blocks

Page 8: Introduction to the Scratch Programming Environment

Sounds Blocks

Page 9: Introduction to the Scratch Programming Environment

Pen Blocks

Page 10: Introduction to the Scratch Programming Environment

Control Blocks

Page 11: Introduction to the Scratch Programming Environment

Sensing Blocks

Page 12: Introduction to the Scratch Programming Environment

Operator Blocks

Page 13: Introduction to the Scratch Programming Environment

Variable Blocks

Page 14: Introduction to the Scratch Programming Environment

A Simple Manual Program

Click on the block to make the sprite walk 10 steps

Page 15: Introduction to the Scratch Programming Environment

The GO Button

This trigger block will make any attached script run when the green flag is clicked.

This is a good way to start multiple scripts at the same time.

Page 16: Introduction to the Scratch Programming Environment

Add some control

Question: How many steps will Sprite1 take?

Page 17: Introduction to the Scratch Programming Environment

About Loops Repeat loop: simplest type

Whatever is inside, do that same thing as many times as it says in the top of the block

Equivalent (but tedious!) solution: Copy/paste ‘move 10 steps’

30 times Not feasible if you want to

repeat something 1000 times We’ll learn about the other

two loop types later: Forever Repeat until

Page 18: Introduction to the Scratch Programming Environment

Stuck!

Our Sprite walked as far as he’s allowed and is now stuck!Here are some possible solutions: • Click on the sprite and move him back to the center (MANUAL

strategy)• Some sprites can walk off and disappear!

• Add a script that walks backward 300 steps (UNDO strategy)• Use Motion blocks to ‘transport’ Sprite1 to a fixed location

(RESET strategy)

Page 19: Introduction to the Scratch Programming Environment

Motion Explained The stage is set on an X-Y plane ‘Move 10 steps’ moves about

10 steps on the grid But you can turn to move in

different directions ‘Go to x: 0 y: 0’ puts Sprite1 in

the center of screen Lower-right shows coordinates

of mouse pointer Xposition, Yposition: current

location of a sprite Can be used for boolean testing,

or for drawing shapes in certain locations

X

Y

Page 20: Introduction to the Scratch Programming Environment

What’s Going On? Scratch tends to ‘go with the flow’

Whatever script you run, Scratch will run it as best as it can

After the script runs, the effects remain Can lead to unintended outcomes

This is actually kind of annoying Testing must always have the same initial situation Games always start in level 1

We’re used to having a ‘reset’ button when our programs develop problems Returns us to the initial error-free configuration

Scratch doesn’t have one, but we can make one

Page 21: Introduction to the Scratch Programming Environment

Green-Flag Reset

Reset Script: return to whatever initial situation you want• Sprite Positions (motion)• Pen Markings• Variable Values (more later)

Green-Flag Reset: • Upside: Green flag acts as

both ‘start’ and ‘reset’ button

• Downsides: • Other green-flag scripts

will start running right away

• Human must force reset

Page 22: Introduction to the Scratch Programming Environment

Keyboard Reset

Keyboard Reset: run reset script when a certain key is pressed (can choose any key)• Upside: Good when testing non-

green flag scripts (so the other green flag scripts don’t run)

• Downside: A human is still in charge of re-setting

Page 23: Introduction to the Scratch Programming Environment

Subroutines Subroutine:

A script that can be run by itself to some useful effect Can be executed independently by the computer or

initiated by a human Re-use Property: the more a subroutine can/will be re-

used, the more valuable it is When combined with abstraction, subroutines make

programs feel simpler Real-world examples:

Cooking: “Separate the yolks from the whites” Driving: “Take Exit 9” Others?

Page 24: Introduction to the Scratch Programming Environment

Reset Subroutine

Subroutines have a: •Definition: The actual subroutine script, starting with a ‘When I receive <name>’ trigger block•Call: Subroutines do nothing unless called upon to execute, via a ‘broadcast <name> (and wait)’ command•Calls can be attached to human input (above) or computer control (next slide)

Page 25: Introduction to the Scratch Programming Environment

Computer Controlled Subroutine Modified walking

script: Repeating 100

times, Move 10 steps If I reach the edge

of the stage: Call the reset

subroutine After I’ve done the

above 100 times, call the reset subroutine one more time

Page 26: Introduction to the Scratch Programming Environment

Summary Scratch: Easy to get started

programming, no experience required Different block types: allow us to modify

a Sprite’s behavior in different ways Script: One or more blocks fastened

together Complex combinations lead to complex

behavior! Loops: Abstraction for repetitive

processes Subroutines: Allow fast re-use of a script

by humans or the computer