Sonic-Story

sketchDownload

//Yanina Shavialenka
//Section B

var walkImage = [];   // an array to store the images
var flying = []; //bird flying array images
var characters = [];
var birds = [];
var bg; //background image
var lightning; //lighting picture
var thunder; //thunder sound
var amb; //ambient sound
var chirp; //bird sound

function preload(){
    // These URLs are for the individual walk cycle images
    var filenames = [];
    filenames[0] = "https://i.imgur.com/14ifmej.png";
    filenames[1] = "https://i.imgur.com/r9GhjWn.png";
    filenames[2] = "https://i.imgur.com/A0L2KVp.png";
    filenames[3] = "https://i.imgur.com/JFE5CBm.png";
    filenames[4] = "https://i.imgur.com/14ifmej.png";
    filenames[5] = "https://i.imgur.com/r9GhjWn.png";
    filenames[6] = "https://i.imgur.com/A0L2KVp.png";
    filenames[7] = "https://i.imgur.com/JFE5CBm.png";
    filenames[8] = "https://i.imgur.com/14ifmej.png";
    filenames[9] = "https://i.imgur.com/r9GhjWn.png";
    filenames[10] = "https://i.imgur.com/A0L2KVp.png";
    filenames[11] = "https://i.imgur.com/JFE5CBm.png";

    // These URLs are for the individual bird cycle images
    var file = [];
    file[0] = "https://i.imgur.com/82McfxQ.png";
    file[1] = "https://i.imgur.com/OMNJ7z2.png";
    file[2] = "https://i.imgur.com/ZP2mduv.png";
    file[3] = "https://i.imgur.com/2ly1P4T.png";
    file[4] = "https://i.imgur.com/5VNmIMy.png";
    file[5] = "https://i.imgur.com/KjJmaEd.png";
    file[6] = "https://i.imgur.com/036LD8g.png";
    file[7] = "https://i.imgur.com/OPD7VfW.png";
    file[8] = "https://i.imgur.com/Rt8p00H.png";

    // These for loops change the images to create a walking and flying effect
    for (var i = 0; i < filenames.length; i++) {
        walkImage[i] = loadImage(filenames[i]);
    }

    for (var i = 0; i < file.length; i++) {
        flying[i] = loadImage(file[i]);
    }

    //loads the images and sounds
    bg = loadImage("https://i.imgur.com/nqIhKWP.png");
    lightning = loadImage("https://i.imgur.com/6kvwrHw.gif");
    lightning.resize(150,0);
    thunder = loadSound("http://localhost:8000/thunder.wav");
    amb = loadSound("http://localhost:8000/amb.wav");
    chirp = loadSound("http://localhost:8000/bird.wav");
}

// Constructor for each walking character
function makeCharacter(cx, cy, cdx, cdy) {
    var c = { x: cx, y: cy, dx: cdx, dy: cdy,
             imageNum: 0,
             stepFunction: stepCharacter,
             drawFunction: drawCharacter
        }
    return c;
}

// Constructor for each blying bird
function makeBird(cx, cy, cdx, cdy) {
    var c = { x: cx, y: cy, dx: cdx, dy: cdy,
             imageNum: 0,
             stepFun: stepBird,
             drawFun: drawBird
        }
    return c;
}

function stepCharacter() {
    this.imageNum++;
    if(this.imageNum >= walkImage.length) {
       this.imageNum = 0;
    }
     
    this.x += this.dx; 
}

function stepBird() {
    this.imageNum++;
    if(this.imageNum >= flying.length) {
       this.imageNum = 0;
    }
     
    this.x += this.dx; 
}

//This function draws the corresponding image
function drawCharacter() {
    image(walkImage[this.imageNum],this.x,this.y);
}

function drawBird() {
    image(flying[this.imageNum],this.x,this.y);
}

//This function creates the canvas and makes the character
function setup() {
    createCanvas(612,408);
    frameRate(15);
    for (var i = 0; i < walkImage.length; i++) {
        walkImage[i].resize(150,0);
    }
    for (var i = 0; i < flying.length; i++) {
        flying[i].resize(50,0);
    }
    characters.push(makeCharacter(0,125,1,0));
    birds.push(makeBird(0,50,3,0));
    useSound(0);
}

function soundSetup() {
    thunder.setVolume(0.5);
    amb.setVolume(0.2);
    chirp.setVolume(0.1);
}

function draw() {
    background(bg);
    if(frameCount >= 100) {
        image(lightning,425,-10);
        image(lightning,-20,-10);
        thunder.play();
        tint(100);
        characters[0].stepFunction();
        characters[0].drawFunction();
        birds[0].stepFun();
        birds[0].drawFun();
        characters[0].dx = 4;
        birds[0].dx = 6;
    }
    else {
        characters[0].stepFunction();
        characters[0].drawFunction();
        birds[0].stepFun();
        birds[0].drawFun();
        amb.play();
        chirp.play();
    }
}

For me, this assignment was really challenging considering the fact that we did not spend a lot of time on the sound topic so I was really struggling and tried to come up with something interesting and easy at the same time. In my code I made a girl walk and a bird fly: I used a sound effect to represent the chirping of a bird and sounds of an outside world. Then, the lightning strikes and the thunder sound comes off with dark background.

I could not upload the file properly here because the thunder sound is more than WordPress accepts and thus the file was not able to get loaded onto here.

Project 10: Sonic Story

wpf-curves.js
//Patrick Fisher wpf@andrew.cmu.edu Assignment-10-project
var fireSound;
var earthSound;
var windSound;
var waterSound;
var rockxarray = []; //array for falling rocks
var rockyarray = [];
var dy = 12; //drop rate for rocks
var fireYPoints = []; //array for the points for drawing the fire 
var noiseParam = 0; //variable for noise function
var noiseStep = 0.05; //variable to change noiseParam
var windx = []; //wind sweep x array
var windy = []; //wind swee y array
var wind2x = [];
var wind2y = [];
var waterPoints = []; //array for the points for drawing the river
var oppoWaterPoints = [];
var framesC = 0; //frame count variable 

function preload() {
    fireSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/fireedit.wav");
    earthSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/earthedit.wav");
    windSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/windedit.wav");
    waterSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/wateredit.wav");
}


function setup() {
    createCanvas(500, 300);
    for(var i = 0; i <= 19; i++){ //establish rocks
        rockxarray[i] = random(0,500);
        rockyarray[i] = random(0,20);
    }
    for(var i = 0; i <= 120; i++){ //establishes fire
        fireYPoints[i] = map(noise(noiseParam),0,1,100,300);
        noiseParam += noiseStep
    }
    for(var i = 0; i <= 120; i++){ //establsihes water
        waterPoints[i] = map(noise(noiseParam),0,1,0,height/2);
        oppoWaterPoints[i] = map(waterPoints[i],0,height/2,height,height/2)
        noiseParam += noiseStep
    }

    for(var i = 0; i <= 19; i++){ //establish wind 1 array
        windy[i] = map(noise(noiseParam),0,1,100,300);
        windx[i] = map(noise(noiseParam),0,1,100,500);
        noiseParam += noiseStep
    }

    for(var i = 0; i <= 19; i++){ //establishes wind 2 array
        wind2y[i] = map(noise(noiseParam),0,1,100,300);
        while(wind2y[i] == windy[i]){
            wind2y[i] = map(noise(noiseParam),0,1,100,300);
        }
        wind2x[i] = map(noise(noiseParam),0,1,100,500);
        while(wind2x[i] == windx[i]){
            wind2x[i] = map(noise(noiseParam),0,1,100,300);
        }

        noiseParam += noiseStep
    }

    createDiv("p5.dom.js library is loaded.");
    //======== call the following to use sound =========
    useSound();
    frameRate(4);

}


