Unit 3, Lesson 27: Student Project not working

I can not figure out why my students game is not working.

(Game Lab - Code.org)

She wants the game to start with the startGame function and then the score should go up by one when the dog moves the baby pigs. After a score of 3, it should change to You won! endGame function.

What is happening is it is starting with the endGame screen and not much else.

I’ve tried moving functions, etc. but everything I do does not work or make s things worse. :frowning:

hello @langilla1,

you have multiple problems with your program that i currently don’t feel like clarifying maybe someone else will but I’ll give you my patched version

// Create your variables here
var textx = 100;
var texty = 300;
var score = 0;
var screen = "start";
var waitout = null;
var start = createSprite(200, 150);
start.setAnimation("start");
start.scale = 0.5;
var big = createSprite(800, 300);
big.setAnimation("big");
big.scale = 0.5;
var baby1 = createSprite(500, 200);
baby1.setAnimation("baby");
var baby2 = createSprite(500, 200);
baby2.setAnimation("baby");
var baby3 = createSprite(500, 200);
baby3.setAnimation("baby");
baby1.scale = 0.2;
baby2.scale = 0.2;
baby3.scale = 0.2;
var dog = createSprite(800, 200);
dog.setAnimation("dog");
dog.scale = 0.2;
// Create your sprites here

function draw() {
  // draw background
  background("pink");
  fill("black");
  textSize(30);
  text("press play to start", textx, texty);

  // update sprites
  if (screen == "start") {
    startGame();
    drawSprites();
  } else if (screen == "play") {
    playerMove();
    movePig();
    pig();
    if (score >= 3 && waitout == null) {
      waitout = setTimeout(function () {
        screen = "win";
        waitout = null;
      }, 2e3);
    }
    drawSprites();
    textSize(50);
    text("score: " + score, 0, 50);
  } else if (screen == "win") {
    endGame();
  }
}
function startGame() {
  if (keyDown("space")) {
    start.x = 500;
    start.y = 500;
    textx = 500;
    big.x = 200;
    big.y = 300;
    baby1.x = 100;
    baby2.x = 250;
    baby3.x = 375;
    baby1.y = 150;
    baby2.y = 40;
    baby3.y = 175;
    dog.x = 20;
    dog.y = 40;
    screen = "play"
  }
}
function endGame() {
  background("black");
  fill("yellow");
  text("You win", 0, 15);
  big.x = 800;
  baby1.x = 800;
  baby2.x = 800;
  baby3.x = 800;
  dog.x = 800;
}
function playerMove() {
  if (keyDown("up")) {
    dog.y = dog.y - 2;
  }
  if (keyDown("down")) {
    dog.y = dog.y + 2;
  }
  if (keyDown("left")) {
    dog.x = dog.x - 2;
  }
  if (keyDown("right")) {
    dog.x = dog.x + 2;
  }
}
function movePig() {
  dog.displace(baby1);
  dog.displace(baby2);
  dog.displace(baby3);
}
function pig() {
  score = 0;
  for (var i = 1; i < 4; i++) {
    var baby = window["baby" + i];
    if (baby.y > 250) {
      baby.x = 100 + ((i - 1) * 90);
      baby.y = 350;
      score = score + 1;
    }
  }
}

// Create your functions here

Hi @langilla1,

Cute game! The first thing I did to see why the endGame() function was running first was to enter the “score” variable into the watcher. It was quickly counting past 3 so it triggered almost immediately. I believe the reason why this happens is because baby3.y adds one to score if it is >=30 but it starts at 200 so it instantly begins adding to to score. To begin fixing, change line 53 so that baby3.y doesn’t automatically add to score (make the number larger) and the game should work. Hope that helps but let us know if you have other questions.
~Michelle