Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/* 
 Title: Cavern
 
 Description: you play as a demon who is trying to put out the flames in his lair.  
 
 Objective: To extingush the flames (find the orb). 
 
 Interactivity: 
 1. Move mouse horizontally to move character. (Please ensure that the mouse stays within the bounds of the canvas)
 2.  When any key is pressed, the player shoots a projectile.  
 
 
 Author: Sukhraj Johal 
 Project: Object Oriented Toy Assignment 
 Date: October 19th, 2016 
 
 */
//declaring global variables that affect multiple objects 
PVector plocation; 
PVector pvelocity; 


//declaring objects as global variables 
Player player; 
Pulse pulse; 

Orb orb;


FirePlatform firePlatform1;
FirePlatform firePlatform2;
FirePlatform firePlatform3; 
Platform platform;

Layers layer1;
Layers layer2;

Ceiling ceiling1;
Ceiling ceiling2;
Ceiling ceiling3; 
Ceiling ceiling4; 

Tunnel tunnel1;
Tunnel tunnel2; 

MovingBlock movingBall;  
MovingBlock2 movingBall2;

Cannon cannon1;
Cannon cannon2;

Door door1;
Door door2; 

Creature creature; 


//creates an array for the Ember 1 and 2 
Ember1[] ember1 = new Ember1[25];
Ember2[] ember2 = new Ember2[25];

//creates an array for the fire and fire 2
Fire[] fires = new Fire[12];
Fire2[] fires2 = new Fire2[12]; 



void setup () { 
  //creates a window that is 400 by 450 pixels 
  size(400, 450);  


  //initalizing character object 
  player = new Player(); 

  //initalizing the pulse object 
  pulse = new Pulse(); 

  //initalizing the creature object 
  creature = new Creature(); 


  //intializing the orb object 
  orb = new Orb(); 

  //initalizing the fire platform 
  firePlatform1 = new FirePlatform(200-mouseX, 190, 10);
  firePlatform2 = new FirePlatform(165-mouseX, 200, 10); 

  //initalizing the platform object 
  platform= new Platform();

  //initalizing the layers objects 
  layer1 = new Layers (color(68, 20, 6, 175), 400, 50, 800, 100); 
  layer2 = new Layers (color(68, 20, 6, 50), 400, 110, 800, 30);


  //initalizing the ceiling objects 
  ceiling1 = new Ceiling(50-mouseX, 15, 100, 30); 
  ceiling2 = new Ceiling(180-mouseX, 30, 160, 60);
  ceiling3 = new Ceiling(570-mouseX, 20, 260, 40); 
  ceiling4 = new Ceiling(750-mouseX, 40, 100, 80);

  //initalizing the tunnel objects 
  tunnel1 = new Tunnel(350-mouseX, 70, 200, 140);
  tunnel2 = new Tunnel(350-mouseX, 340, 200, 220);

  //initalizing the moving ball object
  movingBall = new MovingBlock(); 
  movingBall2 = new  MovingBlock2();

  //initalizing the cannon objects 
  cannon1 = new Cannon(480-mouseX, 50, 20, 40);
  cannon2 = new Cannon(570-mouseX, 50, 20, 40); 

  //initializing the wall objects 
  door1 = new Door(255-mouseX, 180, 10, 100); 
  door2 = new Door(445-mouseX, 180, 10, 100); 


  //initalizing each ember particle using a for loop 

  for (int i= 0; i< ember1.length; i++) { 
    ember1[i] = new Ember1(random(width*2), random(430, 450), -i/7, 4, 4);
  }

  //initalize each ember2 particle using a for loop 
  for (int i=0; i< ember2.length; i++) { 
    ember2[i] = new Ember2 (random(width*2), random(430, 450), -i/5, 2, 2);
  }


  //initalize each fire using a for loop. 
  for (int i= 0; i< fires.length; i++) { 
    fires[i] = new Fire(random(0, 800), 450, -i/2);
  }


  //initalize each fire2 using a for loop. 
  for (int i= 0; i< fires2.length; i++) { 
    fires2[i] = new Fire2(random(0, 800), 440, -i/2);
  }

  //initalizing the PVector that affects the pulse object 
  plocation = new PVector(mouseX, 210); 
  pvelocity = new PVector(10, 0);
} 

void draw() { 
  //creates background color 
  background(0); 

  //update 

  //the fire ember using a for loop. 
  for ( int i= 0; i< ember2.length; i++) { 
    ember1[i].move();
  }

  // the fire ember 2 using a for loop. 
  for ( int i=0; i<ember2.length; i++) { 
    ember2[i].move();
  }

  // update the fire 
  for (int i= 0; i< fires.length; i++) {
    fires[i].move();
  }

  //update fire2
  for (int i= 0; i< fires2.length; i++) {
    fires2[i].move();
  }

  movingBall.update(); 
  movingBall2.update();

  pulse.update(); 

  //display 

  firePlatform1.display();

  //display a weaker fire if pulse passes the firePlatform 1 and 2 location 
  if (plocation.x >= 200-mouseX) { 
    firePlatform1.collision();
  }
  firePlatform2.display(); 
  if (plocation.x >= 200-mouseX) { 
    firePlatform2.collision();
  }

  platform.display();

  cannon1.display();
  cannon2.display();

  layer1.display(); 
  layer2.display();

  ceiling1.display(); 
  ceiling2.display(); 
  ceiling3.display();
  ceiling4.display();

  pulse.display(); 

  creature.display(); 

  player.display();

  orb.display();

  door1.display(); 
  door2.display();

  tunnel1.display();
  tunnel2.display();

  movingBall.display();
  movingBall2.display();

  //if the pulse hits door 1 a collision happens and it flashes black. When the player moves passed the door it stays black. (indicating that the door is open)
  if (plocation.x >= 255-mouseX) { 
    door1.collision();
  }
  //if the pulse hits door 2 a collision happens and it flashes black. When the player moves passed door2 it stays black. (indicating that the door is open) 
  if (plocation.x >= 445-mouseX) { 
    door2.collision();
  }

  //draw the fire ember using a for loop. 
  for ( int i= 0; i< ember2.length; i++) { 
    ember1[i].display();
  }

  // draw the fire ember 2 using a for loop. 
  for ( int i=0; i<ember2.length; i++) { 
    ember2[i].display();
  }

  // draw the fire2 using a for loop. 
  for (int i= 0; i< fires2.length; i++) {
    fires2[i].display();
  }

  // draw the fire using a for loop. 
  for (int i= 0; i< fires.length; i++) {
    fires[i].display();
  }
  //if player moves passed the orb the fire is extinguished and the text box reads " The flames are extinguished". 
  if (plocation.x >= 650-mouseX) { 
    orb.collision();
    println(" The flames are extinguished");
  }

  //if player moves past the creature the creature speaks 
  if (mouseX >= 360-mouseX) { 
    creature.speak();
  }
}
class Cannon {
  //declare the variables that affect the cannons. 
  float xpos;
  float ypos;
  float w;
  float h;

  Cannon(float tempXPos, float tempYPos, float tempW, float tempH) { 
    //constructors parameters are set by the x and y position along with w and h.  
    xpos = tempXPos; 
    ypos = tempYPos; 
    w = tempW; 
    h = tempH;
  }

  void display() { 
    //creates the cannons 
    fill(95, 47, 12);
    rectMode(CENTER); 
    rect(xpos-mouseX, ypos, w, h);
  }
} 
class Ceiling { 

  //declaring variables that affect the ceiling  
  float xpos; 
  float ypos;
  float w;
  float h;

  Ceiling(float tempXPos, float tempYPos, float tempW, float tempH) { 
    //constructor is defined by the paramaters of x,y,w,h  
    xpos= tempXPos; 
    ypos= tempYPos; 
    w= tempW;
    h= tempH;
  } 

  void display() {
    //creates the ceiling 
    fill(62, 20, 4); 
    rectMode(CENTER); 
    rect(xpos-mouseX, ypos, w, h);
  }
}
class Creature { 
  //declare variables that affect the Creature 
  float xpos; 

  Creature() { 
    //constructor
    xpos = 360-mouseX;
  }

  void display() { 
    //creates the creature 
    fill(245, 184, 235);
    ellipse(xpos-mouseX, 190, 20, 20); 
    fill(255); 
    ellipse(xpos-mouseX, 190, 5, 5); 
    fill(34, 41, 106); 
    rect(xpos-mouseX, 220, 20, 40, 200);
  }
  void speak() { 
    println (" Hey, big brother! Someone turned on the fire pipes. Quick, grab the orb!! ");
  }
} 
class Door {
  //declare variables that affect the door object. 
  float xpos; 
  float ypos; 
  float w; 
  float h;

  Door(float tempXPos, float tempYPos, float tempW, float tempH) { 
    //constructor affected by the parameters xpos,ypos, w, h
    xpos = tempXPos; 
    ypos = tempYPos; 
    w = tempW; 
    h = tempH;
  } 

  void display() { 
    //creates the door 
    fill(210, 184, 181); 
    rectMode(CENTER);
    rect(xpos-mouseX, ypos, w, h);
  } 
  void collision() {
    //changes color of the door 
    fill(0); 
    rect(xpos-mouseX, ypos, w, h);
  }
}
class Ember1 { 
  //declare variables that affect ember 1 
  float xpos; 
  float ypos;
  float yspeed; 
  float w; 
  float h;

  Ember1(float tempXPos, float tempYPos, float tempYSpeed, float tempW, float tempH) {
    // constructor controlled by the parameters of x,y,yspeed,w,h. 
    xpos = tempXPos; 
    ypos = tempYPos; 
    yspeed= tempYSpeed; 
    w= tempW;
    h=tempH;
  }

  void display() { 
    //creates the ember 
    fill(247, 51, 40);
    rectMode(CENTER); 

    rect(xpos-mouseX, ypos, 4, 4);
  }

  void move() { 
    //moving ember1
    ypos = ypos + yspeed/4; 

    if (ypos > height) { 
      ypos= height;
    }

    //if the ypos of the ember is less than 150 it dissapears and it resets the embers at the bottom of the screen. 
    if (ypos < 150) { 
      ypos = height + 150; 
      xpos= random(width*2);
    }
  }
} 
class Ember2 { 
  // declare variables that affect ember 2. 
  float xpos; 
  float ypos;
  float yspeed; 
  float w; 
  float h;

  Ember2(float tempXPos, float tempYPos, float tempYSpeed, float tempW, float tempH) {
    // constructor controlled by the parameters of x,y,yspeed,w,h. 
    xpos = tempXPos; 
    ypos = tempYPos; 
    yspeed= tempYSpeed; 
    w= tempW;
    h=tempH;
  }

  void display() { 
    //creates ember 2 
    fill(250, 245, 88); 
    rectMode(CENTER); 

    rect(xpos-mouseX, ypos, 2, 2);
  }

  void move() { 
    //moving the ember2 particle 
    ypos = ypos + yspeed/3; 

    if (ypos > height) { 
      ypos= height;
    }

    //if the ypos of the ember is less than 100 it dissapears and it resets the embers at the bottom of the screen. 
    if (ypos < 100) { 
      ypos = height + 100; 
      xpos= random(width*2);
    }
  }
} 
class Fire { 
  //declare variables that affect ember 1 
  float xpos; 
  float ypos;
  float yspeed; 
  float w; 
  float h;


  Fire(float tempXPos, float tempYPos, float tempYSpeed) {
    // constructor controlled by the parameters of x,y,yspeed,w,h. 
    xpos = tempXPos; 
    ypos = tempYPos; 
    yspeed= tempYSpeed;
  }

  void display() { 
    //creates the fire
    fill(random(200, 255), 50, 20);
    rectMode(CENTER); 
    triangle(40+xpos-mouseX, ypos+ random(60, 70), -40+xpos-mouseX, ypos+ random(60, 70), 10+ xpos-mouseX, ypos-random(20, 30));
  }


  void move() { 
    //moves the fire 
    ypos = ypos + yspeed/4; 

    if (ypos > height) { 
      ypos= height;
    }

    //if the ypos of the fire is less than 420 it dissapears and it resets the embers at the bottom of the screen. 
    if (ypos < 420) { 
      ypos = height + 420; 
      xpos= random(width*2);
    }
  }
} 
class Fire2 { 
  //declare variables that affect ember 1 
  float xpos; 
  float ypos;
  float yspeed; 
  float w; 
  float h;

  Fire2(float tempXPos, float tempYPos, float tempYSpeed) {
    // constructor controlled by the parameters of x,y,yspeed,w,h. 
    xpos = tempXPos; 
    ypos = tempYPos; 
    yspeed= tempYSpeed;
  }

  void display() { 
    //creates the fire
    fill(200, random(100, 170), 20);
    rectMode(CENTER); 
    triangle(40+xpos-mouseX, ypos+ random(60, 70), -40+xpos-mouseX, ypos+ random(60, 70), 10+ xpos-mouseX, ypos-random(20, 30));
  }


  void move() { 
    //moving the fire 
    ypos = ypos + yspeed/4; 

    if (ypos > height) { 
      ypos= height;
    }

    //if the ypos of the fire2 is less than 390 it dissapears and it resets the embers at the bottom of the screen. 
    if (ypos < 390) { 
      ypos = height + 390; 
      xpos= random(width*2);
    }
  }
} 
class FirePlatform { 
  float xpos;
  float ypos;
  float yspeed; 
  float w; 
  float h;


  FirePlatform(float tempXPos, float tempYPos, float tempYSpeed) {
    // constructor controlled by the parameters of x,y,yspeed,w,h. 
    xpos = tempXPos; 
    ypos = tempYPos; 
    yspeed= tempYSpeed;
  }

  void display() { 
    //creates the fire
    fill(random(200, 255), 50, 20);
    rectMode(CENTER); 
    triangle(20+xpos-mouseX, ypos+ random(40, 50), -20+xpos-mouseX, ypos+ random(40, 50), 10+ xpos-mouseX, ypos-random(20, 30));
  }
  void collision() {
    //changes the color of the fireplatform when there is a collision
    fill(200, random(100, 170), 20);
    triangle(random(20,30)+xpos-mouseX, ypos+ random(40, 50), random(-20,-30)+xpos-mouseX, ypos+ random(40, 50), 10+ xpos-mouseX, ypos-random(20, 30));
  }

  void move() { 
    //moving the fire platform 
    ypos = ypos + yspeed/4; 

    if (ypos > height) { 
      ypos= height;
    }

    //if the ypos of the ember is less than 150 it dissapears and it resets the embers at the bottom of the screen. 
    if (ypos < 420) { 
      ypos = height + 420; 
      xpos= random(width*2);
    }
  }
} 
class Layers { 
  //declare variables that affect the layers 
  float xpos; 
  float ypos;
  float w;
  float h;
  color c;


  Layers(color tempC, float tempXPos, float tempYPos, float tempW, float tempH) {
    //constructor with  the parameters of colour, x and y position. 
    c = tempC; 
    xpos = tempXPos; 
    ypos = tempYPos; 
    w = tempW; 
    h = tempH;
  }

  void display() { 
    //creates the layers on the top of the ceiling 
    fill(c); 
    rectMode(CENTER); 
    rect(xpos, ypos, w, h);
  }
}
class MovingBlock { 
  //declare variables that affect MovingBall
  PVector location; 
  PVector velocity; 
  boolean ballMoves = false; 



  MovingBlock() { 
    //constructor affected by location and velocity vectors 
    location= new PVector(480, 60); 
    velocity= new PVector(0, 7);
  }

  void display() { 
    //creates MovingBlock
    fill(random(200, 255), 50, 20);
    rectMode(CENTER); 
    rect(location.x, location.y, 20, 20);
  } 


  void update() { 
    //adding the two PVectors in order to move the MovingBlock
    location.add(velocity);

    //if the mouse is greater than half the width the ball from the cannon starts firing.
    if (mouseX > width/3) { 
      ballMoves = true;
    }
    if (ballMoves) { 
      fill(random(200, 255), 50, 20);
      rect(480-mouseX, location.y, 20, 20);
    }
    if (location.y > 460) { 
      location.y= height - 460;
    }
  }
}
class MovingBlock2 { 
  //declare variables that affect MovingBall
  PVector location; 
  PVector velocity; 
  boolean ballMoves = false; 

  MovingBlock2() { 
    //constructor affected by location and velocity vectors 
    location= new PVector(570, 60); 
    velocity= new PVector(0, 7);
  }

  void display() { 
    //creates MovingBlock2
    fill(random(200, 255), 50, 20);
    rectMode(CENTER); 
    rect(location.x, location.y, 20, 20);
  } 


  void update() { 
    location.add(velocity);  

    //if the mouse is greater than half the width the ball from the cannon starts firing.

    if (mouseX > width/3) { 
      ballMoves = true;
    } 
    if (ballMoves) { 
      fill(random(200, 255), 50, 20);

      rect(570-mouseX, location.y, 20, 20);
    } 
    if (location.y > 460) { 
      location.y= height - 460;
    }
  }
}
class Orb { 
  // declare variables that affect the orb.
  float xpos;
  float ypos; 

  Orb() {
    //constructor includes xpos and ypos 
    xpos = 650 - mouseX;
    ypos = 220;
  }

  void display() { 
    //creates the orb 
    fill(184, 245, 232);
    ellipse(xpos-mouseX, ypos, 20, 20);
  } 
  void collision() { 
    background(0);
  }
} 
class Platform {
  //declare variables that affect the platform 
  color p; 

  Platform() { 
    //constructor affected by the color 
    p = color(95, 47, 12);
  }

  void display() {
    //creates the platform
    fill(p); 
    rect(400, 240, 800, 20);
  }
} 
class Player { 

  //variables that affect the character
  color a; 
  color b;
  color c;

  Player() {
    //constructor affected by the three colours color a, b, and c 
    a= color(5, 48, 16);
    b= color(208, 233, 37);
    c = color(255);
  }

  void display() {
    //creates the player 

    //creates no stroke on primitive shapes 
    noStroke();

    //creates characters body 
    fill(b);
    triangle(mouseX, 185, mouseX-25, 230, mouseX+25, 230);

    //creates the characters horns 
    fill(b);
    rectMode(CENTER); 
    rect(mouseX+18, 182, 10, 5);
    rect(mouseX-18, 182, 10, 5);

    triangle(mouseX-20, 160, mouseX-18, 180, mouseX-23, 180);
    triangle(mouseX+20, 160, mouseX+18, 180, mouseX+23, 180);


    //creates character head
    fill(a); 
    ellipseMode(CENTER);
    ellipse(mouseX, 185, 30, 30);


    //creates the characters eyes 
    fill(c);
    ellipse(mouseX-5, 180, 5, 5);
    ellipse(mouseX+5, 180, 5, 5);
  }
}
class Pulse { 
  //declare variables that affect the Pulse  
  Pulse() { 
    //constructor affected by two vectors plocation and pvelocity 
    plocation = new PVector(mouseX, 210); 
    pvelocity = new PVector(10, 0);
  }

  void display() { 
    //creates the pulse 
    fill(184, 245, 232); 
    rectMode(CENTER); 
    rect(plocation.x, plocation.y, 20, 10);
  } 

  void update() { 
    //if the key is pressed then the pulse will be shot. Once the key is released, the pulse will reset to the mouseX position of the character. 
    if (keyPressed) { 
      plocation.add(pvelocity);
      plocation.x ++;
    } else { 
      reset();
    }
    if (plocation.x > 400) { 
      plocation.x = mouseX;
    }
  }
  void reset() { 
    plocation.x = mouseX;
  }
} 
class Tunnel { 
  //declare variables that affect the tunnel object 
  float xpos; 
  float ypos; 
  float w;
  float h; 

  Tunnel(float tempXPos, float tempYPos, float tempW, float tempH) { 
    //constructor is defined by the parameters x and y position. 
    xpos = tempXPos; 
    ypos = tempYPos; 
    w= tempW;
    h= tempH;
  }

  void display() { 
    //creates the tunnel 
    fill(62, 20, 4);
    rectMode(CENTER); 
    rect(xpos- mouseX, ypos, w, h);
  }
}