In this programming assignment, you will write an active Processing program that uses nested loops to display a grid of balls that follows the mouse. The number of columns of balls will increase each time the mouse is clicked. The number of rows of balls will increase each time a key is pressed. If the number of rows or columns exceeds a maximum (stored in constants), that number will reset to 1.
this is what i have so far but i dont know how to implement loops for the ball grid
final int MAX_COLUMNS = 15;
final int MAX_ROWS = 10;
final int BALL_SIZE = 30;
int numColumns = 1;
int numRows = 1;
void setup() {
size(900, 600);
}
void draw() {
background(200);
drawBall();
}
void mouseClicked(){
numColumns = numColumns % MAX_COLUMNS +1;
}
void keyPressed(){
numRows = numRows % MAX_ROWS +1;
}
void drawBall(){
noStroke();
ellipse(mouseX,mouseY,BALL_SIZE,BALL_SIZE);
}
Khan academy is a good place if you need to understand for loops. Use the videos and live coding environment to practice. The code there will be easily transferable
Asking for helps on assignments is a difficult one as some people do spend a good bit of time to produce a decent answer for the poster. However boundaries of responsibility are broken when a response completely fulfils the assignment, and little effort has been made by the poster to attempt the assignment.
Assignments are not just made for you to pass but for you to learn.
So we can help but in a more limited capacity as were not here to complete your homework assignment
Ok sounds good, my intention wasn’t to get code back but rather how to apply it in my case because I understand how loops work but I just dont get how to apply them in my situation.
A for loop allows you to draw a one dimensional structure, of lines shapes etc. A nested for loop, or a for loop inside another for loop allows you to 2 dimensions. A grid is two dimensional therefore 2 for loops.
For loops require a start and an end
Such as
For (int i = 0;i<20; i ++){
. draw something
}
This will draw something 20 times
The “i” is a useable variable here so it can be used to displace the shape/line if you use
x = 0 + 20 * i
This would draw things spaced out in the x direction by 20
int i = 0 // this is for the intial vaule to start at correct?
i<20 // this is for what i needs to reach in order for the loop to end?
i++ // i have no idea why this is here, maybe to tell the program to add one int whenever something occurs?
and then when you were explaining the x = 0 + 20 * i
i didnt understand that.
I++ is the increment value and is always 1 if used like this.
You can use i+= and then set the step size to whatever you want it to be.
The rest were for the x coordinates of whatever you are going to draw.
To draw something you want to know what you are drawing so,
rect()
Iine()
Etc
The functions each take variables ie
Rect takes (x,y,width,height)
Line takes (x1,y1,x2,y2)
So if you want to change where you draw something in a for loop you need to change the x and y values or it will just draw it in the same place 20 times