A Day of Steve

Story: One beautiful morning Steve heard there’s birds singing outside, so he went out to check for the birds that hides in the tree. He didn’t notice there is a creeper on top of his house, and the creeper follows Steve to the tree. The Steve realized creeper is behind him, but it’s too late for him to stop creeper from exploding.

Reflection: Though I was asked to use 2-3 seconds sounds for this project, I mostly able to find 4-5 second sounds that I felt fits for my story. Somehow the Audacity also does not work for the sounds I selected, so I have to use these 4-5 seconds sounds files directly. I also don’t know how to find the images that directly blends into the canvas.

sketchDownload
/* Jiayi Chen
   jiayiche    Section A */
// 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().)

/*Story:
One beautiful morning Steve heard there's birds singing outside,
so he went out to check for the birds that hides in the tree.
He didn't notice there is a creeper on top of his house, and 
the creeper follows Steve to the tree. The Steve realized creeper 
is behind him, but it's too late for him to stop creeper from exploding.  

Reflection:
Though I was asked to use 2-3 seconds sounds for this project, I 
mostly able to find 4-5 second sounds that I felt fits for my story. 
Somehow the Audacity also does not works for the sounds I selected, 
so I have to use these 4-5 seconds sounds files directly.I also don't 
know how to find the images that directly blends into the canvas. 
*/
//Stevemovements
var steve=[]; //array for stevemovement
var idle;
var walk;
var run;
var swordright;
var damaged;
//creeper
var creeperMovement =[]; //array for creeper movement
var creeper;
var glowing;
var exploasion;
//house and bird for tree
var house;
var bird;
//sounds
var walkingsound;
var exploadsound;
var hurt;
var dooropen;
var morningbirds;

// track the time
var frame=0;
var steveX=200;
var CreeperX=300;
var CreeperY=0;
function preload() {
    //images
    house=loadImage("https://i.imgur.com/hAeVVrM.jpeg");
    exploasion=loadImage('https://i.imgur.com/u0RA0C9.png');
    glowing=loadImage('https://i.imgur.com/wpqSTpR.png');
    creeper=loadImage('https://i.imgur.com/oXqwDBa.png');
    damaged=loadImage('https://i.imgur.com/27nSdZi.png');
    swordright=loadImage('https://i.imgur.com/UgtoVei.png');
    run=loadImage('https://i.imgur.com/3JwarD4.png');
    walk=loadImage('https://i.imgur.com/6adTjcs.png');
    idle=loadImage('https://i.imgur.com/bIFUPG3.png');
    bird=loadImage('https://i.imgur.com/Ttx4W2C.png');
    //sounds
    walkingsound = loadSound('http://localhost:8000/walkingsound.wav');
    exploadsound = loadSound('http://localhost:8000/exploadsound.wav');
    hurt = loadSound('http://localhost:8000/hurt.wav');
    dooropen = loadSound('http://localhost:8000/dooropen.wav');
    morningbirds = loadSound('http://localhost:8000/morningbirds.wav');
}


function setup() {
    // you can change the next 2 lines:
    createCanvas(500, 400);
    //======== call the following to use sound =========
    useSound();
    frameRate(1);
    steve[0]=idle;
    steve[1]=walk;
    steve[2]=run;
    steve[3]=swordright;
    steve[4]=damaged;
    //attribute sounds
    creeperMovement[0]=creeper;
    creeperMovement[1]=glowing;
    creeperMovement[2]=exploasion;
}


function soundSetup() { // setup for audio generation
    // you can replace any of this with your own audio code:
    morningbirds.setVolume(0.3);
    dooropen.setVolume(0.3);
    hurt.setVolume(0.3);
    exploadsound.setVolume(0.3);
    walkingsound.setVolume(0.3)
}


function draw() {
    background('Cyan');
    grassland();
    tree(0,50);
    frame += 1; //track of frames  
    //print("Seconds = " + frame);
    //end the canvas
    if(frame == 28){
        noLoop();
    }
    //house
    image(house, 250, 100, 200, 200);

    steveMoving();

    creeperMoving();

    birdsmoving();

    //start the audio
    if(frame == 4){
        morningbirds.play();//birds chats
    }if(frame==9){
        dooropen.play();//door opens 
    }if(frame==11){
        walkingsound.play();//steve walks
    }if(frame==20){
        exploadsound.play();//creeper exploads
    }if(frame==24){
        hurt.play();//steve cries
    }

}
//build background grassland
function grassland(){
    push();
    fill('lime');
    noStroke();
    rect(0,height/2,width,height/2);
    pop();
}
//build a tree
function tree(x,y){
    push();
    fill('lime');
    noStroke();
    rect(x,y,100,100);
    fill('brown')
    rect(x+25,y+50,50,125);
    pop();
}

function creeperMoving(){//movement of creeper depend on framecount
    var a=0;
    if (frame >= 0 & frame <=11){
        image(creeperMovement[a],CreeperX,CreeperY,75,125);
    }else if(frame > 11 & frame <13){
        CreeperX-=100;
        CreeperY+=25;
        image(creeperMovement[a],CreeperX,CreeperY,75,125);
    }else if(frame >= 13 & frame <=15){
        CreeperX-=25;
        CreeperY+=50;
        image(creeperMovement[a],CreeperX,CreeperY,75,125);
    }else if(frame >= 16 & frame <=20){
        a=1;
        CreeperX-=3;
        CreeperY+=3;
        image(creeperMovement[a],CreeperX,CreeperY,75,125);
    }else if(frame > 20 & frame <=21){
        a=1;
        image(creeperMovement[a],CreeperX,CreeperY,75,125);
    }else if(frame >= 22 & frame <26){
        a=2;
        b=50;
        image(creeperMovement[a],CreeperX-b,CreeperY-b,75+b,125+b);//explosion
    }
}

