Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/* Spitball Frenzy
 By: Porter Crease
 
 Become the bully and launch spitballs, or become the nerd and deflect it with your textbooks!
 Play against friends.
 WS keys to move the textbook up and down.
 Arrow keys to move the Bully up and down.
 Spacebar to shoot!
 
 */

//score
int score=0;
int scoreLine=90;

//positioning of objects
int strawX=240;
int strawY=170;
int bookX=125;
int bookY=110;
int headX=50;
int headY=100;

int geekBodyX=headX-30;
int geekBodyY=headY+70;


//size of objects
int bookHeight=70;
int headSize=60;
int geekBodyWidth=40;


//movement of objects
float geekMovement=1.7;
float bullyMovement=2;

int ballSpeed=0;
int ballSpeed2=0;
int ballSpeed3=0;
int ballSpeed4=0;


//spitball variables
int spitballX=245;
int spitballY=175;

int spitballX2=254;
int spitballY2=175;

int spitballX3=264;
int spitballY3=175;

int spitballX4=274;
int spitballY4=175;

int ballSize=9;

//keycommands
boolean shootBall=false;
boolean shootBall2=false;
boolean shootBall3=false;
boolean shootBall4=false;

boolean bullyUp=false;
boolean bullyDown=false;

boolean bookUp=false;
boolean bookDown=false;


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

  println("Become the bully and launch spitballs, or become the nerd and deflect it with your textbooks!");
  println("Play against friends. WS keys to move the textbook up and down.");
  println("Arrow keys to move the Bully up and down.");
  println("Spacebar to shoot!");
}

void draw() {
  background (250,225,200); 

  //set a speed to the spitballs
  spitballX2-=ballSpeed2;
  spitballX-=ballSpeed;
  spitballX3-=ballSpeed3;
  spitballX4-=ballSpeed4;


  keepBallInStraw();
  limitBullyMovement();
  limitGeekMovement();
  spitballMove();
  bullyMove();
  bookMove();

  //if the spitball hasn't been shot yet, keep the ball in the straw as it moves.
  //if the spitball has been shot, check it's collisions
  scoring();
  if (shootBall==false) {
    keepBallInStraw();
  }

  if (shootBall) {
    checkSpitballCollision();
  }

  if (shootBall2) {
    checkSpitball2Collision();
  }

  if (shootBall3) {
    checkSpitball3Collision();
  }
  if (shootBall4) {
    checkSpitball4Collision();
  }


  //DRAW
  chalkboard();
  drawScore();

  //draw geek with book
  geek();
  //desk
  fill(180, 130, 80);
  rect(0, 340, width, 60);
  //draw bully with spitballs
  bully();
}








void chalkboard() {
  fill(230, 180, 125);
  rect(35, 55, 330, 250, 3);
  fill(40,65, 40);
  rect(40, 60, 320, 240, 10);
}





//score goes up by 1 every time a spitball makes it past the book and under his head.
void drawScore() {
  // used the gone fishing coding examples from http://prog14998.sheridanc.on.ca/assignments/2-interactive-toy/   as reference
  //puts a tallymark on the screen every time the score goes up.
  rectMode(CORNERS);
  for (int chalk=0; chalk<score; chalk++) {
    fill(255);
    rect(-chalk*15+320, 75, -chalk*15+325, 100, 5);
  }
  rectMode(CORNER);
}






