MrThoe

Kitty Array

Oct 8th, 2020
1,388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /** My First Class
  2.   *
  3.   * This is abouts cats
  4.   * @Author: Allen Thoe
  5.   */
  6. var myCats = [];
  7.  
  8. function setup()
  9. {
  10.   createCanvas(400, 400);
  11.   //Populate the array with Cat Objects
  12.   for(var i = 0; i < 100; i++)
  13.   {
  14.       myCats[i] = new Cat();
  15.   }
  16.  
  17. //   myCats[0] = new Cat();  //First CAT
  18. //   myCats[1] = new Cat();  //SECOND CAT
  19. }
  20.  
  21. function draw() {
  22.   background(220);
  23.   for(var i = 0; i < myCats.length; i++)
  24.   {
  25.       myCats[i].show();
  26.   }
  27.  
  28. }
  29.  
  30. function mousePressed(){
  31.   for(var i = 0; i < myCats.length; i++)
  32.   {
  33.       myCats[i].eat();
  34.   }  
  35. }
  36.  
  37. /* This class will have red color and age*/
  38. class Cat{
  39.   constructor()
  40.   {
  41.     this.red = random(255); //RED
  42.     this.blue = random(255);
  43.     this.age = 0;
  44.     this.x = random(width);  //position
  45.     this.y = random(height);
  46.   }
  47.   eat()
  48.   {
  49.     this.age += 1;
  50.   }
  51.   show()
  52.   {
  53.     fill(this.red, 0, this.blue);
  54.     rect(this.x, this.y, 20, 20);
  55.     fill(0);
  56.     text("MY AGE: " + this.age, this.x +40, this.y+10);
  57.   }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment