Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Toy Screen Control
// 0 = Initial Screen
// 1 = Game Screen
// 2 = Game-over Screen

int screen = 0;
int score = 0;

//ShuttleCock Variables
int featherAmount = 0;
int featherEnd = 4;
int featherYSpacing = -10;
int toeSpace = 0;
float scX;
float scY;
float scSizeX = 20;
float scSizeY = 10;
float yOffset = 300;
int scColor = color(50, 50, 50);
float gravity = 0.5;
float scSpeedVerticle = 0;
float scSpeedHorizontal = 10;
float fallResistance = 0.08;
float friction = 0.1;

//Leg Variables
color footColor = color(225, 223, 196);
float footLimitY = -400;
float footWidth = 85;
float footHeight = 30;

//Interaction Variables
int legBounce = 10;


void setup() {
  size(500, 500);
  scX=width/2;
  scY= 80;
}


//Draw the toy screen relevant to the current play
void draw() {
  // Display the contents of the current screen
  if (screen == 0) {
    initScreen();
  } else if (screen == 1) {
    toyScreen();
  } else if (screen == 2) {
    loseScreen();
  }
}


/***** All Screens *****/
void initScreen() {
  background(255);
  textAlign(CENTER);
  fill(0);
  text("To Play ShuttleCock \n Click to Start", height/2, width/2);
}

void toyScreen() {
  background(255);
  //Leg Functions
  drawLeg();
  interactObjects();
  //Shuttle cock Functions
  screenFrame();
  drawShuttleCock();
  setSCGravity();
  setHorizontalSpeed();
  //Game world Functions
  floorContact();
  printScore();
}

void loseScreen() {
  background(255);
  fill(50);
  textAlign(CENTER);
  textSize(30);
  text("Game Over", height/2, width/2);
  textSize(15);
  text("Click to Try Again", height/2, width/2 + 20);
}

/***** Main Input from Player *****/
public void mousePressed() {
  //Starts game on mouse click
  if (screen==0) {
    toyInitiate();
  } 
  if (screen==2) {
    playAgain();
  }
}

//Screen Setters
void toyInitiate() {
  screen=1;
}

void loseToy() {
  screen=2;
}


void playAgain() {
  screen = 1;
  score = 0;
  scX=width/2;
  scY=height/3;
}



/***** Game Functions *****/

void floorContact() {
  if ( scY > 500) {
    loseToy();
  }
}

void score() {
  score++;
}

void printScore() {
  textAlign(CENTER);
  fill(0);
  textSize(50); 
  text(score, height/2, 50);
}



/***** Shuttle Cock Functions *****/

void drawShuttleCock() {
  fill(scColor);
  ellipse(scX, scY, scSizeX, scSizeY);

  //Drawing feathers of the shuttlecock
  for (int i = 0; i < 4; i = i + 1) {
    //Loop 3 times, each iteration increasing the Y value of each feather by -10
    //triangle(-5+scX, scY, scX, featherYSpacing+scY, 5+scX, scY);
    triangle(-5+scX, scY, scX, -10+scY, 5+scX, scY);
    triangle(-5+scX, -10+scY, scX, -20+scY, 5+scX, -10+scY);
    triangle(-5+scX, -20+scY, scX, -30+scY, 5+scX, -20+scY);
    //featherYSpacing -= 10;
  }
    //Left Feather
    triangle(-10+scX, scY, -13+scX, -10+scY, +scX, scY);
    triangle(-15+scX, -10+scY, -18+scX, -20+scY, -5+scX, -10+scY);
    triangle(-20+scX, -20+scY, -23+scX, -30+scY, -10+scX, -20+scY);
    //RightFeathers
    triangle(10+scX, scY, 13+scX, -10+scY, scX, scY);
    triangle(15+scX, -10+scY, 18+scX, -20+scY, 5+scX, -10+scY);
    triangle(20+scX, -20+scY, 23+scX, -30+scY, 10+scX, -20+scY);
}

void setSCGravity() {
  scSpeedVerticle += gravity;
  //Sets the Y of the ShuttleCock to be able to go upwards if it by the foot, not just by gravity
  scY += scSpeedVerticle;
  //Sets air resistance to the shuttle cock
  scSpeedVerticle -= (scSpeedVerticle * fallResistance);
}

