Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
 *  PROG14998: INTRODUCTION TO MEDIA COMPUTING
 *  FALL 2018, PROF: NICOLAS HESLER
 *
 *  BY TING PEI CHEN (ZOE)
 *
 *  ABOUT:
 *    Move the block with your mouse to collect coins.
 *    Avoid the spikes.
 *
 */

/* ALL VARIABLES */

boolean death = true;            //if death is true, draw start screen

/* PARTICLE VARIABLES */

//particles for coin A
float particlePathA = 0;          //particle path on the x-axis
float particleFlingA = 15;        //particle fling altitude

//particles for coin B
float particlePathB = 0;          //particle path on the x-axis
float particleFlingB = 15;        //particle fling altitude

//particles for coin C
float particlePathC = 0;          //particle path on the x-axis
float particleFlingC = 15;        //particle fling altitude

//particles for coin D
float particlePathD = 0;          //particle path on the x-axis
float particleFlingD = 15;        //particle fling altitude

/*COINS VARIABLES*/

//global coin variables
float coinRadius = 10;            //radius of the coins
boolean flipped = true;           //if flipped is true, span coin
boolean coinCollision = false;    //if true, call collision function
int coinSpeedIncrement = 1;       //the speed increment per level

//coin A variables
float randomYcoinA;               //random y value for coin A
float coinSpeedA = 0;             //coinSpeed of coin A
float flipSpeedA = 1;             //coinSpeed of coin flip
float fillA = 155;                //fill of coin A
boolean collectA = false;         //true if coin has been collected

//coin B variables
float randomYcoinB;               //random y value for coin B
float coinSpeedB = 0;             //coinSpeed of coin B
float flipSpeedB = 1;             //coinSpeed of coin flip
float fillB = 255;                //fill of coin A
boolean collectB = false;         //true if coin has been collected

//coin C variables
float randomYcoinC;               //random y value for coin C
float coinSpeedC = 0;             //coinSpeed of coin C
float flipSpeedC = 1;             //coinSpeed of coin flip
float fillC = 255;                //fill of coin A
boolean collectC = false;         //true if coin has been collected

//coin D variables
float randomYcoinD;               //random y value for coin D
float coinSpeedD = 0;             //coinSpeed of coin D
float flipSpeedD = 1;             //coinSpeed of coin flip
float fillD = 255;                //fill of coin A
boolean collectD = false;         //true if coin has been collected


void setup() {

  size(400, 400);
  rectMode(CENTER);
  ellipseMode(CORNERS);
  fill(255);
}

void draw() {

  background(0);
  noStroke();

  checkDeath();  //check if player is dead

  if (death == true) {  //if player is dead, draw start screen
    drawScreen();
  } else {

    drawSpikes();

    collision();
    collection();

    updateCoinA();
    drawCoinA();
    updateCoinB();
    drawCoinB();
    updateCoinC();
    drawCoinC();
    updateCoinD();
    drawCoinD();

    drawHead();
  }
}



/* EVENTS */



void mousePressed() {  //on click, draw toy screen
  death = false;
}

void keyPressed() {  //on press, add coin speed
  coinSpeedIncrement = coinSpeedIncrement + 1;
}



/*CHECK FUNCTIONS*/



void checkDeath() {  //check death

  //if cursor goes above 370px or below 45px on the y-axis, set death to true and reset coin position
  if (mouseY < 45 || mouseY > 370) {   
    death = true;
    coinSpeedIncrement = 1;
  }
}

void collision() {  //check collision and set collection value to true

  if (  //coin must be within the cursor area by 20px on both x-axis and y-axis to be collected
    (((width + coinRadius - coinSpeedA) - mouseX) < 20)
    && ((mouseX - (width + coinRadius - coinSpeedA)) < 20)
    && (((randomYcoinA + coinRadius) - mouseY) < 20)
    && ((mouseY - (randomYcoinA + coinRadius)) < 20)
    ) {
    collectA = true;
  }

  if (  //coin must be within the cursor area by 20px on both x-axis and y-axis to be collected
    (((100 + width + coinRadius - coinSpeedB) - mouseX) < 20)
    && ((mouseX - (100 + width + coinRadius - coinSpeedB)) < 20)
    && (((randomYcoinB + coinRadius) - mouseY) < 20)
    && ((mouseY - (randomYcoinB + coinRadius)) < 20)
    ) {
    collectB = true;
  }

  if (  //coin must be within the cursor area by 20px on both x-axis and y-axis to be collected
    (((200 + width + coinRadius - coinSpeedC) - mouseX) < 20)
    && ((mouseX - (200 + width + coinRadius - coinSpeedC)) < 20)
    && (((randomYcoinC + coinRadius) - mouseY) < 20)
    && ((mouseY - (randomYcoinC + coinRadius)) < 20)
    ) {
    collectC = true;
  }

  if (  //coin must be within the cursor area by 20px on both x-axis and y-axis to be collected
    (((300 + width + coinRadius - coinSpeedD) - mouseX) < 20)
    && ((mouseX - (300 + width + coinRadius - coinSpeedD)) < 20)
    && (((randomYcoinD + coinRadius) - mouseY) < 20)
    && ((mouseY - (randomYcoinD + coinRadius)) < 20)
    ) {
    collectD = true;
  }
}

