Why is the program not adding values to the score

edited January 2016 in Library Questions
int numBalls = 18;
float spring = 0.05;  
float gravity = 0;    
float friction = -1;  
int savedTime;
int totalTime=60000;
int timer;
int score=0;
Ball[] balls = new Ball[numBalls];
Estrelas[] estrelas = new Estrelas[400];
boolean startpremido = false;
import ddf.minim.*;

Minim minim;
AudioPlayer player;

void setup() {
  size(1000, 500);
  background(0);
  for (int i=0; i<numBalls; i++) 
    for (int j=0; j<16; j++)
      for (int q=0; q<14; q++)
        for (int w=0; w<12; w++)
          for (int a=0; a<10; a++)
            for (int z=0; z<9; z++) {

              balls[i] = new Ball(0, random(50,450), 20, i, color(255, 255, 0), 3, balls);  
              balls[i].vx=random(2, 3);
              balls[i].vy=0;

              balls[j] = new Ball(width, random(50,450), 20, j, color(255, 255, 0), 3, balls);     
              balls[j].vx=random(-3, -2); 
              balls[j].vy=0;

              balls[q] = new Ball(0, random(50,450), 30, q, color(255, 0, 0), 3, balls);    
              balls[q].vx=random(2, 3); 
              balls[q].vy=0;

              balls[w] = new Ball(width, random(50,450), 30, w, color(255, 0, 0), 3, balls);     
              balls[w].vx=random(-3, -2); 
              balls[w].vy=0;

              balls[a] = new Ball(0, random(50,450), 50, a, color(0, 0, 255), 3, balls);    
              balls[a].vx=random(2, 3); 
              balls[a].vy=0;

              balls[z] = new Ball(width, random(50,450), 50, z, color(0, 0, 255), 3, balls);    
              balls[z].vx=random(-3, -2); 
              balls[z].vy=0;
            }
  for (int i=0; i<400; i++) {
    estrelas[i] = new Estrelas(random(width), random(500), 2);
  }
  for (int i=0; i<400; i++) {
    estrelas[i].desenha();
  }
  fill(255);
  textSize(20);
  text("Start", width/2, height/2, 50, 50);
  minim = new Minim(this);
  player = minim.loadFile("Melodic Dubstep.mp3");
}

void draw() {
  player.play();
  if (startpremido) {
    int passedTime=millis()-savedTime;
    timer=passedTime/1000;
    background(0);
    for (int i=0; i<400; i++) {
      estrelas[i].desenha();
    }
    strokeWeight(0);
    fill(255);
    rect(0, 450, width, 50);
    strokeWeight(5);
    stroke(255);
    line(350, 0, 350, 20);
    line(650, 0, 650, 20);
    textSize(10);
    fill(0);
    text("Bola Amarela - 20 pontos", 10, 465);
    text("Bola Vermelha - 30 pontos", 10, 480);
    text("Bola Azul - 50 pontos", 10, 495);
    textSize(15);
    text("Aos 60 segundos o jogo acaba! Boa Sorte!", 550, 480);
    if(passedTime>=totalTime-10000) fill(255,0,0);
    else fill(0);
    textSize(40);
    text(timer, 900, 490);
    for (Ball ball : balls) {
      ball.collide();
      ball.move(); 
      ball.display();
    }
    for (int i=0; i<8; i++) {
      balls[0]=new Ball(constrain(mouseX, 50, width-50), constrain(mouseY, 0+50, 450-50), 100, i, color(255), 0, balls);
      balls[1]=new Ball(constrain(mouseX, 50, width-50), constrain(mouseY, 0+50, 450-50), 90, i, color(0), 0, balls);
      balls[2]=new Ball(constrain(mouseX, 50, width-50), constrain(mouseY, 0+50, 450-50), 80, i, color(255), 0, balls);
      balls[3]=new Ball(constrain(mouseX, 50, width-50), constrain(mouseY, 0+50, 450-50), 70, i, color(0), 0, balls);
      balls[4]=new Ball(constrain(mouseX, 50, width-50), constrain(mouseY, 0+50, 450-50), 60, i, color(255), 0, balls);
      balls[5]=new Ball(constrain(mouseX, 50, width-50), constrain(mouseY, 0+50, 450-50), 50, i, color(0), 0, balls);
      balls[6]=new Ball(constrain(mouseX, 50, width-50), constrain(mouseY, 0+50, 450-50), 40, i, color(255), 0, balls);
      balls[7]=new Ball(constrain(mouseX, 50, width-50), constrain(mouseY, 0+50, 450-50), 30, i, color(0), 0, balls);
    }
    if (passedTime>totalTime) {
      noLoop();
      player.close();
    }
    if (balls[8].x>345 && balls[8].x<655 && balls[8].y==-25) score=score+50;
    if (balls[9].x>345 && balls[9].x<655 && balls[9].y==-25) score=score+50;
    if (balls[10].x>345 && balls[10].x<655 && balls[10].y==-15) score=score+30;
    if (balls[11].x>345 && balls[11].x<655 && balls[11].y==-15) score=score+30;
    if (balls[12].x>345 && balls[12].x<655 && balls[12].y==-15) score=score+30;
    if (balls[13].x>345 && balls[13].x<655 && balls[13].y==-15) score=score+30;
    if (balls[14].x>345 && balls[14].x<655 && balls[14].y==-10) score=score+20;
    if (balls[15].x>345 && balls[15].x<655 && balls[15].y==-10) score=score+20;
    if (balls[16].x>345 && balls[16].x<655 && balls[16].y==-10) score=score+20;
    if (balls[17].x>345 && balls[17].x<655 && balls[17].y==-10) score=score+20;

    fill(0);
    textSize(40);
    if(score<0) fill(255,0,0);
    else fill(0);
    text(score, 430, 490);

    if(passedTime>=totalTime-1000){
      gravity=0.3;
      friction=1;
    }
  }
}

