Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
Title: Basket Ball
By: Raj Vijayakumar (Student#: 991 487 624)
Date: October 4, 2018

Intro to Media Computation
Instructor: Nicolas Hesler

Description:
Use the left and right arrow keys to control the basket and collect balls. Be careful about the circles in the background. 
They serve as an optical illusion to thwart you. 

NOTE: You have to catch the balls perfectly between the basket or it will not register it. Have fun!
*/

//////Declare all global variables//////

//variables for the basket
float basketSpeed = 5;
float basketPositionX = 60;
float basketPositionY = 375;
//variables to check which direction the basket needs to be moved
boolean basketMovesLeft;
boolean basketMovesRight;

//variables for the 3 balls that spawn above beyond the sketch
float dropSpeed = 2;
//ball #1
float ballOnePositionX = random(40,360);
float ballOnePositionY = -20;
//ball #2
float ballTwoPositionX = random(40,360);
float ballTwoPositionY = -200;
//ball #3
float ballThreePositionX = random(40,360);
float ballThreePositionY = -400;

void setup(){
  //Setup canvas
  size(400,400);
  //hide cursor
  noCursor();
}

void draw(){
  //Set background 
  background(245,238,203);
  //define how ellipses should be drawn
  ellipseMode(RADIUS);
  
  //return under defined functions
  graphic();
  basketMovement();
  assets();  
  dropBall();  
}

//Contains all the assets that the user sees on screen
void assets(){
  //Adresses the balls that fall
  noStroke();
  fill(134,43,43);
  ellipse(ballOnePositionX,ballOnePositionY,20,20);
  ellipse(ballTwoPositionX,ballTwoPositionY,20,20);
  ellipse(ballThreePositionX,ballThreePositionY,20,20);
  //Adresses the basket
  fill(216,210,210);
  stroke(64,60,60);
  strokeWeight(5);
  quad(basketPositionX - 30,basketPositionY - 5,basketPositionX + 30,basketPositionY - 5,basketPositionX + 20,basketPositionY + 25,basketPositionX - 20,basketPositionY + 25);
}

//Allows the basket to move left and right on the screen but never beyond it
void basketMovement(){ 
  //move basket right
  if(basketMovesRight == true){
    basketPositionX = basketPositionX + basketSpeed;
  }
  //move basket left
  if (basketMovesLeft == true){
    basketPositionX = basketPositionX - basketSpeed;
  }
  //stops basket from exiting the screen on the left
  if(basketPositionX <= 30){
    basketPositionX = 30;
  }
  //stops basket from exiting the screen on the right
  if(basketPositionX >= 370){
    basketPositionX = width-30;
  }
}

//Checks to see if keys are pressed
void keyPressed(){
  //checks if arrow keys are pressed
  if (key == CODED) 
  {
    if (keyCode == LEFT) 
    {
      //if left key is pressed then moving right gets set to false
      basketMovesLeft = true;
      basketMovesRight = false;
    } else if (keyCode == RIGHT) 
    {
      //if right key is pressed then moving left gets set to false
      basketMovesRight = true;
      basketMovesLeft = false;
    }
  }
}

//Checks to see if key is released
void keyReleased()
{
  if (key == CODED) 
  {
    if (keyCode == LEFT) 
    {
      //if the left key is released then then the basket moving left becomes false
      basketMovesLeft = false;
    } else if (keyCode == RIGHT) 
    {
      ///if the right key is released then then the basket moving right becomes false
      basketMovesRight = false;
    }
  }
}

//Collision detection system that checks what the balls come into contact with/ generates random coordinates from which the balls drop
void dropBall(){
  //Add a speed at which the ball will fall to their position
  ballTwoPositionY += dropSpeed;
  ballThreePositionY += dropSpeed;
  
  //collision for ball #1
  if(ballOnePositionY >= basketPositionY - 25 && ballOnePositionX - 20 >= basketPositionX - 30 && ballOnePositionX + 20 <= basketPositionX + 30 || ballOnePositionY > 400)
  {
    //determine a random position the ball will start at on the horizontal axis
    ballOnePositionX = random(40,360);
    //set the ball drop above the screen to give it a falling effect
    ballOnePositionY = -200;
  }
  //collision for ball #2
  if(ballTwoPositionY >= basketPositionY - 25 && ballTwoPositionX - 20 >= basketPositionX - 30 && ballTwoPositionX + 20 <= basketPositionX + 30 || ballTwoPositionY > 400)
  {
    //determine a random position the ball will start at on the horizontal axis
    ballTwoPositionX = random(40,360);
    //set the ball drop above the screen to give it a falling effect
    ballTwoPositionY = -500;
  }
  //collision for ball #3
  if(ballThreePositionY >= basketPositionY - 25 && ballThreePositionX - 20 >= basketPositionX - 30 && ballThreePositionX + 20 <= basketPositionX + 30 || ballThreePositionY > 400)
  {
    //determine a random position the ball will start at on the horizontal axis
    ballThreePositionX = random(40,360);
    //set the ball drop above the screen to give it a falling effect
    ballThreePositionY = -800;
  }
}

//Background circles drawn using while loop
void graphic(){
  //local variable
  int radius = 20;
  while(radius <= width){
    strokeWeight(5);
    stroke(200);
    fill(255 + radius);
    ellipse(200,200,radius,radius);
    radius = radius += 20;
  }
}