void collection() {  //if collection of the coin is true, coin will disappear and fall off using particle

  if (collectA == true) {
    fillA = 0;  //appear black
    drawParticlesA();
  } else {
    fillA = 255;  //appear white when false
  }

  if (collectB == true) {
    fillB = 0;  //appear black
    drawParticlesB();
  } else {
    fillB = 255;  //appear white when false
  }

  if (collectC == true) {
    fillC = 0;  //appear black
    drawParticlesC();
  } else {
    fillC = 255;  //appear white when false
  }

  if (collectD == true) {
    fillD = 0;  //appear black
    drawParticlesD();
  } else {
    fillD = 255;  //appear white when false
  }
}



/* DRAW FUNCTIONS*/



void drawScreen() {
  background(0);
  textSize(32);
  text("Click to Start", 100, 200);  //use text to tell player how to start
}

void drawSpikes() {  
  fill(#392626);  //for as long as i is inside canvas on the x-axis, continue to draw spikes
  for (int i = 0; i < height; i = i + 10) {
    triangle(i, 0, i+5, 45, i+10, 0);
    triangle(i, height, i+5, 370, i+10, height);
  }

  fill(255);  //set fill back to white
}

void drawHead() {

  //make sure head is white
  fill(255);

  //head of worm will follow previous mouse position
  rect(pmouseX, pmouseY, 40, 40, 10);
}

//coin A
void drawCoinA() {

  //draw the coin thickness and the coin ellipse
  //draw coin according to specified positions
  rect(width + coinRadius - coinSpeedA, randomYcoinA + coinRadius, 3, 2*coinRadius, 3);
  ellipse(width - coinSpeedA + flipSpeedB, randomYcoinA, width + 2*coinRadius - coinSpeedA - flipSpeedB, randomYcoinA + 2*coinRadius);
}

//coin B
void drawCoinB() {

  //draw the coin thickness and the coin ellipse
  //offset coin by 100px and draw coin according to specified positions
  rect(100 + width + coinRadius - coinSpeedB, randomYcoinB + coinRadius, 3, 2*coinRadius, 3);
  ellipse(100 + width - coinSpeedB + flipSpeedB, randomYcoinB, 100 + width + 2*coinRadius - coinSpeedB - flipSpeedB, randomYcoinB + 2*coinRadius);
}

//coin C
void drawCoinC() {

  //draw the coin thickness and the coin ellipse
  //offset coin by 200px and draw coin according to specified positions
  rect(200 + width + coinRadius - coinSpeedC, randomYcoinC + coinRadius, 3, 2*coinRadius, 3);
  ellipse(200 + width - coinSpeedC + flipSpeedC, randomYcoinC, 200 + width + 2*coinRadius - coinSpeedC - flipSpeedC, randomYcoinC + 2*coinRadius);
}

//coin D
void drawCoinD() {

  //draw the coin thickness and the coin ellipse
  //offset coin by 300px and draw coin according to specified positions
  rect(300 + width + coinRadius - coinSpeedD, randomYcoinD + coinRadius, 3, 2*coinRadius, 3);
  ellipse(300 + width - coinSpeedD + flipSpeedD, randomYcoinD, 300 + width + 2*coinRadius - coinSpeedD - flipSpeedD, randomYcoinD + 2*coinRadius);
}

//particles
void drawParticlesA() {

  //set mode to center
  ellipseMode(CENTER);

  //moving particle on path
  ellipse(width + coinRadius - coinSpeedA + particlePathA, randomYcoinA + coinRadius + (particlePathA*particlePathA)-(particleFlingA*particlePathA), 15, 15);
  particlePathA = particlePathA + 1;

  //set mode back to corners
  ellipseMode(CORNERS);
}

void drawParticlesB() {

  //set mode to center
  ellipseMode(CENTER);

  //moving particle on path
  ellipse(100 + width + coinRadius - coinSpeedB + particlePathB, randomYcoinB + coinRadius + (particlePathB*particlePathB)-(particleFlingB*particlePathB), 15, 15);
  particlePathB = particlePathB + 1;

  //set mode back to corners
  ellipseMode(CORNERS);
}

void drawParticlesC() {

  //set mode to center
  ellipseMode(CENTER);

  //moving particle on path
  ellipse(200 + width + coinRadius - coinSpeedC + particlePathC, randomYcoinC + coinRadius + (particlePathC*particlePathC)-(particleFlingC*particlePathC), 15, 15);
  particlePathC = particlePathC + 1;

  //set mode back to corners
  ellipseMode(CORNERS);
}

void drawParticlesD() {

  //set mode to center
  ellipseMode(CENTER);

  //moving particle on path
  ellipse(300 + width + coinRadius - coinSpeedD + particlePathD, randomYcoinD + coinRadius + (particlePathD*particlePathD)-(particleFlingD*particlePathD), 15, 15);
  particlePathD = particlePathD + 1;

  //set mode back to corners
  ellipseMode(CORNERS);
}



/*UPDATE FUNCTIONS*/



//coin A
void updateCoinA() {

  fill(fillA);

  if (width - coinSpeedA + flipSpeedA == width + 1) {                  //everytime the coin repositions, assign new y-axis
    randomYcoinA = random(60, 350);
  } else if (width + 2*coinRadius - coinSpeedA - flipSpeedA < 0) {     //if the coin has run out of screen, reposition and reset
    coinSpeedA = 0;
    collectA = false;
    particlePathA = 0;
  }

  if (flipSpeedA < coinRadius && flipped == true) {                    //if coin is flipped, unflip coin
    flipSpeedA++;
  } else if (flipSpeedA == 0) {                                        //if coin becomes unflipped, proceed to flip coin again
    flipped = true;
  } else {                                                             //otherwise, continue to unflip coin
    flipSpeedA--;
    flipped = false;
  }

  //the coin shall move
  coinSpeedA = coinSpeedA + coinSpeedIncrement;
}

//coin B
void updateCoinB() {

  fill(fillB);

  if (100 + width - coinSpeedB + flipSpeedB == width + 101) {                        //everytime the coin repositions, assign new y-axis
    randomYcoinB = random(60, 350);
  } else if (100 + width + 2*coinRadius - coinSpeedB - flipSpeedB < 0) {     //if the coin has run out of screen, reposition and reset
    coinSpeedB = 100;
    collectB = false;
    particlePathB = 0;
  }

  if (flipSpeedB < coinRadius && flipped == true) {                    //if coin is flipped, unflip coin
    flipSpeedB++;
  } else if (flipSpeedB == 0) {                                        //if coin becomes unflipped, proceed to flip coin again
    flipped = true;
  } else {                                                             //otherwise, continue to unflip coin
    flipSpeedB--;
    flipped = false;
  }

  //the coin shall move
  coinSpeedB = coinSpeedB + coinSpeedIncrement;
}

//coin C
void updateCoinC() {

  fill(fillC);

  if (200 + width - coinSpeedC + flipSpeedC == width + 201) {                        //everytime the coin repositions, assign new y-axis
    randomYcoinC = random(60, 350);
  } else if (200 + width + 2*coinRadius - coinSpeedC - flipSpeedC < 0) {     //if the coin has run out of screen, reposition and reset
    coinSpeedC = 200;
    collectC = false;
    particlePathC = 0;
  }

  if (flipSpeedC < coinRadius && flipped == true) {                    //if coin is flipped, unflip coin
    flipSpeedC++;
  } else if (flipSpeedC == 0) {                                        //if coin becomes unflipped, proceed to flip coin again
    flipped = true;
  } else {                                                             //otherwise, continue to unflip coin
    flipSpeedC--;
    flipped = false;
  }

  //the coin shall move
  coinSpeedC = coinSpeedC + coinSpeedIncrement;
}

//coin D
void updateCoinD() {

  fill(fillD);

  if (300 + width - coinSpeedD + flipSpeedD == width + 301) {                        //everytime the coin repositions, assign new y-axis
    randomYcoinD = random(60, 350);
  } else if (300 + width + 2*coinRadius - coinSpeedD - flipSpeedD < 0) {     //if the coin has run out of screen, reposition and reset
    coinSpeedD = 300;
    collectD = false;
    particlePathD = 0;
  }

  if (flipSpeedD < coinRadius && flipped == true) {                    //if coin is flipped, unflip coin
    flipSpeedD++;
  } else if (flipSpeedD == 0) {                                        //if coin becomes unflipped, proceed to flip coin again
    flipped = true;
  } else {                                                             //otherwise, continue to unflip coin
    flipSpeedD--;
    flipped = false;
  }

  //the coin shall move
  coinSpeedD = coinSpeedD + coinSpeedIncrement;
}