xxxxxxxxxx
37
let grid; // Array to store our objects (cells)
let cols, rows; // Number of columns and rows in the grid
let cellSize = 20; // Size of each cell
let flowDirection = 0; // Variable to control the flow direction
function setup() {
createCanvas(600, 600);
cols = width / cellSize; // Calculate the number of columns
rows = height / cellSize; // Calculate the number of rows
grid = [];
// Initialize the grid with Cell objects
for (let i = 0; i < cols; i++) {
grid[i] = [];
for (let j = 0; j < rows; j++) {
grid[i][j] = new Cell(i * cellSize, j * cellSize, cellSize); // Create a new cell for each position
}
}
}
function draw() {
background(0); // Set a dark background
// Draw each cell in the grid
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j].update(); // Update the cell (change its color or motion)
grid[i][j].display(); // Display the cell
}
}
}
function mousePressed() {
// Change the flow direction when the mouse is pressed
flowDirection = (flowDirection + 1) % 2;
}