function steveMoving(){
    var a=0;
    if(frame==9 ){
        image(steve[a],steveX,200,75,125);//movement of steve depend on framecount
    }else if(frame>9 & frame<=11){
        steveX -= 10;
        a=1;
        image(steve[a],steveX,200,75,125);
    }else if(frame>11 & frame<16){
        a = 2;
        steveX -=25;
        if(frame==13){
            steveX -=25;
        }else if(frame==15){
            steveX -=50;
        }
        image(steve[a],steveX,200,75,125);
    }else if((frame>=16 & frame<20)){
        a = 0;
        image(steve[a],steveX,200,75,125);
    }else if(frame>=20 & frame <=22){
        a = 3;
        image(steve[a],steveX,200,75,125);
    }else if(frame >=22 & frame<= 25){ //steve hurt by the explosion
        a=4;
        image(steve[a],steveX,200,75,125);
    }else if(frame == 26){
        a=4;
        image(steve[a],steveX,200+50,75,125);
    }else if(frame == 27){
        a=4;
        image(steve[a],steveX,200+100,75,125);
    }
}

function birdsmoving(){
    if(frame>=4 & frame <=11){//birds scared off by steve
        image(bird,0,50,50,50);
    }
}

Project 10: Sonic Story

Nick’s Prank (Story inspired by Zootopia Teaser Trailer)

Judy the bunny and the goat are walking on the street. Nick the fox sees them and decides to play a prank on Judy. When Judy passes Nick, Nick puts out his foot and trips Judy. In a panic, Judy pushes the goat walking in front of her, so the goat falls down as well. After seeing them both fall, Nick hums with joy.

sketch
//Xinyi Du 
//15104 Section B
//xinyidu@andrew.cmu.edu
//Project-10

//Nick's Prank (Story inspired by Zootopia Teaser Trailer)

//Judy the bunny and the goat are walking on the street. 
//Nick the fox sees them and decides to play a prank on Judy.
//When Judy passes Nick, Nick puts out his foot and trips Judy.
//In panic, judy pushes the goat walking in front of her, so the goat falls down as well.
//After seeing them both fall, Nick hums with joy.

/* 
Images of characters from Zootopia Teaser Trailer:
https://www.youtube.com/watch?v=g9lmhBYB11U

Street image source: 
https://www.etsy.com/au/listing/581765202/the-buildings-of-main-street-walt-disney?
ref=related-2&epik=dj0yJnU9UWhoMkVRQnRMc1hFbk40dXRWQ21FVUdSVG9iYVFZT3QmcD0wJm49RWRs
YUhPc25aWUxvdTY5ZEtod3liQSZ0PUFBQUFBR053QWdv
*/


var filenames = [];
var walkImage = []; //walk cycle images of judy

var filenames2 = [];
var fallImage = []; //fall images of judy

var filenames3 = [];
var walkImage2 = []; //walk cycle images of the goat

var filenames4 = [];
var fallImage2 = []; //fall images of the goat

var character = [];
var street;
var clouds;
var nick;

var walk; //walking footsteps sound variable
var ouch; //ouch sound judy makes when fall
var bodyfall; //body falling sound of the goat
var nickhumming; //happy humming of nick

function preload(){
    //load sounds
    walk = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/footsteps.mp3");
    ouch = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/ouch.mp3");
    bodyfall = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/bodyfall.wav");
    nickhumming = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/happy-huming.mp3");
    
    //load images
    //walk cycle images of judy
    filenames[0] = "https://i.imgur.com/wLVmUpf.png";
    filenames[1] = "https://i.imgur.com/TffffQ8.png";
    filenames[2] = "https://i.imgur.com/5Lirbd1.png";
    filenames[3] = "https://i.imgur.com/N2BUn7y.png";
    filenames[4] = "https://i.imgur.com/pc6Lp46.png";
    filenames[5] = "https://i.imgur.com/lhOcePN.png";
    filenames[6] = "https://i.imgur.com/djXrWqQ.png";
    filenames[7] = "https://i.imgur.com/8nXGJZe.png";
    filenames[8] = "https://i.imgur.com/D8o0eOa.png";
    filenames[9] = "https://i.imgur.com/qzJQm37.png";
    for (var i = 0; i < filenames.length; i++) {
        walkImage[i] = loadImage(filenames[i]);
    }
    //fall images of judy
    filenames2[0] = "https://i.imgur.com/c1vUye5.png";
    filenames2[1] = "https://i.imgur.com/mbibpsr.png";
    filenames2[2] = "https://i.imgur.com/ZSeL7on.png";
    filenames2[3] = "https://i.imgur.com/UNqmLD7.png";
    for (var i = 0; i < filenames2.length; i++) {
        fallImage[i] = loadImage(filenames2[i]);
    }
    //walk cycle images of the goat
    filenames3[0] = "https://i.imgur.com/RRf37Kf.png";
    filenames3[1] = "https://i.imgur.com/ue4D3ig.png";
    filenames3[2] = "https://i.imgur.com/MxIlCqf.png";
    filenames3[3] = "https://i.imgur.com/UDHWsZ8.png";
    filenames3[4] = "https://i.imgur.com/a3nmvz3.png";
    filenames3[5] = "https://i.imgur.com/i75T56A.png";
    filenames3[6] = "https://i.imgur.com/cN4M52I.png";
    filenames3[7] = "https://i.imgur.com/A1iDTIi.png";
    for (var i = 0; i < filenames3.length; i++) {
        walkImage2[i] = loadImage(filenames3[i]);
    }
    //fall images of the goat
    filenames4[0] = "https://i.imgur.com/plfUZHK.png";
    filenames4[1] = "https://i.imgur.com/g7C3kXU.png";
    filenames4[2] = "https://i.imgur.com/14PuZpL.png";
    filenames4[3] = "https://i.imgur.com/Pcl7xcg.png";
    filenames4[4] = "https://i.imgur.com/KC56zzH.png";
    for (var i = 0; i < filenames4.length; i++) {
        fallImage2[i] = loadImage(filenames4[i]);
    }

    street = loadImage("https://i.imgur.com/G0SJIDN.jpg");
    clouds = loadImage("https://i.imgur.com/LbxT2nS.png");
    nick = loadImage("https://i.imgur.com/hEIvnaB.png");
}

