Help Requested With Student Game

Click Here to View Student Game

A student is having difficulty executing his game idea and unfortunately, I’ve been unable to figure it out. In the game, he would like the player to click on the creepy face (Jef) and have the face disappear from the starting point and reappear in another location. We’ve tried various combinations without success. Any ideas?

Here is one way to have the face disappear and reappear in another location. https://studio.code.org/projects/gamelab/7kjdGtlZreRolKnPxZqODA

Hope that this gives the students a place to start.

1 Like

Student’s game changes backgrounds when a certain score is achieved. However, background 3 is NOT changing and has the same code as background1 and 2. Is this a code issue or program issue? Thank you!

I think the issue is in the else if statements.
It always executes the first one that is true and here several can be true.
6 is both greater than 2 and is greater than 4 but it will only execute the greater than 2 branch because it is the first true statement.
The student needs to use an AND statement in the else ifs. else if ((score >= 4 && score < 6)

 } else if ((score >= 2)) {
    backgroundchange1();
  } else if ((score >= 4)) {
    backgroundchange2();
    backgroundchange1 = false;
  } else if ((score >= 6)) {
    backgroundchange3();
    backgroundchange2 = false;
  }
1 Like

A student wants his “You win” background to appear when the score reaches 25. However, it is not appearing. The game simply continues on. When the student’s Lives gets to 0, the “You Lose” background appears, which is correct. I have included his game.

Thank you,
Jenny Stearns

// Create your variables here
var score = 25;

var lives = 3;

// Create your sprites here
var player = createSprite(100, 100);
player.scale = 0.75;
player.setAnimation(“player”);

var platform = createSprite(150, randomNumber(200, 400));
platform.scale = 1.5;
platform.velocityX = -3;
platform.setAnimation(“platform”);

var platform2 = createSprite(350, randomNumber(200, 400));
platform2.scale = 1.5;
platform2.velocityX = -3;
platform2.setAnimation(“platform”);

var livescoin = createSprite(1000, 200);
livescoin.setAnimation(“livescoin”);
livescoin.velocityX = -3;

var coin = createSprite(randomNumber(410, 600), 200);
coin.scale = 0.5;
coin.setAnimation(“coin”);
coin.velocityX = -3;

var barrier = createSprite(-245, 200);
barrier.scale = 5;
barrier.setAnimation(“barrier”);

var barrier2 = createSprite(645, 200);
barrier2.scale = 5;
barrier2.setAnimation(“barrier”);

var barrier3 = createSprite(200, -245);
barrier3.scale = 5;
barrier3.setAnimation(“barrier”);

var barrier4 = createSprite(200, 645);
barrier4.scale = 5;
barrier4.setAnimation(“barrier”);

function draw() {
// draw background
if (score >= 25) {
winBackground();
player.visible = false;
platform.visible = false;
platform2.visible = false;
coin.visible = false;
livescoin.visible = false;
barrier.visible = false;
barrier2.visible = false;
barrier3.visible = false;
barrier4.visible = false;
} else {
drawbackground();
player.visible = true;
platform.visible = true;
platform2.visible = true;
coin.visible = true;
livescoin.visible = true;
barrier.visible = true;
barrier2.visible = true;
barrier3.visible = true;
barrier4.visible = true;
}
if (lives <= 0) {
losebackground();
player.visible = false;
platform.visible = false;
platform2.visible = false;
coin.visible = false;
livescoin.visible = false;
barrier.visible = false;
barrier2.visible = false;
barrier3.visible = false;
barrier4.visible = false;
} else {
drawbackground();
player.visible = true;
platform.visible = true;
platform2.visible = true;
coin.visible = true;
livescoin.visible = true;
barrier.visible = true;
barrier2.visible = true;
barrier3.visible = true;
barrier4.visible = true;
}

playerColectCoins();
// update sprites
platformsMove();
moveCoinsback();
playerGravity();
movePlayer();
playerland();
barriers();

drawSprites();
}

// Create your functions here
function drawbackground() {
noStroke();
background(“skyblue”);
fill(“yellow”);
ellipse(350, 50, 50, 50);
textSize(20);
fill(“black”);
text(“Score :”, 10, 25);
text(score, 75, 27);
text(“Lives :”, 10, 45);
text(lives, 70, 47);
}
function winBackground() {
background(“purple”);
fill(“black”);
noStroke();
textSize(100);
text(“YOU”, 100, 100);
text(“WIN”, 120, 200);
}

function losebackground() {
background(“purple”);
fill(“black”);
noStroke();
textSize(100);
text(“YOU”, 100, 100);
text(“lose”, 120, 200);
}

