// Ryan Kightlinger
// February 20, 2001
// This Program will display a car game


// Get the necessary Java library methods
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;


public class Game extends Applet implements ActionListener{


	private Button right, left, up, down, reset;
        private int z, answer = 1;
	public Car auto;
        private FinishBox finBox;
        public Bomb bExplosion;



	public void init(){

                // sets bg color to white
                setBackground(Color.white);


		//should the car be fancier?
		auto = new Car(20, 20, 40, 30);
                     // xcord,ycoord,length, heigth

                finBox = new FinishBox(0, 450, 500, 450, 20, 30);
                    // xcord,ycord,xcord2,ycord2, car starting ycoord, car heigth

                bExplosion = new Bomb(50,50);
                    // bombwidth,bombheight


		left = new Button("Left");
		add(left);
		left.addActionListener(this);

		right = new Button("Right");
		add(right);
		right.addActionListener(this);

		up = new Button("Up");
		add(up);
		up.addActionListener(this);

		down = new Button("Down");
		add(down);
		down.addActionListener(this);

                reset = new Button("Reset");
                add(reset);
                reset.addActionListener(this);    
	}



	public void actionPerformed(ActionEvent e){
	
		if (e.getSource() == right){
			auto.right();
                        bExplosion.right();
                        bExplosion.check();
                 }

		
                if (e.getSource() == left) {
			auto.left();
                        bExplosion.left();
                        bExplosion.check();
                }
 
		

                if (e.getSource() == up){
			auto.up();
                        bExplosion.up();
                        finBox.check1();
                        bExplosion.check();
                }

		if (e.getSource() == down){
			auto.down();
                        bExplosion.down();
                        finBox.check2();
                        bExplosion.check();
                }

                if (e.getSource() == reset){
                        auto.reset();
                        finBox.reset();
                        bExplosion.reset();
                        answer = 1;
                }


		repaint();
	}




	public void paint(Graphics g) {

		auto.display(g);     // displays the car on screen
                finBox.display(g);   // displays the finish box
                bExplosion.display(g);

                     if (answer == 1){
                         for (z = 1; z<=1; z++) {
	                     bExplosion.drawBomb(g);
                         }
                     }


                answer = 0;
	}
}




    //------------------------------------------

    // BREAK / Next Class    (Car Class)

    //------------------------------------------





class Car{

	public int xCoord, yCoord, width, height;
        protected int x2,y2;



  
	public Car(int x, int y, int w, int h) {


		xCoord = x;
		yCoord = y;
		width = w;
		height = h;

                x2 = x;
                y2 = y;
	}



	public void reset() {
		xCoord = x2;
                yCoord = y2;
	}



	public void right() {
		xCoord += 5;
	}


	public void left() {
		xCoord -= 5;
	}


	public void up() {
		yCoord -= 5;
	}


	public void down() {
		yCoord += 5;
	}

	


	public void display(Graphics g) {

                g.setColor(Color.gray); // font color = gray
		g.fillRect(xCoord, yCoord, width, height);

	}

}




    //------------------------------------------

    // BREAK / Next Class   (Bomb Class)

    //------------------------------------------





class Bomb{

	private int bxCoord, byCoord, bombXcoord, bombYcoord, carYcoord, carXcoord, bombWidth, bombHeight, carXend, carYend, bombXend, bombYend, carW, carH, carX, carY, ans = 0, xCoord = 20, yCoord = 20;


        // defines the bomb's parameters
	public Bomb(int bwidth, int bheight) {
                
                bombWidth = bwidth;
                bombHeight = bheight;
        }



        
        // resets the bomb values
	public void reset() {

		bxCoord = (int) (Math.random() * 420) + 1;
		byCoord = (int) (Math.random() * 420) + 1;

                bombXcoord = bxCoord; 
                bombYcoord = byCoord;

		xCoord = 20;
                yCoord = 20;

                ans = 0;

	}




        // draws the bomb
	public void drawBomb(Graphics g) {

		bxCoord = (int) (Math.random() * 420) + 1;     // sets random coordinates
		byCoord = (int) (Math.random() * 420) + 1;     // sets random coordinates
                g.drawRect(bxCoord, byCoord, bombWidth, bombHeight);
 


	}






	public void left() {
            xCoord -= 5;
	}


	public void right() {
		xCoord += 5;
	}

	public void up() {
		yCoord -= 5;
	}


	public void down() {
		yCoord += 5;
	}




	// bomb check to see if it will explode
	public void check() {

		
                bombXcoord = bxCoord; 
                bombYcoord = byCoord; 

                carXcoord = xCoord; 
                carYcoord = yCoord;

                carXend = carXcoord + 40;
                carYend = carYcoord + 30;

                bombXend = bombXcoord + 30;
                bombYend = bombYcoord + 30;



                while (carXcoord <= carXend) { 
   
                    while (bombXcoord <= bombXend) { 
   
                        if (carXcoord == bombXcoord) { 
  
                            while (carYcoord <= carYend) {
  
                                while (bombYcoord <= bombYend) {

                                    if (carYcoord == bombYcoord) { 
 
                                          carW = 40;
                                          carH = 30;

                                          carX = carXcoord;
                                          carY = carYcoord;

                                          ans = 1;
 
                                    }
                                bombYcoord++;
                                }
                            carYcoord++;
                            }
                       }
                      bombXcoord++;
                   }
                   carXcoord++;
               }  

          

                
	}  // end of bomb check




        // draws the bomb explosion
	public void display(Graphics g) {


            if (ans == 1){
             
                g.setColor(Color.red); // font color = red

		g.fillRect(carX, carY, carW, carH);

                g.drawString("Sorry! You Lose :-(", 100,100);

                g.setColor(Color.black); // font color = black

            }

	}

}





    //------------------------------------------

    // BREAK / Next Class     (Finishbox Class)

    //------------------------------------------





class FinishBox{

	  private int finishX1, finishY1, finishX2, finishY2, carStartY, carHeight, carBottom,carOrginalStartY;


          public FinishBox(int finX1, int finY1, int finX2, int finY2, int carStartingYcoord, int carheigth1) {

                 finishX1 = finX1;
                 finishY1 = finY1;
                 finishX2 = finX2;
                 finishY2 = finY2;

                 carStartY = carStartingYcoord;
                 carHeight = carheigth1;

                 carOrginalStartY = carStartingYcoord;

          }



	public void reset() {

               carBottom = carOrginalStartY;
               carStartY = carOrginalStartY;

	}


       	public void check1() {   // performs step 1 of the finish box check to see if user has won

		carStartY -= 5;
                carBottom = carStartY + carHeight;

	}


       	public void check2() {   // performs step 2 of the finish box check to see if user has won

		carStartY += 5;
                carBottom = carStartY + carHeight;

	}



       
          public void display(Graphics g) {
                  
                  g.setColor(Color.blue);  // font color = blue
                  g.drawLine(finishX1,finishY1,finishX2,finishY2);  // draws the finish line
                  
                  g.setColor(Color.red);  // font color = red
                  g.drawString("Finish Line", 175,480);                  

                  g.setColor(Color.black);   // font color = black



                  if (carBottom > finishY1) {  // checks to see if user has won
                      g.setColor(Color.blue);  // font color = blue
	              g.drawString("Congratulations!!! You Win :-)", 150,200);
                  }

          }


}