//constructor for walking judy
function makeJudy(jx, jy, jdx) {
    var j = {x: jx, y: jy, dx: jdx,
             imageNum: 0,
             stepFunction: stepJudy,
             drawFunction: drawJudy
         }
    return j;
}
function stepJudy() {
    this.x += this.dx; //increase x by dx
    this.imageNum ++; //increase imageNum
    //if reaches the length (end) of the array (go over 10 images), start from 0 again
    if (this.imageNum == walkImage.length) { 
        this.imageNum = 0;
    }
}
function drawJudy() {
    image(walkImage[this.imageNum], this.x, this.y, 63, 80); //draw the image
}


//constructor for falling judy
function makeFallDown(fx, fy) {
    var f = {x: fx, y: fy, imageNum: 0,
            stepFunction: stepFallDown,
            drawFunction: drawFallDown
         }
    return f;
}
function stepFallDown() {
    //increases imageNum only when it has not exceed the length of the array fallImage
    if(this.imageNum < fallImage.length-1) {
        this.imageNum ++;
    }  
}
function drawFallDown() {
    image(fallImage[this.imageNum], this.x, this.y, 170, 100);
}


//constructor for walking goat
function makeNewChar(cx, cy, cdx) {
    var c = {x: cx, y: cy, dx: cdx,
             imageNum: 0,
             stepFunction: stepChar,
             drawFunction: drawChar
         }
    return c;
}
function stepChar() {
    this.x += this.dx; 
    this.imageNum ++; 
    if (this.imageNum == walkImage2.length) { 
        this.imageNum = 0;
    }
}
function drawChar() {
    image(walkImage2[this.imageNum], this.x, this.y, 120, 180); 
}


//constructor for falling goat
function makeFallDown2(fx, fy) {
    var f = {x: fx, y: fy, imageNum: 0,
            stepFunction: stepFallDown2,
            drawFunction: drawFallDown2
         }
    return f;
}
function stepFallDown2() {
    if(this.imageNum < fallImage2.length-1) {
        this.imageNum ++; 
    }  
}
function drawFallDown2() {
    image(fallImage2[this.imageNum], this.x, this.y, 300, 200);
}

//set up sounds
function soundSetup() {
    walk.setVolume(0.4);
    ouch.setVolume(0.8);
    bodyfall.setVolume(1.2);
    nickhumming.setVolume(0.1);
}
//define a function to add the sounds
function addSounds() {
    if(frameCount % 15 == 1) {
        walk.play(); //walking footstep sounds very 15 frameCounts
    }
    if(frameCount == 99) {
        ouch.play(); //judy makes "ouch" sound when being tripped
    }
    if(frameCount > 100){
        walk.stop(); //stop walking footstep sounds when falling down
    }
    if(frameCount == 100) {
        bodyfall.play(); //body falling sound of the goat after judy falls
    }
    if(frameCount == 108) {
        nickhumming.play(); //nick humming after they both falls
        noLoop(); //end the loop
    }
}



function setup() {
    createCanvas(700, 500);
    frameRate(5);
    useSound();
    //store the characters info to the characters array
    var judyX = width/4;
    character.push(makeJudy(judyX, height-94, 0));
    character.push(makeFallDown(judyX, height-112, 0));
    var newCharX = width/3;
    character.push(makeNewChar(newCharX, height-184, 0)); //walking goat before judy falls
    var newChar2X = width/3;
    character.push(makeNewChar(newChar2X, height-184, 10)); //goat after judy falls, continue walking
    character.push(makeFallDown2(290, height-199, 10));
}

function draw() {
    background(25);
    //call the addSounds function to add sounds
    addSounds();
    //constrain the Xs of the images of street and clouds so that they stop rolling when Judy falls
    //street and clouds rolling with different speed according to frameCount
    var streetX = constrain(0-frameCount*10, -1000, 0); 
    var cloudsX = constrain(0-frameCount*5, -500, 0);
    image(street, streetX, -70, 2000, 600);
    image(clouds, cloudsX, -100, 2000, 600);

    //update the steps and draw the characters
    //constrain the X of nick so that it does not move when the street scene stops
    var nickX = constrain(1175-frameCount*10, 175, 1000);
    var judyX = width/4;
    var newCharX = width/11;
    //if judy (width/4) has not meet nick(nickX)
    if (nickX > width/4) {
        //draw the image of nick
        image(nick, nickX, height-111, 80, 100);
        character[2].stepFunction(); 
        character[2].drawFunction(); //walking goat
        character[0].stepFunction();
        character[0].drawFunction(); //walking judy
    } 
    //if judy (width/4) passes by nick(nickX)
    if (judyX == nickX) {
        character[1].stepFunction();
        character[1].drawFunction(); //judy falls down
        //the goat continue walking
        if (character[3].x < 250) { 
            character[3].stepFunction();
            character[3].drawFunction(); //walking goat
        }
    } 
    //if judy pushes the goat
    if (character[3].x >= 250) {
        character[4].stepFunction();
        character[4].drawFunction(); //goat falls down
    }
}




