Discovery - Unit 3 Final Project Help

Hello I am at a lose here… have a student that wants his goal post to go back and forth on the screen. The link below is his game he created.

Many thanks for any and all help.

Hello @rdiaz1,

The easiest solution would be to make edges and have it bounce off of them by setting an initial velocity for the goal post here’s what your updated code would look like

// Create your sprites here
var field = createSprite(200, 200);
field.setAnimation("field");
var goalpost = createSprite(200, 45);
goalpost.setAnimation("goalpost");
goalpost.velocityX = 5;
var goal = createSprite(200, 200);
goal.setAnimation("goal");
goal.visible = false;
var win = createSprite(200, 200);
win.setAnimation("win");
win.visible = false;
var lose = createSprite(200, 200);
lose.setAnimation("lose");
lose.visible = false;
var ball = createSprite(200, 310);
ball.setAnimation("ball");
ball.scale = 0.1;
createEdgeSprites();
// Create your variables here
var timer = 30;
var score = 0;
textSize(20);

function draw() {
  // draw background
  background("black");

  // update sprites
  // goalpost.x = goalpost.x - 5;
  if (keyDown("up")) {
    ball.y = ball.y - 4;
  }
  if (keyDown("down")) {
    ball.y = ball.y + 4;
  }
  if (keyDown("left")) {
    ball.x = ball.x - 4;
  }
  if (keyDown("right")) {
    ball.x = ball.x + 4;
  }
  if (ball.isTouching(goalpost)) {
    goal.visible = true;
    goalpost.visible = false;
    field.visible = false;
    ball.visible = false;
    ball.x = 200;
    ball.y = 310;
    score = score+1;
  }
  // if (goalpost.x < -7) {
  //   goalpost.x = 407;
  // }
  goalpost.bounceOff(edges);
  drawSprites();
  fill("black");
  text(Math.round (timer), 50, 250);
  timer = timer-0.033;
  
}

// Create your functions here
1 Like