///////////////////////////////////
// The BasketBall Game //
// by WangZuo //
////////////// 2018 ///////////////
///////////////////////////
// Ball & Basket
///////////////////////////
float BallX = 75;
float BallY = 20;
float basketY = random(80, 320);
///////////////////////////
// physical engine
///////////////////////////
float gravity = 0.6;
float fallspeed = 8;
float Xspeed = 0 ;
int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom
///////////////////////////////
// collision detection
///////////////////////////////
float distance1 = 0;
float distance2 = 0;
float distance3 = 0;
float puff1Mod;
float colorT=0;
////////////////////////
// effects
////////////////////////
int[] trailX = new int[10]; //These arrays are used to store previous ball positions, to draw the trail
int[] trailY = new int[10];
float effect1=0;
////////////////////////
// GamePlay
////////////////////////
float timeLeft=400;
boolean play = false;
float score=0;
float highScore = 0;
void setup(){
size(400,400);
frameRate(60);
noStroke();
frameRate(60);
}
void draw(){
if(play == false){ // -------------show First interface
gamestart();
score = 0;
}
if (play == true) { // ------------- show Second interface
drawBackground();
drawBasket();
drawBall();
updateBall();
distance(); // Distance calculating between Ball, Basket and Goal area. So that do collisions and goal.
goal();
drawgoal(); // Goal effects
trailUpdate (); // Ball effects
drawGUI ();
}
}
void gamestart(){
rectMode(CENTER); // ------------ The button
fill(#CE6666);
rect (200 ,243 ,85 ,45 , 15);
fill(255);
textSize(30);
text("PLAY", 165, 255);}
void mouseClicked(){ // -------------- mouseclick and start the game
if (mouseX > 150 && mouseX < 250 && mouseY > 220 && mouseY < 270) {
play = true; }
}
void drawBackground(){
background(1,1,1);
}
void drawBasket(){ // --------------- drawBasket
rectMode(CENTER);
fill(255);
rect(385, basketY, 10, 120, 7); // the backboard
rect(375, basketY+25, 10 , 10 , 2); // the edge of basket
noFill();
stroke(255);
strokeWeight(3);
ellipseMode(CENTER);
ellipse(340, basketY+25, 60, 20 ); //the basket
noStroke();
}
void drawBall(){
fill(#D80000);
ellipse(BallX, BallY, 40, 40);
}
void distance(){
distance1 = sqrt(sq(310 - BallX) + sq(basketY+25 - BallY)); // -------- calculate distance between Ball and edge of basket
distance2 = sqrt(sq(375 - BallX) + sq(basketY+25 - BallY)); // -------- calculate distance between Ball and basket
distance3 = sqrt(sq(340 - BallX) + sq(basketY+45 - BallY)); // -------- calculate distance between Ball and goal area
}
void updateBall(){
BallY=BallY+fallspeed; // -------------------- Ball's falling
BallX = BallX + ( Xspeed * xdirection ); // -------------------- Ball's falling
fallspeed = fallspeed + gravity; // -------------------- Ball's falling
if (BallY > height-14) { // -------------------- Ground reflection
fallspeed = abs(fallspeed) * -0.75;
}
if (mousePressed == true) { // -------------------- Ball move to right top when mousepressed
fallspeed = 8 * -1;
xdirection = 1;
Xspeed = 2;
}
BallX = BallX + ( Xspeed * xdirection );
if (BallX > width )
{BallX=15;} // -------------------- Ball appear on the left of screen when reach to the right border
if (BallX < 1 )
{BallX=400;} // -------------------- Ball appear on the right of screen when reach to the left border
if ( BallX < 400 &&BallX > 360 && BallY < basketY+85 && BallY > basketY-85 && xdirection > 0){ // -------------------- collision of backboard
xdirection *= -1;
Xspeed *= 0.9;
}
if( distance1 < 15 && xdirection <0){ // -------------------- collision of edge of basket
fallspeed = abs(fallspeed) * -0.75;
}
if( distance1 < 15 && xdirection > 0){ // -------------------- collision of edge of basket
xdirection *= -1;
Xspeed *= 0.9;
}
if( distance2 < 20 && xdirection <0){ // -------------------- collision of basket
fallspeed = abs(fallspeed) * -0.75;
}
if( distance2 < 20 && xdirection > 0){ // -------------------- collision of basket
xdirection *= -1;
Xspeed *= 0.9;
}
}
void goal() {
if ( distance3 <30 && BallY > basketY+55 && fallspeed > 0){ // -------------------- when ball reach the goal area
basketY= int(random(80, 320)); // renew basket position
puff1Mod=200; // do effects
colorT=colorT+10; // track's color burn
timeLeft += 100; // times add
score += 100; // increase score
effect1 -= 2; // fade of effects
}
if (timeLeft <= 0) // have no time
{
play = false; // show first interface
timeLeft = 400; // reset time
colorT= 40; // track's color reset
} else
{
timeLeft-=1;
}
if (score>highScore){ //record hightscore
highScore = score;}
}
void drawgoal(){ // ------------------------------ draw effect's when goal
fill(#FFF80D, 50+puff1Mod);
ellipse(340, basketY+30, 0.2*(255 - puff1Mod), 0.2*(255 - puff1Mod));
fill(#FFDB0D, puff1Mod);
ellipse(340, basketY+30, 0.3*(255 - puff1Mod), 0.3*(255 - puff1Mod));
fill(#FF0D0D, puff1Mod);
ellipse(340, basketY+30, 0.4*(255 - puff1Mod), 0.4*(255 - puff1Mod));
fill(#D80000, puff1Mod);
ellipse(BallX, BallY, 0.4*(255 - puff1Mod), 0.4*(255 - puff1Mod));
fill(#FA955B, puff1Mod);
ellipse(BallX, BallY, 0.3*(255 - puff1Mod), 0.3*(255 - puff1Mod));
puff1Mod -= 2; // fade of effects
}
void trailUpdate () { // ---------------------------------- Ball's trail
for (int i = 0; i < trailX.length; i++){
fill(#FCAC0A, colorT ); // the color's depth changed by colorT
ellipse (trailX[i], trailY[i], i*3, i*3);}
for (int i = 0; i < trailX.length - 1; i++) {
trailX[i] = trailX[i+1]; // shift the array values for the ball trail down one place
trailY[i] = trailY[i+1];
}
trailX[trailX.length - 1] = (int)BallX; //Add the current position to the end of the arrays. When combined with the shifting, this will make the arrays hold the previous 10 positions, which are used to make the trail.
trailY[trailY.length - 1] = (int)BallY;
}
void drawGUI()
{
rectMode(CENTER);
timeLeft = constrain(timeLeft, 0, 400); //timeLeft changed by framerate
fill(255);
rect(200, 15, timeLeft, 10);
fill(255, puff1Mod);
rect(200, 15, 1.5*(timeLeft - puff1Mod), 0.2*(255 - puff1Mod)); // effects
puff1Mod -= 2; // fade
fill(255);
textSize(20); // --------------------------- score GUI
text("score:", 0, 60);
text(score, 55, 60);
text(highScore, 100, 95);
text("highScore:", 0, 95);
}