INTRODUCTION TO THE SCRATCH PROGRAMMING ENVIRONMENT.

26
INTRODUCTION TO THE SCRATCH PROGRAMMING ENVIRONMENT

Transcript of INTRODUCTION TO THE SCRATCH PROGRAMMING ENVIRONMENT.

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

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

Block Shapes

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

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

Motion Blocks

Looks Blocks

Sounds Blocks

Pen Blocks

Control Blocks

Sensing Blocks

Operator Blocks

Variable Blocks

A Simple Manual Program

Click on the block to make the sprite walk 10 steps

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.

Add some control

Question: How many steps will Sprite1 take?

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

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)

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

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

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

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

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?

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)

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

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