Help with U3 Lesson 27 Student Game

Hi - My student’s game has an issue that we cannot resolve. Hoping someone can help. I’ve included the link to the game. The problem lies with the top and bottom sprites leaving a “trail” as they spawn. The student does not want that to happen. Any help will be very much appreciated!

What she would like to happen: Sprites spawn and move across the screen cleanly.
What is happening: Sprites are moving across the screen leaving a trail of respawned sprites.

https://studio.code.org/projects/gamelab/OMF8nMriIYFLa_6Yf4BRIFw_1Elecm11xQRltH95J5Q

All you have to do is to add a background (which is what most people will say, which is the case), but on the other screens, it will have the same problem, so I will go more in-depth about how to fix such issues.

#1. Make a global variable on the top. You can name the values or strings whatever, but I will use the ones I used.

var stage = "start"

#2. In the functions, make it change the stage.
For example,

function win() {
  stage = "win";
//^^^^^^^^^^^^^^
  background("skyblue");
  noStroke();
var cloudL = createSprite(95, 50);
cloudL.setAnimation("cloud_1");
cloudL.scale = 0.5;
var cloudR = createSprite(285, 65);
cloudR.setAnimation ("cloud_1");
cloudR.scale = 0.75;
}
function startGame() {
    stage = "game";
//^^^^^^^^^^^^^^^
    topB.velocityX = -2;
    topS.velocityX = -3;
    bottomB.velocityX = -2;
    bottomS.velocityX = -3;
}

#3. You make an if statement for each of the values.

if (stage == "start") {

} else if (stage == "game") {
  
} else if (stage == "win") {
  


}

#4. Then you put the executed code in each if statement, for this situation, you use background(); and pick the desired colors.

#5. It should look somewhat like this.

if (stage == "start") {
  background("white");
} else if (stage == "game") {
  background("midnightblue");
} else if (stage == "win") {
  background("skyblue");
}

#6. For the problem of the grass not appearing on the win screen, we will move the code in the win function for making the grass here.

if (stage == "start") {
  background("white");
} else if (stage == "game") {
  background("midnightblue");
} else if (stage == "win") {
  background("skyblue");
  fill("green");
  rect(0, 286, 400, 400);
}

If you did not understand some of the parts, here is the code: Game Lab - Code.org
I have marked it with “-”, so that you can see the changes I have made.

I hope your student does well and that you and your student have learned something new!

Sincerely, pluto :grinning:

2 Likes

Thank you so much! I am going to see my student soon. I’ll let you know how it goes. Thanks again!!