function soundSetup() { // setup for audio generation
    fireSound.setVolume(0.5);
    earthSound.setVolume(0.5);
    waterSound.setVolume(0.5);
    windSound.setVolume(0.5);
}


function draw() {
    background(40);
    if(framesC == 0){ //does the earth section
        earthSound.play();

    }
    if(framesC < 28){
        earthDraw();

    }

    if(framesC == 28){ //does the fire section
        fireSound.play();

    }
    if(framesC >= 28 & framesC < 56){
        fireDraw();
    }

    if(framesC == 56){ //does the wind section
        windSound.play();
    }
    
    if(framesC >= 56 & framesC < 84){ 
        windDraw();
    }

    

    if(framesC == 84){ //does the water section
        background(40);
        waterSound.play();
    }
    if(framesC >= 84 & framesC < 112){

        waterDraw();
    }

    if(framesC == 112){ //ends the program
        background(0);
        noLoop();
    }



    framesC ++;
} 

function waterDraw() { // draws the water
    
    push();
    noStroke();
    fill(158,196,226);
    waterPoints.shift(); //removes the last entry in the array to make room for the next one
    waterPoints.push(map(noise(noiseParam),0,1,0,height/2));
    oppoWaterPoints.shift();
    oppoWaterPoints.push(map(waterPoints[120],0,height/2,height,height/2))
    beginShape(); //begins drawing the mountain
    for(i = 0; i <= 120; i++){    //for loop that makes the peaks     
        vertex(i*5,waterPoints[i]);
        vertex(i*5,oppoWaterPoints[i]);
    }
    endShape(CLOSE);
    noiseParam += noiseStep;
    pop();

}

function fireDraw() { //draws the fire

    push();
    noStroke();
    fill(248,103,19);
    beginShape(); //begins drawing the fire
    vertex(0,height);
    for(i = 0; i <= 120; i++){    //for loop that makes the fire     
        vertex(i*5,fireYPoints[i]);
    }
    vertex(width,height);
    endShape(CLOSE);

    for(var i = 0; i <= 120; i++){ //for loop that initrially fills the array
        fireYPoints[i] = map(noise(noiseParam),0,1,100,300);
        noiseParam += noiseStep
    }
    pop();

}

function earthDraw() { //draws the rocks

    for(var i = 0; i <= 19; i++){
        fill(87,64,52);
        ellipse(rockxarray[i],rockyarray[i],random(5,10,random(5,10))); //draws the rocks, the randomness makes them "tumble"
        rockyarray[i] += dy; //moves the rocks down
    }   
}

function windDraw() { //draws the wind

    push();
    strokeWeight(5);
    stroke(190,239,143);

    for(var i = 0; i <= 9; i++) {
        line(windx[i],windy[i],windx[i+1],windy[i+1]); //draws the wind
    }
    windx.push(random(100,500)); //shifts the array and adds new points
    windy.push(random(100,300));
    windx.shift();
    windy.shift();

    for(var i = 0; i <= 9; i++) {
        line(wind2x[i],wind2y[i],wind2x[i+1],wind2y[i+1]);
    }
    wind2x.push(random(100,500));
    wind2y.push(random(100,300));
    wind2x.shift();
    wind2y.shift();
    
}

I was inspired by the intro to a certain children’s cartoon. My sounds are earth, fire, air, and water. It was surprisingly difficult to find quality sounds of some of these, and I had a lot of trouble with the web server stuff.

Sonic Story

sketch

Fishies! For this week’s project, I wade an infinite animation of a fish eating a fish eating a fish eating a… the colors are randomly generated and there are two pitches playing the jaws theme, and chomping and background underwater sounds from freesounds.org. Enjoy!!


function setup() {
    createCanvas(400, 200);
    useSound();
    frameRate(4);
    background('lightblue');
    chooseColor1();
    chooseColor2();
}


function soundSetup() {
    osc = new p5.Oscillator();
    osc.amp(1);
    osc.setType('sine');
    osc.start();
}

function preload() {
    chomp = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/Chomp.wav");
    bg = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/bg.wav");
    bg.setVolume(0.75);
    chomp.setVolume(0.5);
}

var r;
var g;
var d;
var col1;
var col2;

function chooseColor1(){
    r = random(100,255);
    g = 140;
    b = random(100,255);
    col1 = color(r,g,b);
}

function chooseColor2(){
    r = random(100,255);
    g = 140;
    b = random(100,255);
    col2 = color(r,g,b);
}

function drawFish1(x, y, s, c, mouthClosed, lookingFord) {
    push();
    translate(x, y);
    scale(s);
    strokeCap(SQUARE)
    strokeWeight(1);

    var fil = color(red(c)+20, green(c)+20, blue(c)+20);

    if (mouthClosed == true) {
        //top half of fish (colored w/ c)
        //fill
        stroke(fil);
        fill(fil);
        triangle(-17, -5, -12,-2, -17, -1);
        rect(-17, -1, 7, 3);
        rect(-16, 2, 3, 1);
        rect(-6,-5, 16, 5);
        line(-6, -2, -10,-2);
        line(-6, -3, -8,-3);
        line(10, -2, 17, -2);
        line(10, -3, 14, -3);

        //outline
        stroke(c);
        line(-17, 2, -17, -6);
        point(-16,-6);
        point(-15,-5);
        point(-14,-4);
        point(-13,-3);
        point(-12,-2);
        point(-11,-2);
        point(-10,-3);
        point(-9,-3);
        point(-8,-4);
        point(-7,-4);
        line(-6,-5, -2, -5);

        //fin
        stroke(fil);
        rect(-1, -8, 4, 4);
        triangle(-1, -7,-3, -5, -1, -5);
        line(3, -6, 5, -6);

        stroke(c);
        point(-4, -6);
        point(-3, -7);
        line(-2, -8, 3, -8);
        point(3, -7);
        point(4, -7);
        point(5, -6);

        line(2,-5, 10, -5);
        point(11,-4);
        point(12,-4);
        point(14,-3);
        point(15,-3);
        line(16,-2, 18, -2);

        //bottom half of fish
        //finn fill
        stroke(240);
        fill(240);
        rect(-5, 4, 4, 2);

        stroke(220);
        point(-16, 4);
        point(-15, 4);
        line(-14, 3, -10, 3);
        line(-10, 4, -6, 4);
        line(-6, 5, 0, 5);

        //fin
        stroke(220)
        point(-6,5);
        point(-7,5);
        point(-7, 6);
        line(-6, 7, -2, 7);
        line(-2, 6, 10, 6)

        stroke(240);
        line(11, 5, 0, 5);
        line(12, 5, 0, 5);
        line(13, 4, -5, 4);
        line(14, 3, -10, 3);
        line(15, 2, -10, 2);
        line(16, 1, -10, 1);
        line(17, 0, -10, 0);
        line(18, -1, -10, -1);

        //gills
        stroke(204,194,194);
        point(0,2);
        point(1,1);
        point(2,2);
        point(3,1);
        point(4,2);
        point(5,1);
        point(6,2);

        stroke(220);
        point(10, 5);
        point(11, 5);
        point(12, 4);
        point(13, 3);
        point(14, 2);
        point(15, 1);
        point(16, 0);
        point(17, -1);



        //eye
        fill(255);
        stroke(255);
        rect(9, -1, 4, 2);
        rect(10, -2, 2, 4);
        stroke(0);
        //if (lookingFord) {
            point(11,0);
        //} else if (!lookingFord) {
        //    point(10, -1);
        //}

        pop();
    } else {
        //top half of fish (colored w/ c)
        stroke(fil);
        fill(fil);
        triangle(-17, -5, -12,-2, -17, -1);
        rect(-17, -1, 7, 3);
        rect(-16, 2, 3, 1);
        rect(-6,-5, 16, 5);
        line(-6, -2, -10,-2);
        line(-6, -3, -8,-3);
        line(10, -2, 17, -2);
        line(10, -3, 14, -3);

        stroke(c);
        line(-17, 2, -17, -6);
        point(-16,-6);
        point(-15,-5);
        point(-14,-4);
        point(-13,-3);
        point(-12,-2);
        point(-11,-2);
        point(-10,-3);
        point(-9,-3);
        point(-8,-4);
        point(-7,-4);
        line(-6,-5, -2, -5);

        //fin
        stroke(fil);
        rect(-1, -8, 4, 4);
        triangle(-1, -7,-3, -5, -1, -5);
        line(3, -6, 5, -6);

        stroke(c);
        point(-4, -6);
        point(-3, -7);
        line(-2, -8, 3, -8);
        point(3, -7);
        point(4, -7);
        point(5, -6);

        line(2,-5, 10, -5);
        point(11,-4);
        point(12,-4);
        point(14,-3);
        point(15,-3);
        line(16,-2, 18, -2);

        //bottom half of fish
        //finn fill
        stroke(240);
        fill(240);
        rect(-5, 4, 4, 2);

        stroke(220);
        point(-16, 4);
        point(-15, 4);
        line(-14, 3, -10, 3);
        line(-10, 4, -6, 4);
        line(-6, 5, 0, 5);

        //fin
        stroke(220)
        point(-6,5);
        point(-7,5);
        point(-7, 6);
        line(-6, 7, -2, 7);
        line(-2, 6, 10, 6)

        stroke(240);
        line(11, 5, 0, 5);
        line(13, 5, 0, 5);
        line(16, 4, -5, 4);
        line(17, 3, -10, 3);
        line(14, 2, -10, 2);
        line(13, 1, -10, 1);
        line(12, 0, -10, 0);
        line(18, -1, -10, -1);

        stroke(204,194,194);
        point(0,2);
        point(1,1);
        point(2,2);
        point(3,1);
        point(4,2);
        point(5,1);
        point(6,2);

        stroke(220);
        point(10, 5);
        point(11, 5);
        point(12, 5);
        point(13, 4);
        point(14, 4);
        point(15, 4);
        point(16, 3);
        point(17, 3);

        //eye
        fill(255);
        stroke(255);
        rect(9, -1, 4, 2);
        rect(10, -2, 2, 4);
        stroke(0);
        point(11,0);

        pop();
    }
}