void setHorizontalSpeed() {
  //Sets Horizontal speed to decrease the X of the shuttlecock to prevent the shuttlecock
  //from sitting still on the foot
  scX += scSpeedHorizontal;
  scSpeedHorizontal -= (scSpeedHorizontal * fallResistance);
}


//Keeps the ShuttleCock from leaving the screen
void screenFrame() {
  // Calls between making the ShuttleCock bounce between the top and side parameters
  // Alternates to the top
  if (scY-(scSizeY/2) < 0) {
    shuttleCockTopBounce(0);
  }

  if (scX-(scSizeX/2) < 0) {
    footBounceLeft(0);
  }
  if (scX+(scSizeX/2) > width) {
    footBounceRight(width);
  }
}

/*****Player Foot Code*****/
void drawLeg() {
  noStroke();
  fill(footColor);
  rectMode(CENTER);
  //Sets a Limit for the Y of the foot by calculating if the Y and offset is greater than the limit
  if (mouseY - yOffset >= footLimitY) {
    rect(mouseX, mouseY, footWidth, footHeight);
    ellipse(-50 + mouseX, -10 + mouseY, 20, 10);
    //Draws Toes
    //int i = 0;
    //while(i < 5){
    //toeSpace -= 10;
    //}
    {
      ellipse(-45 + mouseX, mouseY, 10, 5);
      ellipse(-45 + mouseX, 5+mouseY, 10, 5);
      ellipse(-45 + mouseX, 10 + mouseY, 10, 5);
      ellipse(-45+ mouseX, 15 + mouseY, 10, 5);
    }
  }
}

//Physics//

//Makes the Shuttle cock collide with the foot
void interactObjects() {
  //Hitbox extender for the foot depending on movement
  float allowance = mouseY - pmouseY;
  //Compares X coordinates of the foot to the Shuttle cock's to X coordiantes
  if ((scX+(scSizeX/2) > mouseX-(footWidth/2)) && (scX-(scSizeX/2) < mouseX+(footWidth/2))) {
    //Checking the distance (radius of the shuttle cock) to the foot to determine if they have collided
    if (dist(scX, scY, scX, mouseY)<=(scSizeX/2)+abs(allowance)) {
      shuttleCockFootBounce(mouseY);
      // Shutttle cock passes throught foot if distance between mouse positions is too high
      if (allowance<0) {
        scY+=allowance;
        //Increases the velocity of the Shuttle cock if moved up by the foot
        scSpeedVerticle+=allowance;
      }
    }
  }

  //Angle of the next launch determined by side area of the foot
  if ((scX+(scSizeX/2) > mouseX-(footWidth/2)) && (scX-(scSizeX/2) < mouseX+(footWidth/2))) {
    //Checks if the distance between the coordinates of object is 
    //less or equal to the size to check for collisons
    if (dist(scX, scY, scX, mouseY)<=(scSizeX/2)+(allowance)) {
      scSpeedHorizontal = (scX - mouseX)/3;
    }
  }
}

void shuttleCockFootBounce(float foot) {
  scY = foot-(scSizeY);
  scSpeedVerticle *= -0.5;
  //Bottom air resistance
  scSpeedVerticle -= (scSpeedVerticle * friction);
  score();
}

//Top Ceiling
void shuttleCockTopBounce(float surface) {
  scY = surface+(scSizeY/2);
  scSpeedVerticle *= -1;
  //Top air resistance
  scSpeedVerticle -= (scSpeedVerticle * friction);
}

//Causes the lose function to be called the Shuttle cock goes past the bottom of the screen
void shuttleCockBottom(float surface) {
  surface = 500;
  loseToy();
}

void footBounceLeft(float surface) {
  scX = surface+(scSizeX/2);
  scSpeedHorizontal *= -0.5;
  scSpeedHorizontal -= (scSpeedHorizontal * friction);
}

void footBounceRight(float surface) {
  scX = surface-(scSizeX/2);
  scSpeedHorizontal *= -0.5;
  scSpeedHorizontal -= (scSpeedHorizontal * friction);
}

//Really sorry for the late Submission
//Richard Kim