Srishty’s Project 10

I made a simple story about a girl (me) who goes on a walk to the park. During her time there as she walks by, she sees tom and jerry start chasing each other all over the park and she starts laughing. The background d elements and all elements other than tom and jerry were done on illustrator and imported to imgur. My 4 characters are the cloud making the wind sound, the tom making the meow sound, the girl laughing, and the bird chirping.

sketch

// SRISHTY BHAVSAR
//SECTION C
//PROJECT 10
/*
I made a simple story about a girl (me) who goes on a walk to the park. During her time there as she walks by, 
she sees tom and jerry start chasing each other all over the park and
she starts laughing. The background d elements and all elements other than tom and jerry were done on illustrator
and imported to imgur. My 4 characters are the cloud making the wind sound, the tom making the meow sound, the girl 
laughing, and the bird chirping.
*/



// call global variables

//background image
var bgPhoto

//CHARACTERS
// cloud
    var cloudX = 0;

    // bird
    var birdImage = [];
    var birdX = 500;
    var birditem = 0;
    var birdwidth = 80

    // srishty (me)
    var srishtyImage;
    var srishtyx =500;

    // cloud
    var cloudImage;
    var cloudx = 300

    // tom and jerry
    var tomandjerry = [];
    var tomandjerryx = 70;
    var tomandjerryitem = 0;
    var tjdy = 1
    var tjdx = 1

// frame counter initializer
var count = 0; 




//load images to imgur
function preload() {
    bgPhoto = loadImage("https://i.imgur.com/75aDbMe.png");
    // images
    birdImage[0] = loadImage("https://i.imgur.com/o1P0pBO.png"); // first image in the bird list
    birdImage[1] = loadImage("https://i.imgur.com/48woLcm.png"); // first image in the bird list
    srishtyImage = loadImage("https://i.imgur.com/b5N2LzO.png"); // srishty image
    cloudImage = loadImage("https://i.imgur.com/xG8RjYF.png"); // cloud image
    tomandjerry[0] =loadImage("https://i.imgur.com/8nNCYkE.png");
    tomandjerry[1] =loadImage("https://i.imgur.com/cDSNU65.png");
    tomandjerry[2] =loadImage("https://i.imgur.com/LcYpbag.png");
    tomandjerry[3] =loadImage("https://i.imgur.com/wYhvMJo.png"); 

    //sounds
    wind = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/wind.wav");
    meow = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/meow.wav");
    laugh = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/laugh.wav");    
    chirp = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/chirp.wav");  
}


function soundSetup(){
    wind.setVolume(.1);
    meow.setVolume(2);
    chirp.setVolume(.1);
    laugh.setVolume(2);
}

function setup() {
    createCanvas(400, 400);
    //frameRate(5);
    imageMode(CENTER);
    useSound();

}

function draw() {
    count++; // add one frame to draw function
    //background image
    image(bgPhoto, width/2, height/2, width, height);



    wind.play();
    chirp.play();



    //call characters
    drawCloud();
    drawbird();
    drawTandJ()
    drawSrishty();
  
    if (count == 450 || count == 750) {
        meow.play();
    }
    if (count == 500 || count ==800) {
        laugh.play();
    }
}

// function for bird flapping

function drawbird() {

    image(birdImage[birditem], birdX, 104, birdwidth,30); 
    // if the frame counter is odd have the bird's item be the first one, if even have it be the second one 
    //fkapping affect 
    if (count % 2 === 0 ) { // bird frame 1
        birditem = 1;
        birdwidth = 100
        birdX -= .5; // bird flies to left
    }
    if (count % 2 === 1 ) {    // bird frame 0
        birditem = 0;
        birdwidth = 80;
        birdX -= .5; // bird flies to left
    }

}

function drawSrishty() {
    var heightcount = .5
    if (count >= 30) { // srishty walks in 30 frames after bird

        // staggering heights so it looks like walking rather than glide
        if (count % 2 === 0 ) { //odd
            image(srishtyImage, srishtyx - heightcount, 325, 66.2218, 205.4745); 
            srishtyx -= .3; // srishty walks to left

        }
        if (count % 2 === 1 ) { //even
            image(srishtyImage, srishtyx, 325 + heightcount, 66.2218, 205.4745); 
            srishtyx -= .3; // srishty walks to left

        }
    }

}

function drawCloud() { // draw cloud
    image(cloudImage, cloudx, 69, 130.2123 , 46.6313 );
    cloudx -= 0.09; //cloud moves very slowly
}

