Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
* Block Boy's Adventure: Escaping Mt. Khan
* By: Daniel Nava
* Last Updated: October 21 2016
*
* Help Block Boy escape Mt. Khan by climbing to the top and dodging the incoming boulders.
* Move left with the "a" key and right with the "d" key. Jump with the spacebar.
* If you fall into the mountain or get hit by a boulder, you will respawn at the lowest platform.
*/

////GLOBAL VARIABLES////

//Variables for player class for the side to side movement and gravity.
float left = 0;
float right = 0;
float gravity = 0.06;

//variable for win screen
boolean winCondition = false;

////END OF GLOBAL VARIABLES////

////CREATING OBJECTS////

Player p1;

//Lower set of Platforms
Platform[] plats = new Platform[4];
//Higher set of platforms
Platform[] plats2 = new Platform[4];

Boulder[] boulds = new Boulder[9];

////END OF OBJECT CREATION////

void setup ()
{
  size(600, 600);

  p1 = new Player ();

  // Lower Platforms
  for (int i = 0; i < plats.length; i++) {
    plats[i] = new Platform (0 + (180 * i), 550 -(50 * i), 60);
  }
  // Higher Platforms
  for (int i = 0; i < plats2.length; i++) {
    plats2[i] = new Platform (0 + (130 * i), 70 +(80 * i), 60);
  }
  for (int i = 0; i < boulds.length; i++) {
    boulds[i] = new Boulder (random(0, 600), random(-800, 0));
  }
}

void draw ()
{
  //Background color is light brown
  background (#795A39);

  drawSky();
  drawSpikes();

  p1.playerFunctions();

  for (int i = 0; i < plats.length; i++) {
    plats[i].collisionCheck(p1.playerPosition);
    p1.jump( plats[i].onFloor);
    plats[i].drawPlatform();
  }
  for (int i = 0; i < plats2.length; i++) {
    plats2[i].collisionCheck(p1.playerPosition);
    p1.jump( plats2[i].onFloor);
    plats2[i].drawPlatform();
  }

  for (int i=0; i < boulds.length; i++) {
    boulds[i].boulderFunctions();
  }

  win();
  reset();
}

////DRAWS MOUNTAIN TOP SPIKES VISUALS////
void drawSpikes() {
  //Spike color is light brown same as background
  fill(#795A39);
  //creates spikes every 150 units for a total of 4 spikes
  for (int i =0; i < 600; i += 150) {
    triangle(0 + i, 60, 60 + i, 20, 120 + i, 60);
  }
}

////DRAWS SKY VISUALS////
void drawSky() {
  //sky color is light blue
  fill(#68CED6);
  rect(0, 0, 600, 60);
}

////CREATES WIN SCREEN////
void win () {
  //creates the win screen only if the player reaches the highest platform
  if (plats2[0].onFloor == true) {
    winCondition = true;
  }

  if (winCondition == true) {
    fill(#29861B);
    rect(0, 0, 600, 600);
    fill(255);
    textSize(30);
    text("You Win!", 230, 300);
    textSize(20);
    text("press R to play again", 210, 350);
  }
}

////RESETS PLAYER LOCATION////
void reset() {
  //resets player location and removes "win" screen 
  if (keyPressed == true && key == 'r' && winCondition == true) {
    p1.playerPosition.y = 500;
    p1.playerPosition.x = 0;
    winCondition = false;
  }
}

////KEYBOARD CHECKS FOR LEFT AND RIGHT MOVMENT CONTROLS////
void keyPressed() {

  if ( key == 'a') {
    left = 1;
  }

  if ( key == 'd') {
    right = 1;
  }
}

////KEYBOARD CHECKS FOR LEFT AND RIGHT MOVMENT CONTROLS////
void keyReleased() {
  if ( key == 'a') {
    left = 0;
  }

  if ( key == 'd') {
    right = 0;
  }
}
/*
* Boulder Class
* By: Daniel Nava
* Last Updated: October 21 2016
*
* Boulders fall down from the top section of the screen at a constant rate.
* Their X spawn locations are random.
* If they hit the Player, the player's position is reset.
*/
class Boulder {
  
////INSTANCE VARIABLES////

  PVector boulderLocation = new PVector ();

////END OF INSTANCE VARIABLES////

  Boulder(float spawnBoulderX, float spawnBoulderY) {
    boulderLocation.x = spawnBoulderX;
    boulderLocation.y = spawnBoulderY;
  }
  
////BOULDER TO PLAYER COLLISION DETECTION////
  void boulderCollide(PVector playerPos, PVector playerVel) {
   
    //Resets player location if it hits a boulder
    if (playerPos.y >= boulderLocation.y - 10  && playerPos.y <= (boulderLocation.y + 50)
      && playerPos.x >= boulderLocation.x  && playerPos.x <= (boulderLocation.x +  50))
    {
      playerPos.x = 10;
      playerPos.y = 500;
      playerVel.y = 0;
      playerVel.x = 0;
    }
  }

////MOVES BOULDER AT A CONSTANT SPEED IN A FIXED DIRECTION////
  void fall() {
    boulderLocation.y += 3;
  }

////DRAWS BOULDER VISUALS////
  void drawBoulder () {
    //Grey color for boulders
    fill(150);
    rect(boulderLocation.x - 5, boulderLocation.y + 20, 60, 20);
    rect(boulderLocation.x, boulderLocation.y, 50, 50);
  }

////RESETS BOULDER LOCATION ONCE OUT OF SCREEN////
  void boulderRespawn()
  {
    if (boulderLocation.y >= 600) {
      boulderLocation.x = random(100, 400);
      boulderLocation.y = random(-800, 0);
    }
  }

////GROUPS ALL FUNCTIONS INTO ONE////
//makes it easier to place within the main class
  void boulderFunctions() {
    boulderCollide( p1.playerPosition, p1.playerVelocity);
    fall();
    drawBoulder();
    boulderRespawn();
  }
}
/*
* Platform Class
* By: Daniel Nava
* Last Updated: October 21 2016
*
* the Player can land and move on Platforms.
*/

class Platform {
  
////INSTANCE VARIABLES////

  PVector spawnLocation = new PVector ();
  
  float platformH = 10;
  float platformL = 0;
  boolean onFloor = false;
  
////END OF INSTANCE VARIABLES////

  Platform(float x, float y, float l) {
    spawnLocation.x =  x ;
    spawnLocation.y = y ;
    platformL = l;
  }

////DRAWS PLATFORM VISUALS////
  void drawPlatform () {
    //Dark brown color for platforms
    fill(#362818);
    rect(spawnLocation.x, spawnLocation.y, platformL, platformH);
    triangle(spawnLocation.x,spawnLocation.y + 10,spawnLocation.x , spawnLocation.y + 30,spawnLocation.x + 20,spawnLocation.y + 10);
    triangle(spawnLocation.x + 60,spawnLocation.y + 10,spawnLocation.x + 60 , spawnLocation.y + 30,spawnLocation.x + 40,spawnLocation.y + 10);
  }

////PLATFORM TO PLAYER COLLISION DETECTION
  void collisionCheck(PVector playerPos) {
    
    //Allows player to land on platforms 
    if (playerPos.y >= spawnLocation.y - 10  && playerPos.y <= (spawnLocation.y + platformH)
        && playerPos.x >= spawnLocation.x && playerPos.x <= (spawnLocation.x +  platformL)) {
    //notifies the Player class that the Player is on a platform
      this.onFloor = true;
   // Makes sure the player doesn't phase slightly through the platform
     playerPos.y = spawnLocation.y - 10;
    }
    else this.onFloor = false;
  }
}
/*
* Player Class
* By: Daniel Nava
* Last Updated: October 21 2016
*
* The Player can move left and right.
* The Player can also jump.
* If the Player falls of the map or hits a boulder their position is reset to the lowest platform.
* 
*/
class Player {

////INSTANCE VARIABLES////

  PVector playerPosition = new PVector ();
  PVector playerVelocity = new PVector ();

  float speed = 5;

////END OF INSTANCE VARIABLES////

  Player() {  
    playerPosition.x = 0;
    playerPosition.y = 500;
  }

////ALLOWS PLAYER TO JUMP ONLY ON A PLATFORM////
  void jump ( boolean touching) {
    if ( touching ) {  
      playerVelocity.y = 0;

      if (keyPressed == true && key == ' ') {      
        playerVelocity.y = -10;
      }
    } else {
      playerVelocity.y += gravity;
    }
  }

////RESETS PLAYER LOCATION IF GOES OFF SCREEN////
  void playerRespawnFall() {
    if (playerPosition.y >= 600) {
      playerPosition.x = 0;
      playerPosition.y = 500;
    }
  }

////UPDATES PLAYER LOCATION////
  void update () {
    playerVelocity.x = (right - left) * speed;
    playerPosition.add(playerVelocity);
  }

////RESTRICTS PLAYER FROM GOING OFF SCREEN FROM THE SIDES
  void boundaryCheck ()
  {
    //Right side check
    if (playerPosition.x >= width - 10) {
      playerPosition.x = width - 10;
      playerVelocity.x = 0;
    }
    //Left side check
    if (playerPosition.x <= 0) {
      playerPosition.x = 0;
      playerVelocity.x = 0;
    }
  }

////DRAWS PLAYER VISUALS////
  void drawP () {
    fill(0);
    noStroke();
    rect(playerPosition.x, playerPosition.y, 10, 10);
    fill(#29861B);
    beginShape();
    vertex(playerPosition.x-3, playerPosition.y);
    vertex(playerPosition.x, playerPosition.y - 3);
    vertex(playerPosition.x + 5, playerPosition.y - 6);
    vertex(playerPosition.x + 10, playerPosition.y - 3);
    vertex(playerPosition.x + 12, playerPosition.y);
    vertex(playerPosition.x-3, playerPosition.y);
    endShape();
  }

////GROUPS ALL FUNCTIONS INTO ONE////
  void playerFunctions() {
    update();
    boundaryCheck ();
    playerRespawnFall();
    drawP();
  }
}