Back to homeDigital Culture · High School Year 1 · Change subject
Start for free
Prep 1 · Digital Culture
Digital content creation · Simple creation and programming

Block-Based Programming

Ximena wants to make a game where a kitten dodges balls, but she doesn't know how to write code. In a block-based program she doesn't need to: you snap pieces together, like a puzzle, to tell the computer what to do, in what order, when to repeat something, and when to choose between two paths. At the end of the page you'll see how to fix the most common bugs in this kind of game.

Las tres estructuras básicas

Diagram of the three basic programming structures: sequence of three stacked blocks, loop with a group of blocks that repeats, and conditional with two paths, if and else. sequence repeat loop if / else conditional
Practice★☆☆Not started
Explain in your own words what the three basic structures of a program do — sequence, loop, and conditional — and give an everyday example of each that is not related to…

Explain in your own words what the three basic structures of a program do — sequence, loop, and conditional — and give an everyday example of each that is not related to computers.

Solve
Practice★☆☆Not started
A program has these standalone blocks: "say Goodbye!", "when green flag clicked", "wait a moment", "say Hello!". Arrange them in the correct stack and describe exactly…

A program has these standalone blocks: "say Goodbye!", "when green flag clicked", "wait a moment", "say Hello!". Arrange them in the correct stack and describe exactly what will happen when running the program.

Solve
Practice★☆☆Not started
These looks blocks are disconnected: "switch costume to pirate", "say Hello, my name is Marco! for 2 seconds", "when green flag clicked", "wait 1 second". Put them in…

These looks blocks are disconnected: "switch costume to pirate", "say Hello, my name is Marco! for 2 seconds", "when green flag clicked", "wait 1 second". Put them in order to introduce a character who greets first and then dresses up, and describe what the viewer will see.

Solve
Practice★☆☆Not started
A program has this stack: "when green flag clicked", "say 3", "wait a moment", "say 2", "wait a moment", "say 1", "say Go!". Describe in detail what the user will see…

A program has this stack: "when green flag clicked", "say 3", "wait a moment", "say 2", "wait a moment", "say 1", "say Go!". Describe in detail what the user will see and hear from pressing the green flag until the end, and explain the purpose of the wait blocks.

Solve

Eventos, apariencia, sonido y escenario

Apariencia y sonido

Looks blocks change what you see: "say Hello! for 2 seconds," "switch costume to pirate," "next costume," "show," "hide." Sound blocks change what you hear: "start sound meow" and "play sound meow until done."

A sound block is just another instruction in the stack: it plays at the moment its turn comes. If you put it right after "when green flag clicked," it will play at the start; if you put it inside an "if," it will play only when the condition is true. To make the sound happen when one sprite reaches another, put it inside "if then," and place that check inside a "forever" block so it's checked all the time.

Mover, tocar el borde y rebotar

The block "move 10 steps" moves the sprite 10 units in the direction it's facing. It runs only once, so to make the sprite move continuously you have to put it in a "forever" loop.

The block "if " is a check: it is true when any part of the costume reaches the limit of the stage. The block "if on edge, bounce" changes the sprite's direction so that it turns back.

The usual way to combine them is: "forever: move 10 steps; if on edge, bounce." Each loop moves a little and checks whether it has reached the edge. Scratch calculates contact using the whole costume, not just its center: if the costume isn't centered, part of it may still be touching the edge after bouncing, and on the next loop the check is met again. The sprite gets stuck "jittering" against the edge.

Practice★☆☆Not started
Event blocks decide when a stack of blocks starts running. Explain what triggers each of these three events: "when green flag clicked", "when space key pressed", and…

Event blocks decide when a stack of blocks starts running. Explain what triggers each of these three events: "when green flag clicked", "when space key pressed", and "when this sprite clicked", and describe what would happen if a project has two different stacks with two different events.

Solve
Practice★☆☆Not started
The program in the figure, made of interlocking blocks, should make a character follow the dotted square and alert when finished, but inside the loop a block is missing.…

The program in the figure, made of interlocking blocks, should make a character follow the dotted square and alert when finished, but inside the loop a block is missing.

when green flag clicked repeat 4 times move 1 step ? say I've arrived!

Indicate which block is missing inside the loop so that the character completes the path around the square, and explain what each part of the program does.

Solve
Practice★☆☆Not started
In a game, a cat runs across the stage and a dog chases it. Explain where you would place the block "play sound meow" so that it plays exactly when the dog reaches the…

