xxxxxxxxxx
40
let changeColor = false; //pressing to change color
function setup() {
createCanvas(400, 400); //canvas background
rectMode(CENTER) //Alignment of cubes
}
function draw() {
background(220); // Light gray background
//Loop pattern
for (var i = 0; i < 20; i++){ // Loop through 20 columns
for (var j = 0; j < 20; j++){ // Loop through 20 rows
//Position and Size of the square
var x = i * 50 + 25
var y = j * 50 + 25
var d = 25
//Changing color when pressed
if (changeColor) {
fill(random(255), random(255), random(255));
} else {
fill(i * j * 5); // Original color
}
rect(x, y, d) //Drawing the squares according to the variable
// Random colors upon pressing
let r = random(100, 255);
let g = random(100, 255);
let b = random(100, 255);
}
}
}
function keyPressed() {
if (keyCode === ENTER) // Pressing Enter Key
changeColor = !changeColor; // Changes color of the square
}