xxxxxxxxxx
59
let cellSize, halfCellSize, numCells;
let w, h;
let player;
let cells;
function setup() {
numCells = 25;
setSize();
createCanvas(w, h);
textAlign(CENTER,CENTER);
textFont("Courier");
textSize(cellSize);
cells = [];
for (let r = 0; r < numCells; r++) {
cells[r] = [];
for (let c = 0; c < numCells; c++) {
cells[r][c] = {col: random(255)};
}
}
player = {};
player.r = int(random(0,numCells-1));
player.c = int(random(0,numCells-1));
}
function draw() {
background(220);
for (let r = 0; r < numCells; r++) {
for (let c = 0; c < numCells; c++) {
fill(cells[r][c].col);
rect(c*cellSize,r*cellSize,cellSize,cellSize);
}
}
fill(color(255,0,255));
text("@", player.c*cellSize+halfCellSize,player.r*cellSize+halfCellSize);
frameRate(10);
}
function windowResized() {
setSize();
resizeCanvas(w, h);
}
function setSize() {
w = windowWidth;
h = windowHeight;
if (w > h) w = h;
else h = w;
cellSize = w / numCells;
halfCellSize = cellSize / 2;
textSize(cellSize);
}