function drawTandJ() { // tom and jerry frames chasing eachother

        if (count >= 0 & count <= 400 ) { 
            image(tomandjerry[tomandjerryitem], tomandjerryx, 300, 216.9349, 162.7012);
            tomandjerryitem = 0
        }

        if (count >= 400 & count <= 500) { 
            image(tomandjerry[tomandjerryitem], (tomandjerryx + 100) + tjdx, 300, 216.9349, 162.7012);
            tomandjerryitem = 1
            tjdx += .5
        }

        if (count >= 500 & count <= 750) {
            image(tomandjerry[tomandjerryitem], (tomandjerryx + 300) -tjdx, 400 - tjdy, 220, 120.7012);
            tomandjerryitem = 2
            tjdy += .5;
            tjdx += .5
        }
        if (count >= 750 & count <= 2000 ) { 
            image(tomandjerry[tomandjerryitem], (tomandjerryx + 200)-tjdx, 400 - tjdy, 216.9349/2, 162.7012/2); 
            tomandjerryitem = 3
            tjdy += .5;
            tjdx += 2;

        }

}
 


Project 10

Under the sea! My 5 characters are the ocean, jellyfish, fish, big whale, and small whale.

sketch
// Sowang Kundeling Project 10 Section C

var count = 0;
x = 50
var count = 0

function preload() {
  ocean = loadSound("http://localhost:8887/ocean.wav");
  low = loadSound("http://localhost:8887/low.wav");
  bubble = loadSound("http://localhost:8887/bubble.wav");
  high = loadSound("http://localhost:8887/high.wav");
  splash = loadSound("http://localhost:8887/splash.wav");

  sea = loadImage("https://i.imgur.com/cDVwxGl.jpg");
  big = loadImage("https://i.imgur.com/3j8OG8S.png");
  little = loadImage("https://i.imgur.com/eBmN7hL.png");
  jellyfish = loadImage("https://i.imgur.com/LbEcOSU.png");
  fish = loadImage("https://i.imgur.com/FSQzu31.png")
}

function soundSetup() {
  ocean.setVolume(.1);
  low.setVolume(.3);
  bubble.setVolume(.1);
  high.setVolume(.3);
  splash.setVolume(.1);
}

function setup() {
  createCanvas(sea.width, sea.height);
  frameRate(1);
  useSound();
}

function draw() {
  image(sea, 0, 0, sea.width, sea.height);
  ocean.play();

  if(count == 2 || count == 16) {
    low.play();
  } else if(count == 4 || count == 18) {
    bubble.play();
  } else if (count == 10 || count == 25) {
    high.play();
  } else if (count == 1 || count == 30 || count == 15) {
    splash.play();
  }

  if(count >= 5) {
    image(big, 20, 50, big.width/4, big.height/4);
  } if(count >= 10) {
    image(little, 400, 200, little.width/2, little.height/2);
  } if(count >= 4) {
    image(jellyfish, x, 300, jellyfish.width/4, jellyfish.height/4);
  } if (count >= 15) {
    image(fish, mouseX, mouseY, fish.width/6, fish.height/6);
  }

  count++
  x += 2

  print(count);
}

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();
}

Project 10: Sonic Story

One Night on the Farm
One night on the farm, the farmer left the animals out. The crickets were chirping and the chickens were clucking. A whirring hum was heard and an alien space ship appeared. The alien first abducted the sheep who let out a surprised “baa” as he rose into the air and shrunk. The pig was next. He let out a startled “oink” as he was lifted away from his mud. Then it was the cows turn. He let out a shocked “moo” before being whisked away. The space ship left with its new specimens, and the crickets chirped and the chickens clucked some more.

The farm images are licensed from Adobe stock images. The sounds used are from freesound.org and edited with Audacity. The sounds are used under the Creative Commons license. The owners did not want attribution. The sounds are: crickets.wav – crickets chirping, cluckcluck.wav – chickens clucking, spaceShip.wav – space ship sounds, moo.wav – a cow, baa.wav – a sheep, oink.wav – a pig.

sketch
/* Evan Stuhlfire
 * estuhlfi@andrew.cmu.edu Section B
 * Project-10:  Sonic Story - One Night on the Farm 
Assignment-10:  Sonic Story 
One Night on the Farm 
One night on the farm, the farmer left the animals 
out. The crickets were chirping and the chickens were
clucking. A whiring hum was heard and an alien space ship appeared.
The alien first abducted the sheep who let out a surprised "baa" as he
rose into the air and shrunk. The pig was next. He let out a startled
"oink" as he was lifted away from his mud. Then it was the cows turn. 
He let out a shocked "moo" before being whisked away. The 
space ship left with its new specimens, and the crickets chirped
and the chickens clucked some more.

The farm images are liscensed from Adobe stock images. The sounds used are from freesound.org and edited with Audacity.
The sounds are used under the Creative Commons liscense. The owners did not 
want attribution. The sounds are:
crickets.wav - crickets chirping, cluckcluck.wav - chickens clucking,
spaceShip.wav - space ship sounds, moo.wav - a cow, baa.wav - a sheep,
oink.wav - a pig.
 */

var farmImg = [];   // an array to store the images
var farmObjArray = []; // array to store images

// sound variables
var crickets;
var cluck;
var baa;
var moo;
var oink;
var spaceShip;

// starting sky and foreground colors
var rNight = 50;
var gNight = 100;
var bNight = 150;
var rFore = 50;
var gFore = 160;
var bFore = 80;
var moon = 100;
// position of the moon
var xMoon;
var yMoon;

// map to array indexes
var fence = 0;
var barn = 1;
var hay = 2;
var cow = 3;
var pig = 4;
var sheep = 5;
var chicken = 6; 
var rooster = 7;
var ship = 8;