function drawFish2(x, y, s, c, mouthClosed, lookignFord) {
    push();
    translate(x, y);
    scale(s);
    strokeCap(SQUARE)
    strokeWeight(1);

    var fil = color(red(c)+20, green(c)+20, blue(c)+20);

    if (mouthClosed == true) {
        //top half of fish (colored w/ c)
        stroke(fil);
        fill(fil);
        triangle(-15, -5, -12,-2, -15, -1);
        rect(-15, -1, 7, 3);
        rect(-14, 2, 3, 1);
        rect(-6,-5, 16, 5);
        line(-6, -2, -8,-2);
        line(-6, -3, -6,-3);
        line(10, -2, 17, -2);
        line(10, -3, 14, -3);

        stroke(c);
        line(-15, 2, -15, -4);
        //point(-16,-6);
        point(-15,-5);
        point(-14,-4);
        point(-13,-3);
        point(-12,-2);
        point(-11,-2);
        point(-10,-3);
        point(-9,-3);
        point(-8,-4);
        point(-7,-4);
        line(-6,-5, -2, -5);

        //fin
        stroke(fil);
        rect(-1, -8, 4, 4);
        triangle(-1, -7,-3, -5, -1, -5);
        line(3, -6, 5, -6);

        stroke(c);
        point(-4, -6);
        point(-3, -7);
        line(-2, -8, 3, -8);
        point(3, -7);
        point(4, -7);
        point(5, -6);

        line(2,-5, 10, -5);
        point(11,-4);
        point(12,-4);
        point(14,-3);
        point(15,-3);
        line(16,-2, 18, -2);

        //bottom half of fish
        //finn fill
        stroke(240);
        fill(240);
        rect(-5, 4, 4, 2);

        stroke(220);
        point(-16, 4);
        point(-15, 4);
        line(-14, 3, -10, 3);
        line(-10, 4, -6, 4);
        line(-6, 5, 0, 5);

        //fin
        stroke(220)
        point(-6,5);
        point(-7,5);
        point(-7, 6);
        line(-6, 7, -2, 7);
        line(-2, 6, 10, 6)

        stroke(240);
        line(11, 5, 0, 5);
        line(12, 5, 0, 5);
        line(13, 4, -5, 4);
        line(14, 3, -10, 3);
        line(15, 2, -10, 2);
        line(16, 1, -10, 1);
        line(17, 0, -10, 0);
        line(18, -1, -10, -1);

        stroke(204,194,194);
        point(0,2);
        point(1,1);
        point(2,2);
        point(3,1);
        point(4,2);
        point(5,1);
        point(6,2);

        stroke(220);
        point(10, 5);
        point(11, 5);
        point(12, 4);
        point(13, 3);
        point(14, 2);
        point(15, 1);
        point(16, 0);
        point(17, -1);



        //eye
        fill(255);
        stroke(255);
        rect(9, -1, 4, 2);
        rect(10, -2, 2, 4);
        stroke(0);
        //if (lookingFord) {
            point(11,0);
        //} else if (!lookingFord) {
        //    point(10, -1);
        //}

        pop();
    } else {
        //top half of fish (colored w/ c)
        stroke(fil);
        fill(fil);
        triangle(-17, -5, -12,-2, -17, -1);
        rect(-17, -1, 7, 3);
        rect(-16, 2, 3, 1);
        rect(-6,-5, 16, 5);
        line(-6, -2, -10,-2);
        line(-6, -3, -8,-3);
        line(10, -2, 17, -2);
        line(10, -3, 14, -3);

        stroke(c);
        line(-17, 2, -17, -6);
        point(-16,-6);
        point(-15,-5);
        point(-14,-4);
        point(-13,-3);
        point(-12,-2);
        point(-11,-2);
        point(-10,-3);
        point(-9,-3);
        point(-8,-4);
        point(-7,-4);
        line(-6,-5, -2, -5);

        //fin
        stroke(fil);
        rect(-1, -8, 4, 4);
        triangle(-1, -7,-3, -5, -1, -5);
        line(3, -6, 5, -6);

        stroke(c);
        point(-4, -6);
        point(-3, -7);
        line(-2, -8, 3, -8);
        point(3, -7);
        point(4, -7);
        point(5, -6);

        line(2,-5, 10, -5);
        point(11,-4);
        point(12,-4);
        point(14,-3);
        point(15,-3);
        line(16,-2, 18, -2);

        //bottom half of fish
        //finn fill
        stroke(240);
        fill(240);
        rect(-5, 4, 4, 2);

        stroke(220);
        point(-16, 4);
        point(-15, 4);
        line(-14, 3, -10, 3);
        line(-10, 4, -6, 4);
        line(-6, 5, 0, 5);

        //fin
        stroke(220)
        point(-6,5);
        point(-7,5);
        point(-7, 6);
        line(-6, 7, -2, 7);
        line(-2, 6, 10, 6)

        stroke(240);
        line(11, 5, 0, 5);
        line(13, 5, 0, 5);
        line(16, 4, -5, 4);
        line(17, 3, -10, 3);
        line(14, 2, -10, 2);
        line(13, 1, -10, 1);
        line(12, 0, -10, 0);
        line(18, -1, -10, -1);

        stroke(204,194,194);
        point(0,2);
        point(1,1);
        point(2,2);
        point(3,1);
        point(4,2);
        point(5,1);
        point(6,2);

        stroke(220);
        point(10, 5);
        point(11, 5);
        point(12, 5);
        point(13, 4);
        point(14, 4);
        point(15, 4);
        point(16, 3);
        point(17, 3);

        //eye
        fill(255);
        stroke(255);
        rect(9, -1, 4, 2);
        rect(10, -2, 2, 4);
        stroke(0);
        point(11,0);

        pop();
    }
}

