The line mouseX!=b[r=10] in your if-statement is attempting to exclude the middle column from being highlighted when clicked. However, there are a few problems with this line:
-
The assignment r=10 is immediately assigning the value 10 to the variable r within the expression. So mouseX!=b[r=10] literally sets r to 10 every time the expression is evaluated, which is not the intended behavior.
-
The condition mouseX!=b[r=10] checks if mouseX is not equal to b[10], which corresponds to the left boundary of the 11th column (zero-indexed). However, even if this line worked as intended, it would only prevent highlighting the exact pixel column where mouseX equals b[10], not the whole middle column.
-
You're checking for mouse clicks directly in the draw function, which is not suitable for discrete event handling, like mouse clicks. Instead, you should use the mousePressed function because mousePressed will handle individual click events, whereas draw runs continuously.
The middle column would be the one at mouseX == 250 because the squares in your grid are 25 pixels wide, and the middle column starts at 250 pixels from the left edge of the window.
Here's a fixed version of your code:
int [] b = {0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 525};
int [][] grid = new int [21][20];
void setup() {
background(250);
size(525, 500);
drawgrid();
}
void draw() {
}
void mousePressed() {
if (mouseButton == LEFT && !(mouseX >= 250 && mouseX < 275)) { // Exclude the middle column
for (int r=0; r<21; r++) {
for (int c=0; c<20; c++) {
if(mouseX>b[r] && mouseX<b[r+1] && mouseY>b[c] && mouseY<b[c+1]) {
fill(50);
square(b[r], b[c], 25);
}
}
}
}
}
void drawgrid() {
for (int r=0; r<525; r+=25) {
for (int c=0; c<500; c+=25) {
fill(100);
square(r, c, 25);
}
}
fill(25);
square(250, 0, 25);
rect(250, 0, 25, 500); // this line will draw the middle column
drawgrid(); // you have to call drawgrid() function again after drawing middle column so not to overwrite the whole column
}
In the mousePressed function, I've set an appropriate conditional statement to check if the mouse is not clicking within the middle column by checking if the mouseX is not between 250 and 275 (since the squares are 25 pixels wide).
It's worth noting that if you click on two squares quickly, the last one will be drawn over the first one, as the grid isn't being redrawn each time you press the mouse. To fix this, you might want to keep track of which squares have been clicked and redraw the entire grid each time with highlights on the clicked squares. This approach requires a more complex state management within your sketch.