yunzhous_final project_resubmission

I reposted this project so that audio file would work.

sketch

//Kathy Song
//Section D
//yunzhous@andrew.cmu.edu
//Final Project

var trees = [];
var treeWidth = 50;
var treeHeight = 67;
var FL = 380;//y position of finish line
var numMiss = 0;
var combo = 0;
var score = 0;
var happy = "https://i.imgur.com/vDITqlq.png";//happy charlie brown
var backImg = "https://i.imgur.com/4QArmk0.jpg";//background image
var gamescreen = "https://i.imgur.com/kLeHBF4.jpg";//start screen
var CharlieBrown = "https://i.imgur.com/8OMKsmc.png";
var ChristmasTree = "https://i.imgur.com/mQ5fO5A.png";
var drawScreen = 0;//determines if start screen should be drawn
var mySnd;//background music
var count = 1; //framecount after game starts
var gameEnd = false;//determines if game has ended

function preload(){
    mySnd = loadSound("https://courses.ideate.cmu.edu/15-104/f2017/wp-content/uploads/2017/12/We-Wish-You-A-Merry-Christmas.wav");//background music
    mySnd.setVolume(0.5);
    backgroundImg = loadImage(backImg);
    screenImg = loadImage(gamescreen);
    CLImag = loadImage(CharlieBrown);
    treeImg = loadImage(ChristmasTree);
    happyImg = loadImage(happy);
}

function setup() {
    createCanvas(480, 480);
    if (drawScreen == 0){//initial screen
        image(screenImg, 0, 0);
        strokeWeight(2);
        stroke(255);
        fill(0);
        textSize(24);
        textAlign(CENTER);
        text("Click To Start", width/2, height - 40);
        text("CHARLIE BROWN'S", width/2, 50);
        text("CHRISTMAS TREE", width/2, 80);
    }
}

function draw() {
    if (count % 3150 == 0){//music ends
        gameEnd = true;//game ends
        text("Press R To Restart", width/2, height - 40);
        count = 1;//reset count to 1
    }

    if (drawScreen == 0){
        //nothing happens
    }
    else if (gameEnd == false) {//if not initial screen, and game hasn't end, do following
        count += 1;//count frames after game starts
        print(count);
        image(backgroundImg, 0, 0);
        strokeWeight(2);
        stroke(255);
        line(0, FL, width, FL);//finish line
        fill(0);
        textSize(20);
        text("Combo " + combo, width - 60, 30);
        text("Score " + score, width - 50, 60);
        treeFunctions();//trees dropping from sky
        addTree();
        var CLx = mouseX - 20;
        if (CLx > width){
            CLx = width - 50;
        }
        image(CLImag, CLx, FL);//charlie brown image with cursor
    }

}

function keyPressed(){
    if (keyCode == 82){//if R is pressed
        gameEnd = false;//gameEnd becomes false, game restart
        mySnd.play();//music restart
    }
}

function mousePressed(){//after mouse is pressed, draw screen becomes 1,
                        //initial screen disappears and music starts playing
  if (drawScreen == 0) {
    drawScreen = 1;
    mySnd.play();
  }
}

function makeTree(bx, by){//make tree object
    var tree = {x: bx,
                y: by,
                w:treeWidth,
                h:treeHeight,
                speed: 3,
                move: treeMove,
                display: treeDisplay,
                click:treeClick,
                miss:treeMiss,
                died: false,//after clicking on the tree, the tree dies and disappear
                counted: false,
                countedMiss: false,
    }
    return tree;
}


function treeDisplay(){//draw tree
    if(!this.died) {
        image(treeImg, this.x, this.y);
    }
}

function treeMove()  {
    this.y += this.speed;//move trees by speed

    //change tree speed and the game gets harder
    if (count > 560 & count < 1100) {
        this.speed = 5;
    }
    if (count > 1100 & count < 2000) {
        this.speed = 6;
    }
    if (count > 2000 & count < 3150) {
        this.speed = 7;
    }

}

function treeFunctions(){//update tree's position and display them
    for (var i = 0; i < trees.length; i++){
        trees[i].move();
        trees[i].display();
        trees[i].click();
        trees[i].miss();
    }
}

function addTree(){//add new trees
    var spacing = 40;

    if (count % 35 == 0) {
        trees.push(makeTree(30 + random(10) * spacing, 0));
    }

}

function treeClick(){
    //if click on tree, tree disappear
    if (mouseIsPressed & mouseX > this.x && mouseX< this.x + this.w && mouseY > this.y && mouseY < this.y + this.h){
        this.died = true;
    }
    //if click on tree at the finish line, get one point
    if (mouseIsPressed & mouseX > this.x && mouseX< this.x + this.w && this.y > FL - this.h && this.y < FL + this.h && !this.counted){
        image(happyImg, width - 50, FL);
        this.counted = true;
        combo += 1;
        score += 1;
    }
}

function treeMiss(){
    //if miss one tree, combo starts from 0
    if (this.y >= FL + 5 & !this.countedMiss && !this.counted){
        numMiss += 1;
        combo = 0;
        this.countedMiss = true;
    }
}

My final project a game called “Charlie Brown’s Christmas Tree”. Christmas trees would randomly drop form the sky and if the player clicks on the trees, they would disappear. However, the only way the player can get score is by clicking on the tree when it’s at the finish line. If the player continuously getting the tree he or she will receive a combo. If he or she missed one tree the combo would start from zero. The flash start with an start screen and the game only starts when players click on the screen. The game ends when the music ends, and if the players press “R” it will restart.

Leave a Reply