var count = 0;
var mouthClosed;
var lookingFord;

function draw() {
    background('lightblue');

    if (count%32==0) {
        count = 0;
        col1 = col2;
        chooseColor2();
        osc.start();
        bg.play();
        bg.loop();
    }

    if (count > 8) {
        mouthClosed = true;
        osc.stop();
        //lookignFord = false;
    } else {
        mouthClosed = false;
        //lookignFord = true;
    }

    if (count <= 8) {
        if (count%2==0) {
            drawFish1(width/2, height/2, 3, col1, true);
            osc.freq(midiToFreq(52));
        } else if ((count+1)%2==0) {
            osc.freq(midiToFreq(53));
            drawFish2(width/2, height/2, 3, col1, true);
        }
    }

    var bigFishX = map(count, 0, 12, -200, 200);
    var bigFishS = map(count, 12, 24, 10, 3);

    if (count == 5) {
        chomp.play();
    }

    if (count <= 12) {
        if (count%2==0) {
            drawFish1(bigFishX, height/2, 10, col2, mouthClosed);
        } else if ((count+1)%2==0) {
            drawFish2(bigFishX, height/2, 10, col2, mouthClosed);
        }
    } if (count > 12 & count <= 24) {
        if (count%2==0) {
            drawFish1(width/2, height/2, bigFishS, col2, mouthClosed);
        } else if ((count+1)%2==0) {
            drawFish2(width/2, height/2, bigFishS, col2, mouthClosed);
        }
    } if (count > 24) {
        if (count%2==0) {
            drawFish1(width/2, height/2, 3, col2, mouthClosed);
        } else if ((count+1)%2==0) {
            drawFish2(width/2, height/2, 3, col2, mouthClosed);
        }
    }

    count += 1;
}

Project-10: Sonic Story

For this project, I created a scenery of life under ocean. The four elements in my drawing are a starfish, which makes a sound every 25 frames; a fish, which spits bubbles and make the bubble spitting sound every 15 frames; an octopus, which swimming in up-left direction and makes tentacle sound every 10 frames when it shrinks before the push to swim forward; and the beach on the soft moving ocean floor accompanied by the mysterious sound starting from the beginning of this animation. During the process, I struggled with matching the sound effects with the animation changes, but I made it working eventually.

sketchDownload
/*Name:Camellia(Siyun) Wang; 
Section: C; 
Email Address: siyunw@andrew.cmu.edu;
Assignment-10;*/
//This is a normal day of underocean life
//Violet little fish is spitting bubbles,
//octopus is swimming left upwards,
//and the starfish is just lying on the soft
//moving beach on the ocean floor  
var x = 300;
var dir = 1;
var speed = 5;
var boing;
var xfish;
var yfish;
var dxfish;
var xoct;
var yoct;
var dyoct;
//setup the underwater beach
var marketvalue = [];
var noiseParam = 0;
var noiseStep=0.005;
var count;
//2 arrays to form the starfish
var x = [50, 61, 83, 69, 71, 50, 29, 31, 17, 39];
var y = [18, 37, 43, 60, 82, 73, 82, 60, 43, 37];
//sounds
var ocean;
//variables to change the background
var R = 102;
var G = 178;
var B = 255;

function preload(){
    ocean = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/ocean.wav");
    star = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/star.wav");
    bubble = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/bubble.wav");
    oct = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/oct.wav");
}

function setup() {
    frameRate(3);
    createCanvas(600, 400);
    useSound();
    count = 1;
    //fish setup
    xfish = 200;
    yfish = 250;
    dxfish = 15;
    //oct setup
    xoct = 0;
    yoct = 0;
    dyoct = 3;
    //beach setup
    for(var i = 0; i < 50*width; i++){
        var n = noise(noiseParam);
        var value = map(n,0,1,height / 1.5,height);
        marketvalue.push(value);
        noiseParam+=noiseStep;
    }   
}
function soundSetup(){
    ocean.setVolume(0.1);
    star.setVolume(0.3);
    bubble.setVolume(0.5);
    oct.setVolume(1);
}

function draw() {
    ocean.play();
    background(R,G,B);
    //change background color
    //from light blue to dim blue 
    //to show the change of time
    //from day to night
    R -= 3;
    G -= 5;
    B -= 2;
    //draw the beach
    draw_beach();
    
    //draw fish
    draw_fish();
    xfish += dxfish;
    if (xfish > width-25 || xfish < 25) {
        dxfish = -dxfish;
    }

    //draw octopus
    draw_oct();
    yoct -= dyoct;

    //draw starfish
    draw_starfish();
    

    count += 1;

//end of the short animation
    if(count == 90){
        background(0);
        fill(255);
        textSize(32);
        text('The End', 250, 200);
        noLoop();
        ocean.stop();
        star.stop();
        bubble.stop();
        oct.stop();
    }
}

function draw_beach(){
    marketvalue.shift();
    var n = noise(noiseParam);
    var value = map(n,0,1,height / 1.5,height);
    marketvalue.push(value);
    noiseParam+=noiseStep;
    stroke(255,215,0);
    fill(255,215,0);
    beginShape();
    for(var i = 0; i < 50*width; i++){
        vertex(i*5,height);
        vertex( i*5 ,marketvalue[i]);
        vertex( i*5+5 ,marketvalue[i+1]);
        vertex(i*5+5,height);
    };
    endShape();
    noStroke();
}

function draw_fish() {
    fill(127,0,255);
    ellipse(xfish, yfish, 35, 20);
    if (dxfish < 0) {
        triangle(xfish+15, yfish, xfish+25, yfish-5, xfish+25, yfish+5);
    } else {
        triangle(xfish-15, yfish, xfish-25, yfish-5, xfish-25, yfish+5);
    }
     //bubles every 15 frames
     //small first one
    if(count % 15 == 0){
        bubble.play();
        if(dxfish > 0){
            fill(224,255,255);
            ellipse(xfish + 25, yfish - 5, 5, 5);
        }
        
        if(dxfish < 0){
            fill(224,255,255);
            ellipse(xfish - 25, yfish - 5, 5, 5);
        }
    }
    //middle second one
    if(count % 15 == 1){
        bubble.play();
        if(dxfish > 0){
            fill(224,255,255);
            ellipse(xfish + 25, yfish - 5, 5, 5);
            ellipse(xfish + 35, yfish - 15, 10, 10);
        }
        if(dxfish < 0){
            fill(224,255,255);
            ellipse(xfish - 25, yfish - 5, 5, 5);
            ellipse(xfish - 35, yfish - 15, 10, 10);
        }
    }
    //large last one
    if(count % 15 == 2){
        bubble.play();
        if(dxfish > 0){
            fill(224,255,255);
            ellipse(xfish + 25, yfish - 5, 5, 5);
            ellipse(xfish + 35, yfish - 15, 10, 10);
            ellipse(xfish + 45, yfish - 25, 15, 15);
        }
        if(dxfish < 0){
            fill(224,255,255);
            ellipse(xfish - 25, yfish - 5, 5, 5);
            ellipse(xfish - 35, yfish - 15, 10, 10);
            ellipse(xfish - 45, yfish - 25, 15, 15);
        }
    }
}

