Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//VARIABLES

//initialize variables for Player physics
float playerX = 150;
float playerY = 100;
float speed = 0;
float fall = 0;
float momentum = 0.1;
float gravity = 0.1;

int ground = 200;

//Arrays for confetti to fall; x and y coordinates and color
float[] confettiX = new float[50];
float[] confettiY = new float[50];
float[] confettiColor = new float[50];

//initialize variables for confetti fall and sway.
float y = 0;
float x = 0;
int xSpeed = 0;

//initialize variables for button and confetti activation
int button;
int buttonColor;
boolean party;



//SETUP

//Set up program and set all confetti arrays to random values.
void setup() {
  size(300, 300);
  smooth();

  println("Jump the wall and smash the button as hard as you can!");

  for (int i = 0; i < confettiX.length; i++) {
    confettiX[i] = random(1, 300);
    confettiY[i] = random(-300, -30);
    confettiColor[i]= random(0, 255);
  }
}


//DRAW

//Draw function used only to call to functions and draw ground.
void draw() {
  background(255);
  fill(0);

  character(); //character function
  target(50, ground-10+button); //button function
  wall(100, ground-90); //wall collision function
  fill(0);
  rect(0, ground, width, height); //ground



  //If the button is fully pressed, initiate "party" function; confetti falls.
  if (party == true) {
    callFall();
    callParty();          
    println("Yay!");
  }
}




//FUNCTIONS


//CHARACTER FUNCTION

void character() {
  fill(fall*20,-speed*20,speed*20);
  ellipse(playerX, playerY, 20, 20);

  //CONTROLS

  if (keyPressed) {
    if (keyCode == RIGHT) {
      playerX = playerX + speed;
      speed = speed + momentum;
    }
    if (keyCode == LEFT) {
      playerX = playerX + speed;
      speed = speed - momentum;
    }
    if (keyCode == UP) { //First checks if player is moving left or right while jumping, otherwise jump in place.
      playerY = playerY-5;
      if (speed >= 0.6) {  
        playerX = playerX + speed;
      } else if (speed <= -0.1) {
        playerX = playerX + speed;
      } else if (speed == 0.5 || momentum == 0.1) {
        playerX = playerX;
        speed = 0;
      }
    }
  } else {
    speed = 0.5;
  }  

  if (playerY != ground) {  //Move freely while off ground.
    playerX = playerX;
  }

  if (playerY+10 >= ground) { //Collide with ground.
    playerY = ground-10;
    fall = 0.1;
    gravity = 0.1;
  } else if (playerY <= ground) {
    playerY = playerY + fall;
    fall = fall + gravity;
  }
}


//BUTTON FUNCTION

void target(int locX, int locY) {
  fill(buttonColor, 0, 0);
  rect(locX, locY, 30, 10);

  //If player jumps on button with enough force; button lowers and changes color.
  if (playerX <= locX+35 && playerX >= locX-5 && playerY >=ground-10 && fall >= 5 ) {
    button = button +2;
    buttonColor = buttonColor+50;

    if (locY ==198) { //If the button is pully pushed; initiate party confetti.
      party = true;
    }
  }
}



//PARTY FUNCTION

void callParty() { //Use confetti arrays to generate random confetti.
  for (int i = 0; i < confettiX.length; i++) {
    fill(confettiColor[i], confettiColor[i]*2, confettiColor[i]/2);
    rect(confettiX[i]+x, confettiY[i]+y/2, 10, 5);
  }
}


//FALL FUNCTION for confetti

void callFall() {
  y++;  //Confetti falls
  xSpeed++; //Confetti sways
  if (xSpeed == 10 || xSpeed == 20 || xSpeed == 30) {
    x+=3; //Confetti moves 3 units every 10 increments of xSpeed
  } else if (xSpeed == -10 || xSpeed == -20 || xSpeed == -30) {
    x-=3;
  }

  if (xSpeed == 50) { //xSpeed reverses periodically to create sway
    xSpeed = xSpeed*-1;
  } else if (xSpeed == -50) {
    xSpeed = xSpeed*-1;
  }
}

//WALL FUNCTION

void wall(int locX, int locY) {
  rect(locX, locY, 10, 90);

  if (playerX <= locX+20 && playerY >= locY && playerX >= locX) {
    playerX = locX+20; //If player hits wall, stop player X position
  }
}