> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.MX3OeTy7UE8/rev.2227
 * 
 * authors: 
 *   Benjamin Y

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



// Pressing Control-R will render this sketch.

int i = 0; 
int timer = 181*30;
int[] ballX = {350,350,350};
int[] ballY = { 15,150,285};
int[] ballDX = { 0,  0,  0};
int[] ballDY = { 0,  0,  0};
int yourBotX = 30;
int yourBotY = 150;
int enemyBotX = 670;
int enemyBotY = 150;
boolean yourBotHas = false;
boolean enemyBotHas = false;
int diffX, diffY;
int score = 0;
void setup() {  // this is run once.   
    
    // set the background color
    background(255);
    
    // canvas size (Variable aren't evaluated. Integers only, please.)
    size(700, 300); 
      
    // smooth edges
    smooth();
    
    // limit the number of frames per second
    frameRate(30);
    
    // set the width of the line. 
    strokeWeight(6);
} 

float dist(int x, int y, int z, int w) {
    return sqrt((x*x-z*z)+(y*y-w*w));
}
bool isGrabbing() {
    if (keyPressed) {
        if (key = 'a') {
            return true;
        } else {
            return false;
        }
    }
}
void draw() {  // this is run repeatedly.  
    background(204);
    noFill();
    // set the color
    stroke(255);
    line(350,0,350,300);//midline
    line(0,0,700,0);
    line(0,300,700,300);
    stroke(255,0,0);
    line(700,0,700,300);
    line(600,0,600,300);
    stroke(0,0,255);
    line(0,0,0,300);
    line(100,0,100,300);
    stroke(0);// goals
    fill(0);
    ellipse(50,150,20,20);
    ellipse(650,150,20,20);
    stroke(0,255,0);// refueling stations
    noFill();
    rect(0,0,30,30);
    rect(0,270,30,30);
    rect(0,135,30,30);
    rect(670,0,30,30);
    rect(670,270,30,30);
    rect(670,135,30,30);
    rect(335,0,30,30);
    rect(335,270,30,30);
    rect(335,135,30,30);
    fill(0,128,0);
    myFont = createFont("Georgia", 32);
    textFont(myFont);
    textAlign(CENTER, CENTER);
    text(int(timer/30),150,150);
    text(int(timer/30),550,150);
    text("Press A to Grab",150,20);
    text(score,500,20);
    timer -= 1;
    fill(0,0,255);
    stroke(128,128,255);
    rect(yourBotX-8, yourBotY-8, 16, 16);
    stroke(255,128,128);
    fill(255,0,0);
    rect(enemyBotX-8, enemyBotY-8, 16, 16);
    for (int i=0; i<ballX.length; i++) {
        noStroke();
        fill(0,128,0);
        if (ballX[i] != -99) {
            ellipse(ballX[i], ballY[i], 20, 20);
        }
        ballX[i] += ballDX[i];
        ballY[i] += ballDY[i];
        if (ballX[i] <= 0) {
            ballX[i] = 0;
            ballDX[i] = -ballDX[i];
        }
        if (ballY[i] <= 0) {
            ballY[i] = 0;
            ballDY[i] = -ballDY[i];
        }
        if (ballX[i] >= width) {
            ballX[i] = width;
            ballDX[i] = -ballDX[i];
        }
        if (ballY[i] >= height) {
            ballY[i] = height;
            ballDY[i] = -ballDY[i];
        }
        if (dist(50,150,ballX[i],ballY[i]) < 10) {
            score -= 1;
            ballX[i] = -99;
            ballY[i] = -99;
            ballDX[i] = 0;
            ballDY[i] = 0;
        }
        if (dist(650,150,ballX[i],ballY[i]) < 10) {
            score += 1;
            ballX[i] = -99;
            ballY[i] = -99;
            ballDX[i] = 0;
            ballDY[i] = 0;
        }
        if (dist(yourBotX, yourBotY, ballX[i], ballY[i]) < 20 &&
                 yourBotHas == false && isGrabbing()) {
            yourBotHas = true;
            ballX[i] = -99;
            ballY[i] = -99;
            ballDX[i] = 0;
            ballDY[i] = 0;
        }
        
        
    }
    // move!!
    diffX = mouseX-yourBotX;
    diffY = mouseY-yourBotY;
    yourBotX += diffX/38;
    yourBotY += diffY/38;
    if (yourBotX > 586) {
        yourBotX = 586;
    }
    if (yourBotHas) {
        fill(0,128,0);
        ellipse(yourBotX, yourBotY, 20, 20);
    }
    if (mousePressed && yourBotHas == true) {
        i = ballX.length;
        ballX[i] = yourBotX;
        ballY[i] = yourBotY;
        ballDX[i] = diffX/38;
        ballDY[i] = diffY/38;
        yourBotHas = false;
    }
}