//draw bully
void bully() {
  noStroke();
    //DRAW SPITBALLS
  spitball();

  fill(200, 50, 100);
  //Left Shoulder
  ellipse(strawX+55, strawY+67, 30, 30);

  //left bicep
  quad(strawX+42, strawY+70, strawX+80, strawY+70, strawX+10, strawY+115, strawX+10, strawY+100);
  //left forearm
  quad(strawX+17, strawY+20, strawX+27, strawY+20, strawX+10, strawY+115, strawX-10, strawY+115);
  //left fist
  rect(strawX+5, strawY-5, 30, 30);

  //neck
  fill(180, 30, 80);
  quad(strawX+60, strawY+30, strawX+80, strawY+30, strawX+110, strawY+60, strawX+90, strawY+60);


  //body
  fill(100);
  quad(strawX+50, strawY+55, strawX+120, strawY+55, strawX+200, 340, strawX+110, 340);

  //arms
  fill(200, 50, 100);
  //Right bicep
  quad(strawX+105, strawY+60, strawX+125, strawY+60, strawX+100, strawY+145, strawX+85, strawY+130);

  //right forearm
  quad(strawX+85, strawY+129, strawX+100, strawY+144, strawX+45, strawY+180, strawX+35, strawY+167);
  //fist
  rect(strawX+5, strawY+146, 25, 39, 7);
  quad(strawX+15, strawY+145, strawX+60, strawY+165, strawX+45, strawY+180, strawX+15, strawY+185);
  //Right Shoulder
  ellipse(strawX+115, strawY+60, 30, 30);
  //ear
  quad(strawX+97, strawY-5, strawX+110, strawY, strawX+102, strawY+25, strawX+90, strawY+20);


  //Earrings
  fill(100);
  quad(strawX+105, strawY+7, strawX+113, strawY+12, strawX+110, strawY+19, strawX+102, strawY+15);
  quad(strawX+93, strawY+17, strawX+100, strawY+20, strawX+97, strawY+30, strawX+90, strawY+27);


  //hair
  stroke(0);
  line(strawX+95, strawY-37, strawX+100, strawY-49);
  line(strawX+80, strawY-43, strawX+82, strawY-55);
  line(strawX+72, strawY-46, strawX+72, strawY-60);
  noStroke();
  //head
  fill(200, 50, 100);
  quad(strawX+60, strawY-50, 
    strawX+105, strawY-33, 
    strawX+85, strawY+40, 
    strawX+30, strawY+35);


  //draw Straw
  fill(255);
  quad(strawX+50, strawY+12, strawX+50, strawY+3, strawX, strawY, strawX, strawY+10);


  //Left fingers
  fill(200, 50, 100);
  arc(strawX+20, strawY-4, 30, 30, 0, PI);

  //eyes
  //make eyes follow Geek== make eyes move slower than rest of body
  float eye1=strawY+(strawY*-0.04)-25;
  float eye2=strawY+(strawY*-0.04)-18;

  fill(255);
  //left eye
  ellipse(strawX+55, strawY-30, 20, 20);

  //NOSE
  fill(100, 0, 0);
  triangle(strawX+67, strawY-28, strawX+60, strawY-4, strawX+38, strawY-10);
  //cheek
  fill(150, 25, 60);
  ellipse(strawX+65, strawY+10, 40, 40);

  // right eye and pupils
  fill(255);
  ellipse(strawX+75, strawY-23, 20, 20);
  fill(10, 0, 0);
  ellipse(strawX+50, eye1, 5, 5);
  ellipse(strawX+70, eye2, 5, 5);

  //eyebrows
  stroke(0);
  strokeWeight(5);
  line(strawX+50, strawY-49, strawX+65, strawY-33);
  line(strawX+65, strawY-33, strawX+87, strawY-34);
  noStroke();

  //head stubble
  ellipse(strawX+100, strawY-30, 3, 3);
  ellipse(strawX+98, strawY-24, 3, 3);
  ellipse(strawX+94, strawY-28, 3, 3);
}






void spitball() {
  fill(230,230,200);
  ellipse(spitballX, spitballY, ballSize, ballSize);
  ellipse(spitballX2, spitballY2, ballSize, ballSize);
  ellipse(spitballX3, spitballY3, ballSize, ballSize);
  ellipse(spitballX4, spitballY4, ballSize, ballSize);
}








