Arrays causing problems?

Hi, I am making a game where balls are being projected (with gravity) and the user has to make the balls bounce and land in a basket. If a ball drops, it should be game over.

There are so many problems with my code (this is just a part of it) and i am unsure what exactly is the cause of them. Ive created balls using arrays like this and i feel like this may be part of the problem:

Ball[] bballs = new Ball[5];

void setup(){
size(640,480);

// create balls
for(int i = 0; i<bballs.length; i++) {
bballs[i] = new Ball();
}
}

For example:

  • When 1 ball falls on floor, game over screen shows WITH game still playing till the other balls drop
  • When all balls fall, it works fine
  • Score will only update with +1 when all balls go in basket instead of +5
  • If 1 ball were to enter basket, ahead of the other balls, no change to score

Because i used an array, are the balls counted as 1 entity/object? Would really appreciate help

1 Like

no, the creation of your 5 bballs ( of type: class Ball ) looks good.
so most probably the program logic causes the problems.

check your logic:
easy with a

println("score: "+score);

so you know that specific IF
was working/executed
and the score is counted up.

Its just going up by 1 when all the balls enter the basket instead of going up by 1 for EACH ball that enters.
The only part of my code that tracks the how many of the balls go in is the above code

and is that a part of the class Ball code?
and how is it updated from draw(){}
and is the “score” a local variable of Ball class or a global variable?

1 Like

Yes that is part of class Ball
Dont know what you mean about how it is updated (sorry beginner-ish)
And it is a local variable of Ball class

Oh i see what the issue, it had to be a global variable. Thank you very much

Now im left with the problem of having:

  • When 1 ball falls on floor, game over screen shows WITH game still playing till the other balls drop
  • When all balls fall, it works fine

and where is that logic?

better just set a global variable
boolean game_over

and set it true if ONE of the bballs
hit the ground.
for that can use in the class a

boolean gameOver() {
  boolean isover = false;
  if((pos.y >= 440)){
    vel.y=0;
    vel.x=0;
    isover = true;
  }
return isover;
}

the gameOverScreen part should anyway be part of draw() ( level thinking )
and not part of class.

how can i set it as a global variable if it takes values FROM the class i.e. pos.y, vel.x ,vel,y.

sorry, too complicated,
same like with score, a global level or screen variable

int many = 5;
Ball[] balls = new Ball[many];
int score=0, level=0;

void setup() {
  size(640, 480);
  for (int i = 0; i<many; i++) balls[i] = new Ball();                     // create balls
}

void draw() {
  background(200, 200, 0);
  if ( level == 0 ) {                                                     // play balls
    for (int i = 0; i<many; i++)  balls[i].draw();
  }
  if ( level == 1 ) {                                                     // game over
    background(#FF943B);
    fill(0, 0, 200);
    text("GAME OVER", width/2, height/2-40);
    text("Press Mouse To Retry", width/2, height/2);
    text("Your Score: " + "$" + score, width/2, height/2+80);
  }
}

class Ball {
  PVector pos, vel;

  Ball() {
    pos = new PVector( random(0, 26), random(0, 5));
    vel = new PVector(random(1.15, 1.2), random(0.2, 1.2));
  }

  void draw() {
    pos.add(vel);  
    circle(pos.x, pos.y, 10);

    if ((pos.y >380) && (pos.x >560)) {                                // resets ball if in basket
      pos.x = random(0, 26);
      pos.y = random(0, 5);
      vel.x = random(1.15, 1.2);
      vel.y = random(0.2, 1.2);
      score += 1;
      println("score "+score);
    }

    if ((pos.y >= 440)) level+= 1;                                      // GAME OVER
  }  // end draw
}   // end class

1 Like

yea… thanks for the help but it tried it and it just complicated the rest of my code.
I feel like maybe there might be another way that is simpler.
I see what you were trying to do though.