xxxxxxxxxx
35
let cols; let rows; let size = 20;
let grid = [];
function setup() {
createCanvas(400, 400);
cols = width/size;
rows = height/size;
rectMode(CENTER);
for (let i=0; i<cols; i++) {
grid[i] = [];
for (let j=0; j<rows; j++) {
grid[i][j] = new Rect(size/2 + i*size, size/2 + j*size);
}
}
}
function draw() {
background(220);
let x = floor(mouseX / size);
let y = floor(mouseY / size);
for (let i=-1; i<2; i++) {
for (let j=-1; j<2; j++) {
let xIndex = x + i;
let yIndex = y + j;
xIndex = constrain(xIndex, 0, cols-1);
yIndex = constrain(yIndex, 0, rows-1);
grid[xIndex][yIndex].display();
grid[xIndex][yIndex].life = 255;
}
}
}