function draw_oct(){
    if(count % 10 == 1){
        //make sound when shrink to push
        oct.play();
        push();
        translate(400,150);
        rotate(-PI / 3);
        fill(216,191,216);
        strokeWeight(5);
        stroke(216,191,216);
        line(xoct-10,yoct+10,xoct-20,yoct+30);
        line(xoct-20,yoct+30,xoct-10,yoct+50);
        line(xoct+10,yoct+10,xoct+20,yoct+30);
        line(xoct+20,yoct+30,xoct+10,yoct+50);
        line(xoct,yoct+15,xoct,yoct+50);
        ellipse(xoct,yoct,45,35);
        noStroke();
        pop();
    }
    else{
        push();
        translate(400,150);
        rotate(-PI / 3);
        fill(238,130,238);
        stroke(238,130,238);
        strokeWeight(5);
        ellipse(xoct,yoct,30,50);
        line(xoct-10,yoct+10,xoct-30,yoct+50);
        line(xoct,yoct+15,xoct,yoct+55);
        line(xoct+10,yoct+10,xoct+30,yoct+50);
        noStroke();
        pop();
    }
}

function draw_starfish(){
    push();
    translate(450,300);
    var nPoints = x.length;
    fill(244,164,96);
    if(count % 25 == 1){
        fill(255,140,0);
        star.play();
    }
    if(count % 25 == 2){
        fill(255,140,0);
    }
    if(count % 25 == 3){
        fill(255,140,0);
    }
    if(count % 25 == 4){
        fill(255,140,0);
    }
    if(count % 25 == 5){
        fill(255,140,0);
    }
    beginShape();
    for (var i = 0; i < nPoints; i++) {
        vertex( x[i] + random(-3,3), y[i] + random(-3,3) );
    }
    endShape(CLOSE);
    pop();
}

Project 10: Sonic Story

sketch
//Christy Zo
//Section C

var cricketSound;
var roosterSound;
var cowSound;
var pigSound;
var sunHue;

function preload() {
    cricketSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/cricketSound.wav");
    roosterSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/roosterSound.wav");
    cowSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/cowSound.wav");
    pigSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/pigSound.wav")
 }

function soundSetup() {
    osc = new p5.TriOsc();
}
function setup() {
    createCanvas(600, 300);
    useSound();
    frameRate(30);
}



function barn(x) {
    stroke(255);
    strokeWeight(2);
    fill(50);
    rect(x, 100, 200, 150);
    fill(255);
    rect(x+10, 110, 180, 80);
    fill(50);
    rect(x+10, 200, 180, 40);
    line(x+10, 200, x+190, 240);
    line(x+10, 240, x+190, 200);
    triangle(x, 100, x+200, 100, x+100, 30);
    triangle(x+15, 100, x+185, 100, x+100, 40);
}

function rooster(x) {
    stroke(0);
    fill(255);
    line(x, 150, x-30, 190); 
    line(x, 150, x+30, 190);
    circle(x, 150, 50); 
    fill(220);
    circle(x, 180, 20);
    triangle(x, 140, x-10, 110, x+10, 110); 
    fill(150);
    triangle(x, 150, x-10, 160, x+10, 160);
    triangle(x, 170, x-10, 160, x+10, 160);
    fill(255);
    circle(x-10, 145, 5);
    circle(x+10, 145, 5);

}

function cow(x) {
    fill(255);
    stroke(0);
    rect(x, 125, 50, 50);
    fill(200);
    ellipse(x+25, 170, 52, 30); //nose
    fill(255);
    circle(x+12, 170, 10);
    circle(x+37, 170, 10);
    circle(x+15, 145, 5);
    circle(x+35, 145, 5);
    ellipse(x-10, 125, 25, 10); //ears
    ellipse(x+55, 125, 25, 10);
    line(x, 150, x-30, 190);
    line(x+50, 150, x+80, 190);

}

function pig(x) {
    line(x, 150, x-30, 190);
    line(x, 150, x+30, 190);
    circle(x, 150, 60);
    circle(x-10, 145, 5);
    circle(x+10, 145, 5);
    fill(150);
    ellipse(x, 160, 30, 15);
    fill(255);
    circle(x-5, 160, 5);
    circle(x+5, 160, 5);
    fill(100);
    triangle(x-20, 120, x-25, 130, x-15, 125);
    triangle(x+20, 120, x+25, 130, x+15, 125);

    

}

var light = 0;
var position = 255;
function draw() {
    
    background(light);
    fill(light);
    rect(0, 0, 600, 300);

    sunHue = map(position, 0, height, 255, 0);
    fill(sunHue, 150, 0);
    ellipse(200, position, 50, 50);
    if (position < -50) {
        position = 300;
        light = 0;
        frameCount = 0;

    }
    fill(220);
    noStroke();
    rect(0, 200, 600, 300);
    barn(0);
    barn(200);
    barn(400);



    rooster(100);
    cow(275);
    pig(500);

    light = light + 1;
    position = position - 1;
    if (frameCount == 30) {
        cricketSound.play();
    }
    if (frameCount == 80) {
        roosterSound.play();

    } else if (frameCount == 140) {
        cowSound.play();
    } else if (frameCount == 200) {
        pigSound.play();
    }

    if (roosterSound.isPlaying()) {
        fill(255,0,0);
        circle(100, 180, 20);
        triangle(100, 140, 90, 110, 110, 110);
        fill(255,255,0);
        triangle(100, 150, 100-10, 160, 100+10, 160);
        triangle(100, 170, 100-10, 160, 100+10, 160);
    } else if (cowSound.isPlaying()) {
        fill(165,42,42);
        ellipse(275+25, 170, 52, 30);
        fill(255);
        circle(275+12, 170, 10);
        circle(275+37, 170, 10);
    } else if (pigSound.isPlaying()) {
        fill(255, 192, 203);
        triangle(500-20, 120, 500-25, 130, 500-15, 125);
        triangle(500+20, 120, 500+25, 130, 500+15, 125);
        ellipse(500, 160, 30, 15);
        fill(255);
        circle(500-5, 160, 5);
        circle(500+5, 160, 5);

    }

}


I was reminded of the story Animal Farm, and wanted to create a simple yet cute! I wanted to make everything greyscale, but emphasize the colors when each of the animals sound play!

Project 10: Sonic-Story

sketch

// Yash Mittal
// Section D
//I made a basic 1v1 fighting game where two figures are fighting with swords
//The health bar of the two figures is shown at the bottom of the screen 

var count = 0; //variable for tracking frame count

function preload(){
    pimg = loadImage ("https://i.imgur.com/Fn5pkxI.jpg"); //background arena image
    thunderimg = loadImage ("https://i.imgur.com/VAJWvZZ.png"); //thunder image
    countdownThree = loadSound("http://localhost:8000/countdown_three.wav");
    countdownTwo = loadSound("http://localhost:8000/countdown_two.wav");
    countdownOne = loadSound("http://localhost:8000/countdown_one.wav");
    countdownFight = loadSound("http://localhost:8000/countdown_fight.wav");
    thunder = loadSound("http://localhost:8000/thunder_1.wav");
    sword = loadSound("http://localhost:8000/sword_1.wav");
    ouch = loadSound("http://localhost:8000/ouch_1.wav");
}

function soundSetup () {
    countdownThree.setVolume(0.5);
    countdownTwo.setVolume(0.5);
    countdownOne.setVolume(0.5);
    countdownFight.setVolume(0.5);
    thunder.setVolume(0.5);
    sword.setVolume(0.5);
    ouch.setVolume(0.9);
}

function setup () {
    createCanvas (480, 300);
    frameRate (10); 
    pimg.resize (480, 300); //resizing background image to canvas
    thunderimg.resize(480,300);
    useSound();
}

function draw () {

    image(pimg,0,0,width,height); //drawing background

    if (count>0 & count<9) { //countdown
        textSize(100);
        fill(255);
        stroke(255);
        text("3",width/2-40,height/2)
    }

    if (count>1 & count<3) {
        countdownThree.play();
    }

    if (count>=10 & count<19) { //countdown
        text("2",width/2-40,height/2)
    }

    if (count>10 && count < 12) {
        countdownTwo.play();
    }

    if (count>19 & count<28) { //countdown
        text("1",width/2-40,height/2)
    }

    if (count>19 && count<21) {
        countdownOne.play();
    }

    if (count>28 & count <38) { //fight
        text("FIGHT!",width/2-170,height/2)
    }

    if (count>28 && count<30) {
        countdownFight.play();
    }

    if (count>56 & count<=60) { //thunder
        image(thunderimg,45,-55,400,400)
    }

    if (count>56 && count <=58) {
        thunder.play();
    }

    if (count>=43) { //UI bars appear
        leftCharacterUI(0,0)
        rightCharaterUI(0,0)
    }
 
    if (count>=43 & count<=65) { //left player appears
        leftPlayer(160,122)
        swordLeft(0,0)
    }

    if (count>=43 && count<=75) { //right player appears
        rightPlayer(300,122)
        swordRight(0,0)
    }

    if (count>=43 && count<75) { //right player health bar shows up
        push();
        strokeWeight(0);
        fill (0,255,0)
        rect(320,270,70,5)
        pop();
    }

    if (count>=43 & count<105) { //left player health bar shows up 
        push();
        strokeWeight(0);
        fill(255,0,0);
        rect(90,270,70,5);
        pop();
    }

    if (count>=66 & count<=105) { //left player moves  right
        leftPlayer(245,122)
        swordLeft(60,60)
    }

    if (count>=75 && count<=105) { //right player moves right & health decreases
        rightPlayer(380,122)
        swordRight(-57,-57)
        fill(0,255,0)
        rect(320,270,40,5) //green health bar
    }

    if (count>70 && count<76) {
        sword.play();
    }

    if (count>=105 & count<=120) { //right & left player move left
        rightPlayer(280,122)
        swordRight(14,14)
        leftPlayer(145,122)
        swordLeft(-10,-10)
        fill(255,0,0)
        rect(90,270,70,5) //red health bar
        fill(0,255,0)
        rect(320,270,40,5) //green health bar
    }

    if (count>=121 && count<=145) { //right player moves left and hits left player
        rightPlayer(203,122)
        swordRight(69,69)
        leftPlayer(145,122)
        swordLeft(-10,-10)
        fill(0,255,0)
        rect(320,270,40,5) //green health bar
        fill(255,0,0)
        rect(90,270,30,5) //red health bar decreases
        sword.play();
    }

    if (count>=146 & count<=170) { // players move to bottom floor
        rightPlayer(300,196)
        swordRight(-54,52)
        fill(0,255,0)
        rect(320,270,40,5) //green health bar
        leftPlayer(230,192)
        swordLeft(0,100)
        fill(255,0,0)
        rect(90,270,30,5)
    }

    if (count>=171 && count<=195) { //left player kills right player
        rightPlayer(300,196)
        swordRight(-54,52)
        leftPlayer(271,192)
        swordLeft(28,128)
        fill(255,0,0)
        rect(90,270,30,5) //red health bar
    }

    if (count>169 && count<171) {
        sword.play();
    }

    if (count>196 & count<198) {
        ouch.play();
    }

    if (count>=196 & count<=270) { //player falls on ground and text appears
        leftPlayer(271,192)
        swordLeft(28,128)
        fill(255,0,0) 
        rect(90,270,30,5) //red health bar
        rightPlayerDead(330,220);
        textSize(13)
        fill(0)
        stroke(0)
        strokeWeight(0.5)
        text("Winner",100,287)
        text("Loser",335,287)
    }

    count++;
}

function leftPlayer(x,y) {

    noStroke();
    fill(232,190,172); //light head fill
    circle(x,y,20); //head of left character

    fill(200,0,0); //body fill
    beginShape(); //main body of left character
    curveVertex(x,y+22);
    curveVertex(x-10,y+8);
    curveVertex(x,y+13);
    curveVertex(x+10,y+8);
    endShape(CLOSE);

    fill(0,0,200); //feet fill
    beginShape(); //lower body of left character
    vertex(x,y+20);
    vertex(x-8,y+35);
    vertex(x,y+30);
    vertex(x+8,y+35);
    endShape(CLOSE);
}

function rightPlayer(x,y) {

    noStroke();
    fill(68,59,49); //dark head fill
    circle(x,y,20); //head of right character

    fill(255,215,0); //body fill
    beginShape(); //main body of right character
    curveVertex(x,y+22);
    curveVertex(x-10,y+8);
    curveVertex(x,y+13);
    curveVertex(x+10,y+8);
    endShape(CLOSE);

    fill(192,96,168); //feet fill
    beginShape(); //lower body of right character
    vertex(x,y+20);
    vertex(x-8,y+35);
    vertex(x,y+30);
    vertex(x+8,y+35);
    endShape(CLOSE);
}

function rightPlayerDead(x,y) {

    noStroke();
    fill(68,59,49); //dark head fill
    circle(x,y,20); // head of right character

    fill(255,215,0); //body fill
    beginShape(); //main body of right character
    curveVertex(x-22,y);
    curveVertex(x-8,y+10);
    curveVertex(x-13,y);
    curveVertex(x-8,y-10);
    endShape(CLOSE);

    fill(192,96,168); //feet fill
    beginShape(); //lower body of right character
    vertex(x-20,y);
    vertex(x-35,y-8);
    vertex(x-30,y);
    vertex(x-35,y+8);
    endShape(CLOSE);
}

function swordLeft(x,y) {

    push();
    translate(155,130);
    angleMode(DEGREES);
    rotate(315);
    noStroke();
    fill(0,230,40);
    rect(x+10,y+9,13,4); //bottom rectangle of sword
    fill(30,50,80);
    rect(x+20,y+5,4,12);//middle rectangle of sword
    fill(125,125,125);
    beginShape();//blade of sword
    vertex(x+24,y+13);
    vertex(x+39,y+13);
    vertex(x+43,y+11);
    vertex(x+39,y+9);
    vertex(x+24,y+9);
    endShape(CLOSE);
    pop();
}

function swordRight(x,y){

    push();
    translate(305,130);
    angleMode(DEGREES);
    rotate(45);
    scale(-1,1);
    noStroke();
    fill(0,250,0);
    rect(x+10,y+9,13,4); //bottom rectangle of sword
    fill(30,50,80);
    rect(x+20,y+5,4,12);//middle rectangle of sword
    fill(125,125,125);
    beginShape();//blade of sword
    vertex(x+24,y+13);
    vertex(x+39,y+13);
    vertex(x+43,y+11);
    vertex(x+39,y+9);
    vertex(x+24,y+9);
    endShape(CLOSE);
    pop();
}