function preload(){
    // These URLs are images stored on imgur 
    // images liscensed from Adobe Stock images
    // preload farm images
    var filenames = [];
    filenames[fence] = "https://i.imgur.com/bbBOrZ7.png"; 
    filenames[barn] = "https://i.imgur.com/inS5Xdt.png"; 
    filenames[hay] = "https://i.imgur.com/IXPEVak.png"; 
    filenames[cow] = "https://i.imgur.com/8XjQyMt.png"; 
    filenames[pig] = "https://i.imgur.com/7VPvVRB.png"; 
    filenames[sheep] = "https://i.imgur.com/vIFlqDY.png"; 
    filenames[chicken] = "https://i.imgur.com/vdTxUKf.png"; 
    filenames[rooster] = "https://i.imgur.com/SCYoQoX.png"; 
    filenames[ship] = "https://i.imgur.com/lAHddxj.png"
    // load farm images
    for (var i = 0; i < filenames.length; i++) {
        farmImg[i] = loadImage(filenames[i]);
    }

    // load sounds
    crickets = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/crickets2.wav");
    cluck = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/cluckcluck.wav");
    baa = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/baa.wav");
    moo = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/moo.wav");
    oink = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/oink.wav");
    spaceShip = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/spaceShip.wav");

}

function stepImage() {
    // // move and grow the space ship to get the sheep
    if(this.imageNum == ship & this.moving) {
        if(this.target == sheep) {
            if(this.x < width/4) {
                this.x += this.speed;
            }
            if(this.y < height/2.5) {
                this.y += this.speed;
            }
            if(this.ySize <= 100) {
                this.ySize += this.speed;
            }
            if(this.xSize <= 200) {
                this.xSize += this.speed;
            } else {
                if(this.drawBeam) {
                    drawBeam(this.x, this.y, this.target); 
                }
            }
        } else if(this.target == pig) {
            // move the space ship to get the pig
            this.drawBeam = true; // turn the beam back on

            if(this.ySize >= 80) {
                this.ySize -= this.speed;
            }
            if(this.xSize >= 180) {
                this.xSize -= this.speed;
            }

            if(this.y > height/3) {
                this.y -= this.speed;
            }
            if(this.x <= width/2) {
                this.x += this.speed;
            } else {
                if(this.drawBeam) {
                    drawBeam(this.x, this.y, this.target); 
                }
            }
        } else if(this.target == cow) {
            // move the space ship to get the cow
            this.drawBeam = true; // turn the beam back on

            if(this.ySize <= 120) {
                this.ySize += this.speed;
            }
            if(this.xSize <= 240) {
                this.xSize += this.speed;
            }

            if(this.y < height/1.8) {
                this.y += this.speed;
            }
            if(this.x >= width/4) {
                this.x -= this.speed;
            } else {
                // the ship is in the correct location for the beam
                if(this.drawBeam) {
                    drawBeam(this.x, this.y, this.target); 
                }
            }
        } else if(this.target < cow) {
            // fly away
            if(this.ySize >= 0) {
                this.ySize -= this.speed;
            }
            if(this.xSize >= 0) {
                this.xSize -= this.speed;
            }
            if(this.y > 0) {
                this.y -= this.speed;
            }
            if(this.x <= width) {
                this.x += this.speed;
            }
        }
    } 

    // when an animal is being abducted it shrinks and rises into the ship
    if(this.imageNum == farmObjArray[ship].target & this.moving) {
        // decrease y to rise into the ship
        if(this.y >= farmObjArray[ship].y) {
            this.y -= this.speed;
        } else {
            // turn the beam off and set target to pig
            farmObjArray[ship].drawBeam = false;

            // don't decrease the target below the cow index
            if(farmObjArray[ship].target >= cow) {
                farmObjArray[ship].target--;
            }
            
            // stop drawing this image
            this.moving = false;
            this.drawOn = false;
        }
        
        // shrink
        if(this.xSize > 0) {
            this.xSize *= .95;
        }
        if (this.ySize > 0) {
            this.ySize *= .95;
        } 
        if(this.imageNum == sheep & frameCount % 15 == 0) {
            baa.play();
        } else if(this.imageNum == pig & frameCount % 15 == 0) {
            oink.play();
        } else if(this.imageNum == cow & frameCount % 12 == 0) {
            moo.play();
        }

    }
}

function drawImage() {
    // draw image
    if(this.drawOn){
       image(farmImg[this.imageNum], this.x, this.y, this.xSize, this.ySize);
    } 
}

// Constructor for each farm image
function makeObj(cx, cy, imNum, xs, ys) {
    var c = {x: cx, y: cy, xSize: xs, ySize: ys, 
             imageNum: imNum,
             moving: false,
             drawBeam: true,
             target: sheep,
             speed: 5,
             drawOn: true,
             stepFunction: stepImage,
             drawFunction: drawImage
         }
    return c;
}

function setup() {
    createCanvas(480, 480);
    // set farm colors
    background(rNight, gNight, bNight);
    foreground(rFore, gFore, bFore);
    // set initial moon position
    xMoon = width + 25;
    yMoon = height/2.25;
    xShip = width/4;
    yShip = height/2 + 50;

    imageMode(CENTER);
    // create array of farm objects
    createFarmObjs(); 

    frameRate(10);
    useSound();
}

function soundSetup() { // setup for audio generation
    crickets.setVolume(0.1);
    cluck.setVolume(0.3);
    baa.setVolume(0.3);
    moo.setVolume(0.5);
    oink.setVolume(0.3);
    spaceShip.setVolume(0.5);
}

