# 14 – Final Project

``````//bgoeke Sec. B Final Project

var gravity = 0.3;   // downward acceleration
var spring = 0.7; // how much velocity is retained after bounce
var drag = 0.0001;    // drag causes particles to slow down
var np = 0;      // how many particles
var rpx = [];
var rpy = [];
var rpc = 10000;
var count = 0;

}

function particleStep() {
this.age++;
this.x += this.dx;
this.y += this.dy;
if (this.x > width - 80 & this.y > height - 100) { // bounce off right wall
this.x = (width - 80) - (this.x - (width-80));
this.dx = 0;
} else if (this.x < 0) { // bounce off left wall
this.x = -this.x;
this.dx = -this.dx * spring;
}

if (this.x < width - 180 & this.y > height - 100) { // bounce off right wall
this.x = (width) - (this.x - (width));
this.dx = 0;
}

if (this.y > height-20) { // bounce off bottom
this.y = (height-20) - (this.y - (height-20));
this.dy = 0;
} else if (this.y < 0) { // bounce off top
this.y = -this.y;
this.dy = -this.dy * spring;
}

//deals with repeler
var rpx = mouseX
var rpy = mouseY
var dp = dist(this.x, this.y, rpx, rpy);
var f = rpc / (Math.pow(dp , 2));
var dirx = (this.x - rpx) / dp;
var diry = (this.y - rpy) / dp;
this.x += f * dirx;
this.y += f * diry;

this.dy = this.dy + gravity; // force of gravity
// drag is proportional to velocity squared
// which is the sum of the squares of dx and dy
var vs = Math.pow(this.dx, 2) + Math.pow(this.dy, 2);
// d is the ratio of old velocty to new velocity
var d = vs * drag;
// d goes up with velocity squared but can never be
// so high that the velocity reverses, so limit d to 1
d = min(d, 1);
// scale dx and dy to include drag effect
this.dx *= (1 - d);
this.dy *= (1 - d);
}

function particleDraw() {
image(bin,180,290,210,120);
image(gas, this.x, this.y, 40, 40);
}

// create a "Particle" object with position and velocity
function makeParticle(px, py, pdx, pdy) {
p = {x: px, y: py,
dx: pdx, dy: pdy,
age: 0,
stepFunction: particleStep,
drawFunction: particleDraw
}
return p;
}

function countParticle() {
count ++
//count = (count);
}

function gameOver() {
if (int(count/1000) > 80) {
rect(width/2, height/2, width, height);
textSize(60);
text('YOU WIN !', 10, height/2)
textSize(40);
text(int((millis())/1000), 100, 250);
text('seconds', 155, 250);
noLoop();
}

}

var particles = [];

function setup() {
createCanvas(400, 400);
frameRate(10);

}

// draw all particles in the particles array
//
function draw() {
background('lightblue');
noStroke();
fill(80);
ellipse(80,100,160,50);
ellipse(250,20,350,60);
ellipse(100,100,100,50);
stroke(0);
strokeWeight(1);
fill(255,10,5);
textSize(15);
text('How Fast Can You Get Rid Of The Gas?', 120,20)
fill(0,255,0);
rectMode(CENTER);
textSize(50);
text(int(count/1000), 10, height/2);

image(plant,mouseX,mouseY,80,80);
var newp = makeParticle(50, 50,
random(-10, 10), random(-10, 0));
particles.push(newp);

stroke(0);
strokeWeight(10);
newParticles = [];

//if (mouseIsPressed) {
//var newp = makeParticle(mouseX, mouseY,
//random(-10, 10), random(-10, 0));
//particles.push(newp);
//}

// newParticles will hold all the particles that we want to
// retain for the next call to draw() -- we will retain particles
// if the age is < 200 (frames). Initially, newParticle is empty

for (var i = 0; i < particles.length; i++) { // for each particle
var p = particles[i];
p.stepFunction();
p.drawFunction();

if (p.y > height - 80 & p.x > width-180 && p.x < width-80) {
newParticles.push(p);
countParticle()
}

// since we are "looking" at every particle in order to
// draw it, let's use the opportunity to see if particle[i]
// is younger than 200 frames. If so, we'll push it onto the
// end of newParticles.
if (p.age < 3000) {
newParticles.push(p);
}
}

gameOver();

// now, newParticles has EVERY particle with an age < 200 frames.
// these are the particles we want to draw next time, so assign
// particles to this new array. The old value of particles, i.e.
// the entire array, is simply "lost" -- Javascript will reclaim
// and reuse the memory since that array is no longer needed.
particles = newParticles;

}``````

For my program I wanted to create a falling game where you can manipulate multiple objects to get them in some sort of cup or final destination. In this game you use the plant as a repeler to direct the bad greenhouse gasses into the recycling bin. The goal is to get the counter to eighty and there’s some bonuses when you use the plant to bounce the gas around quicker. At eighty the game stops and gives you the time it took to finish the game. The ultimate competition in the game is to get the time as quickly as you can. The best time I got was 15 seconds. If I had some more time I wanted to make it so the recycling bin actually gets filled up with gases and overfills. I would’ve also liked to make more levels where there are existing obstacles that you have to get around with the plant.