void geek() {
  fill(100, 100, 200);
  //neck
  quad(geekBodyX, geekBodyY, geekBodyX+geekBodyWidth, geekBodyY, geekBodyX+geekBodyWidth/2, geekBodyY-40, geekBodyX, geekBodyY-40);
  //body
  rect(geekBodyX, geekBodyY, geekBodyWidth, 90);

  fill(200, 150, 170);

  //hair
  ellipse(headX+5, headY-35, 10, 20);
  ellipse(headX-5, headY-32, 20, 10);

  //ear
  fill(125, 125, 175);
  ellipse(headX+32, headY+20, 20, 30);

  //head
  fill(150, 150, 200);
  ellipse(headX, headY, headSize, headSize);
  ellipse(headX-10, headY+20, 70, 60);

  //eyes
  noFill();
  stroke(50, 50, 100);
  strokeWeight(2);
  arc(headX-23, headY-5, 10, 20, PI, TWO_PI-0.3);
  arc(headX+5, headY+3, 10, 20, PI+0.3, TWO_PI);

  //eyebrows
  arc(headX+7, headY-22, 20, 25, 1.2, PI);
  arc(headX-23, headY-27, 14, 20, 0, PI-1);

  //glasses
  noFill();
  stroke(0);
  ellipse(headX-23, headY-10, 25, 25);
  ellipse(headX+5, headY, 25, 25);
  line(headX-10, headY-7, headX-7, headY-5);


  //mouth
  stroke(50, 50, 100);
  arc(headX-25, headY+30, 10, 5, PI, TWO_PI-0.3);
  noStroke();

  //nose
  fill(200, 200, 250);
  ellipse(headX-15, headY, 10, 15);

  //legs
  fill(75, 75, 125);
  noStroke();
  quad(geekBodyX, geekBodyY+90, geekBodyX+40, geekBodyY+90, geekBodyX+80, height, geekBodyX+40, height);
  stroke(50, 50, 100);
  line(geekBodyX, geekBodyY+90, 60, 341);
  noStroke();

  book();
  //shoulders
    fill(100, 100, 200);
  ellipse(geekBodyX, geekBodyY-10, 35, 35);
}





//draw book
void book() {
  //make Geek's hands/books tremble
  float shake= random(bookX-1.5, bookX+1.5);

  //book
  fill(75, 75, 200);
  rect(shake, bookY, 10, 70);
  quad(shake, bookY, shake+20, bookY+10, shake+20, bookY+60, shake, bookY+70);

  //arm
  fill(150, 150, 200);
  quad(geekBodyX, geekBodyY-15, shake, bookY+40, shake, bookY+60, geekBodyX, geekBodyY);

  //hand
  ellipse(shake, bookY+50, 30, 30);
}



void keepBallInStraw() {

  //if spitball is not moving and hasn't been shot yet
  if (spitballX>240 && ballSpeed==0) {
    spitballY=strawY+5;
  }

  if (spitballX2>240 && ballSpeed2==0) {
    spitballY2=strawY+5;
  }
  if (spitballX3>240 && ballSpeed3==0) {
    spitballY3=strawY+5;
  }
  if (spitballX4>240 && ballSpeed4==0) {
    spitballY4=strawY+5;
  }
}



void bullyMove() {

  //all variables associated with strawY (all of bully) moves when the arrow keys are pressed
  if (bullyUp==true) {
    strawY+=-bullyMovement;
  }
  if (bullyDown==true) {
    strawY+=bullyMovement;
  }
}


void bookMove() {
  //book (and hands and arms) moves when the WS keys are pressed
  if (bookUp==true) {
    bookY+=-geekMovement;
  }
  if (bookDown== true) {
    bookY+=geekMovement;
  }
}

void spitballMove() {
  //spitball gets assigned a speed and moves when spacebar is pressed.
  if (shootBall==true) {
    ballSpeed=5;
  }
  if (shootBall2==true) {
    ballSpeed2=5;
  }
  if (shootBall3==true) {
    ballSpeed3=5;
  }
  if (shootBall4==true) {
    ballSpeed4=5;
  }
}


