Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

スクラッチ!

マウスをクリックすると,スクラッチできる.

/**
* Scratch
*
* @author aa_debdeb
* @date 2015/09/13
*/

float CELL_SIZE = 10;
boolean[][] isScratched;

void setup(){
  size(500, 500);
  smooth();
  noStroke();
  frameRate(30);
  
  isScratched = new boolean[int(height / CELL_SIZE)][int(width / CELL_SIZE)];
  for(int h = 0; h < int(height / CELL_SIZE); h++){
    for(int w = 0; w < int(width / CELL_SIZE); w++){
      isScratched[h][w] = false;
    }
  }
}

void draw(){
  for(int h = 0; h < int(height / CELL_SIZE); h++){
    for(int w = 0; w < int(width / CELL_SIZE); w++){
      if(isScratched[h][w]){
        fill(255);
      } else {
        fill(100);
      }
      rect(w * CELL_SIZE, h * CELL_SIZE, CELL_SIZE, CELL_SIZE);
    }
  }
  int currentH = int(mouseY / CELL_SIZE);
  int currentW = int(mouseX / CELL_SIZE);
  if(isInFrame(currentH, currentW) && mousePressed && !isScratched[currentH][currentW]){
    isScratched[currentH][currentW] = true;
  }
}

boolean isInFrame(int h, int w){
 if(0 <= h && h < int(height / CELL_SIZE) && 0 <= w && w < int(width / CELL_SIZE)){
   return true;
 } else {
   return false;
 }
}