Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//////////////////
////JUMPY BALL by Ata Dogan
//A to go LEFT 
//B to go RIGHT
//Spacebar to JUMP
//Object Oriented Toy
//////////////////

//Name Background, Player and BArrier classes


Background b;
Player p;
Barrier one;
Barrier two;
Barrier three;

boolean left = false;
boolean right = false;
boolean jump = false;
boolean jumped = false;

// randomize location of barriers
float onerandomY;
float onerandomX;
float tworandomY;
float tworandomX;
float threerandomY;
float threerandomX;

//determines death
int state;
//determines invert
int invertstate;
//counts score
int counterscore;

void setup() {

  size(600, 600);
  rectMode(CORNERS);
  ellipseMode(CENTER);
  frameRate(60);
  colorMode(HSB, 255, 100, 100);

  // Initialize Classes

  //Background
  b = new Background();

  //Player Location and Size
  p = new Player(60, 450, 25, 0, 0);

  // Barrier
  one = new Barrier();
  two = new Barrier();
  three = new Barrier();

  // Random Height and Location of first barrier
  onerandomX = random (0, 200);
  onerandomY = random(0, 150);

  one.abovebottomPos.y += onerandomY ;
  one.belowtopPos.y += onerandomY;

  one.abovetopPos.x += onerandomX;
  one.abovebottomPos.x += onerandomX;
  one.belowtopPos.x += onerandomX;
  one.belowbottomPos.x += onerandomX;

  // Random Height and Location of second barrier
  tworandomX = random (400, 600);
  tworandomY = random(0, 150);

  two.abovebottomPos.y += tworandomY ;
  two.belowtopPos.y += tworandomY;

  two.abovetopPos.x += tworandomX;
  two.abovebottomPos.x += tworandomX;
  two.belowtopPos.x += tworandomX;
  two.belowbottomPos.x += tworandomX;

  // Random Height and Location of third barrier
  threerandomX = random (800, 1000);
  threerandomY = random(0, 150);

  three.abovebottomPos.y += threerandomY ;
  three.belowtopPos.y += threerandomY;

  three.abovetopPos.x += threerandomX;
  three.abovebottomPos.x += threerandomX;
  three.belowtopPos.x += threerandomX;
  three.belowbottomPos.x += threerandomX;

  // makes it so that when the ball is not touching the ground it draw everything
  if (p.ypos <= height - p.diam/2) {
    state=1;
  }
}

void draw() {    

  if (state== 0) {
    fill (255);
    rect(0, 0, 600, 600);
  } else if (state==1) {

    if (one.abovebottomPos.x <= 0) {
      onerandomX += 800;
    }
    b.display();
    p.display();
    one.displayStart();
    one.display();
    two.display();
    three.display();
    p.controls();
    p.constrainplayer();
    p.collision();
    invertdisplay();
  }
  // makes it so that when the ball hits the ground it ends the game 
  if (p.ypos >= height - p.diam/2) {
    state=0;
  }
}



// Invert after 15 seconds
void invertdisplay() {

  counterscore += 1;
  //print(counterscore);

  if (counterscore >= 1800) {
    invertstate = 1;
  }  

  if (invertstate == 1) {
    filter(INVERT);
  }
}


// Controls
void keyPressed() {

  if (keyCode == LEFT || key=='a' || key=='A') {
    left = true;
  }
  if (keyCode == RIGHT || key=='d' || key=='D') {
    right = true;
  }
  //allows player to jump
  if (key==' ' && jumped == false) {
    p.speed = p.speed - 6;
    jumped = true;
    p.speed = constrain(p.speed, -6, 6);
  } else if (key==' ' && p.xpos >= width - p.diam/2 && jumped == true) {
    p.speed = p.speed - 5;
    p.movs = p.movs - 5;
    p.speed = constrain(p.speed, -6, 6);
  }
}

void keyReleased() {

  if (keyCode == LEFT || key=='a' || key=='A') {
    left = false;
  }
  if (keyCode == RIGHT || key=='d' || key=='D') {
    right = false;
  }
}
class Background {
  int invertstate;
  int counter = 239;
  int colorHue = 40;

  void display() {

    
    noStroke();
    counter += 1;
    int y =600;////////background rectangular's y position///////
    float S=55;
    Float B=82.0;
    while (B<97) {   
      B+=0.1;
      S-=0.4;
      fill(colorHue, S, B);
      rect(0, y, width, 0);  //draws rectangles from bottom to top//
      y = y -5;
    }

    // Changes hue every 4 seconds (240 frames)
    if (counter > 239) {

      colorHue += 20;
      counter = 0;
    }
    if (colorHue > 230) {
      colorHue = 0;
    }
  }
}
class Barrier {

  PVector starttopPos = new PVector(0, 400);
  PVector startbottomPos = new PVector(160, 600);

  PVector abovetopPos = new PVector(240, 0);
  PVector abovebottomPos = new PVector(440, 330);

  PVector belowtopPos = new PVector(240, 430);
  PVector belowbottomPos = new PVector(440, 600);

  PVector glasstopPos = abovebottomPos;
  PVector glassbottomPos = belowtopPos;


  void displayStart() {

    rectMode (CORNERS);
    // Starting Barrier
    fill(255);
    rect(starttopPos.x, starttopPos.y, startbottomPos.x, startbottomPos.y);
  }


  void display() {

    colorMode(RGB);
    rectMode (CORNERS);

    // Top of Barrier One
    fill(255);
    rect(abovetopPos.x, abovetopPos.y, abovebottomPos.x, abovebottomPos.y);

    // Bottom of Barrier
    rect(belowtopPos.x, belowtopPos.y, belowbottomPos.x, belowbottomPos.y);

    // Glass (see through)
    fill(255, 255, 255, 60);
    rect(glasstopPos.x, glasstopPos.y, glassbottomPos.x, glassbottomPos.y);
    colorMode(HSB);
  }
}
class Player {

  float xpos;
  float ypos;
  float diam;
  float speed;
  float movs;
  float barrierMovement = -300;
  float[] trailx = new float [3];

  Player(float tempxpos, float tempypos, float tempdiam, float tempspeed, float tempmovs) {
    xpos = tempxpos;
    ypos = tempypos;
    diam = tempdiam;
    speed = tempspeed;
    movs = tempmovs;
  }

  // Character Display
  void display() {

    colorMode(RGB);
    noStroke();
    ellipseMode(CENTER);

    // Draw Player Trail
    for (int i = 0; i<=2; i++) {
      trailx[i] = xpos;
      fill(255, 255, 255, 60);
      ellipse(trailx[i] - movs *3 * (1+i), ypos, 25, 25);
    }
    // Draw Character
    fill(255);
    ellipse(xpos, ypos, diam, diam);
  }

  void controls() {
    // x position is affected by movspeed
    xpos = xpos + movs;

    ypos = ypos + speed;
    speed = speed + 0.2;






    // Moves barriers to make it seem as if progressing through level    
    //First Barrier
    one.starttopPos.x = one.starttopPos.x - movs*2;
    one.startbottomPos.x = one.startbottomPos.x - movs*2;

    one.abovetopPos.x = one.abovetopPos.x - movs*2;
    one.abovebottomPos.x = one.abovebottomPos.x - movs*2;

    one.belowtopPos.x = one.belowtopPos.x - movs*2;
    one.belowbottomPos.x = one.belowbottomPos.x - movs*2;

    //2nd Barrier
    two.starttopPos.x = two.starttopPos.x - movs*2;
    two.startbottomPos.x = two.startbottomPos.x - movs*2;

    two.abovetopPos.x = two.abovetopPos.x - movs*2;
    two.abovebottomPos.x = two.abovebottomPos.x - movs*2;

    two.belowtopPos.x = two.belowtopPos.x - movs*2;
    two.belowbottomPos.x = two.belowbottomPos.x - movs*2;

    //3rd Barrier
    three.starttopPos.x = three.starttopPos.x - movs*2;
    three.startbottomPos.x = three.startbottomPos.x - movs*2;

    three.abovetopPos.x = three.abovetopPos.x - movs*2;
    three.abovebottomPos.x = three.abovebottomPos.x - movs*2;

    three.belowtopPos.x = three.belowtopPos.x - movs*2;
    three.belowbottomPos.x = three.belowbottomPos.x - movs*2;

    // Brings back barriers
    if (xpos >450) {

      xpos = xpos + barrierMovement;

      //First Barrier
      one.starttopPos.x = one.starttopPos.x + barrierMovement;
      one.startbottomPos.x = one.startbottomPos.x + barrierMovement;

      one.abovetopPos.x = one.abovetopPos.x + barrierMovement;
      one.abovebottomPos.x = one.abovebottomPos.x + barrierMovement;

      one.belowtopPos.x = one.belowtopPos.x + barrierMovement;
      one.belowbottomPos.x = one.belowbottomPos.x + barrierMovement;

      //2nd Barrier
      two.starttopPos.x = two.starttopPos.x + barrierMovement;
      two.startbottomPos.x = two.startbottomPos.x + barrierMovement;

      two.abovetopPos.x = two.abovetopPos.x + barrierMovement;
      two.abovebottomPos.x = two.abovebottomPos.x + barrierMovement;

      two.belowtopPos.x = two.belowtopPos.x + barrierMovement;
      two.belowbottomPos.x = two.belowbottomPos.x + barrierMovement;

      //3rd Barrier
      three.starttopPos.x = three.starttopPos.x + barrierMovement;
      three.startbottomPos.x = three.startbottomPos.x + barrierMovement;

      three.abovetopPos.x = three.abovetopPos.x + barrierMovement;
      three.abovebottomPos.x = three.abovebottomPos.x + barrierMovement;

      three.belowtopPos.x = three.belowtopPos.x + barrierMovement;
      three.belowbottomPos.x = three.belowbottomPos.x + barrierMovement;
    }



    //Left Right
    if (left == true) {

      movs = movs - 0.1;
      movs = constrain(movs, -4, 4);
    } else if (right == true) {
      movs = movs + 0.1;
      movs = constrain(movs, -4, 4);
    }

    // when neither left or right key is pressed decrease the speed of the character 
    if (right == false && left == false) {
      movs = movs * 0.95;
    }
  }


  void constrainplayer() {

    //circle does not go through floor
    if (ypos >= height - diam/2) {
      ypos = height - diam/2;
      jumped = false;
      speed = speed * -0.5;
    }
    //circle does not go through roof
    if (ypos <= 0 + diam/2) {
      ypos = 0 + diam/2;
      speed = speed - 0.001;
      speed = speed * -0.1;
    }
    // cicrcle does not go through sides
    if (xpos <= 0 + diam/2) {
      xpos = diam/2;
      movs = 0;
    } else if (xpos >= width - diam/2) {
      xpos = width - diam/2;
      movs = 0;
    }
    //Moves ball away from corners so it doesn't get stuck
    if (xpos <= 0 + diam/2) {
      movs = movs + 0.1;
    } else if (xpos >= width - diam/2) {
      movs = movs - 0.1;
    }
  }

  //Collisions
  void collision() {

    ////COLLISION WITH STARTING BARRIER
    //Top (GROUND)
    if (ypos >= one.starttopPos.y - diam/2 && xpos <= one.startbottomPos.x  && xpos >= one.starttopPos.x ) {
      ypos = one.starttopPos.y - diam/2;
      speed = speed * -0.4;
      jumped = false;
    }       

    // Left Wall
    if (xpos >= one.starttopPos.x - diam/2 && ypos >=  one.starttopPos.y && xpos <= one.startbottomPos.x) {
      xpos = one.starttopPos.x - diam/2;
      movs = movs *- 0.3;
    }

    // Right Wall
    if (xpos <= one.startbottomPos.x + diam/2 && ypos >=  one.starttopPos.y + diam/2 && xpos >= one.startbottomPos.x) {
      xpos = one.startbottomPos.x + diam/2;
      movs = movs + 0.3;
    }


    ////COLLISION WITH BARRIER ONE////

    //TOP OF BARRIER
    //Bottom (ROOF)
    if (ypos <= one.abovebottomPos.y + diam/2 && xpos >= one.abovetopPos.x && xpos <= one.abovebottomPos.x) {
      ypos = one.abovebottomPos.y + diam/2;
      speed = speed - 0.001;
      speed = speed * -0.4;
    }
    // Left Wall
    if (xpos >= one.abovetopPos.x - diam/2 && ypos <=  one.abovebottomPos.y && xpos <=  one.abovetopPos.x) {
      xpos = one.abovetopPos.x - diam/2 ;
      movs = movs *- 0.3;
    }

    // Right Wall
    if (xpos <= one.abovebottomPos.x + diam/2 && ypos >=  one.abovetopPos.y && xpos >=  one.abovetopPos.x && ypos <=  one.abovebottomPos.y) {
      xpos = one.abovebottomPos.x + diam/2;
      movs = movs * 0.3;
    }



    //BOTTOM OF BARRIER
    //Top (GROUND)
    if (ypos >= one.belowtopPos.y - diam/2 && xpos <= one.belowbottomPos.x && xpos >= one.belowtopPos.x) {
      ypos = one.belowtopPos.y - diam/2;
      speed = speed * -0.4;
      jumped = false;
    }       

    // Left Wall
    if (xpos >= one.belowtopPos.x - diam/2 && ypos >=  one.belowtopPos.y && xpos <=  one.belowtopPos.x) {
      xpos = one.belowtopPos.x - diam/2;
      movs = movs *- 0.3;
    }

    // Right Wall
    if (xpos <= one.belowbottomPos.x + diam/2 && ypos >=  one.belowtopPos.y && xpos >=  one.belowtopPos.x) {
      xpos = one.belowbottomPos.x + diam/2;
      movs = movs * 0.3;
    }

    ////COLLISION WITH BARRIER TWO////

    //TOP OF BARRIER
    //Bottom (ROOF)
    if (ypos <= two.abovebottomPos.y + diam/2 && xpos >= two.abovetopPos.x && xpos <= two.abovebottomPos.x) {
      ypos = two.abovebottomPos.y + diam/2;
      speed = speed - 0.001;
      speed = speed * -0.1;
    }
    // Left Wall
    if (xpos >= two.abovetopPos.x - diam/2 && ypos <=  two.abovebottomPos.y && xpos <=  two.abovetopPos.x) {
      xpos = two.abovetopPos.x - diam/2 ;
      movs = movs *- 0.3;
    }

    // Right Wall
    if (xpos <= two.abovebottomPos.x + diam/2 && ypos >=  two.abovetopPos.y && xpos >=  two.abovetopPos.x && ypos <=  two.abovebottomPos.y) {
      xpos = two.abovebottomPos.x + diam/2;
      movs = movs * 0.3;
    }



    //BOTTOM OF BARRIER
    //Top (GROUND)
    if (ypos >= two.belowtopPos.y - diam/2 && xpos <= two.belowbottomPos.x && xpos >= two.belowtopPos.x) {
      ypos = two.belowtopPos.y - diam/2;
      speed = speed * -0.4;
      jumped = false;
    }       

    // Left Wall
    if (xpos >= two.belowtopPos.x - diam/2 && ypos >=  two.belowtopPos.y && xpos <=  two.belowtopPos.x) {
      xpos = two.belowtopPos.x - diam/2;
      movs = movs *- 0.3;
    }

    // Right Wall
    if (xpos <= two.belowbottomPos.x + diam/2 && ypos >=  two.belowtopPos.y && xpos >=  two.belowtopPos.x) {
      xpos = two.belowbottomPos.x + diam/2;
      movs = movs * 0.3;
    }

    ////COLLISION WITH BARRIER THREE////

    //TOP OF BARRIER
    //Bottom (ROOF)
    if (ypos <= three.abovebottomPos.y + diam/2 && xpos >= three.abovetopPos.x && xpos <= three.abovebottomPos.x) {
      ypos = three.abovebottomPos.y + diam/2;
      speed = speed - 0.001;
      speed = speed * -0.1;
    }
    // Left Wall
    if (xpos >= three.abovetopPos.x - diam/2 && ypos <=  three.abovebottomPos.y && xpos <=  three.abovetopPos.x) {
      xpos = three.abovetopPos.x - diam/2 ;
      movs = movs *- 0.3;
    }

    // Right Wall
    if (xpos <= three.abovebottomPos.x + diam/2 && ypos >=  three.abovetopPos.y && xpos >=  three.abovetopPos.x && ypos <=  three.abovebottomPos.y) {
      xpos = three.abovebottomPos.x + diam/2;
      movs = movs * 0.3;
    }



    //BOTTOM OF BARRIER
    //Top (GROUND)
    if (ypos >= three.belowtopPos.y - diam/2 && xpos <= three.belowbottomPos.x && xpos >= three.belowtopPos.x) {
      ypos = three.belowtopPos.y - diam/2;
      speed = speed * -0.4;
      jumped = false;
    }       

    // Left Wall
    if (xpos >= three.belowtopPos.x - diam/2 && ypos >=  three.belowtopPos.y && xpos <=  three.belowtopPos.x) {
      xpos = three.belowtopPos.x - diam/2;
      movs = movs *- 0.3;
    }

    // Right Wall
    if (xpos <= three.belowbottomPos.x + diam/2 && ypos >=  three.belowtopPos.y && xpos >=  three.belowtopPos.x) {
      xpos = three.belowbottomPos.x + diam/2;
      movs = movs * 0.3;
    }
  }
}