function leftCharacterUI(x,y) {

    push();
    translate(80,250);
    fill(232,190,172);
    circle(x,y,30);
    fill(255);
    rect(x+10,y+20,70,20);
    pop();
}

function rightCharaterUI(x,y) {

    push();
    translate(310,250);
    fill(68,59,49);
    circle(x+90,y,30);
    fill(255);
    rect(x+10,y+20,70,20);
    pop();
}

For this project, I wanted to have the the sound of swords colliding with each other, and some environmental sounds like the thunder and the announcer voice at the beginning of the animation.

Project-10-Sonic Story

sketch

//Tim Nelson-Pyne
//tnelsonp@andrew.cmu.edu
//section C
//Assignment-10-Project
//a sound bomb drops from the sky as a zombie rises from the grave
//the sound bomb makes a musical explosion and the zombie dances to the beat

var noteImg
var explosionImg
var danceGifImg
var danceGif
var fall
var explosion
var tam
var beat


var i = 0;


function preload() {
    noteImg = loadImage("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/eighthNote.png");
    explosionImg = loadImage("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/musicExplosion.png");
    danceGifImg = loadImage("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/Mojo_the_zombie.gif");  
    fall = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/falling.wav");
    explosion = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/explosion.wav");
    tam = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/Tam95bpm.wav");
    beat = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/Beat189bpm.wav")
    danceGif = createImg("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/Mojo_the_zombie.gif");
    danceGif.hide();
}

function setup() {
    createCanvas(480, 600);
    useSound();
    background(255);
    frameRate(60);
}

function soundSetup() { 
    
}


function draw() {
    //adds one to i each frame
    i ++;
    background(3, 0, 50);
    imageMode(CENTER);
    //makes the background flash multi colors when the sound bomb drops
    if(i >= 600 & i % 30 < 15) {
        fill(random(100, 255), random(100, 255), random(100,255));
        rect(0,0, 480, 400);
    }
    fill(23, 150, 20);
    noStroke();
    rect(0, 400, 480, 200);
    fill(100);
    rect(250, 325, 100, 150);
    //writes RIP on grave
    fill(0);
    textAlign(CENTER);
    text("RIP", 300, 375);
    //static zombie image rises from the grave
    if (i < 600){
        image(danceGifImg, 300, 700 - map(i, 0, 600, 0, 350), 400, 400);
        fill(23, 150, 20);
        rect(0, 500, 480, 100); 
    }
    //plays the cartoon falling noise and tambourine loop @ start
    if (i == 1) {
        fall.play();
        tam.play();
    }
    //displays the noteImg falling
    if (i < height - 25){
        image(noteImg, 100, i, 50, 50);
    }
    //when the noteImg hits the ground the explosion sound plays
    if (i == height - 25) {
        explosion.play();
    }
    //when the noteImg hits the ground the explosion image flashes
    if (i >= height - 25 & i % 30 < 15 && i < 800) {
        image(explosionImg, 100, height-150, 300, 300);
    }
    //a moment after the noteImg hits the groud the beat starts
    if (i == 600){
        beat.play();
    }
    //and the zombie dances
    if(i >= 600) {
        danceGif.show();
        danceGif.position(0,100);
    }
    //after the beat is over everything goes black
    if (i >= 1800) {
        danceGif.remove();
        fill(0);
        rect(0, 0, 480, 600);
        noLoop();
    }


    
}

LO 10: Sonic Story

sketch

// sketch.js template for sound and DOM
//
// This is the 15104 Version 1 template for sound and Dom.
// This template prompts the user to click on the web page
// when it is first loaded.
// The function useSound() must be called in setup() if you
// use sound functions.
// The function soundSetup() is called when it is safe
// to call sound functions, so put sound initialization there.
// (But loadSound() should still be called in preload().)
var img;
var bx = 20;
var by = 400;
var cx = 20;
var cy = 300;
var tx = 20;
var ty = 200;
var mx = 20;
var my = 100;
var bikeTone;
var bikeDynamics;
var carTone;
var carDynamics;
var trainTone;
var trainDynamics;
var motorcycleTone;
var motorcycleDynamics;

function preload() {
    // call loadImage() and loadSound() for all media files here
}


function setup() {
    // you can change the next 2 lines:
    createCanvas(480, 480);
    createDiv("p5.dom.js library is loaded.");
    //======== call the following to use sound =========
    useSound();
}


function soundSetup() { // setup for audio generation
    // you can replace any of this with your own audio code:
    bikeTone = new p5.Oscillator();
    bikeTone.setType('sine');
    bikeTone.freq(440.0);
    bikeTone.amp(0.1);
    bikeTone.start();
    bikeDynamics = new p5.Oscillator();
    bikeDynamics.setType('sine');
    bikeDynamics.disconnect();
    bikeDynamics.start();
    carTone = new p5.Oscillator();
    carTone.setType('sine');
    carTone.freq(261.63);
    carTone.amp(0.1);
    carTone.start();
    carDynamics = new p5.Oscillator();
    carDynamics.setType('sine');
    carDynamics.disconnect();
    carDynamics.start();
    trainTone = new p5.Oscillator();
    trainTone.setType('sine');
    trainTone.freq(164.81);
    trainTone.amp(0.1);
    trainTone.start();
    trainDynamics = new p5.Oscillator();
    trainDynamics.setType('sine');
    trainDynamics.disconnect();
    trainDynamics.start();
    motorcycleTone = new p5.Oscillator();
    motorcycleTone.setType('sine');
    motorcycleTone.freq(110);
    motorcycleTone.amp(0.1);
    motorcycleTone.start();
    motorcycleDynamics = new p5.Oscillator();
    motorcycleDynamics.setType('sine');
    motorcycleDynamics.disconnect();
    motorcycleDynamics.start();
}


function draw() {
    // you can replace any of this with your own code:
    background(255);
    stroke(0);
    makeBike(bx, by);
    bx += .5;
    makeCar(cx, cy);
    cx += 1;
    makeTrain(tx, ty);
    tx += 1.5;
    makeMotorcycle(mx, my);
    mx += 2;
    bikeDynamics.amp(0.5);
    bikeDynamics.freq(.034);
    bikeTone.amp(bikeDynamics);
    carDynamics.amp(1);
    carDynamics.freq(.06);
    carTone.amp(carDynamics);
    trainDynamics.amp(2);
    trainDynamics.freq(.085);
    trainTone.amp(trainDynamics);
    motorcycleDynamics.amp(2);
    motorcycleDynamics.freq(.12);
    motorcycleTone.amp(motorcycleDynamics);
    if (millis() > 4200 + soundStart) {
       motorcycleTone.stop();
       motorcycleDynamics.stop();
    }
    if (millis() > 6000 + soundStart) {
        trainTone.stop();
        trainDynamics.stop();
    }
    if (millis() > 8400 + soundStart) {
        carTone.stop();
        carDynamics.stop();
    }
    if (millis() > 15800 + soundStart) {
        bikeTone.stop();
        bikeDynamics.stop();
    }
}

function makeBike(x, y) {
    circle(x, y, 20);
    circle(x + 40, y, 20);
    line(x, y - 10, x + 40, y - 10);
    line(x, y - 10, x, y - 20);
    line(x + 40, y - 10, x + 40, y - 30);
    line(x + 30, y - 25, x + 50, y - 35);
    line(x - 10, y - 20, x + 10, y - 20);
}

