beginners-step-by-step-coding-course-learn-computer-programming-the-easy-way

Page 40

Managing program flow When writing code, programmers not only have to tell the computer what to do, but also when to do it. In Scratch, the Control and Events blocks are used to manage when an instruction is carried out. Event-driven programming In event-driven programming, the program’s actions are started by events, such as user input, sensor input (see pp.46–47), or messages sent by other programs or parts of the program. The Events section of the Blocks Palette contains blocks that can start scripts when something happens. Also called hat blocks due to their shape, these Events blocks provide many more ways to start scripts than simply clicking on them.

when this sprite clicked

when loudness

Use mouse click to start This block starts the attached script when the sprite is clicked. It is ideal for creating on-screen buttons for users to click on.

when space

key pressed

when

clicked

Use green flag to start Provides an easy way for users to start the program. Copies of this block can be used to start multiple scripts simultaneously.

when I receive message1

> 10

Use sounds to start When the microphone detects a volume more than 10 (on a scale of 0 to 100), the script can be activated.

Use message to start Scripts can send messages to each other (see pp.48–49). This block starts a script when a particular message is received.

when backdrop switches to backdrop 1

Use key press to start Starts a script when a key is pressed. The menu in the block can be used to select the required key.

Use background to start This block is particularly useful in story-based projects. It enables scripts to start when the scene (or background image) changes.

Making a clickable drum This example uses an Events block to make a simple clickable drum. When the drum is clicked or tapped on the Stage, the script plays a sound and briefly changes the image (or costume) to show that it is playing. The blue highlight indicates this is the selected sprite

the Drum Kit sprite 1 Add Hover over the Choose a Sprite icon in the Sprite List and select the magnifying glass to see the library. Sprites are listed alphabetically. Scroll down to find the Drum Kit sprite and click or tap on it to add it.

Add the script Select the Code tab and add the following script to the Drum Kit sprite. The sprite already has the required sound. Click the sprite on the Stage to hear it play, and see its playing costume for half a second.

2

when this sprite clicked

This costume indicates that the Drum Kit is playing

switch costume to drum-kit-b start sound drum bass1 wait 0.5 seconds Drum Kit

switch costume to drum-kit

Reverts to the original costume


Turn static files into dynamic content formats.

Create a flipbook

Articles inside

Other programming languages

5min
pages 344-347

PROJECT: Responsive website

52min
pages 304-343

ACKNOWLEDGMENTS

1min
page 360

INDEX

26min
pages 352-359

GLOSSARY

16min
pages 348-351

Graphic user interfaces

2min
pages 286-287

Libraries and frameworks

2min
pages 284-285

Object-oriented JavaScript

2min
pages 282-283

JavaScript debugging

3min
pages 280-281

Functions in JavaScript

2min
pages 278-279

Input and output

3min
pages 272-273

Loops in JavaScript

5min
pages 274-277

Logic and branching

2min
pages 270-271

Variables and data types

6min
pages 266-269

PROJECT: Styling the web

29min
pages 242-263

Code editors

4min
pages 208-209

Build a better website

4min
pages 214-215

Responsive layouts

2min
pages 240-241

HTML forms and hyperlinks

3min
pages 212-213

How the Web works

3min
pages 206-207

Exploring basic HTML

4min
pages 210-211

Pygame Zero

2min
pages 176-177

PROJECT: Budget manager

20min
pages 158-175

Logic operators and branching

3min
pages 104-105

Objects and classes

2min
pages 156-157

PROJECT: Team allocator

14min
pages 118-129

Libraries

3min
pages 116-117

What is Python?

3min
pages 94-95

Loops in Python

5min
pages 108-111

Installing Python

3min
pages 96-97

Using functions

3min
pages 50-51

Sending messages

3min
pages 48-49

Input

3min
pages 46-47

Logic and decisions

3min
pages 44-45

Managing program flow

4min
pages 40-41

Manipulating data

3min
pages 42-43

Output using movement

4min
pages 36-37

Output using looks and sounds

4min
pages 38-39

Coloured blocks and scripts

3min
pages 34-35

FOREWORD

2min
pages 10-11

What is programming?

3min
pages 16-17

Programming languages

6min
pages 22-27

What is Scratch?

2min
pages 28-29

About this book

3min
pages 12-15

Working as a software developer

2min
pages 20-21

Coding in the real world

1min
pages 18-19
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.
beginners-step-by-step-coding-course-learn-computer-programming-the-easy-way by Jayhshahzad24 - Issuu