// Parts 1 - 4 took between 2-3 hours
// Part 5 took about 3 - 4 hours

// Part 5 is star()... a boat moves along the bottom of the screen and shoots the
// eight stars that are up in the sky, and when all the stars are gone, a spiral
// appears and then disappears in random places.

import javax.swing.*;
import java.awt.*;
import java.lang.Object;

public class Ball extends YourTurtle {
  
  //private Color turtleColor= Color.black;  // color of the turtle
  
  private int radius;
  private int vx;
  private int vy;  
  private int save;
  
  /** Constructor: a window without anything in it */
  public Ball() {
      clear();
  }
 
  
  private static int boatx;
  private static int boaty;
  private static int bx; 
  private static int by;
  private static int boatdirection;
  private static int shotx;
  private static int shoty;
  private static int star;
  private static int starx;
  private static int stary;
  private static int rectanglex;
  private static int rectangley;
  
  private static int xstar;
  private static int ystar;
  
  public int getRadius() {
    return radius;
  }
  
  public int getVX() {
    return vx;
  }
  
  public int getVY() {
    return vy;
  }
  
  public Ball(int x, int y, int r, int c) {
    moveTo(x, y, 0);
    radius = r;
    save = c;
    setColor(save); 
    fillCircle(r*2);
  }
  
  public Ball(int x, int y, int horizontal, int vertical, int r, int c) {
    this(x, y, r, c);
    vx = horizontal;
    vy = -vertical;
    
    fillCircle(r*2);    
  }
  
  public void moveBallOnce() {
    setColor(Color.white);
    fillCircle(radius*2);
    setColor(save);
    moveTo(this.getX() + vx, this.getY() + vy, 0);
    fillCircle(radius*2); 
    
    if(this.getX() >= radius) vx = -vx;      
    if(this.getX() <= height - radius) vx = -vx;
    if(this.getY() >= radius) vy = -vy;
    if(this.getY() <= width - radius) vy = -vy;   
  }
  
  public void inMotion() {
    while(true) {
      pause(100);
      this.moveBallOnce();
    }
  }
  
  // MY PROGRAM
  
  
  
  private int randInt() {
    int a = (int) (498 * Math.random());
    return a;
  }
  
  private int randInt2() {
    int a = (int) (151 * Math.random());
    return a;
  }
  
  private int randInt3() {
    int a = (int) (480 * Math.random());
    return a;
  }
  
  private int randStar() {
    int a = (int) (8 * Math.random());
    return a;
  }
  
  public void drawSkyStarUp(int d, boolean black) {
    double a = 25.714285714285722;
    double b = 154.28571428571428;
    int line = 1;
    setColor(save);
    if(black) setColor(Color.black);
    addAngle(0);
    move(d);
    while(line <= 6) {
      addAngle(b);
      move(d);
      line = ++line;
    }
  } 
  
  public void moveBoatOnce() {
    setColor(Color.black);
    moveTo(boatx, boaty, 0);
    fillRectangle(40, 10);
    setColor(2);
    bx = boatdirection;
    boatx = boatx + bx;
    boaty = boaty + by;
    moveTo(boatx, boaty, 0);
    fillRectangle(40, 10); 
    
    if(boatx >= 480 || boatx <= 20) bx = -bx; 
    
    boatdirection = bx;
  }
  
  public void boatInMotion() {
    boaty = 490;
    boolean found = true; 
    while(found) {
      pause(50);
      this.moveBoatOnce();
      if(boatx == rectanglex || boatx == rectanglex + 1 || 
         boatx == rectanglex + 2 || boatx == rectanglex + 3 ||
         boatx == rectanglex - 1 || boatx == rectanglex - 2 || 
         boatx == rectanglex - 3 || boatx == rectanglex - 4) found = false;
    }
  }
  
  
  public void moveShotOnce() {
    setColor(Color.black);
    moveTo(shotx, shoty, 0);
    fillCircle(4);
    setColor(11);
    shotx = shotx + vx;
    shoty = shoty + vy;
    moveTo(shotx, shoty, 0);
    fillCircle(4);     
  }
  