void mousePressed() {  
  if ((mouseX > width/2) && (mouseX < width/2+40) && (mouseY > height/2) && (mouseY <height/2+20)) {
    startpremido=true;
    savedTime=millis();
    noCursor();
  }
}

class Ball {

  float x, y;
  float diameter;
  float vx = 0;
  float vy = 0;
  int id;       
  color c;
  int s;
  Ball[] others; 

  Ball(float xin, float yin, float din, int idin, color cor, int stroke, Ball[] oin) {
    x = xin;
    y = yin;
    diameter = din;
    id = idin;
    c=cor;
    s=stroke;
    others = oin;
  } 

  void collide() {   
    for (int i = id + 1; i < numBalls; i++) {    
      float dx = others[i].x - x;  
      float dy = others[i].y - y;
      float distance = sqrt(dx*dx + dy*dy);
      float minDist = others[i].diameter/2 + diameter/2;
      if (distance < minDist) {   
        float angle = atan2(dy, dx);  
        float targetX = x + cos(angle) * minDist; 
        float targetY = y + sin(angle) * minDist;
        float ax = (targetX - others[i].x) * spring;   
        float ay = (targetY - others[i].y) * spring;
        vx -= ax;
        vy -= ay;
        others[i].vx += ax; 
        others[i].vy += ay;
      }
    }
  }

  void move() {  
    vy += gravity;
    x += vx;
    y += vy;
    if (x + diameter/2 > width) {
      x = width - diameter/2;
      vx *= friction;
    } else if (x - diameter/2 < 0) {
      x = diameter/2;
      vx *= friction;
    }
    if (y + diameter/2 > 450) {
      y = 450 - diameter/2;
      vy *= friction;
    } 
    if (y+diameter/2<=-25) {
      y=random(50,450);
      int prob = int(random(1, 3));
      if (prob<2) {
        x=0;
        vy=0;
        vx=random(2, 3);
      } else {
        x=width;
        vy=0;
        vx=random(-3, -2);
      }
    }
  }

  void display() {
    strokeWeight(s);
    stroke(255);
    fill(c);
    ellipse(x, y, diameter, diameter);
  }
}

class Estrelas{

  float x;
  float y;
  float w;

  Estrelas(float xi, float yi, int wi){
    x=xi;
    y=yi;
    w=wi;
  }

  void desenha(){
    noStroke();
    fill(255,255,0);
    ellipse(x,y,w,w);
  }
}

So i have this code that corresponds to a game and i want it to add points to the score each time the balls pass through the two lines, but that is not happening and i don't know why... I would appreciate some help.

Tagged:

Answers

Sign In or Register to comment.