function platformsMove() {
if ((platform.x) <= -40) {
platform.x = 440;
platform.y = randomNumber(200, 400);
}
if ((platform2.x) <= -40) {
platform2.x = 440;
platform2.y = randomNumber(200, 400);
}
}

function moveCoinsback() {
if ((coin.x) <= -20) {
coin.x = randomNumber(410, 600);
coin.y = 200;
}
if ((livescoin.x) <= -20) {
livescoin.x = 1000;
livescoin.y = 200;
}
}

function playerGravity() {
player.velocityY = player.velocityY + 0.25;
}

function movePlayer() {
if (keyDown(“up”)) {
player.velocityY = -5;
}
if (keyDown(“left”)) {
player.x = player.x - 4;
}
if (keyDown(“right”)) {
player.x = player.x + 4;
}
}

function playerland() {
player.collide(platform);
player.collide(platform2);
}
function playerColectCoins() {
if (player.isTouching(coin)) {
coin.x = randomNumber(410, 600);
coin.y = 200;
score = score + 1;
}
if (player.isTouching(livescoin)) {
livescoin.x = 1000;
livescoin.y = 200;
lives = lives + 1;
}
}

function barriers() {
if (player.isTouching(barrier)) {
player.x = 100;
player.y = 100;
lives = lives - 1;
}
if (player.isTouching(barrier2)) {
player.x = 100;
player.y = 100;
lives = lives - 1;
}
if (player.isTouching(barrier3)) {
player.x = 100;
player.y = 100;
lives = lives - 1;
}
if (player.isTouching(barrier4)) {
player.x = 100;
player.y = 100;
lives = lives - 1;
}
}

Can you provide a link to the project instead of pasting the code? It makes helping a lot easier.

I’m sorry. Here is the link:

https://studio.code.org/s/csd3-2018/stage/22/puzzle/12?section_id=1998576&user_id=47029437

Thank you,

Jennifer Stearns

I’m not seeing any code in the link.

I’m not sure why you are not receiving the code. I copied and pasted the student’s link into the email. Should I be doing something else?

Here it is again.

https://studio.code.org/s/csd3-2018/stage/22/puzzle/12? section_id=1998576&user_id=47029437

It still does not work. Can you click the share button at the top of your page and then post that link?

Here you go…

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

sweet. That link works.
Let me take a look.

I changed the structure of the if/else if/else into one big branch of if/else if/else
Seems like it works.
I guess the big thing is with the two if/else branches there are two blocks that will get executed every time through the draw loop when what you want is only one of the choices. Changing the structure to an if/else if/else makes this happen. Let us know how it goes.

 if (score >= 25) {
    winBackground();
    player.visible = false;
    platform.visible = false;
    platform2.visible = false;
    coin.visible = false;
    livescoin.visible = false;
    barrier.visible = false;
    barrier2.visible = false;
    barrier3.visible = false;
    barrier4.visible = false;
  } else {
    drawbackground();
    player.visible = true;
    platform.visible = true;
    platform2.visible = true;
    coin.visible = true;
    livescoin.visible = true;
    barrier.visible = true;
    barrier2.visible = true;
    barrier3.visible = true;
    barrier4.visible = true;
  }
  if (lives <= 0) {
    losebackground();
    player.visible = false;
    platform.visible = false;
    platform2.visible = false;
    coin.visible = false;
    livescoin.visible = false;
    barrier.visible = false;
    barrier2.visible = false;
    barrier3.visible = false;
    barrier4.visible = false;
  } else {
    drawbackground();
    player.visible = true;
    platform.visible = true;
    platform2.visible = true;
    coin.visible = true;
    livescoin.visible = true;
    barrier.visible = true;
    barrier2.visible = true;
    barrier3.visible = true;
    barrier4.visible = true;
  }

into

if (score >= 25) {
    winBackground();
    player.visible = false;
    platform.visible = false;
    platform2.visible = false;
    coin.visible = false;
    livescoin.visible = false;
    barrier.visible = false;
    barrier2.visible = false;
    barrier3.visible = false;
    barrier4.visible = false;
  } 
  else if (lives <= 0) {
    losebackground();
    player.visible = false;
    platform.visible = false;
    platform2.visible = false;
    coin.visible = false;
    livescoin.visible = false;
    barrier.visible = false;
    barrier2.visible = false;
    barrier3.visible = false;
    barrier4.visible = false;
  } else {
    drawbackground();
    player.visible = true;
    platform.visible = true;
    platform2.visible = true;
    coin.visible = true;
    livescoin.visible = true;
    barrier.visible = true;
    barrier2.visible = true;
    barrier3.visible = true;
    barrier4.visible = true;
  }

Did you have any luck with my student’s game?

Thanks,

Jenny

I took a look at this one.
Here’s what I said.

Let me know how it goes!

1 Like