I'm trying to use arrarys to make circles fall


#1

Hello!
I’m new to processing and coding in general and for our final project I want to make a game where circles move across the screen horizontally and you have to avoid them. We didn’t learn how to use arrays, but I want to use them to do this.

I managed to get them coming from different y-coordinates but am not sure how to make a new array to get them to be at different x coordinates, I’m currently just using a variable for the x coordinates. Right now the next batch of circles only comes when all the circles cross the window, but I want to have the circles come from random x-coordinates and only reset individual circles when it crosses the window

Sorry for wording this so badly lol

This is what I have so far:

int [] num=new int [5];

int x=int(random(-200, -100));

int ballx=mouseX;

int bally=mouseY;

void setup() {

size(400, 400);

}

void draw() {

background(255);

for (int i=0; i<5; i++) //makes the circle

{

ellipse(x, num[i], 40, 40);

x++;



ellipse(mouseX, mouseY, 20, 20);

if (dist(mouseX, mouseY, x, num[i])<=40)

  println("you lose");

   if (x>t;=width)

{

x=int(random(-200, -100));

for ( i=0; i<;5; i++)     //makes the location of the circles different

  num[i]=int(random(height));

}

}

}


#2

Try using PVector()


#4
PVector[] ellX = new PVector[5];// circles that move in X
PVector[] ellY = new PVector[5];// circles that move in y
void setup() {
  size(400, 400);
  for (int i = 0; i < 5; i++) {
    ellX[i] = new PVector(random(-200, -100), random(0, height));
    ellY[i] = new PVector(random(0, width), random(-200, -100));
  }
}

void draw() {
  background(255);
  //draw cirles
  for (int i = 0; i < 5; i++) {
    ellipse(ellX[i].x, ellX[i].y, 20, 20);
    ellipse(ellY[i].x, ellY[i].y, 20, 20);
  }
  //move them
  for (int i = 0; i < 5; i++) {
    ellX[i].x++;
    ellY[i].y++;
    float dist1 = dist(ellX[i].x, ellX[i].y, mouseX, mouseY);
    float dist2 = dist(ellY[i].x, ellY[i].y, mouseX, mouseY);
    if (dist1 <= 20 || dist2 <= 20) {
      println("YOU LOSE");
    }
  }
  //reset circle if it reeached the end
  for (int i = 0; i < 5; i++) {
    if(ellX[i].x >= width){
      ellX[i] = new PVector(random(-200, -100), random(0, height));
    }
   if(ellY[i].y >= height){
      ellY[i] = new PVector(random(0, width), random(-200, -100));
    }
  }
}

This works, I hope you understand something


#5

Thank you!
This is what I’m trying to do, but is there a way to do this without pvector? I haven’t learned how to use that


#6

So, PVector() is something that exist in Processing.
you have two ways
1 ) Create a class with float x and y,(this is what a PVector is), but if you have never used arrays, I think you dont know what classes are
2 ) ellXx store ellipse that move on X x coordinate
ellXy store ellipse that move on X y coordinate
ellYx …
ellYy…
you get the idea right? 2 arrays that stores X and Y of circles that move on X and the same for circles that move on Y


#7

and if the player gets hit, you can usetext(YOU LOSEx,y) delay() and exit()