Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Ballpit Man
//Emily Bendevis

//declare my array of balls in the ballpit
Balls[] ball = new Balls[100];
//declare my background objects
BG myLight;
BG myPlatform;
Stickman myStickman;
Stickman jump;

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

  myStickman = new Stickman();
  jump = new Stickman();

  //initializes my lights, platform, and balls
  myLight = new BG();
  myPlatform = new BG();
  for (int i = 0; i < ball.length; i++) {
    ball[i] = new Balls();
  }

  //tell player how to interact with program
  println("Click to make the man jump.");
}

void mousePressed() {
  myStickman.jump();
}

void draw() {
  //creates a dark grey background
  background(160); 


  myStickman.display();

  //displays my lights and platform
  myLight.light();
  myPlatform.platform();

  //displays my array of balls.
  for (int i = 0; i < ball.length; i++) {
    ball[i].move();
    ball[i].display();
  }
}
//class controlling the balls at the bottom of the screen
//used as reference: https://www.openprocessing.org/sketch/104885

class Balls {

  //variables for colours and sizes of balls
  color c;
  int diameter;

  //variables to change position of balls
  PVector position;
  PVector bounce;
  PVector gravity;

  //constructor
  Balls() {
    //placement of ball
    position = new PVector(random(5, 395), height - random(height/2));
    
    //controls speed of ball
    bounce = new PVector(0, -5);
    gravity = new PVector(0, 1);

    //calls colour of balls
    colour();
  }

  void colour() {
    //each ball to be a random colour
    c = color(random(255), random(255), random(255));
  }

  void display() {
    //size of ball
    diameter = 10;
    //makes the balls random colour
    fill(c);
    noStroke();
    //balls will start at random position at bottom of the screen
    ellipse(position.x, position.y, diameter, diameter);
  }

  void move() {
    //creates motion for the balls
    position.y = position.y + bounce.y;
    bounce.y = bounce.y + gravity.y;

    // Condition to make the ball stop
    if (bounce.y < 0.6 && position.y > height - 5) {
      bounce.y = 0;
      gravity.y = 0;
    } else if (position.y > height-5) {
      bounce.y = bounce.y * -0.65;
    }
  }
}
//creates class background

class BG {

  //variables needed to create a row of lights
  int lightWidth;
  int lightHeight;
  int spacing;
  int x;
  int y;

  //constructor
  BG() {
    //sets up positioning of lights
    lightWidth = 20;
    lightHeight = 60;
    spacing = 60;
    x = 0;
    y = 20;
  }


  void light() {
    //creates a row of yellow rectangle lights across the top of screen. NOT DISPLAYING
    x = 0;
    while (x <= 400) {
      stroke(255, 255, 0);
      fill (255, 255, 190);
      rect(x, y, lightWidth, lightHeight);
      x = x + spacing;
    }
  }


  void platform() {
    //creates a mini platform for stickman to stand on when program initialized
    stroke(0);
    strokeWeight(5);
    line(0, height/2, 40, height/2);
  }
}
//class to display and control stickman

class Stickman {

  //variables to display stickman
  PVector centre;
  PVector headCentre;
  int head;
  PVector shoulders;
  PVector rightHand;
  PVector leftHand;
  PVector rightFoot;
  PVector leftFoot;

  //variables to create motion
  PVector speed;
  PVector gravity;

  boolean isJumping;

  //constructor
  Stickman() {

    //used to create illusion of jump.
    speed = new PVector(2, -2);
    gravity = new PVector(0, .5);

    //diameter of stickman's head
    head = 5;

    //ensures all variables are relative to the centre of the stickman (his hips)
    centre = new PVector(15, 180);
    headCentre = new PVector(centre.x, centre.y - 20);
    shoulders = new PVector(centre.x, centre.y - 15);
    rightHand = new PVector(centre.x + 10, centre.y - 20);
    leftHand = new PVector(centre.x - 10, centre.y - 20);
    rightFoot = new PVector(centre.x + 10, centre.y + 20);
    leftFoot = new PVector(centre.x - 10, centre.y + 20);

    isJumping = false;
  }

  void display() {
    //draws out stickman
    strokeWeight(2);
    stroke(0);
    fill(0);
    //head
    ellipse(headCentre.x, headCentre.y, head, head);
    //torso
    line(headCentre.x, headCentre.y, centre.x, centre.y);
    //arms
    line(shoulders.x, shoulders.y, rightHand.x, rightHand.y);
    line(shoulders.x, shoulders.y, leftHand.x, leftHand.y);
    //legs
    line(centre.x, centre.y, rightFoot.x, rightFoot.y);
    line(centre.x, centre.y, leftFoot.x, leftFoot.y);

    if (isJumping == true) {
      move();
    }
  }

  //function to make the stickman jump when the mouse is pressed.
  //used this as reference https://www.openprocessing.org/sketch/92234
  void jump() {
    isJumping = true;
    println("This is gr8.");
  }

  void move() {

    centre.x += speed.x;
    centre.y += speed.y;
    speed.y += gravity.y;
    headCentre = new PVector(centre.x, centre.y - 20);
    shoulders = new PVector(centre.x, centre.y - 15);
    rightHand = new PVector(centre.x + 10, centre.y - 20);
    leftHand = new PVector(centre.x - 10, centre.y - 20);
    rightFoot = new PVector(centre.x + 10, centre.y + 20);
    leftFoot = new PVector(centre.x - 10, centre.y + 20);


    if (centre.y > 380) {
      speed.y = 0;
    }
    if (centre.x > 380 || centre.x < 0) {
      speed.x = speed.x * -1;
    }
  }

  void constrainToScreen() {
    if (centre.x >= 380) {
      centre.x = 380;
    }
    if (centre.x <= 20) {
      centre.x = 20;
    }
    if (centre.y >=380) {
      centre.y = 380;
    }
  }
}