William Su – Project 10 – Sonic Sketch

For this project I saw the opportunity to make a simple game. In this case, a very cruddy p5 version of CS:GO. Enjoy!

sketch

//William Su
//wsu1@andrew.cmu.edu
//Section E
//Project 10 

var bgurl = "https://i.imgur.com/CYEy7BV.jpg"; //background.
var muzzleFlash = "https://i.imgur.com/68jJZkV.png"; //muzzle flash from the tip of gun.
var mfTrue = false; //currently false if no click;
var T1Alive = true; //Whether enemies are currently alive or not.
var T2Alive = true;
var T3Alive = true;
var Tenemy = "https://i.imgur.com/Nh8X1RG.png"; //Enemy image
var bg; // Background img
var mf; // MuzzleFlash
var T1HitCount = 0; //Number of times each enemy has been hit.
var T2HitCount = 0;
var T3HitCount = 0;

function preload() {
    bg = loadImage(bgurl);
    mf = loadImage(muzzleFlash);
    T1 = loadImage(Tenemy);
    T2 = loadImage(Tenemy);
    T3 = loadImage(Tenemy);

    // Local File
    // AKshot = loadSound("assets/AK.mp3");
    // Death = loadSound("assets/Death.mp3");
    // Death2 = loadSound("assets/Death2.mp3");
    // Hit = loadSound("assets/Hit.mp3");
    AKshot = loadSound("https://courses.ideate.cmu.edu/15-104/f2019/wp-content/uploads/2019/11/AK.mp3");
    Death = loadSound("https://courses.ideate.cmu.edu/15-104/f2019/wp-content/uploads/2019/11/Death.mp3");
    Death2 = loadSound("https://courses.ideate.cmu.edu/15-104/f2019/wp-content/uploads/2019/11/Death2.mp3");
    Hit = loadSound("https://courses.ideate.cmu.edu/15-104/f2019/wp-content/uploads/2019/11/Hit.mp3");
}


function setup() {
    createCanvas(720, 400);
    bg.resize(720,400);
    frameRate(60);
    noCursor();
}


function draw() {
    if (mfTrue == true) { //If mouse pressed, activate muzzle flash.
        mfTrue = false; //Reset muzzle flash as false.
        image(bg, 0, 0);
        image(mf, 230, 30); //Draw muzzle flash and background.
    } else {
        image(bg, 0, 0);
    }

    //Draw the three enemies.
    if (T1Alive == true) {
        push();
        image(T1,180,180);
        T1.resize(35,70);
        pop();
    } 

    if (T2Alive == true) {
        push();
        image(T2,500,180);
        T2.resize(25,50);
        pop();
    } 

    if (T3Alive == true) {
        push();
        image(T3,370,192);
        T3.resize(13,26);
        pop();
    } 

    if (T1Alive == false & T2Alive == false && T3Alive == false) { //Reset if all enemies are dead.
        T1Alive = true;
        T2Alive = true;
        T3Alive = true;
    }

    crosshairs(); //Draw Crosshair
}

function crosshairs() { //Crosshair 
    stroke("white");
    strokeWeight(2);
    line(mouseX,mouseY,mouseX+20,mouseY);
    line(mouseX,mouseY,mouseX-20,mouseY);
    line(mouseX,mouseY,mouseX,mouseY+20);
    line(mouseX,mouseY,mouseX,mouseY-20);
}

function mousePressed() {
    //Play gunshot every click.
    AKshot.play();
    mfTrue = true;
    
    //Enemy 1 Hitbox
    if(mouseX > 180 & mouseX < 205 && mouseY > 180 && mouseY < 250 && T1Alive == true) {

        if (T1HitCount <= 4) { //Need 5 hits to kill.
            Hit.amp(2.5);
            Hit.play();
            T1HitCount += 1;
        } else { //Play death
            Death.amp(3);
            Death.play();
            Hit.amp(2);
            Hit.play();
            T1Alive = false;
            T1HitCount = 0;
        }
    }

    //Enemy 2 Hitbox
    if(mouseX > 500 & mouseX < 525 && mouseY > 180 && mouseY < 230 && T2Alive == true) {
        if (T2HitCount <= 4) { //Need 5 hits to kill.
            Hit.amp(2.5);
            Hit.play();
            T2HitCount += 1;
        } else { //Play death
            Death2.amp(3);
            Death2.play();
            Hit.amp(2);
            Hit.play();
            T2Alive = false;
            T2HitCount = 0;
        }
    }

    //Enemy 3 Hitbox
    if(mouseX > 370 & mouseX < 383 && mouseY > 192 && mouseY < 218 && T3Alive == true) {
        if (T3HitCount <= 4) { //Need 5 hits to kill.
            Hit.amp(2.5);
            Hit.play();
            T3HitCount += 1;
        } else { //Play death
            Death.amp(3);
            Death.play();
            Hit.amp(2);
            Hit.play();
            T3Alive = false;
            T3HitCount = 0;
        }
    }
}

Leave a Reply