mositech / CS2015

All course materials for MOSI Home School Computer Science will be stored here
2 stars 1 forks source link

HW #24 #54

Open chasestarr opened 8 years ago

chasestarr commented 8 years ago

In this homework we will practice using class inheritance. Use this week's exercise for help.

_Description:_ Just like last week, make your own project! The only requirement is to create at least 3 different variations of the particle object. Look at the example from this week for help.

Brandonsugar commented 8 years ago

Do they need to be removed? I seem to be having trouble with that aspect.

The-Space-Core commented 8 years ago

ParticleSystem ps;

void setup(){ size(800,1000);

ps = new ParticleSystem(new PVector(400,200)); } void draw(){

background(255);

ps.run(); ps.addParticle(); }

The-Space-Core commented 8 years ago

class Confetti extends Particle { color c; Confetti(PVector l){

super(l); c = color(random(255),random(255), 0); } void display(){ rectMode(CENTER); fill(255,255,0); stroke(0); rect(location.x,location.y,8,8); } }

The-Space-Core commented 8 years ago

class Face extends Particle{ Face(PVector l) { super(l); } void display(){ fill(255,0,0); ellipse(location.x,location.y,5,5); } }

The-Space-Core commented 8 years ago

class Particle{ PVector location; PVector velocity; PVector acceleration; int lifespan;

Particle(PVector l){ acceleration = new PVector(0, 0.05); velocity = new PVector(random(-2, 2), random(-6, 0)); location = l.get(); lifespan = 255; }

void run(){ update(); display(); }

void update(){ velocity.add(acceleration); location.add(velocity); lifespan -= 1; }

void display(){ stroke(lifespan); fill(175,lifespan); ellipse(location.x, location.y, 8, 8); } void applyForce(PVector force){ acceleration.add(force); } boolean isDead(){ if(lifespan>0){ return false; }else{ return true; } }

}

The-Space-Core commented 8 years ago

class ParticleSystem{ ArrayList particles; PVector origin; //ArrayList plist; ParticleSystem(PVector location){ origin = location.get(); particles = new ArrayList(); }

void addParticle(){ float r = random(1); println(r); if(r < 0.5){ particles.add(new Face(origin)); } else { particles.add(new Confetti(origin)); } }

void run(){ for(int i = 0; i < particles.size(); i++){ Particle p = particles.get(i);

p.run(); if (p.isDead()){ println("rent in panama"); particles.remove(i); }

} } }

chasestarr commented 8 years ago

@brandonsugar you do not need to delete the objects

Brandonsugar commented 8 years ago

Tab 1;

ParticleSystem ps = new ParticleSystem(300,300);
PVector spon = new PVector(0,0.01);

void setup(){
  size(1000,1000);
}

void draw(){
  background(0);
  ps.run();
  ps.addParticle();
  ps.center = new PVector(mouseX,mouseY);

}

void keyPressed(){
if(keyCode == RIGHT){
  ps.applyForce(new PVector(0.25,0));
}
if(keyCode == LEFT){
  ps.applyForce(new PVector(-0.25,0));
}
if(keyCode == UP){
  ps.applyForce(new PVector(0,-0.25));
}
if(keyCode == DOWN){
  ps.applyForce(new PVector(0,0.25));
}
}

Tab 2;

class Kitty extends Particle{
  int lifeSpan;

  Kitty(PVector l){
    super(l);
    lifeSpan = 255;
  }
  void display(){
    rectMode(CENTER);
    fill(255,200,200,lifeSpan);
    stroke(0, lifeSpan);
    //head
    rect(location.x,location.y,30,20);
    //whiskers
    fill(0, lifeSpan);
    line(location.x+7,location.y+2,location.x+10,location.y+1);
    line(location.x+7,location.y,location.x+10,location.y-1);
    line(location.x+7,location.y-2,location.x+10,location.y-3);
    line(location.x-7,location.y+2,location.x-10,location.y+1);
    line(location.x-7,location.y,location.x-10,location.y-1);
    line(location.x-7,location.y-2,location.x-10,location.y-3);
    //eyes
    ellipse(location.x+4,location.y-4,3,3);
    ellipse(location.x-4,location.y-4,3,3);
    //mouth
    ellipse(location.x,location.y+5,6,3);
    //ears
    fill(255,200,200, lifeSpan);
    triangle(location.x+5,location.y-10,location.x+15,location.y-10,location.x+12.5,location.y-20);
    triangle(location.x-5,location.y-10,location.x-15,location.y-10,location.x-12.5,location.y-20);
  }
  Boolean isDead(){
        if(lifeSpan<0){
      return true;
    } else { 
      return false;
    }
  }
}

Tab 3;

class Particle{
  PVector location;
  PVector acceleration;
  PVector velocity;
  int lifeSpan;

  Particle(PVector l){
    location = l.get();
    velocity = new PVector(random(-7,7),random(-2,2));
    acceleration = new PVector();
    acceleration.mult(0);
    lifeSpan = 255;
  }
  void display(){
    stroke(0,lifeSpan);
    fill(random(1,255),random(1,255),random(1,255),lifeSpan);
    ellipse(location.x,location.y,20,20);
  }
  void update(){
    velocity.add(acceleration);
    location.add(velocity);
    lifeSpan -= 1;
    velocity.limit(15);
  }
  void run(){
    update();
    display();
    borders();
  }
  void applyForce(PVector f){
    acceleration.add(f);
  }
  void borders(){
    if(location.x > width){
      velocity.x *= -0.98;
    } else if(location.x < 0){
      velocity.x *= -0.98;
    } 
    if(location.y > height){
      velocity.y *= -0.98;
    } else if(location.y < 0){
      velocity.y *= -0.98;
    }
  }
  Boolean isDead(){
    if(lifeSpan<0){
      return true;
    } else { 
      return false;
    }
}
}

Tab 4;

class ParticleSystem{
  ArrayList<Particle> PList;
  PVector center;

  ParticleSystem(float xLoc, float yLoc){
    PList = new ArrayList<Particle>();
    center = new PVector(xLoc,yLoc);
  }

  void addParticle(){
    PList.add(new Particle(center));
    PList.add(new Kitty(center));
    PList.add(new Party(center));
  }

  void applyForce(PVector f){
     for(int index = 0; index < PList.size(); index++){
      Particle p = PList.get(index);
    p.applyForce(f);
  }
  }

  void run(){
    for(int index = 0; index < PList.size(); index++){
      Particle p = PList.get(index);
      p.run();
      p.applyForce(spon);
      if(p.isDead()){
       PList.remove(index);
      }
    }
  }
}

Tab 5;

class Party extends Particle{

  Party(PVector l){
    super(l);
  }
  void display(){
    rectMode(CENTER);
    fill(random(255),random(255),random(255),lifeSpan);
    stroke(0,lifeSpan);
    triangle(location.x+10,location.y,location.x-10,location.y,location.x,location.y-40);
  }
  Boolean isDead(){
     if(lifeSpan<0){
      return true;
    } else { 
      return false;
    }
}
}