/* The Mover class is a bsic class that just handles some physics math It contains position, speed, acceleration and the ability to use them. I've even upgraded it to include wind and gravity. This program is designed to show 2 options when making classes and objects Using classes by making simply creating objects Designing classes to be based on simpler classes (called Inheritance) If someone in an interview asked about polymorphism, this should be part of your answer. --> More on than in a future course :) */ //This class is designed to be used as part of other class definitions class Mover { float x, y, sx, sy, ax, ay; Mover(float a, float b) { //Look at all these beautiful default values this.x = a; this.y = b; this.sx = 0; this.sy = 0; this.ax = 0; this.ay = 0; } void move() { this.x += this.sx; this.y += this.sy; this.sx += this.ax; this.sy += this.ay; this.sx += -0.05; //Some wind because its cool this.ay += 0.1; //Some gravity because its also cool } } /* This class does NOT inherit anything. It just makes use of another class. Just like our older PEWPEW example, it like a turret making bullets, not a turret that is part bullet */ class Poof { int x, y, fade; Mover[] particle; //Uses movers, but is not based on the Mover class Poof(int a, int b, int size) { this.x = a; this.y = b; this.particle = new Mover[size]; //Makes some movers //Create all the Mover particles that make up the little sparkles for (int i=0; i < size; i++) { this.particle[i] = new Mover(a, b); this.particle[i].sx = random(10) - 5; this.particle[i].sy = random(4) - 2; } this.fade = 50 + int(random(50)); //Neat fade effect } //This method handles operating its particles with some fade effects for fun void render() { for (int i=0; i < this.particle.length; i++) { fill(155 + this.fade); //Make the colour change for fun fade effect this.fade--; if (this.fade < 0) this.fade = 0; this.particle[i].move(); //Use the particles move method rect(this.particle[i].x, this.particle[i].y, 10, 10); //Draw the particle } } } /* This class is different. This class is everything that makes a Mover, but with extra stuff 'extends' borrows everything contained for the PARENT or SUPER class That makes all Fireworks come with everything from Mover (position, speed, physics, gravity) All Firework adds is target coordinates, aiming, drawing, and the ability to create some Poof */ class Firework extends Mover { float tx, ty; Firework (float a, float b, float c, float d) { super(a, b); //Since Firework is a Mover with extra, we call the Mover contructor first //then we can add stuff to it this.tx = c;//Target coordinates this.ty = d; } void render() { this.move(); //Uses the move that is got from its parent Mover //Guidance to make it head to target //Notice I can access acceleration variables because I borrwed them from Mover if (this.x < this.tx) this.ax = 0.1; else this.ax = -0.1; if (this.y < this.ty) this.ay = 0.1; else this.ay = -0.2; //I made it a bigger value to try and counteract gravity fill(255, 0, 0); rect(this.x, this.y, 10, 20); //Draw a rectangle //Detect when near target, explode a new Poof object if (dist(this.x, this.y, this.tx, this.ty) < 20) { sparky.add(new Poof(floor(this.x), floor(this.y), int(5 + random(10)))); this.x = -1000; //Get out of the universe so its not too crowded } } } ArrayList sparky = new ArrayList(); ArrayList sauce = new ArrayList(); //Thanks thomas, its the best 'sauce' void setup() { size(500, 500); } void draw() { fill(0); rect(0, 0, 500, 500); //Handles all the Poof objects for (int i=0; i < sparky.size(); i++) sparky.get(i).render(); //Handles all the fireworks, its called sauce because I let a student name it for (int i=0; i < sauce.size(); i++) sauce.get(i).render(); //Remove all the fireworks that have reached their targets for (int i=0; i < sauce.size(); i++) { if (sauce.get(i).x < 0) sauce.remove(i); } } void mousePressed() { //Make Poofs //sparky.add(new Poof(mouseX, mouseY, int(5 + random(10)))); //Make fireworks sauce.add(new Firework(250, 500, mouseX, mouseY)); } /* Practice Problems Write a class Mammal, give it 3 variables and 2 methods that ALL mammals can do. Now make a class Dog that INHERITs Mammal, but has 3 new variable and 1 method that dogs do. Now make a class Cat that INHERITs Mammal, but has 3 new variable and 1 method that cats do, but not dogs. Example: ResistPetting(), dogs never resist petting ================================================================================= Create a class called Character -- 5 variables -- 3 methods This is for any common character in a game or show or whatever Create a new class called Hero All Heros are Characters but with extra stuff only Heros can do. -- 2 variables -- 1 method Create a new class called Villian just like you did for Hero Again, only put in new variables / methods that are unique to Villians Create a few villians for you hero to 'deal' with --> thay can just be coloured rectangles, just have fun with it Upgrade the Character class for another variable and another method of your choice Make some use of those extras in both you Heros and Villians without updating either class. */