How to switch backgrounds using the spacebar

I have a student that wants her background to switch when she clicks the screen with her mouse. She wants the new background to stay on the screen. She has the code created that makes the new background appear, but I am not sure how to make the background stay on screen when she stops pressing the mouse.


Declare a variable that tracks if the spacebar was pressed at all.

var spacePressed = false;

Then, check if space has been pressed, and make spacePressed true if it has.

if (keyDown("space")) {
  spacePressed = true;

Then, have the confetti show if spacePressed is true.

if (spacePressed) {


There are a number of ways to make it work, but I think one of the easiest might be to create a variable at the top of the program. I called mine “screen” and set it equal to 1.

var screen=1;

Then, inside the draw loop, you would have one conditional that says “if mouseDown, screen = 2” and then, you would have a second conditional that tells it if screen == 2, choose the pink background and have the cupcake rise and display the text for the spacebar, etc. ELSE, make the screen black and display the text to click the mouse to blow out the candle.

Hope that helps!