  public void shotInMotion() {
    shotx = boatx;
    shoty = boaty;
    boaty = 482;
    setColor(Color.black);
    moveTo(shotx, shoty-8, 0);
    fillCircle(4);
    boolean found = true;
    while(found) {
      pause(10);
      this.moveShotOnce();
      int bombx = this.getX();
      int bomby = this.getY();
      if(bombx == starx || bombx == starx + 1 || bombx == starx - 1) found = false;
      if(bomby <= stary - 10 ) found = false;
      
    }
    setColor(Color.black);
    moveTo(shotx, shoty, 0);
    fillCircle(4);
  }
  
  public void fireworks(boolean black) {
    moveTo(starx, stary, 0);
    int times = 2;
    int n = 40;
    int d = 1;
    int length = d;
    addAngle(0);
    double angle = 140;
    setColor(Color.green);
    if(black) setColor(Color.black);
    move(d);
    while(times <= n) {      
      if(times%3 == 1) setColor(Color.green);
      if(times == 2 || times%3 == 2) setColor(Color.blue);
      if(times == 3 || times%3 == 0) setColor(Color.red);
      if(black) setColor(Color.black);
      addAngle(140);       
      length = d*times;
      move(length);       
      times = ++times;    
      pause(25);      
    }  
  }
  
  public void afterworks(boolean black) {
    moveTo(xstar, ystar, 0);
    int times = 2;
    int n = 80;
    int d = 1;
    int length = d;
    addAngle(0);
    double angle = 140;
    setColor(Color.green);
    if(black) setColor(Color.black);
    move(d);
    while(times <= n) {      
      if(times%3 == 1) setColor(Color.green);
      if(times == 2 || times%3 == 2) setColor(Color.blue);
      if(times == 3 || times%3 == 0) setColor(Color.red);
      if(black) setColor(Color.black);
      addAngle(140);       
      length = d*times;
      move(length);       
      times = ++times;    
      pause(25);      
    }  
  }
  
