Project 10 – SANIC

Sanic make “dash” noises every now and then

Eggman has “intro” and “escape” lines

Rings make sounds when grabbed by sanic

Knuckle makes sounds in the end

/*
 * Andrew J Wang
 * ajw2@andrew.cmu.edu
 * Section A
 *
 * This Program is Sanic
 */

//rotation variables for limb movements for sanic and the coins
var rot = 0;
//controls whether the limbs are moving forwards or backwards
var forward = true;
//sanic locations
var x=0;
//eggman image
var egg;
//eggman Ylocation
var eggY = -150;
//uganda knuckle's Ylocation
var ugandaY = 450;

//Green Hill Zone notes (BACKGROUND MUSIC)
var pitch = [72,69,72,71,72,71,67, 0, 69,76,74,72,71,72,71,67, 0, 72,69,72,71,72,71,67, 0, 69,69,65,69,67,69,67,60, 0];
var duration = [3,9,3,9,3,9,12, 15, 3,3,9,3,9,3,9,12, 15, 3,9,3,9,3,9,12, 15, 3,3,9,3,9,3,9,12, 15];

//counter for durations
var counter = 0;
//which note on the pitch list to play
var note = 0;

//rings' existance
var coin = [true,true,true,true,true];

//sound variables
var eggmanIntro;
var ringSound;
var dawae;
var spinDash;
var eggmanDefeat

//https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/name-eggman.wav
//https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/Sonic_Ring_Sound_Effect.wav
//https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/do-you-know-the-way.wav
//https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/Spin.wav
//https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/no.wav


//loading the images and sounds
function preload() {
    egg = loadImage("https://i.imgur.com/Wy46mQF.png");
    uganda = loadImage ("https://i.imgur.com/HRB5kdy.png");
    eggmanIntro = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/name-eggman.wav");
    ringSound = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/Sonic_Ring_Sound_Effect.wav");
    dawae = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/do-you-know-the-way.wav");
    spinDash = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/Spin.wav");
    eggmanDefeat = loadSound ('https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/no.wav');
}

//setups Don't think I need to elaborate
function setup() {
    createCanvas(600,300);
    useSound();
    frameRate(20);
}

function soundSetup() { 
    osc = new p5.Oscillator();
    osc.amp(0.25);
    eggmanIntro.setVolume(1.0);
    ringSound.setVolume(0.5);
    dawae.setVolume(0.5);
    spinDash.setVolume(0.5);
    eggmanDefeat.setVolume(1.0);
    osc.setType('square');
    osc.start();
}



function draw() {

    background(255);
    //rotation till 1 then change directions
    if (rot>=1 || rot <=-1)
    {
        forward = !forward;
    }
    if (forward)
    {
        rot+=0.1;
    }
    else
    {
        rot-=0.1;
    }

    //move sanic by 1 per frame
    x+=1;

    //check whether sanic ran pass through those rings or not
    for (var k=0; k<5; k++)
    {
        if (x>(100+k*100))
        {
            coin[k] = false;
        }
    }

    //if sanic is on the ring play ring sound
    if (x%100==0 & x<600 && x>0)
    {
        ringSound.play();
    }

    //sanic spins every now and then if he's on the screen
    if (x%100==50 & x<=600 && x>=0)
    {
        spinDash.play();
    }

    //rings
    ring(100,170,rot,coin[0]);
    ring(200,170,rot,coin[1]);
    ring(300,170,rot,coin[2]);
    ring(400,170,rot,coin[3]);
    ring(500,170,rot,coin[4]);

    //eggman shows up
    if (eggY<50)
    {eggY+=2;}

    //eggman "escapes"
    if (x>300 & eggY<1000)
    {
        eggY = 50 + (x-300) * (x-300);
    }

    //eggman intro right before he stops
    if (eggY==48)
    {
        eggmanIntro.play();
    }

    //eggman says no moment after he's defeated
    if (eggY==54)
    {
        eggmanDefeat.play();
    }
    //eggman image
    eggman(300,eggY);

    //sanic
    sanicLimbs(x,100,rot);
    sanic(x,100);

    //uganda knuckle shows up
    if (x>700 & ugandaY > 200)
    {
        ugandaY -=2;
    }

    //"DO YOU KNOW DA WAE" before stoping
    if (ugandaY==202)
    {
        dawae.play();
    }

    //kunckle
    knuckle(300,ugandaY);


    //if pitch is 0 stop playing
    if (pitch[note] == 0)
    {
        osc.stop();
    }

    //if pitch before is 0 but this note isn't restart
    else if (pitch[note-1] == 0 & pitch[note] != 0)
    {
        osc.start();
    }

    //play notes
    osc.freq(midiToFreq(pitch[note]));

    //duration counter ++
    if (counter < duration[note] & note < pitch.length)
    {
        counter++
    }
    //if reach max duration change note and reset
    else if (counter >= duration[note] && note < pitch.length)
    {   
        counter = 0;
        note++;
    }
    //if reach the end restart from the beginning to loop
    else
    {
        note=0;
    }

}