void checkSpitballCollision() {

  //if spitball hits book
  if (spitballY>bookY && spitballY<bookY+70 && spitballX==bookX+10) {
    //spitball moves backwards
    shootBall=false;
    ballSpeed= ballSpeed*-1;
  }
  //if spitball hits body, spitball sticks to Geek
  if (spitballY>geekBodyY && spitballX<geekBodyX+geekBodyWidth+ballSize/2) {
    shootBall=false;
    ballSpeed=0;
  }

  //if spitball hits head, spitball sticks to Geek
  //online coding forum https://forum.processing.org/two/discussion/4246/how-to-detect-ellipse-collision
  if (dist(headX, headY, spitballX, spitballY)<headSize/2) {
    shootBall=false;
    ballSpeed=0;
  }
  if (dist(headX-10, headY+20, spitballX, spitballY)<60/2) {
    shootBall=false;
    ballSpeed=0;
  }

  //if spitball hits shoulders, spitball sticks to Geek
  if (dist(geekBodyX, geekBodyY-10, spitballX, spitballY)<35/2) {
    shootBall=false;
    ballSpeed=0;
  }
}

void checkSpitball2Collision() {

  //if spitball moves outside of frame, stop it
  if (spitballX2<0-10 || spitballX2>width+10) {
    shootBall2=false;
    ballSpeed2=0;
  }

  //if spitball hits book
  if (spitballY2>bookY && spitballY2<bookY+70 && spitballX2<=bookX+10) {
    //spitball moves backwards
    shootBall2=false;
    ballSpeed2=ballSpeed2*-1;
  }

  //if spitball hits body, spitball sticks to Geek
  if (spitballY2>geekBodyY && spitballX2<geekBodyX+geekBodyWidth+ballSize/2) {
    shootBall2=false;
    ballSpeed2=0;
  }

  //if spitball hits head, spitball sticks to Geek
  //online coding forum https://forum.processing.org/two/discussion/4246/how-to-detect-ellipse-collision
  if (dist(headX, headY, spitballX2, spitballY2)<headSize/2) {
    shootBall2=false;
    ballSpeed2=0;
  }
  if (dist(headX-10, headY+20, spitballX2, spitballY2)<60/2) {
    shootBall2=false;
    ballSpeed2=0;
  }
  //if spitball hits shoulders, spitball sticks to Geek
  if (dist(geekBodyX, geekBodyY-10, spitballX2, spitballY2)<35/2) {
    ballSpeed2=0;
    shootBall2=false;
  }
}