function draw() {
    noStroke();
    background(rNight, gNight, bNight);
    foreground(rFore, gFore, bFore);
    makeSounds();

    var currentImage;
    // loop over farm images and draw them
    for(var i = 0; i < farmObjArray.length; i++) {
        currentImage = farmObjArray[i];
        // draw the farm
        currentImage.stepFunction();
        currentImage.drawFunction();
    }

    // darken to night sky
    fadeNightSky();        

}

function makeSounds() {
    // make sounds at frameCount intervals
    // play crickets
    if(frameCount >5 & frameCount < 350 && frameCount % 25 == 0) {
        crickets.play();
    } 
    // play cluck cluck
    if ((frameCount > 24 & frameCount < 350 && frameCount % 50 == 0)) {
        cluck.play();
    }
    if(frameCount > 24 & frameCount < 200 && frameCount % 25 == 0) {
        spaceShip.play();
    }

    if(frameCount > 400){
        // stop all sounds
        crickets.stop();
        cluck.stop();
        spaceShip.stop();
        baa.stop();
        moo.stop();
        oink.stop();

        // stop looping
        noLoop();
    }
}

function createFarmObjs() {
    // create objects for farm: fence, barn, hay
    // set initial positions xPos and yPos
    var xPos = width/1.25;
    var yPos = height/2;

    // set initial size
    var xs = 125;
    var ys = 125;

    // create farm objects
    farmObjArray[fence] = makeObj(width/2, yPos + 20, fence, xs * 5, 
        ys * .25);
    farmObjArray[barn] = makeObj(xPos, yPos, barn, xs, ys);
    farmObjArray[hay] = makeObj(xPos + 50, yPos + 50, hay, xs/2, ys/2);
    farmObjArray[cow] = makeObj(width/4, height/1.2, cow, 100, 70);
    farmObjArray[pig] = makeObj(width/2, height/1.68, pig, 55, 45);
    farmObjArray[sheep] = makeObj(width/4, height/1.65, sheep, 30, 50);
    farmObjArray[chicken] = makeObj(width/1.2, height/1.35, 
        chicken, 25, 35);
    farmObjArray[rooster] = makeObj(width/1.3, height/1.45, 
        rooster, 30, 40);
    farmObjArray[ship] = makeObj(0, height/8, ship, 20, 1);
}

function drawBeam(x, y, target) {
    // draw beam
    fill(240, 250, 240, 80);
    triangle(x, y, x + 65,
        farmObjArray[target].y + 40, 
        x - 65,
        farmObjArray[target].y + 40);

    // set target to moving
    farmObjArray[target].moving = true;
}

function fadeNightSky() {
    // decrease the background colors to darken the sky to night
    var offset = 5;
    rNight -= offset;
    gNight -= offset;
    bNight -= offset;
    rNight = max(0, rNight);
    gNight = max(0, gNight);
    bNight = max(0, bNight);

    // draw moon
    if(rNight == 0) {
        fill(moon);
        ellipse(xMoon, yMoon, 50);
        moon++;
        moon = min(moon, 245);  
        xMoon -= offset;
        yMoon -= offset;
        xMoon = max(width/1.35, xMoon);
        yMoon = max(height/7.75, yMoon);
    }

    // when it is dark start moving space ship
    if(bNight == 0) {
          farmObjArray[ship].moving = true;
    }
}

function foreground(r, g, b) {
    // create lawn and mud puddle for pig
    fill(r, g, b);
    rect(0, height/2, width, height/2);

    fill(90, 80, 15);
    ellipse(width/2 - 10, height/1.6, 150, 25);
}

Space battle

The spaceship engages in fights with enemies by using energy machine guns and lasers. The most challenging part actually comes in making all the visuals work correctly.

Four characters/sounds:

  • Firing spaceship
  • Explosion
  • Charge laser
  • Shoot laser

Space battle

//Jason Jiang
//Section E

///Storyline: The spaceship engages fights with enemies through using energy machine gun and laser.

//setting up variables for stars
var star = [];
var N = 10; //number of starts
var dx = 10; //speed of star
var w = 600; //x location of enemy
var dShip = 10; //speed of enemy
var a = 0;//scale of laser beam
var imgX = 120;//x location of ship
var imgY = 100;//y location of ship
var angle = 0;//angle of ship
var laserCharged = false; //whether laser is charged
var laserFired = false; //whether laser is fired
var enemyDestroyed = false; //whether enemy is destroyed


//defining assets
var machineGun;
var explosion1;
var explosion2;
var explosion3;
var laserEnergy;
var laserBeam;
var flyEngine;
var machineGun;
var explosion;
var charge;
var laser;

//add assets
function preload(){
    spaceShip = loadImage("https://i.imgur.com/D1QDG4A.png");
    spaceShipFire = loadImage("https://i.imgur.com/h8bQMXa.png");
    enemy1 = loadImage("https://i.imgur.com/OE8l2tK.png");
    explosion1 = loadImage("https://i.imgur.com/c9tLLTo.png");
    explosion2 = loadImage("https://i.imgur.com/kLTBXIa.png");
    explosion3 = loadImage("https://i.imgur.com/0zhmrzn.png");
    laserEnergy = loadImage("https://i.imgur.com/bmn9wZX.png");
    laserBeam = loadImage("https://i.imgur.com/ABposfH.png");
    machineGun = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/machineGun.mp3");
    explosion = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/explosion.wav");
    charge = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/charge.wav")
    laser = loadSound("https://courses.ideate.cmu.edu/15-104/f2022/wp-content/uploads/2022/11/laser.wav")
}