//drawing sanic
function sanic(xL,yL)
{   
    push();
    translate(xL,yL);
    noStroke();
    stroke(0);
    fill("blue");
    curve(-10,100,-7,-10,-50,-30,-50,50);
    curve(-10,100,-7,10,-50,10,-50,50);
    curve(-10,100,-7,30,-65,20,-50,100);
    curve(-10,100,-7,40,-50,30,-50,50);
    curve(-10,100,-7,90,-75,70,-50,200);
    strokeWeight(1);
    ellipse(0,25,10,20);
    ellipse(0,0,50,45);
    ellipse(0,70,70,75);
    fill(255,255,170);
    ellipse(0,70,40,45);
    ellipse(3,15,25,10);
    fill(255);
    ellipse(10,0,15,13);
    ellipse(-8,0,15,10);
    fill(0);
    ellipse(12,0,3,3);
    ellipse(-6,0,3,3);
    ellipse(0,8,4,4);
    stroke(5);
    curve(-7,5,-7,15,16,15,16,5);
    pop();
}

//drawing sanic's llimbs 
function sanicLimbs (xL,yL,r)
{
    push();
    translate(xL,yL);

        push();
        translate(15,50);
        rotate(r);
        fill("blue");

        beginShape();
        vertex(0, 0);
        bezierVertex(0,0,30,-50,60,0);
        bezierVertex(60,0,30,-25,0,0);
        endShape();

        fill(255);
        ellipse(60,0,20,20);
        pop();


        push();
        translate(-15,60);
        rotate(r);
        fill("blue");

        beginShape();
        vertex(0, 0);
        bezierVertex(0,0,-30,-55,-60,0);
        bezierVertex(-60,0,-30,-15,0,0);
        endShape();

        fill(255);
        ellipse(-60,0,30,25);
        pop();

        push();
        translate(0,100);
        rotate(r);
        fill("blue");

        beginShape();
        vertex(0, 0);
        bezierVertex(0,0,-60,0,-60,60);
        bezierVertex(-60,60,-60,40,0,0);
        endShape();

        fill("red");
        ellipse(-70,60,40,20);
        pop();

        push();
        translate(20,100);
        rotate(r);
        fill("blue");

        beginShape();
        vertex(0, 0);
        bezierVertex(0,0,0,40,60,60);
        bezierVertex(60,60,0,80,0,0);
        endShape();

        fill("red");
        ellipse(70,60,40,20);
        pop();


    pop();
}

//ring spins through scalling
function ring (xR,yR,sc,sh)
{
    if (sh==true)
    {
    push();
    translate(xR,yR);
    scale(sc,1.0);
    fill("gold");
    ellipse(0,0,50,50);
    fill(255);
    ellipse(0,0,30,30);
    pop();
    }

}

function eggman (xE,yE)
{
    image(egg,xE,yE,150,150);
}

function knuckle (xU, yU)
{
    push();
    imageMode(CENTER);
    image(uganda,xU, yU, 250,250);
    pop();
}

Leave a Reply