Arrays and Functions

Hello,

I have created the code for what i want to be the beginning of a ball eating game but I am just learning about functions and arrays as we speak so my program does not work. All it does is draw one circle in the top right corner and thats it.

final int TEAMS = 3;
final int NUMBER_BALLS = 7;
//team 1
float[] ballXOne = new float[ NUMBER_BALLS ],
        ballYOne = new float[ NUMBER_BALLS ];
//team 2        
float[] ballXTwo = new float[ NUMBER_BALLS ],
        ballYTwo = new float[ NUMBER_BALLS ]; 
//team 3
float[] ballXThree = new float[ NUMBER_BALLS ],
        ballYThree = new float[ NUMBER_BALLS ];


int[] ballR = new int[ NUMBER_BALLS ],
      ballG = new int[ NUMBER_BALLS ],
      ballB = new int[ NUMBER_BALLS ];
float ballSize = random(10,25);

void setup(){
  size(500,500);
}


void randomizeBallOne(float ballXOne[], float ballYOne[]){
   for ( int i = 0; i < NUMBER_BALLS; i++ ) {
    ballXOne[i] = random(ballSize/2,width-ballSize);
    ballYOne[i] = random(ballSize/2,height-ballSize);
   }
}
void randomizeBallTwo(float[] ballXTwo, float[] ballYTwo){
   for ( int i = 0; i < NUMBER_BALLS; i++ ) {
    ballXTwo[i] = random(ballSize/2,width-ballSize);
    ballYTwo[i] = random(ballSize/2,height-ballSize);
   }
}

void randomizeBallThree(float[] ballXThree, float[] ballYThree){
   for ( int i = 0; i < NUMBER_BALLS; i++ ) {
    ballXThree[i] = random(ballSize/2,width-ballSize);
    ballYThree[i] = random(ballSize/2,height-ballSize);
   }
}

void drawAllPlayers(){
  for(int i = 0; i < NUMBER_BALLS; i++){
    fill(0,0,255);
    ellipse(ballXOne[i],ballYOne[i],ballSize,ballSize);
  }
  for(int i = 0; i < NUMBER_BALLS; i++){
    fill(0,255,0);
    ellipse(ballXTwo[i],ballYTwo[i],ballSize,ballSize);
  }
  for(int i = 0; i < NUMBER_BALLS; i++){
    fill(255,0,0);
    ellipse(ballXThree[i],ballYThree[i],ballSize,ballSize);
  }
}

void draw(){
  background(125);
  drawAllPlayers();
}

How do i get it to properly draw the circles all at random positions, I feel like this is a simple issue but I just do not know how to fix it.