In a game, a cat runs across the stage and a dog chases it. Explain where you would place the block "play sound meow" so that it plays exactly when the dog reaches the cat, which checking block accompanies it, and what would happen if the sound were outside that check.

Solve
Practice★☆☆Not started
The stage in a block-based program works like a coordinate grid: "x" indicates horizontal position and "y" vertical position, with the center at point $(0, 0)$. Explain…

The stage in a block-based program works like a coordinate grid: "x" indicates horizontal position and "y" vertical position, with the center at point $(0, 0)$. Explain where the character will appear with the block "go to x: 0, y: 0" and which direction it will move if "x" increases or if "y" decreases.

Solve

Variables, constantes y bucles controlados

There are three kinds of operators: arithmetic (+, −, ×, ÷), relational (<, >, =, which compare and give true or false), and logical (and, or, not).

Animation of a variable "points" in a loop: a highlight moves through the blocks; on each loop the loop header is checked, "add 1 to points" fills an indicator, and then a step is taken. With three indicators filled the check is met, the flow leaves the loop, and "say Goal!" is highlighted. set points to 0 repeat until points = 3 add 1 to points take one step say Goal!

Un cronómetro con «esperar 1 s» se atrasa

A typical timer is "forever: wait 1 s; change Seconds by 1." It seems accurate, but each loop takes a little more than 1 second: the "wait" block only guarantees an approximate time, and besides, the program takes a moment to run the other blocks and to handle the other sprites. That small delay accumulates loop after loop.

For example, if each loop falls behind by 0.04 s, after 600 loops (10 minutes) the drift is $600 \times 0.04 = 24$ s: the timer shows less time than the real time. The more sprites and movements there are, the bigger the delay. The solution is not to count loops, but to read a real clock: the "timer" sensor (or "current time") and subtract the starting value, so that the result doesn't depend on the speed of the loop.

Practice★★☆Not started
A program aims to count from one to ten with these blocks: "repeat 10 times: say number; change number by 1". When executed, the count does not start at one and, each…

A program aims to count from one to ten with these blocks: "repeat 10 times: say number; change number by 1". When executed, the count does not start at one and, each time it is run again, it starts at a different number. Explain which block is missing and where it should be placed, and where in the stack the initial value of the number should be set.

Solve
Practice★★☆Not started
The figure shows a program with a variable called "points" that controls a loop. Trace the program: indicate how many times the loop executes and how many steps the…

The figure shows a program with a variable called "points" that controls a loop.

when green flag clicked set points to 0 repeat until points = 3 change points by 1 move 1 step say Goal!

Trace the program: indicate how many times the loop executes and how many steps the character takes in total. Then reason about what would happen if the block "set points to 0" were inside the loop instead of before it.

Solve
Practice★★☆Not started
To animate a character, "repeat 4 times: next costume; wait half a second" is used. Explain what this loop achieves and calculate how long the full animation lasts if…

To animate a character, "repeat 4 times: next costume; wait half a second" is used. Explain what this loop achieves and calculate how long the full animation lasts if each wait is $0.5\ \text{s}$ and the costume switch is instantaneous.

Solve
Practice★★☆Not started
The figure shows two programs that move a character using two different types of loop. Explain the difference between the two loops, give a situation where it is…

The figure shows two programs that move a character using two different types of loop.

on start repeat 10 times move 1 step on start repeat until goal move 1 step

Explain the difference between the two loops, give a situation where it is convenient to use each one, and reason about what could happen with the loop on the right if the goal did not exist.

Solve
Practice★★★Not started
In a time trial game, you want to record the record for the shortest time taken to complete a track. A student writes this code upon reaching the finish line: "if…

In a time trial game, you want to record the record for the shortest time taken to complete a track. A student writes this code upon reaching the finish line:

"if <Game_Time < Record> then [set Record to Game_Time]"

However, when playing for the first time after creating the Record variable (which Scratch initializes by default to 0), the record never updates, no matter how fast the car goes.

  1. a) Justify why this check fails and explain the difference in initialization between looking for a maximum score record and a minimum time record.
  2. b) Design the block structure that solves this problem for the first game and subsequent ones without having to manually enter a dummy initial value before clicking the green flag.
Solve
Practice★★★Not started
To measure the duration of a game, this timer is created: "when green flag clicked: set Seconds to 0 forever: wait 1 s change Seconds by 1" When comparing this counter…

To measure the duration of a game, this timer is created:

"when green flag clicked:

set Seconds to 0

forever:

wait 1 s

change Seconds by 1"

When comparing this counter with a real clock after 10 minutes of intense gameplay, the Scratch timer marks only 9 minutes and 35 seconds (a 25-second delay).

  1. a) Justify why a loop with the "wait 1 s" block accumulates lag when the game processes many sprites and simultaneous movements.
  2. b) Design an alternative block timer using the "timer" sensor and variables that does not depend on the game's refresh rate and displays the elapsed time in seconds with one decimal place (for example: 14.2 s).
  3. c) Justify why your proposal maintains exact accuracy even if there are slowdowns on the screen.
Solve

Condicionales anidados y operadores lógicos

Terminar la partida: «detener todos»

The control block "stop all" stops all the scripts in the project: nothing moves or runs afterward. There is also "stop this script" (only stops the stack it's in) and "stop other scripts in sprite." To end a game when the lives run out, you use a conditional: "if then stop all."

Like any "if," it only checks at the moment its turn comes. If you put it only once at the start, right after "set Lives to 3," it is checked when Lives is 3 and never again: the game never ends. It must go inside the "forever," together with whatever changes the lives.

Another detail: if a trap subtracts 2 lives and you have 1 left, Lives goes to −1 and the condition "Lives = 0" is never met. Use a condition that covers the extra cases: "Lives < 1" (or "Lives = 0 or Lives < 0").

Practice★★☆Not started
Describe in your own words the blocks of a program that asks for the user's age and responds "you may enter" if they are 14 or older, and "sorry" otherwise. State what…

Describe in your own words the blocks of a program that asks for the user's age and responds "you may enter" if they are 14 or older, and "sorry" otherwise. State what type of decision block you would use and what check it would perform exactly.

Solve
Practice★★☆Not started
A local cafe offers a free black coffee to members, but only before 10 a.m. Describe the nested decision blocks of the program that checks this, and explain the…

A local cafe offers a free black coffee to members, but only before 10 a.m. Describe the nested decision blocks of the program that checks this, and explain the difference between putting two "if" blocks inside one another versus two "if" blocks in sequence, using this example.

Solve
Practice★★☆Not started
The operator blocks "and", "or", and "not" combine checks. Explain what each one does and describe the full conditions for these two systems: a door that opens if the…

The operator blocks "and", "or", and "not" combine checks. Explain what each one does and describe the full conditions for these two systems: a door that opens if the card is valid and the fingerprint matches, and an alarm that sounds if the window opens or the glass breaks.

Solve
Practice★★★Not started
A student programs the movement of a ball with this code: "forever: move 10 steps if on edge, bounce" When running the program with a ball whose costume is not perfectly…

A student programs the movement of a ball with this code:

"forever:

move 10 steps

if on edge, bounce"

When running the program with a ball whose costume is not perfectly centered, he observes that at certain angles the ball shakes and gets stuck on the edge of the screen, unable to leave.

  1. a) Explain step by step why this lockup occurs based on how Scratch calculates collision with the edge and the position of the costume center.
  2. b) Would the problem be solved simply by swapping the order of the two blocks inside the loop? Justify your answer by predicting what would happen in each iteration.
  3. c) Design a modification to the blocks that guarantees the ball detaches from the edge and continues its path normally.
Solve
Practice★★★Not started
A platform game has this script on the character: "when green flag clicked: set Lives to 3 if <Lives = 0> then [stop all] forever: if <touching Trap?> then: change Lives…

A platform game has this script on the character:

"when green flag clicked:

set Lives to 3

if then [stop all]

forever:

if <touching Trap?> then:

  change Lives by -2

  wait 0.5 s"

The game has two bugs: the game never ends even if the player touches the trap repeatedly, and if a player hits the trap with 1 life left, the counter goes to -1 lives without stopping the game.

  1. a) Justify why the game over check, positioned as it is, does not stop the game at any time.
  2. b) Explain why using the strict condition is a design flaw when the damage subtracts more than 1 life.
  3. c) Design the solution by relocating and modifying the conditional blocks to ensure that the game ends exactly when lives run out or drop below zero.
Solve

Comunicación entre personajes

Diagram of communication by messages: the button's stack (when clicked, broadcast "appear") sends the message to the wizard, whose stack "when I receive appear" runs show. sender: button receiver: wizard button: when clicked broadcast "appear" wizard: when I receive "appear" show message

Back to the beginning: Ximena's game needs the kitten to touch a ball and lose a single life, not twenty. A condition inside "forever" is checked many times per second, so after subtracting the life you have to wait a moment or send a message that resets the ball. And when the lives reach zero, a "stop all" inside the same loop ends the game.

Practice★★☆Not started
In a project there are two sprites: a button and a wizard. Clicking the button should make the wizard appear. Explain the purpose of the "broadcast message" and "when I…

In a project there are two sprites: a button and a wizard. Clicking the button should make the wizard appear. Explain the purpose of the "broadcast message" and "when I receive message" blocks, describe the block stacks each sprite would carry, and reason why it is not enough to put the wizard's blocks inside the button.

Solve
Practice★★★Not started
In a video game, this block has been included inside the "forever" loop of the main character: "if <touching Coin?> then [change Points by 1]" "if <touching Enemy?> then…

In a video game, this block has been included inside the "forever" loop of the main character:

  • "if then [change Points by 1]"

  • "if then [change Lives by -1]"

  • "if then [stop all]"

When testing it, two serious bugs are detected: when touching a single coin the counter jumps up by 15 to 20 points all at once, and when grazing an enemy, all 3 lives are lost instantly.

  1. a) Technically justify why this happens, taking into account the execution speed of the loops per second.
  2. b) Design the necessary modification to the coin and character blocks to correct both behaviors without pausing the overall movement of the game (consider hiding/destroying sprites and invulnerability periods).
Solve
Practice★★★Not started
In a space game, the 'Asteroid' sprite has these three stacks programmed: Stack 1: "when green flag clicked: forever [wait 2 secs; create clone of myself]" Stack 2:…

In a space game, the 'Asteroid' sprite has these three stacks programmed:

Stack 1: "when green flag clicked: forever [wait 2 secs; create clone of myself]"

Stack 2: "when I receive Meteor_Alert: create clone of myself"

Stack 3: "when I start as a clone: show; forever [change y by -5; if <y position < -170> then [hide]]"

After playing for a couple of minutes, the game experiences severe slowdowns, asteroids stop being created, and every time the Meteor_Alert message arrives, the number of clones multiplies uncontrollably.

  1. a) Justify why the "hide" block causes the game to stop responding and reach Scratch's clone limit.
  2. b) Explain the bug in Stack 2: why do the existing clones themselves duplicate the number of asteroids when receiving a message?
  3. c) Design the correct solution with blocks for the creation, movement, and permanent deletion of the clones.
Solve
Practice★★★Not started
A classmate has programmed the cycle for a traffic light for cars (green: 10 s; amber: 3 s; red: 12 s) and a synchronized pedestrian traffic light. To do this, he has…

A classmate has programmed the cycle for a traffic light for cars (green: 10 s; amber: 3 s; red: 12 s) and a synchronized pedestrian traffic light. To do this, he has created two independent scripts with "when green flag clicked" and "forever" loops with wait blocks in each sprite. After one minute of simulation, the cars and pedestrians have a green light at the same time.

  1. a) Justify why this dangerous desynchronization occurs when using loops with independent wait times.
  2. b) Design a solution with blocks that ensures they never show green at the same time, explaining whether you use state variables or messages ("broadcast and wait") and detailing the exact order of the light changes.
Solve
Practice★★★Not started
In a two-player Pong game (left and right paddles), the following interaction bugs are detected: 1. When passing the paddle, the ball bounces off the back edge and…

In a two-player Pong game (left and right paddles), the following interaction bugs are detected:

  1. When passing the paddle, the ball bounces off the back edge and scores several consecutive points before the point resets.

  2. While the ball returns to the center after a goal, players can continue hitting it, scoring false points.

  3. If the ball brushes the back of the paddle when it moves up quickly, the ball shoots into the player's own goal.

  1. a) Design the ball's block structure to handle scoring: goal detection, momentary pause of the game, and communication with the scoreboards using messages.
  2. b) Design the paddle bounce condition so that it only returns the ball toward the opponent's side if the impact occurs on its front face, that is, when the ball is moving toward the paddle.
  3. c) Explain how the paddles and the ball should react to the 'new point' message to resume the game in a fair and synchronized manner.
Solve

Resumen

See all the exercises in this section