> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.cfy5tYEUSof/rev.112
 * 
 * authors: 
 *   Noah Donnelly

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 




// A simple version of the classic PONG, with basic hard-coded graphics and simple bounce-physics. Gets faster with more bounces and bumps.
// Created (proudly, :P) by Noah Donnelly, 05/02/2012. 
// Mess around with it as much as you like.

float xspeed;
float yspeed;

void print_speed(){
  //prints ball speeds.
  println("XSPEED: " + xspeed);
  println("YSPEED: " + yspeed);
}

void new_xy(){
  //Create a new X and Y speed for the ball: initiated on new game and game-over.
  xspeed = (random(2) + 4);
  if((random(0,1) == 0)){
      xspeed*= -1;
  }
  yspeed = 5;
  if((random(0,1) == 0)){
      yspeed*= -1;
  }
  println("NEW_XY CALLED: NEW GAME STARTED");
  print_speed();
}

void setup(){
  //Simple instructions for setup: screen size, noStroke, frameRate, etc.
  new_xy();
  size(500,500);
  background(0);
  noStroke();
  rectMode(CENTER);
  frameRate(200);
}

//Declaration of soo many variables. rX is used to lag the AI a few frames behind real time.
float x = random(width);
float y = 250;
int bouncecount = 0;
int bumpcount = 0;
float r1;
float r2;
float r3;
float r4;
float r5;
float r6;
float r7;


float bump2x = 250;

void bounce(){
  //is called when the ball hits the left or right edges of the screen.
  bouncecount ++;
  print("\nBounce! " + bouncecount + "\n");
  xspeed *= 1.009;
  yspeed *= 1.009;
}

void bump(){
  //is called when the ball hits a bumper.
  bumpcount ++;
  yspeed *= -1;
  xspeed *= 1.015;
  yspeed *= 1.015;
  println("Bump! " + bumpcount);
  println("BALLX: " + x + " BUMPERX: " + bump2x);
}

void game_over(){
  //is called when ball goes off top or bottom of screen.
  x = 250;
  y = 250;
  new_xy();
}

void draw(){
  //LOOP!
  //show speed for debug purposes.
  print_speed();
  //let the bumper follow the mouse.
  float bump1x = mouseX;
  //print over EVERYTHING! MWAHAHAHAHAHAHAHAHA!
  background(0, 0, 0, 15);
  //Draw the ball.
  ellipse(x, y, 15, 15);
  // speed it up appropriately
  x += xspeed;
  y += yspeed;
  //those bounce mechanics we all know and love
  if(x <= 0 || x >= width){
    xspeed *= -1;
    bounce();
  }
  
  //what happens if the ball hits the top or bottom? Bounce mechanics still there, from original testing: just commented out, and replaced with new game_over() function.
  if(y <= 0 || y >= height){
    //yspeed *= -1;
    //bounce();
    game_over();
  }
  
  //actually draw the bumpers... almost forgot!
  rect(bump1x, height - 20, 100, 10);
  rect(bump2x, 20, 100, 10);
  
  //check if ball is in contact with either of the bumpers.
  if(y > (height - 30) && y < (height - 20)){
    if(x > (bump1x - 50) && x < (bump1x + 50)){
      bump();
    }
  }
  if(y > (20) && y < 30){
    if(x > (bump2x - 50) && x < (bump2x + 50)){
      bump();
    }
  }
  
  //here's that AI lag I mentioned earlier, in all its glory.
  bump2x = r7 + (random(0.1,3) * (random(0,1)-1));
  r7 = r6;
  r6 = r5;
  r5 = r4;
  r4 = r3;
  r3 = r2;
  r2 = r1;
  r1 = x;
}