Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** My First Class
- *
- * This is abouts cats
- * @Author: Allen Thoe
- */
- var myCats = [];
- function setup()
- {
- createCanvas(400, 400);
- //Populate the array with Cat Objects
- for(var i = 0; i < 100; i++)
- {
- myCats[i] = new Cat();
- }
- // myCats[0] = new Cat(); //First CAT
- // myCats[1] = new Cat(); //SECOND CAT
- }
- function draw() {
- background(220);
- for(var i = 0; i < myCats.length; i++)
- {
- myCats[i].show();
- }
- }
- function mousePressed(){
- for(var i = 0; i < myCats.length; i++)
- {
- myCats[i].eat();
- }
- }
- /* This class will have red color and age*/
- class Cat{
- constructor()
- {
- this.red = random(255); //RED
- this.blue = random(255);
- this.age = 0;
- this.x = random(width); //position
- this.y = random(height);
- }
- eat()
- {
- this.age += 1;
- }
- show()
- {
- fill(this.red, 0, this.blue);
- rect(this.x, this.y, 20, 20);
- fill(0);
- text("MY AGE: " + this.age, this.x +40, this.y+10);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment