Exercises: Block-based programming
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.
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.
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.
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.
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.
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.
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.
The stage in a block-based program works like a coordinate grid: "x" indicates horizontal position and "y" vertical position, with the centre 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 centre 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
- a) Justify why this dangerous desynchronization occurs when using loops with independent wait times.
- 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.
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.
- a) Technically justify why this happens, taking into account the execution speed of the loops per second.
- 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).
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.
- 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.
- 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.
- c) Design a modification to the blocks that guarantees the ball detaches from the edge and continues its path normally.
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.
- a) Justify why this check fails and explain the difference in initialization between looking for a maximum score record and a minimum time record.
- 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.
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.
- a) Justify why the "hide" block causes the game to stop responding and reach Scratch's clone limit.
- b) Explain the bug in Stack 2: why do the existing clones themselves duplicate the number of asteroids when receiving a message?
- c) Design the correct solution with blocks for the creation, movement, and permanent deletion of the clones.
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.
- a) Justify why the game over check, positioned as it is, does not stop the game at any time.
- b) Explain why using the strict condition is a design flaw when the damage subtracts more than 1 life.
- 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.
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).
- a) Justify why a loop with the "wait 1 s" block accumulates lag when the game processes many sprites and simultaneous movements.
- 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).
- c) Justify why your proposal maintains exact accuracy even if there are slowdowns on the screen.
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:
-
When passing the paddle, the ball bounces off the back edge and scores several consecutive points before the point resets.
-
While the ball returns to the center after a goal, players can continue hitting it, scoring false points.
-
If the ball brushes the back of the paddle when it moves up quickly, the ball shoots into the player's own goal.
- a) Design the ball's block structure to handle scoring: goal detection, momentary pause of the game, and communication with the scoreboards using messages.
- 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.
- 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.