//Alison Marklein
//CS100J Assignment 4
//My interesting procedure draws a night sky.  There are some blinking stars and a shooting star.  
//I spent about 10 hours on this.. (time flies when you're programming! I lost track of time sometimes...)

import javax.swing.*;
import java.awt.*;
//import java.awt.geom.Color;
public class Ball extends YourTurtle
{
    private int radius;   //= radius size of ball
    private int vx;       //= speed in horizontal direction (in pixels)
    private int vy;       //= speed in vertical direction (in pixels)
    
    /** Constructor: a window without anything in it */
    public Ball() {
        clear();
    }
    
    /*Constructor: initializes a new ball folder so that the turtle starts
     * at position (x, y) and has speed (vx, vy), has radius r, and is drawn
     * with Color c.  */
    public Ball(int x, int y, int vx, int vy, int r, Color c)
    {
        setColor(c);
        liftPen();
        moveTo(x, y, 0);
        fillCircle(r);
        radius= r;
        this.vx= vx;
        this.vy= vy;
    }
    
    //getter methods for radius, vx, vy
    //= the radius of the ball
    public int getRadius()
    {
        return radius;
    }
    
    //= the speed in horizontal direction (in pixels)
    public int getVX()
    {
        return vx;
    }
    
    //= the speed in vertical direction (in pixels)
    public int getVY()
    {
        return vy;
    }
    
    //procedure: moves the ball once, as given by its speed (vx, vy)
    public void moveBallOnce()
    {  
        //Step 1: Pause for 100 microseconds
        pause(100);
        
        //Step 2a: Draw the ball at its current position using color white, thus erasing it
        Color save= getColor();
        setColor(Color.white);
        fillCircle(getRadius());   
        
        //Step 2b: Move the position of the turtle
        setAngle(0);
        move(getVX());
        setAngle(270);
        move(getVY());
        
        //Step 2c: Draw the ball in its own color
        setColor(save);
        fillCircle(getRadius());      
        
        //Make the ball bounce off the walls
        if (getY() < radius) //Bounce off top wall
        {
            vy= -vy;
        }
        if (height < getY() + radius) //Bounce off bottom wall
        {
            vy= -vy;
        }
        if (getX() < radius) //Bounce offleft wall
        {
            vx= -vx;
        }
        if (width < getX() + radius) //Bounce off right wall
        {
            vx= -vx;
        }
    }
    
    //procedure: puts the ball perpetually in motion
    public void inMotion()
    {
        int i= 1;
        while (i > 0)
        {
            //pause for 100 microseconds
            pause(100);
            //move ball once
            moveBallOnce();
        }
    }
   
    //My method: first method draws a filled in star
    //The real method, drawNight(int), draws a night landscape with a shooting star of size starSize
    //Draw a filled in star of the color the pen is
    public void drawFilledStar(int x, int y, int starSize)
    {
        liftPen();
        int size= 0;
        
        while(size <starSize) //loop to make star grow so that it is filled
        {           
            moveTo(x-size/2, y, 0);
            putPenDown();
            drawStarUp(size);
            size= size+1;
            liftPen();
        }
    }
    
    /** draw a nightscape with a shooting star with
        a size starSize that moves every howOften iterations
        parameter dy is not explained.
    */
    public void drawNight(int dy, int starSize, int howOften)
    {
        //Set background to black
        liftPen();
        moveTo(width/2, height/2, 0);    
        setColor(Color.black);
        fillRectangle(width, height);
        
        //Draw Moon
        moveTo(375, 100, 0);
        setColor(Color.yellow);
        fillCircle(50);
        liftPen();
        moveTo(366, 112, 0);
        setColor(Color.black);
        fillCircle(50);
        
        //Draw stationary stars
        setColor(Color.yellow);
        drawFilledStar(150, 200, 20);
        drawFilledStar(375, 200, 15);
        drawFilledStar(400, 225, 15);
        drawFilledStar(50, 190, 25);
        drawFilledStar(350, 150, 25);
        drawFilledStar(50, 55, 25);
        
        //Loop to make stars blink
        int i=1; //counter
        while (i != 0) //infinite loop
        {
            //Draw small stars
            setColor(Color.yellow);
            drawFilledStar(100, 150, 50);
            drawFilledStar(315, 225, 55);
            drawFilledStar(200, 75, 40);
            drawFilledStar(450, 50, 25);
            pause(500);
            
            //Draw large stars and erase
            drawFilledStar(100, 150, 75);
            drawFilledStar(315, 225, 80);
            drawFilledStar(200, 75, 60);
            drawFilledStar(450, 50, 40);
            pause(500);
            
            //Shooting star: moves across screen every 20 iterations
            if (i%howOften==0)
            {
                int xVal= 50;
                while (xVal < width) //loop to move star
               {   
                  //Draw the star at its current position thus erasing it
                   setColor(Color.yellow);
                   drawFilledStar(xVal, (500-xVal)/2+250, starSize);                
                   pause(100);
                   
                   //Move the position of the turtle
                   liftPen();
                   setAngle(0);
                   move(20);
                   setAngle(90);
                   move(dy);
        
                   //Draw the star in black to erase
                   setColor(Color.black);        
                   drawFilledStar(xVal, (500-xVal)/2+250, starSize);              
                
                   xVal= xVal+20; //increment x by 20
               }//End of shooting star while loop       
            }//End of shooting star if-statement
            
            //make stars blink
            setColor(Color.black);
            drawFilledStar(100, 150, 75);
            drawFilledStar(315, 225, 80);
            drawFilledStar(200, 75, 60);
            drawFilledStar(450, 50, 40);
            i= i+1;//increment
        }//End of while loop
    }//EOM
}//EOP