Answers

  • where do you set up the random ball positions?

  • each function randomizeBallOne,Two,Three does it but then I don't know how to get them to work after that

  • each function randomizeBallOne,Two,Three does it

    but where do you call them?

  • You need to call them otherwise they don’t run

  • okay but where do i call them? This is what I don't know what to do

  • I made some changes to your code, you only need one function, and one loop. you also need to call the function like this: function(variable1,variable2); I also added unique colours to the different teams.

    final int TEAMS = 3;
    final int NUMBER_BALLS = 7;
    //team 1
    float[] ballXOne = new float[ NUMBER_BALLS ], 
      ballYOne = new float[ NUMBER_BALLS ];
    //team 2        
    float[] ballXTwo = new float[ NUMBER_BALLS ], 
      ballYTwo = new float[ NUMBER_BALLS ]; 
    //team 3
    float[] ballXThree = new float[ NUMBER_BALLS ], 
      ballYThree = new float[ NUMBER_BALLS ];
    
    
    int[] ballR = new int[ NUMBER_BALLS ], 
      ballG = new int[ NUMBER_BALLS ], 
      ballB = new int[ NUMBER_BALLS ];
    float ballSize = random(10, 25);
    
    void setup() {
      size(500, 500);
      randomizeBall(ballXThree, ballYThree);
      randomizeBall(ballXTwo, ballYTwo);
      randomizeBall(ballXOne, ballYOne);
    }
    void randomizeBall(float ballx[], float bally[]) {
      for ( int i = 0; i < NUMBER_BALLS; i++ ) {
        ballx[i] = random(ballSize/2, width-ballSize);
        bally[i] = random(ballSize/2, height-ballSize);
      }
    }
    
    void drawAllPlayers() {
      for (int i = 0; i < NUMBER_BALLS; i++) {
        fill(0, 0, 255);
        ellipse(ballXOne[i], ballYOne[i], ballSize, ballSize);
        fill(255, 0, 0);
        ellipse(ballXTwo[i], ballYTwo[i], ballSize, ballSize);
        fill(0, 255, 0 );
        ellipse(ballXThree[i], ballYThree[i], ballSize, ballSize);
      }
    }
    
    void draw() {
      background(125);
      drawAllPlayers();
    }
    
  • I recommend using classes and arraylists so that you can control the balls individually

  • This is my attempt using classes.

    Kf

    final int N_TEAMS = 3;
    ArrayList<TeamBall> teams;
    
    
    void setup() {
      size(500, 500);
      noStroke();
    
      teams=new ArrayList<TeamBall>();
      for (int i=0; i<N_TEAMS; i++) {
        teams.add(new TeamBall());
      }
    }
    
    
    
    void draw() {
      background(125);
    
      for (TeamBall cteam : teams)
        cteam.drawAllPlayers();
    }
    
    
    class Player {
    
      float px, py, rad;
      color col;
    
      Player() {
        rad=15;
        px=random(rad, width-rad);
        py=random(rad, height-rad);
        col=color(random(255),150,random(255));
      }
    
      void update() {
    
        // To be implemented....
      }
    
      void draw() {
        fill(col);
        ellipse(px, py, rad, rad);
      }
    }
    
    
    class TeamBall {
    
      final int NUMBER_BALLS = 7;
    
      int nplayers;
      ArrayList<Player> teamList;
    
      TeamBall() {
        nplayers=NUMBER_BALLS;    
        assignPlayers();
      }
    
      TeamBall(int n) {
        nplayers=n;
        assignPlayers();
      }
    
      void assignPlayers() {
    
        if (teamList==null)
          teamList = new ArrayList<Player>();
        else
          teamList.clear();
    
        for (int i=0; i<nplayers; i++) {
          Player newMember=new Player();
          teamList.add(newMember);
        }
      }
    
      void drawAllPlayers() {
    
        for (int i=0; i<nplayers; i++) {
          teamList.get(i).draw();
        }
      }
    }
    
  • edited April 2018

    I attempted to fix it and shrink the code down using a colour array and 1 variable for each but now my code crashes and i don't know why

    final int TEAMS = 3;
    final int NUMBER_BALLS = 21;
    
    float[] ballSize = new float[ NUMBER_BALLS ];
    
    float[] ballX = new float[ NUMBER_BALLS ], 
            ballY = new float[ NUMBER_BALLS ];
    
    int colour;
    color[] colarray = new color[TEAMS];{
        colarray[0] = color(0,0,255);
        colarray[1] = color(0,255,0);
        colarray[2] = color(255,0,0);
    }
    
    void setup() {
      size(500, 500);
      randomizeBall(ballX, ballY, ballSize, colarray);
    }
    
    void randomizeBall(float ballx[], float bally[],float ballsize[],int colours[]) {
      for ( int i = 0; i < NUMBER_BALLS; i++ ) {
        ballsize[i] = random(5,45);
        ballx[i] = random(25, width-35);
        bally[i] = random(25, height-35);
        colours[i] = int(random(0,2));
      }
    }
    
    void drawAllPlayers() {
      for (int i = 0; i < NUMBER_BALLS; i++) {
        fill(colarray[i]);
        ellipse(ballX[i], ballY[i], ballSize[i], ballSize[i]);
      }
    }
    
    void draw() {
      background(125);
      drawAllPlayers();
    }
    
  • edited April 2018

    It's always useful to tell us the exact error you get

    Here I'm guessing it's because you're trying to use 21 colours that you haven't defined (line 26)

  • edited April 2018

    Oh, looking at it more, you pass in colArray as colours. But colArray is only 3 elements and you're trying to use 21.

    It's easier to use a class than multiple parallel arrays. There's a Common Question post about this (also replies to this post!)

  • Change line 10 to color[] colarray = new color[NUMBER_BALLS];. This will get your code running. Then you need to go back and figure out the proper logic to properly colored your groupings.

    Kf

  • Okay that makes sense and I fixed that, so then isn't the idea that on line 26 now the loop will continue looping and filling each value with a number between 0 and 2. Which you then can set as each colour?

  • edited April 2018

    instead now it just colours 3 balls

    final int TEAMS = 3;
    final int NUMBER_BALLS = 21;
    
    float[] ballSize = new float[ NUMBER_BALLS ];
    
    float[] ballX = new float[ NUMBER_BALLS ], 
            ballY = new float[ NUMBER_BALLS ];
    
    int colour;
    color[] colarray = new color[NUMBER_BALLS];{
    
    }
    void setup() {
      size(500, 500);
      randomizeBall(ballX, ballY, ballSize, colarray);
    }
    void randomizeBall(float ballx[], float bally[],float ballsize[],int colours[]) {
      for ( int i = 0; i < NUMBER_BALLS; i++ ) {
        ballsize[i] = random(5,45);
        ballx[i] = random(25, width-35);
        bally[i] = random(25, height-35);
        colours[i] = int(random(0,2));
        colours[0] = color(0,0,255);
        colours[1] = color(0,255,0);
        colours[2] = color(255,0,0);
      }
    }
    
    void drawAllPlayers() {
    
      for (int i = 0; i < NUMBER_BALLS; i++) {
        fill(colarray[i]);
        ellipse(ballX[i], ballY[i], ballSize[i], ballSize[i]);
    
      }
    }
    
    void draw() {
      background(125);
      drawAllPlayers();
    }
    

    i'm so bad at this stuff :(

  • Line 19 should be colarray[i] = int(colours[i%TEAMS]); Check the modulo operator in the reference. With this change, you are ensuring you have three groups of different color. You also need to move lines 20,21 and 22 to setup():

    colours[0] = color(0,0,255);
    colours[1] = color(0,255,0);
    colours[2] = color(255,0,0);
    

    Kf

  • (btw, you need a blank line between the text and the code or it won't format correctly)

Sign In or Register to comment.