This project is about the life of a Balloon.
It inflates, slightly deflates while floating up, quickly deflates and shoots around the canvas, inflates again, then finally popping.
sketch
// tjchen
// hand in 10 project
// life of a balloon
var vol = 0.75 // set global volume
function preload() {
// call loadImage() and loadSound() for all media files here
deflate = loadSound("https://courses.ideate.cmu.edu/15-104/f2020/wp-content/uploads/2020/11/shortdeflate.wav");
inflate = loadSound("https://courses.ideate.cmu.edu/15-104/f2020/wp-content/uploads/2020/11/inflate.wav");
bpop = loadSound("https://courses.ideate.cmu.edu/15-104/f2020/wp-content/uploads/2020/11/pop.wav");
slowDeflate = loadSound("https://courses.ideate.cmu.edu/15-104/f2020/wp-content/uploads/2020/11/longdeflate_2.wav");
}
function soundSetup() { // setup for audio generation
deflate.setVolume(vol);
inflate.setVolume(vol);
bpop.setVolume(vol);
slowDeflate.setVolume(vol);
}
// set up balloon movement
function balloonStep(){
this.dx = random(-20,20);
this.dy = random(-20,20);
//first inflate
if (this.age > 0 & this.age <= 200){
this.size += 1;
//slow deflate
} else if (this.age > 200 & this.age <= 400){
this.y -= 1;
this.size -= 0.3
// quick deflate and fly around
} else if (this.age > 400 & this.age <= 600){
this.x += this.dx;
this.y += this.dy;
if (this.x > width) { // bounce off right wall
this.x = width - (this.x - width);
this.dx = -this.dx;
} else if (this.x < 0) { // bounce off left wall
this.x = -this.x;
this.dx = -this.dx;
}
if (this.y > height) { // bounce off bottom
this.y = height - (this.y - height);
this.dy = -this.dy;
} else if (this.y < 0) { // bounce off top
this.y = -this.y;
this.dy = -this.dy;
}
this.size -= 0.4;
//inflate again
}else if (this.age > 600 & this.age <800){
this.size += 2;
if(this.size > width){
// balloon pops
rect(0,0,width,height);
}
// resets
} else if (this.age>800) {
this.age = 0;
this.size = 20;
this.x = width/2;
this.y = height/2;
}
this.age ++;
}
function balloonDraw(){
noStroke();
fill(255,0,0);
circle(this.x,this.y,this.size);
}
function makeballoon(px,py,pdx,pdy){
balloon = {x: px, y: py, dx: pdx, dy: pdy, size: 20, stepfunction: balloonStep, drawfunction: balloonDraw, age: 0}
return balloon;
}
function setup() {
createCanvas(400, 400);
useSound();
var b = makeballoon(width/2, height/2, random(-10,10), random(-10,10));
}
function draw() {
background(0);
balloon.stepfunction();
balloon.drawfunction();
// pop! text graphic
if (balloon.size > width){
push();
textSize(150);
textAlign(CENTER);
fill(0);
text('POP!',width/2,height/2+40);
pop();
}
var bo = false;
//check balloon age and toggle sound based on it's state
if (balloon.age >= 0 & balloon.age <= 1){
bo = true
if (bo == true){
inflate.play();
}
}
if (balloon.age >= 200 & balloon.age <= 201){
bo = true
if (bo == true){
slowDeflate.play();
}
}
if (balloon.age >= 400 & balloon.age <= 401){
bo = true
if (bo == true){
deflate.play();
}
}
if (balloon.age >= 600 & balloon.age <= 601){
bo = true
if (bo == true){
inflate.play();
}
}
if (balloon.age >= 750 & balloon.age <= 751){
bo = true
if (bo == true){
bpop.play();
}
}
}