MrThoe

con2

Jan 17th, 2023
1,225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let num, w;
  2. let cell = [];  //Declare the array
  3. function setup() {
  4.   createCanvas(400, 400);
  5.   num = 3;
  6.   for(let i = 0; i < num; i++){
  7.     let arr = [];
  8.     for(let j = 0; j < num; j++){
  9.       arr.push(new Cell(i,j));
  10.     }
  11.     cell.push(arr);
  12.   }
  13.   w = width/num;
  14. }
  15. function draw() {
  16.   background(220);
  17.   for(let i = 0; i < num; i++){
  18.     for(let j = 0; j < num; j++){
  19.       cell[i][j].show();
  20.     }
  21.   }
  22. }
  23.  
  24. class Cell{
  25.   constructor(x,y){
  26.     this.x = x;
  27.     this.y = y;
  28.     this.count = 0;
  29.     if(random(2)>1){
  30.       this.isAlive = true;
  31.     } else {
  32.       this.isAlive = false;
  33.     }
  34.   }
  35.  
  36.   show(){
  37.     if(this.isAlive){
  38.       fill(255);
  39.     }  else {
  40.       fill(0);
  41.     }
  42.     rect(this.x*w, this.y*w, w, w);
  43.   }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment