How would I fill a cell red if the mouse is over a cell equal to 1 or 2, and green if 0?

How would I fill a cell red if the mouse is over a cell on the path or on a rock, and green if not?

PImage map;

int[][] grid = 
  {
  // 0  1  2  3  4  5  6  7  8 9 10 11 12 13 14
  {0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1}, //0
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //1
  {0, 1, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //2
  {0, 0, 2, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0}, //3
  {0, 0, 2, 0, 0, 2, 0, 0, 0, 2, 2, 2, 2, 2, 2}, //4
  {2, 2, 2, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0}, //5
  {0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0}, //6
  {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //7
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //8
  {1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0}, //9
};

int cols = 10;
int rows = 15;


void setup() {

  size(900, 600);

  map = loadImage("map.png");
  map.resize(900, 600);
}

void draw() {

  background(map);

  for (int j = 0; j < cols; j ++) {
    for (int i = 0; i < rows; i ++) {

      noFill();
      rect(i * 60, j * 60, 60, 60);

      if (mouseX <= i * 60 + 60 && mouseX >= i * 60 && mouseY <= j * 60 + 60 && mouseY >= j * 60 && grid [j][i] == 1 || grid [j][i] == 2) {
        fill(255, 0, 0, 20);
      } else if (mouseX <= i * 60 + 60 && mouseX >= i * 60 && mouseY <= j * 60 + 60 && mouseY >= j * 60 && grid [j][i] == 0)
        fill(0, 255, 0, 20);
    }
  }
}
Tagged:

Answers

  • Fixed it, lmao, i'm stupid, I did fill without putting what I wanted to fill.

  • It happens. I would guess that most of us have done that at least once -- some many, many times.

    I'm assuming that something like moving rect(i * 60, j * 60, 60, 60); below your if() block solved the problem....

  • Fill must be before the rect, yes

Sign In or Register to comment.