Scratch: scroll a backdrop with the arrow keys
Part 1: draw a floor wider than the stage, then move it by 5 pixels with the arrow keys, three variables and a stop at each end.
Getting started
Home Assistant
Buying
Part 1: draw a floor wider than the stage, then move it by 5 pixels with the arrow keys, three variables and a stop at each end.


AI-generated illustration
This is the first part of a series. It builds the floor of a game and scrolls it with the arrow keys. The character, the platforms and the jump are not on this page.
Install mBlock 5, or open Scratch. You should already know the stage, the sprite and the block categories: mBlock 5 interface. The square activity is a first script on one sprite.
Several scripts can live on the same sprite, and they run at the same time. The script area shows the object you have selected.
The floor is a sprite, not a backdrop. The backdrop has no Motion category, so it cannot change x.
The stage runs from x = −240 to x = 240. The sprite’s x is the centre of the costume. Right arrow: the floor moves 5 pixels to the left. Left arrow: the opposite.
In Variables, create three variables for this sprite only:
decor_largeur: the width you read in the costume editor;decor_debut: the starting x;decor_fin: the ending x.When the green flag is clicked:
decor_largeur to that width (7,000 in the example);decor_debut to (decor_largeur / 2) − 240;decor_fin to 240 − (decor_largeur / 2);decor_debut, y: 0.With 7,000, the start is 3,260 and the end is −3,260. The left edge of the costume then lines up with the left edge of the stage. This sum was not replayed in Scratch for this update. If the floor does not start on the left, the sign or the width you read does not match the costume.
The and block runs the move only when both tests are true. Inside forever:
Check: green flag, the floor is on the left. Right arrow shifts it, then it stops when x reaches decor_fin. Left arrow brings it back and stops at decor_debut.
Open examples on Scratch. On this site: the square, install mBlock 5, first mBot program.
Was this page helpful?
Thank you!
What should we improve?
Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.
Cookies and analytics
We use Google Analytics 4 to understand how the site is used (page views, navigation). It is only switched on if you accept. Declining does not stop you using the site. More: cookie settings and privacy policy (in French).