  public void star() {
    clear();
    save = 1;
    setColor(save);
    fillRectangle(1000, 1000);
    
    int starcounter = 1;
    
    starx = 0;
    stary = 0;
    
    boatx = width/2;
    int boaty= height - 10;     
    int boatlength = 40;
    int boatheight = 10;
    
    save = 2;
    setColor(save);
    moveTo(boatx, boaty, 0);
    fillRectangle(boatlength, boatheight);
    
    int firstx = randInt();
    int secondx = randInt();
    int thirdx = randInt();
    int fourthx = randInt();
    int fifthx = randInt();
    int sixthx = randInt();
    int seventhx = randInt();
    int eighthx = randInt();
    
    int firsty = randInt2();
    int secondy = randInt2();
    int thirdy = randInt2();
    int fourthy = randInt2();
    int fifthy = randInt2();
    int sixthy = randInt2();
    int seventhy = randInt2();
    int eighthy = randInt2();
    
    save = 13;
    setColor(save);
    
    moveTo(firstx, firsty, 0);
    drawSkyStarUp(5, false);
    moveTo(secondx, secondy, 0);
    drawSkyStarUp(5, false);
    moveTo(thirdx, thirdy, 0);
    drawSkyStarUp(5, false);
    moveTo(fourthx, fourthy, 0);
    drawSkyStarUp(5, false);
    moveTo(fifthx, fifthy, 0);
    drawSkyStarUp(5, false);
    moveTo(sixthx, sixthy, 0);
    drawSkyStarUp(5, false);
    moveTo(seventhx, seventhy, 0);
    drawSkyStarUp(5, false);
    moveTo(eighthx, eighthy, 0);
    drawSkyStarUp(5, false);
    
    star = 1;
    bx = 6;
    by = 0;
    boatdirection = bx;
    
    while(starcounter <=8) {
            
      if(star == 1) {
        if(firstx <= width/2) {
          rectanglex = firstx + 20;
          starx = firstx;
          stary = firsty;
        }
        else {
          rectanglex = firstx - 20;
          starx = firstx;
          stary = firsty;
        }
      }
      if(star == 2) {
        if(secondx <= width/2) {
          rectanglex = secondx + 20;
          starx = secondx;
          stary = secondy;
        }
        else {
          rectanglex = secondx - 20;
          starx = secondx;
          stary = secondy;
        }
      }
      if(star == 3) {
        if(thirdx <= width/2) {
          rectanglex = thirdx + 20;
          starx = thirdx;
          stary = thirdy;
        }
        else {
          rectanglex = thirdx - 20;
          starx = thirdx;
          stary = thirdy;
        }
      }
      if(star == 4) {
        if(fourthx <= width/2) {
          rectanglex = fourthx + 20;
          starx = fourthx;
          stary = fourthy;
        }
        else {
          rectanglex = fourthx - 20;
          starx = fourthx;
          stary = fourthy;
        }
      }
      if(star == 5) {
        if(fifthx <= width/2) {
          rectanglex = fifthx + 20;
          starx = fifthx;
          stary = fifthy;
        }
        else {
          rectanglex = fifthx - 20;
          starx = fifthx;
          stary = fifthy;
        }
      }
      if(star == 6) {
        if(sixthx <= width/2) {
          rectanglex = sixthx + 20;
          starx = sixthx;
          stary = sixthy;
        }
        else {
          rectanglex = sixthx - 20;
          starx = sixthx;
          stary = sixthy;
        }
      }
      if(star == 7) {
        if(seventhx <= width/2) {
          rectanglex = seventhx + 20;
          starx = seventhx;
          stary = seventhy;
        }
        else {
          rectanglex = seventhx - 20;
          starx = seventhx;
          stary = seventhy;
        }
      }
      if(star == 8) {
        if(eighthx <= width/2) {
          rectanglex = eighthx + 20;
          starx = eighthx;
          stary = eighthy;
        }
        else {
          rectanglex = eighthx - 20;
          starx = eighthx;
          stary = eighthy;
        }
      }           
      
      boatInMotion();
      
      vx = ((starx - boatx)/10);
      vy = (stary - boaty + 8)/10;
      
      Ball shot = new Ball(boatx, boaty - 8, vx, vy, 2, 11);
      shotInMotion();     
      fireworks(false);
      pause(3000);
      fireworks(true);
      
      if(star == 1) {
        moveTo(firstx, firsty, 0);
        drawSkyStarUp(5, true);
      }
      if(star == 2) {
        moveTo(secondx, secondy, 0);
        drawSkyStarUp(5, true);
      }
      if(star == 3) {
        moveTo(thirdx, thirdy, 0);
        drawSkyStarUp(5, true);
      }
      if(star == 4) {
        moveTo(fourthx, fourthy, 0);
        drawSkyStarUp(5, true);
      }
      if(star == 5) {
        moveTo(fifthx, fifthy, 0);
        drawSkyStarUp(5, true);
      }
      if(star == 6) {
        moveTo(sixthx, sixthy, 0);
        drawSkyStarUp(5, true);
      }
      if(star == 7) {
        moveTo(seventhx, seventhy, 0);
        drawSkyStarUp(5, true);
      }
      if(star == 0) {
        moveTo(eighthx, eighthy, 0);
        drawSkyStarUp(5, true);
      }
      
      starcounter = ++starcounter;
      star = ++star;
      pause(1000);
    }
    pause(1000);
    setColor(Color.black);
    moveTo(boatx, boaty, 0);
    fillRectangle(40, 10);
    moveTo(0, 0, 0);
    fillRectangle(width, height);
    
    while(true) {
      xstar = randInt3();
      ystar = randInt3();      
      afterworks(false);
      pause(2000);
      afterworks(true);
      pause(2000);
    }      
    
  }  
}
