Unit 3: Create a background with an image?

Is there a way to create a background with an image the student has? If so, what does the code look like? I am looking for this to happen in Unit 3, Game Lab.

I haven’t worked with Unit 3 yet but one of my students used a picture of chocolate ice cream as his background. Here is the style.css coding
body {
background-image: url(“chocolate.jpeg”);


@bobd is correct for Unit 2! That CSS will work. Here are some resources that will control whether it repeats etc. https://www.w3schools.com/cssref/css3_pr_background.asp. You can also control if it scrolls or is fixed https://www.w3schools.com/cssref/pr_background-attachment.asp.

If your question was about Game Lab, let us know.

I would like to know this for Unit 3, Game Lab

Ahh…you upload the photo as a sprite and add it to your code so that it is all the way in the back. Here is an example of a simple scene where the background is a sprite (vs just the background color block). https://studio.code.org/projects/gamelab/cOj6ZcO4LeKLvCUlDwlq4ymU85ATl1NZJV09ILPAYtk

Good luck!

1 Like

Yes. You can add a picture as a background by creating a background sprite. Keep in mind that the sequencing of commands matters in javascript. So if you have a showScore() function to display a scoreboard for the game, it would have to be called after the drawSprites() call. Here is an example:

var gameBackground = createSprite(200, 200);
gameBackground.scale = 3;

var dayBtn = createSprite(200, 350, 40,30);

var nightBtn = createSprite(300, 350, 40, 30);

var score = 0;

function draw() {

function showScore(){
  text("Score:", 10, 25);
  text(score, 70, 25);

function chooseBackground(){
  else if(mousePressedOver(nightBtn))

Link to the sample