We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
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.
I recommend using classes and arraylists so that you can control the balls individually
This is my attempt using classes.
Kf
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
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)
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?
instead now it just colours 3 balls
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 tosetup()
:Kf
(btw, you need a blank line between the text and the code or it won't format correctly)
https://github.com/Kango/Processing-snippets/wiki/Variables,-Arrays-and-object-oriented-programming