xxxxxxxxxx
64
let spots = [];
let cols = 5;
let rows = 7;
let scl;
function setup() {
createCanvas(500, 700);
scl = 100;
for (let x = 0; x < cols; x++) {
spots.push([])
for (let y = 0; y < rows; y++) {
spots[x].push(new Spot(x,y));
}
}
}
function draw() {
background(220);
for (let x = 0; x < cols; x++) {
for (let y = 0; y < rows; y++) {
spots[x][y].show();
}
}
}
class Spot {
constructor(x,y) {
this.x = x;
this.y = y;
this.toShow = false;
}
show() {
if (this.toShow) {
fill(0);
square(this.x*scl,this.y*scl,scl);
} else {
fill(255);
square(this.x*scl,this.y*scl,scl);
}
}
}
function mousePressed() {
let x = floor(mouseX/scl);
let y = floor(mouseY/scl);
spots[x][y].toShow = !spots[x][y].toShow;
}
function keyPressed() {
let ret = [];
for (let x = 0; x < cols; x++) {
for (let y = 0; y < rows; y++) {
if (spots[x][y].toShow) {
ret.push([x- 2,y-3]);
}
}
}
console.log(JSON.stringify(ret, null, 1))
}