Back to homeAP® Computer Science Principles · AP Computer Science Principles · Change subject
Start for free
AP CSP · AP® Computer Science Principles
Digital content creation · Simple creation and programming

Block-based programming

Picture Aaliyah building a simple game for her high school's computer science club: a cat that runs across the screen, and a score that goes up when she catches it. A block-based program is built by snapping pieces together, like a jigsaw puzzle: you do not need to type a single letter of code to tell the computer exactly what to do, in what order, when to repeat something and when to choose between two paths. By the end of this page you will be able to read Aaliyah's blocks and spot why a stack misbehaves. On the AP® exam, programs appear both as blocks and as text, so this skill carries over.

The three basic structures

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
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
These looks blocks are disconnected: "switch costume to pirate", "say Hello, my name is Mateo! 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 Mateo! 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

Events, movement and the stage

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 and controlled loops

Animation of a variable «points» in a loop: a highlight moves through the blocks; on each iteration 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 Finish!» is highlighted. set points to 0 repeat until points = 3 add 1 to points take a step say Finish!
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 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
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
In a time trial game, you want to keep track of 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 keep track of 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

Nested conditionals and logical operators

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 café 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 café 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 classmate has programmed the cycle for a traffic light for cars (green: 10 s; yellow: 3 s; red: 12 s) and a synchronized pedestrian signal. To do this, he has created…

A classmate has programmed the cycle for a traffic light for cars (green: 10 s; yellow: 3 s; red: 12 s) and a synchronized pedestrian signal. 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
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

Communication between sprites

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
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 explain 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
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

Summary

See all the exercises in this section