// setup for audio generation
function soundSetup() { 
    machineGun.setVolume(0.1);
    explosion.setVolume(0.1);
    charge.setVolume(0.2);
    laser.setVolume(0.1);
}

//Create galaxy background
function updateStar(){
    this.x -= dx;
}

function createStar(){
    strokeWeight(3);
    stroke(255);
    point(this.x, this.y);
}

function generateStar(sx, sy){
   for (var i=0; i<N; i++){
        var s = {
            x: sx, y: sy,
            stepFunction: updateStar,
            drawFunction: createStar
        }

    }
    return s;
} 

function setup() {
    createCanvas(400, 200);
    frameRate(10);
    imageMode(CENTER);
    for (var i=0; i<N; i++){
        var s = generateStar(random(width), random(height))
        star.push(s);
 }
     useSound();
}


function draw() {
    background(0);
    //setting up sequence for charging laser and firing laser
    if (a >= 1){
            if(laserCharged == false){
                laserCharged = true
                a = 0
                charge.stop()
                laser.play();
            }
            else if(laserCharged == true & laserFired == false){
                laserFired = true
                a = 0
                laser.pause();
            }
        }
    //resetting enemy position if destroyed  
    if (enemyDestroyed == true){
            w = 600;
        }

   //Update star
    for (var i=0; i<N; i++){
        var s = star[i]
        s.stepFunction();
        s.drawFunction();
        //Remove star that is out of bounds
        if (s.x < 0) {
            var s = generateStar(random(width+5, width+500), random(height))
            star.splice(i, 1, s);
        }
   
    }
    
    //flying spaceship
    if (frameCount >= 0 & frameCount < 30) { 
        flyinSpace(imgX, imgY, 0); 
    }
    
    //killing enemy
    if (frameCount >= 30 & frameCount < 60) { 
        
        //play ship firing sound
        if (w == 600){
            machineGun.loop();  
        }
        
            //ship firing
            shipFiring(w);
            //enemy Ship approaching
            enemyShip(w, height/2);
            w -= dShip; 
       
    
        }
    
    if (frameCount >= 60 & frameCount < 70) { 
        //stop firing
        flyinSpace(imgX, imgY, 0);
        //play explosion sound when the enemy is close
        if(frameCount == 60){
            machineGun.stop();
            explosion.play()
        }
            explode(w, height/2, 60);
        }
    
    if (frameCount >= 70 & frameCount < 110) { 
        //setting up next enemy
        enemyDestroyed = false;
        if(frameCount == 70){
            charge.loop();
        }

        //dodge laser 
        if(dist(imgX, imgY, 120, 160)>0 & laserCharged == true){
        flyinSpace(imgX, imgY, 0);
        imgY += dShip;  
        }
        else{
            flyinSpace(imgX, imgY, 0);
        }

        //enemy Ship
            enemyShip(w, height/2);
            w -= 0.5*dShip;
        
        //charge laser
        if (laserCharged == false){
            laserCharge(w, height/2, a);
            a+=0.05;
        }
        //fire laser
        else if(laserCharged == true & laserFired == false){
            
            laserCharge(w, height/2, 1);
            laserFire(w, height/2, -a);
            a+=0.05;
        }
    
   
}
        
        if (frameCount >= 110 & frameCount < 140) {
            //preparing for next shot
            if (frameCount == 110){
              laserCharged = false;
              laserFired = false; 
              charge.loop();
            }

            //enemy Ship
            enemyShip(w, height/2);
            w -= 0.5*dShip;
            
            //ship fire laser
            //rotate ship
            if (angle>=-18){
                flyinSpace(imgX, imgY, angle);
                angle-=3;
            }
            //fire laser after rotation
            else{
                //charge laser
                flyinSpace(imgX, imgY, angle)
                if (laserCharged == false){
                    laserCharge(imgX, imgY, a);
                    a+=0.1;
                }
                //fire laser
                else if(laserCharged == true & laserFired == false){
                    
                    laserCharge(imgX, imgY, 1);
                    laserFire(imgX, imgY, a, angle);
                a+=0.1;
            }
             
        }
    }
        
        if (frameCount >= 140 & frameCount < 150) { 
        flyinSpace(imgX, imgY, angle);
        //play explosion sound when the enemy is close
            if(frameCount == 140){
                explosion.play();
            }
            explode(w, height/2, 140);
        }

        if (frameCount >= 150){
            background(0);
            noLoop();
        }
}

//adding spaceship
function flyinSpace(x, y, a){
        push()
        translate(x, y);
        rotate(radians(a));
        image(spaceShip, 0, 0);
        pop()
        
}

function shipFiring(w){
        //ship firing
        if(w%20 == 0){
            image(spaceShipFire, 120, height/2);
        }
        else{
            image(spaceShip, 120, height/2);
        }
    }

function enemyShip(w, h){ 
        //enemy ship flying
            image(enemy1, w, h);
        }

function explode(w, h, f){
        //create explosion when the enemy is close
                image(explosion1, w, h);
                if(frameCount-f >= 3){
                    image(explosion2, w, h);
                }
                if(frameCount-f >= 6){
                    image(explosion3, w, h);
                    enemyDestroyed = true;
                }
        
        }

//adding laser charge
function laserCharge(x, y, a){
        push()
        translate(x, y);
        scale(a);
        image(laserEnergy, 0, 0);
        pop()

}

//adding laser beam
function laserFire(x, y, a, angle){
        push()
        translate(x, y);
        rotate(radians(angle));
        scale(-a, 1);
        image(laserBeam, -211, -0.5);
        pop()

}