How would i make the ball keep moving back every time a goal is scored?

edited July 2015 in Library Questions
import ddf.minim.*; //sound 

Minim minim;
AudioPlayer player1, player2, player3, player4;
AudioInput input;

PImage bg, sball; 
float goalKeeper;
float xspeed = 4;
float ballX= 170;
float ballY= 170;
float ballYspeed=0;
int score=0;
int miss=3;
float goalieL=20;

void setup()
{
  size (600, 600);
  bg = loadImage("SoccerField.png");
  sball = loadImage("ball.jpg");
  minim = new Minim(this);
  player1 = minim.loadFile("kick.wav");
  goalKeeper=170;
}

void draw()
{
  if (mousePressed) {
    player1.play(0);
  }
  background(bg); 
  fill(255, 255, 255);
  textAlign(CENTER);
  textSize(10);

  textSize(40);
  text("SCORE: "+score, 90, 520);
  text("LIVES: " + miss, 80, 580);


  stroke(255, 0, 0);
  strokeWeight(4);

  //goalie moving back and forth
  line(goalKeeper, 100, goalKeeper+goalieL, 100);
  if (goalKeeper >= 350-goalieL)
  {
    xspeed *= -1;
    goalKeeper= 200-goalieL;
  }
  if ( goalKeeper  <= 80)
  {
    xspeed *= -1;
  }
  goalKeeper=goalKeeper+xspeed;



  //soccer ball
  imageMode(CENTER);
  image( loadImage("ball.png"), 300, ballY, 50, 30);
  ballY=ballY+ballYspeed;

  // println(ballY + ", " + ballYspeed);


  if (ballY <=90)
  {
    ballYspeed = 0;
    ballY = 170;

    if (goalKeeper <= 200 && goalKeeper >= 200-goalieL)
    {
      miss--;
    } else
    {
      score=score+1;
      if ( abs(xspeed) < 20)
      {
        xspeed=xspeed*1.1;
        goalieL= goalieL+1;
      }
    }
  }
  if (miss==0)
  {
    fill(35, 193, 34);
    rect(0, 0, width, height);
    fill(255, 255, 255);
    text("End Game", 300, 200);
    text ("Reset: Spacebar", 300, 250);
    text("Score: " + score, 300, 300);
    noLoop();
  }
}

void mousePressed()
{
  ballYspeed = -7;
}


void keyPressed()
{
  if (key == ' ')
  {
    loop();
    ballY = 170;
    goalieL=20;
    score=0;
    miss=3;
    xspeed=4;
    goalKeeper=width/2;
  }
}

Answers

Sign In or Register to comment.