function makeCar (x, y) {
    rect(x - 20, y - 20, 40, 20);
    rect(x - 40, y, 80, 20);
    circle(x - 30, y + 30, 20);
    circle(x + 30, y + 30, 20);
}

 function makeTrain(x, y) {
    rect(x, y, 20, 20);
    rect(x, y + 20, 40, 20);
    line(x, y + 30, x - 10, y + 30);
    rect(x - 40, y + 20, 30, 20);
    line(x - 40, y + 30, x - 50, y + 30);
    rect(x - 80, y + 20, 30, 20);
    circle(x + 5, y + 45, 10);
    circle(x + 35, y + 45, 10);
    circle(x - 15, y + 45, 10);
    circle(x - 35, y + 45, 10);
    circle(x - 55, y + 45, 10);
    circle(x - 75, y + 45, 10);
} 

function makeMotorcycle(x, y) {
    rect(x, y, 40, 20);
    triangle(x, y, x + 40, y, x + 40, y - 15);
    triangle(x - 20, y, x, y + 15, x, y);
    triangle(x + 40, y, x + 60, y, x + 40, y + 15);
    circle(x - 10, y + 20, 20);
    circle(x + 50, y + 20, 20);
}

4. Project 10: Sonic Story

sketch – CopyDownload
// This program displays a game of cup pong.
// The 'characters' are the ball, cups, table, and water in the cups.
// When the ball bounces on different surfaces (on the floor, into a cup, etc.),
// it produces different sounds.
// One side wins when all of the cups are empty
//(or just a few if you change the 'win' variable in the draw function).

// sounds:
var watercup;
var emptycup;
var bouncefloor;
var throwing;
var gameover;


// objects:
var cup; // will have fields for cx, cy, and boolean isFull
var ball; // will have fields for x, y, dx, dy, and functions

// arrays:
var leftCups = [];
var rightCups = [];

function preload() {
    watercup = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/pong-sounds-with-liquid.wav");
    bouncefloor = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/bouncing-ping-pong-ball.wav");
    throwing = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/throwing-paper-in-trash.wav");
    gameover = loadSound("https://courses.ideate.cmu.edu/15-104/f2021/wp-content/uploads/2021/11/ending.wav");
}

// makes a cup object, always full to begin
function makeCup(cx, cy) {
    cup = {x:cx, y:cy, isFull: true, drawc: drawCup};
    return(cup);
}

// makes a ball object
function makeBall(cx, cy, dirx, diry) {
    ball = {x:cx, y:cy,
            dx:dirx, dy:diry,
            drawb: drawBall,
            throwb: throwBall,
            resetb: resetBall,
            leftright: true};
    return(ball);
}

function setup() {
    createCanvas(480, 300);
    frameRate(35);	// high framerate is better for this program
    useSound();
    rectMode(CENTER);

    // fill cup arrays:
    // set x and y positions to create pyramid shape.
    // the if statements create the rows for the pyramid
    for (var i=0; i<10; i++) {
        if (i<4) {
	    lcup = makeCup(width/10+10,	(i*20)+120);
            rcup = makeCup(9*width/10-10, (i*20)+120);
        }
        else if (i<7) {
	    lcup = makeCup(width/10+30,	(i*20)+50);
            rcup = makeCup(9*width/10-30, (i*20)+50);
        }
        else if (i<9) {
	    lcup = makeCup(width/10+50,	i*20);
            rcup = makeCup(9*width/10-50, i*20);
        }
        else {
	    lcup = makeCup(width/10+70,	height/2);
            rcup = makeCup(9*width/10-70, height/2);
        }
        leftCups.push(lcup);
        rightCups.push(rcup);
    }

    // create ping pong ball
    let dy = random(-4, 4);
    ball = makeBall(5, height/2, 5, dy);
}

function soundSetup() { // setup for audio generation
    watercup.setVolume(0.5);
    bouncefloor.setVolume(0.5);
    throwing.setVolume(0.5);
    gameover.setVolume(0.5);
}


function draw() {
    background(133, 94, 66);

    // draw the table
    drawTable();

    // draw the cups
    for (var c=0; c<leftCups.length; c++) {
        leftCups[c].drawc();
        rightCups[c].drawc();
    }

    // draw the ball
    ball.drawb();

    // throw the ball
    ball.throwb();

    // check for winner
    let checkl = 0;
    let checkr = 0;
    for (l=0; l<leftCups.length; l++) {
        if (leftCups[l].isFull == false) { checkl+=1 }
    }
    for (r=0; r<rightCups.length; r++) {
        if (rightCups[r].isFull == false) { checkr+=1 }
    }
    // when one side wins, display text and play sound
    // change 'win' value to see game over sooner:
    let win = 10;
    if (checkl == win|| checkr == win) {
        gameover.play();
        textAlign(CENTER, CENTER);
        textSize(25);
        fill(0);
        rect(width/2, height/2, width/2, 50, 10);
        fill(255);
        text('Game Finished!', width/2, height/2);
        noLoop();
    }


}

// draws a green table with offwhite diagonal lines:
function drawTable() {
    let linescol = color(250, 250, 225);
    let tcol = color(0, 100, 100);
    strokeWeight(7);
    stroke(linescol);
    fill(tcol);
    rect(width/2, height/2, 4*width/5, 4*height/5);
    line(width/10, height/10, 9*width/10, 9*height/10);
    line(width/10, 9*height/10, 9*width/10, height/10);
}

// draws individual cups based on x and y fields:
function drawCup() {
    let diam = 20;
    fill(200, 0, 0);	// red cup
    stroke(255);
    strokeWeight(2);
    circle(this.x, this.y, diam);
    // if the cup hasnt been hit, it has water in it:
    if (this.isFull == true) {
        noStroke();
        fill(0, 0, 200);	// blue water
        circle(this.x, this.y, diam/2);
    }
}

// draws the ball
function drawBall() {
    stroke(0);
    strokeWeight(.5);
    fill(250);
    circle(this.x, this.y, 10);
}

//throws the ball
function throwBall() {
    if (this.leftright==true) {		// going left to right
        if (this.x < width/2) {
            this.x += this.dx;
            this.y -= this.dy;
        } else {
            this.x += this.dx;
            this.y += (1.5*this.dy);
        }
        // remove the water if the ball goes into a full cup and reset ball placement
	// play water cup noise if cup is hit
        for (var j=0; j<rightCups.length; j++) {
            if (dist(rightCups[j].x, rightCups[j].y, this.x, this.y)<=7 &
		rightCups[j].isFull==true) {
	   	watercup.play();
                rightCups[j].isFull = false;
                this.resetb();
            }
        }
    } else {				// going right to left
        if (this.x > width/2) {
            this.x -= this.dx;
            this.y -= this.dy;
        }
        else {
            this.x -= this.dx;
            this.y += (1.5*this.dy);
        }
	// same code to remove water from hit cup
        for (var j=0; j<leftCups.length; j++) {
            if (dist(leftCups[j].x, leftCups[j].y, this.x, this.y)<=7 &
		leftCups[j].isFull==true) {
	   	watercup.play();
                leftCups[j].isFull = false;
                this.resetb();
            }
        }
    }
    // no cups are hit, reset the ball position & play floor bounce track
    if (this.x > width || this.x < 0 ) {
       	this.resetb();
        bouncefloor.play();
    }
}

//resets the ball to the next players starting position
function resetBall() {
    this.leftright = -this.leftright;
    if (this.leftright == true) {this.x=10}
    else {this.x =width-10}
    this.y = height/2;
    this.dy = random(-4, 4);
    // plays a 'whoosh' track for each throw
    throwing.play();
}

I chose to make a program that displays a game of water pong. The sounds I use are a ‘whoosh’ noise when a ball is thrown, a ‘clink’ sound if the ball goes into a full cup, a bouncing pingpong ball noise if the ball does not go into any full cups, and a bell noise if there is a winner. My code is randomized in a way that every game will be slightly different.