xxxxxxxxxx
101
let canvas = [];
let h = 100;
let w = 100;
let ch = 4;
let cw = 4;
function setup() {
createCanvas(cw * w, ch * h);
for(let y = 0; y < h; y++){
let rows = [];
for(let x = 0; x < w; x++){
rows.push(0);
}
canvas.push(rows);
}
for(let i = 0; i < 100; i++){
canvas[i][50] = 3;
}
frameRate(60);
noStroke();
}
function draw() {
background(220);
for(let y = h - 1; y >= 0; y--){
for(let x = w - 1; x >= 0; x--){
if(canvas[y][x] < 2){
if(canvas[y][x] === 0){
fill(255);
}
else{
fill(50);
}
}
else{
if(canvas[y][x] === 2){
fill(200, 200, 0);
}
else{
fill(0, 0, 255);
}
}
rect(x * cw, y * ch, cw, ch);
if(canvas[y][x] === 2 && y != h - 1){
if(canvas[y + 1][x] === 0){
canvas[y][x] = 0;
canvas[y + 1][x] = 2;
}
if(canvas[y + 1][x] === 3){
canvas[y][x] = 3;
canvas[y + 1][x] = 2;
}
else if(canvas[y + 1][x - 1] === 0){
canvas[y][x] = 0;
canvas[y + 1][x - 1] = 2;
}
else if(canvas[y + 1][x + 1] === 0){
canvas[y][x] = 0;
canvas[y + 1][x + 1] = 2;
}
}
if(canvas[y][x] === 3){
if(y != h - 1 && canvas[y + 1][x] === 0){
canvas[y][x] = 0;
canvas[y + 1][x] = 3;
}
else if(y != h - 1){
if(random([1, 2]) === 1){
if(canvas[y][x + 1] === 0){
canvas[y][x + 1] = 3;
canvas[y][x] = 0
}
else if(canvas[y][x - 1] === 0){
canvas[y][x - 1] = 3;
canvas[y][x] = 0
}
}
else if(canvas[y][x - 1] === 0){
canvas[y][x - 1] = 3;
canvas[y][x] = 0
}
else if(canvas[y][x + 1] === 0){
canvas[y][x + 1] = 3;
canvas[y][x] = 0
}
}
}
}
}
}