void checkSpitball3Collision() {

  if (spitballX3<0-10 || spitballX3>width+10) {
    shootBall3=false;
    ballSpeed3=0;
  }

  //if spitball hits book
  if (spitballY3>bookY && spitballY3<bookY+70 && spitballX3<=bookX+10) {
    //spitball moves backwards
    shootBall3=false;
    ballSpeed3= ballSpeed3*-1;
  }
  //if spitball hits body, spitball sticks to Geek
  if (spitballY3>geekBodyY && spitballX3<geekBodyX+geekBodyWidth+ballSize/2) {
    shootBall3=false;
    ballSpeed3=0;
  }

  //if spitball hits head, spitball sticks to Geek
  //online coding forum https://forum.processing.org/two/discussion/4246/how-to-detect-ellipse-collision
  if (dist(headX, headY, spitballX3, spitballY3)<headSize/2) {
    shootBall3=false;
    ballSpeed3=0;
  }
  if (dist(headX-10, headY+20, spitballX3, spitballY3)<60/2) {
    shootBall3=false;
    ballSpeed3=0;
  }
  //if spitball hits shoulders, spitball sticks to Geek
  if (dist(geekBodyX, geekBodyY-10, spitballX3, spitballY3)<35/2) {
    shootBall3=false;
    ballSpeed3=0;
  }
}
void checkSpitball4Collision() {

  //if spitball moves outside of frame, stop it
  if (spitballX4<0-10 || spitballX4>width+10) {
    shootBall4=false;
    ballSpeed4=0;
  }

  //if spitball hits book
  if (spitballY4>bookY && spitballY4<bookY+70 && spitballX4<=bookX+10) {
    //spitball moves backwards
    shootBall4=false;
    ballSpeed4=ballSpeed4*-1;
  }

  //if spitball hits body, spitball sticks to Geek
  if (spitballY4>geekBodyY && spitballX4<geekBodyX+geekBodyWidth+ballSize/2) {
    shootBall4=false;
    ballSpeed4=0;
  }

  //if spitball hits head, spitball sticks to Geek
  //online coding forum https://forum.processing.org/two/discussion/4246/how-to-detect-ellipse-collision
  if (dist(headX, headY, spitballX4, spitballY4)<headSize/2) {
    shootBall4=false;
    ballSpeed4=0;
  }
  if (dist(headX-10, headY+20, spitballX4, spitballY4)<60/2) {
    shootBall4=false;
    ballSpeed4=0;
  }
  //if spitball hits shoulders, spitball sticks to Geek
  if (dist(geekBodyX, geekBodyY-10, spitballX4, spitballY4)<35/2) {
    ballSpeed4=0;
    shootBall4=false;
  }
}


void scoring() {
  //if the spitball gets past the book, and doesn't go over the Geek's head, the score goes up
  if (spitballX==scoreLine && spitballY>71 ) {
    score++;
  }
  //for some reason spitballX won't register as == scoreline, so i made the scoreline wider
  if (spitballX2<scoreLine&& spitballX2>scoreLine-5 && spitballY2>71) {
    score++;
  }
  if (spitballX3<scoreLine&& spitballX3>scoreLine-5 && spitballY3>71) {
    score++;
  }
  if (spitballX4<scoreLine&& spitballX4>scoreLine-5 && spitballY4>71) {
    score++;
  }
}



void limitBullyMovement() {
  //restrains bully movement
  if (strawY<61) {
    bullyUp=false;
  }
  if (strawY>230) {
    bullyDown=false;
  }
}

void limitGeekMovement() {
  //restrains geek movement
  if (bookY<61-ballSize) {
    bookUp=false;
  }

  if (bookY>240-bookHeight) {
    bookDown=false;
  }
}


void keyPressed() {
  //used keyboard controls template
  if  (key==' ' ) {
    //if the ball hasn't been shot yet
    if (ballSpeed==0) {
      shootBall=true;
    }
    //if spacebar is pressed and first spitball is launched
    if (spitballX<243 || spitballX>250) {
      if (ballSpeed2==0) {
        shootBall2=true;
      }
    }
    //if spacebar is pressed and first two spitballs are launched
    if (spitballX2<253 || spitballX2>260) {
      if (ballSpeed3==0) {
        shootBall3=true;
      }
    }
    if (spitballX3<263 || spitballX3>270) {
      if (ballSpeed4==0) {
        shootBall4=true;
      }
    }
  }

  //move book up when W key is pressed
  if (key=='w') {
    bookUp=true;
  }
  if (key=='s') {
    bookDown=true;
  }
  //used https://processing.org/reference/keyCode.html
  if (key==CODED) {

    if (keyCode==UP) {
      bullyUp=true;
    }

    if (keyCode==DOWN) {
      bullyDown=true;
    }
  }
}

void keyReleased() {
  if (key=='w') {
    bookUp=false;
  }
  if (key=='s') {
    bookDown=false;
  }
  if (key==CODED) {
    if (keyCode==UP) {
      bullyUp=false;
    }
    if (keyCode==DOWN) {
      bullyDown=false;
    }
  }
}