Paint Box
Paint Box
Paint box
Make your own paint program
Step 1 Introduction
Software
Scratch 3 (either online (https://rpf.io/scratchon) or offline (https://rpf.io/scratchoff))
Downloads
Offline starter project (https://rpf.io/p/en/paint-box-go)
If you need to print this project, please use the printer-friendly version (https://projects.raspberrypi.org/e
n/projects/paint-box/print).
You can find the completed project here (https://rpf.io/p/en/paint-box-get).
Step 2 Make a pencil
Start by making a pencil that you can use to draw on the Stage.
If you have a Scratch account you can make a copy by clicking Remix.
Offline: open the starter project (https://rpf.io/p/en/paint-box-go) in the offline editor.
If you need to download and install the Scratch offline editor, you can find it at rpf.io/scratchoff (http
s://rpf.io/scratchoff)
In the starter project, you should see pencil and eraser sprites:
Add the Pen extension to your project.
The Pen section then appears at the bottom of the blocks menu.
Add some code to the pencil sprite to make the sprite follow the mouse pointer forever so that you
can draw:
when clicked
forever
go to mouse pointer
Click the flag and then move the mouse pointer around the Stage to test whether your code works.
Next, make your pencil only draw if the mouse button is being clicked.
Add this code to your pencil sprite:
when clicked
forever
go to mouse pointer
pen down
else
pen up
Test your code again. This time, move the pencil around the Stage and hold down the mouse button.
Can you draw with your pencil?
Now move the pencil around on the Stage and draw. The pencil should now draw a line from its tip.
Step 3 Coloured pencils
Now you’re going to add different coloured pencils to your project and allow the user to choose between them.
Name the new costume ‘pencil-green’, and colour the pencil green.
Draw two new sprites: one blue square and one green square. These are for choosing between the blue
and green pencil.
Rename the new sprites so that they are called ‘blue’ and ‘green’
broadcast green
You can create a message to be broadcast. The message text can be anything you like, but it is
useful to give it a sensible description.
Find the broadcast block under Events
broadcast shrink
broadcast start
Receive a broadcast
Sprite can react to a broadcast by using a when I receive block. Multiple sprites can respond
when they receive the same message.
You can add blocks below a when I receive block to tell the sprite(s) what to do when they
receives the message.
go to x: 100 y: 50
show
The pencil sprite should listen for the “green” message and change its costume and pencil colour in response.
Switch to your pencil sprite. Add some code so that when this sprite receives the green broadcast, it
switchs to the green pencil costume and changes the pen colour to green.
To set the pencil to colour to green, click the coloured square in the set pen color block, and then
click on the green square sprite.
Then to a similar thing so that you can switch the pencil colour to blue.
Click on the blue square sprite and add this code:
broadcast blue
when clicked
erase all
forever
go to mouse pointer
pen down
else
pen up
Add the ‘X-block’ sprite from the library’s letters section. Colour the sprite’s costume in red and make it a
little smaller. This sprite is the ‘clear’ button.
You can search for a sprite, or browse for one by category. Click on a sprite to add it to your project.
Add code to the ‘X-block’ sprite to clear the Stage when the sprite clicked.
erase all
You don’t need to use a broadcast to clear the Stage, because the erase all block does that job.
Add code to the eraser sprite to send an 'eraser' broadcast when the eraser sprite is clicked.
broadcast eraser
When the pencil sprite receives the ‘eraser’ message, it should switch its costume to the eraser and switch the pen
colour to white, which is the same colour as the Stage!
Add some code to create the eraser.
I need a hint
Here is what the code should look like:
Test your project to see if you can clear the Stage and erase pencil lines.
There’s one more problem with the pencil: you can draw anywhere on the Stage, including near the ‘clear’ and
eraser buttons!
To fix this, change the code so that the pen is only down if the mouse is clicked and the y position of the
mouse pointer is greater than -120:
when clicked
erase all
forever
go to mouse pointer
pen down
else
pen up
Test your project. You now should not be able to draw near the buttons.
Step 5 Change the pen width
Next you will add code to allow the person using your program to draw things with different pen widths.
Type in the name of your variable. You can choose whether you would like your variable to be
available to all sprites, or to only this sprite. Press OK.
Once you have created the variable, it will be displayed on the Stage, or you can untick the
variable in the Scripts tab to hide it.
Add this line inside the forever loop of the pencil sprite’s code:
when clicked
erase all
forever
go to mouse pointer
pen down
else
pen up
The pen width now repeatedly gets set to the value of the width variable.
Right-click on the width variable displayed on the Stage, and then click on slider.
You can now drag the slider that is visible below the variable to change the variable’s value.
Test your project and see if you can change the pen width.
Challenge!
If you want to, you can also add code so that pressing the arrow keys changes the pen width.
Step 6 What next?
Now that you have completed the ‘Paint box’ project, try the ‘Boat race’ project (https://projects.raspberrypi.or
g/en/projects/boat-race?utm_source=pathway&utm_medium=whatnext&utm_campaign=projects),
which helps you make a game where you have to stir a boat around obstacles.