//File: WallDestroyer.java
//it creates a game which you have court
//and a racket and a ball and blocks
//which you have to clear.
//It has 5 levels.
//
//written by Ferudun Findik, Wednesday, March 20, 2002



import java.applet.*;   //imports applet library for various methods
import java.awt.*;      //imports various graphics such button
import java.lang.Math;  //imports variuos math function such cos

public class WallDestroyer extends Applet implements Runnable{
// class WallDestroyer is extenden applet and implemented runnable
	private Thread	 m_Wall_Destroyer = null;
        // a m_Wall_Destroyer Thread object is null, will call by run()

	Image offScreen;
        // image must be obtained in a platform-specific manner
	Graphics offGraphics;
        // decleares offGraphics type class Graphics

	AudioClip audioHit;	//Decleares audioHit as Audioclip
	AudioClip audioFall;	//Decleares audioFall as Audioclip
	AudioClip audioFinish;	//Decleares audioFinish as Audioclip
	Color color[];		//Declares a color array as Color class

	Point ball;			//point representing a ball
	Point racket;			//point representing a racket
	int speed = 5;		        //speed type int set to 5 unit
	int mx = (int)( speed * Math.cos(radian(45.0)));
        // sets the target mx (x coordinates)
	int my = (int)(-speed * Math.sin(radian(45.0)));
        // sets the target my (y coordinates)

	int score = 0;		// declares a score and sets to 0
	int incrementScore = 1;	// declares the increment of the score
	int restLive = 2;	// declares the rest live
	int Level = 0;		// sets level init 0
	int Level_loop = 0;	// sets the loop level
	boolean clear;		// sets a bolean clear
	boolean block[][];	// sets 2 dimensional block array type Bool


	TextField textLevel;        // declares textLevel as TextField
        TextField textPoints;        // declares textPoints as TextField
	TextField textLives;        // declares textLives as TextField


	Choice choice;          // a pop-up menu of choices


	Button start_button;    // declares a starts_button as Button

	public void init(){     //initializing
	       	setLayout(new BorderLayout());
                // Sets the layout manager BorderLayout for this container
		Panel p=new Panel();
		// sets the panel p
		textLevel=new TextField("push start!!", 8);
                // sets textLevel "push start!!" with 8 column
		textLevel.setBackground(Color.white);
                // sets textLevel to color white
		textPoints=new TextField(Integer.toString(score), 4);
                // sets textPoints to display the score
		textPoints.setBackground(Color.white);
		textLives=new TextField(Integer.toString(restLive), 2);
                // sets textLives to dosplay the restLive
		textLives.setBackground(Color.white);
		p.add(textLevel);
		// adds textLevel to panel p
		p.add(textPoints);
		// adds textPoints to panel p
		p.add(textLives);
		// adds textLives to panel p
		choice = new Choice();
                // choice is class Choice
		choice.setBackground(Color.cyan);
                // choice backgroundcolor to cyan
		choice.addItem("easy");   // adds "easy" to choice
		choice.addItem("normal"); // adds "normal" to choice
                choice.addItem("hard");   // adds "hard" to choice
		p.add(choice); //adds choice to panel p
		start_button = new Button("Start");
                // start_button is class Button and is set "Start"
		start_button.setBackground(Color.gray);
		p.add(start_button);
		add("South",p);   //adds panel p

		resize(300,350);  //resizes to 300 to 350
		setBackground(Color.black); // sets background to black

		audioHit = getAudioClip(getDocumentBase(), "hit.au");
                // sets "hit.au" as audioHit
		audioFall = getAudioClip(getDocumentBase(), "fall.au");
                // sets "fall.au" as audioFall
		audioFinish = getAudioClip(getDocumentBase(), "finish.au");
                // sets "finish.au" as audioFinish

		Dimension d=size();
                // d as Class Dimension size
		offScreen=createImage(d.width,d.height);
                // offScreen assigned as size d's width and height
		offGraphics=offScreen.getGraphics();
                // Creates graphics context for drawing to an offscreen image

		block = new boolean[10][6];   //creates a 2D block array(bool)
		color = new Color[6];         //creates a color array
		color[0] = Color.red;         //first element is red
		color[1] = Color.blue;        //second element is blue
		color[2] = Color.yellow;      //third element is yellow
		color[3] = Color.green;       //fourth element is green
		color[4] = Color.cyan;        //fifth element is cyan
		color[5] = Color.pink;        //sixth element is pink
		Level_set(); // calls Level_sets function
	}

	public void Level_set(){
        // sets each levels begining
		ball = new Point(145, 288);
                // sets the pall position to 145 to 288
		racket = new Point(135, 305);
                // sets the racket position to 135 to 305
                mx = (int) ( speed * Math.cos(radian(45.0)));
                // sets the destination of ball x coordinate
		my = (int) (-speed * Math.sin(radian(45.0)));
                // sets the destination of ball y coordinate

                // sets each level for blocks
		for(int i = 0; i < 6; i++)
		{
			for(int j = 0; j < 10; j++)
			{
				block[j][i] = true;
			}
		}// first level is all blocks

		switch (Level_loop)
		{
			case 0 : break;
			case 1 :
			{
				for (int i = 0; i < 6; i++)
				{
					for (int j = 0; j < 10; j += 3)
					{
						block[j][i] = false;
					}
				}
				break;
			}// second level each 3rd column is free
			case 2 :
			{
				for (int i = 0; i < 6; i += 2)
				{
					for (int j = 0; j < 10; j += 3)
					{
						block[j][i] = false;
					}
				}
				break;
			}// third level
			case 3 :
			{
				for (int i = 0; i < 6; i += 2)
				{
					for (int j = 0; j < 10; j += 4)
					{
						block[j][i] = false;
					}
				}
				break;
			}// fourth level
			case 4 :
			{
				for (int i = 0; i < 6; i += 2)
				{
					for (int j = 0; j < 10; j += 2)
					{
						block[j][i] = false;
					}
				}
				break;
			}// fifth level
		}
	} // end of functtion set level

	public void destroy(){// required through the class library
	}

	public void stop(){
        // required through the class library
		if (m_Wall_Destroyer != null)
		{
			m_Wall_Destroyer.stop();
			m_Wall_Destroyer = null;
		}
	}

	public void paint(Graphics g){
        // paints the graphics
		Dimension d=size();
		offGraphics.setColor(getBackground());
		offGraphics.fillRect(0, 0, d.width, d.height);
		//shows the blocks with different color for each coloumn
		for (int i = 0; i < 6; i++)
		{
			offGraphics.setColor(color[i]);
                        // set i th colomun to i th color
			for (int j = 0; j < 10; j++)
			{
                            if (block[j][i])
                            // block of [j][i] is true
                              offGraphics.fillRect((j*30)+1,(i*16)+30, 28, 8);
                              // paint a rectangle 28 times 8 pixel
			}
		}

		offGraphics.setColor(Color.magenta);
		offGraphics.fillOval(ball.x, ball.y, 8, 8);
                //paint ball in the defined point color magenta
		offGraphics.setColor(Color.blue);
		offGraphics.fillRect(racket.x, racket.y, 35, 6);
                // paint racket in the defined point color blue

		g.drawImage(offScreen, 0, 0, null);
                // draw the g(whole)
	}

	public void update(Graphics g){
        // update with paint
		paint(g);
	}

	public void run(){ // run function
		audioHit.play();
		audioHit.stop();
		audioFall.play();
		audioFall.stop();
		audioFinish.play();
		audioFinish.stop();
                // to load the audio files
		while(true)
		{
                    move();
                    //call the move function
		    repaint();
		    //call the repaint function
		    if (ball.y > 330)
                    {// if the ball out field's bottom
			if(restLive == 0)
			{// if the restLive is 0
                            textLevel.setText("Game over!");
                            // set textLevel "Game over!"
			    audioFall.play();
                            // paly audioFall
			    break;
			}

			else
			{// else
			    restLive--;
                            // decrease restlive by one
                            textLevel.setText("miss!!");
                            //write "miss!!" on textLevel
			    textLives.setText(Integer.toString(restLive));
                            //write the restlive on textLives
			    audioFall.play();
                            // play audioFall
			    ball.y = 300;
                            //put the ball on y 300
			    mx = (int)( speed * Math.cos(radian(45.0)));
			    my = (int)(-speed * Math.sin(radian(45.0)));

                            try
			    {
				Thread.sleep(1000);
			    }// wait one sec
			    catch(InterruptedException e){}
			    textLevel.setText("click mouse");
                            // write "click mouse" on textLevel
			    m_Wall_Destroyer.suspend();
                            // suspend the thread
                        }
                    }

                    textPoints.setText(Integer.toString(score));
                    //
                    if (clear)
                    {//if clear is true
		        textLevel.setText(Level + "Level clear!");
                        // "Level clear" textLevel
                      	audioFinish.play();
                        //play audioFinish
                      	if (Level % 2 == 0) speed++;
                        //even numbered level the increase the speed by one
                            if (speed > 15) speed = 15;
                            // speed is bigger the 15 the set the speed to 15
                        Level++;      //increase the level by one
			Level_loop++; //increase the level_loop by one
			if (Level_loop == 5) Level_loop = 0;
                        // leveel_loop is 5 then level_loop is 0
		            if (restLive < 5) restLive++;
                            // restLive is smaller the 5 then increase it
                        Level_set(); //set the level
                        textLives.setText(Integer.toString(restLive));

			try
			{
			    Thread.sleep(1000);
			}
			catch (InterruptedException e){}

			textLevel.setText("click mouse");
			m_Wall_Destroyer.suspend();
		    }
		    clear = false;
                    //set the clear to false
                    try
                    {
		        Thread.sleep(20);
		    }
			catch (InterruptedException e){}
		}
		m_Wall_Destroyer = null;
	}

	public void move(){// fuction to move
            if (ball.x>racket.x - 8 &&
                ball.x racket.y - 8 &&
                ball.y < racket.y + 6)
            {// if the balls hits the racket
		int tempx = ball.x + 4;
		if (tempx < racket.x + 5)
                {
		    mx = (int)(-speed * Math.cos(radian(30.0)));
		    my = (int)(-speed * Math.sin(radian(30.0)));
                }// set destination to 30 degree
		else if (tempx < racket.x + 10)
		{// if it 10 units right
                    mx = (int)(-speed * Math.cos(radian(45.0)));
		    my = (int)(-speed * Math.sin(radian(45.0)));
		}// then 45 degree
                else if (tempx < racket.x + 15)
		{// if it 15 right
                    mx = (int)(-speed * Math.cos(radian(30.0)));
		    my = (int)(-speed * Math.sin(radian(30.0)));
		}//then 30 degree
                else if (tempx < racket.x + 20)
		{// if in teh middle
                    mx = (int)(-speed * Math.cos(radian(90.0)));
		    my = (int)(-speed * Math.sin(radian(90.0)));
		}// straigth back
                else if (tempx < racket.x + 25)
		{
		    mx = (int)( speed * Math.cos(radian(60.0)));
		    my = (int)(-speed * Math.sin(radian(60.0)));
		}// 60 degrees
                else if (tempx < racket.x + 30)
		{
		    mx = (int)( speed * Math.cos(radian(45.0)));
		    my = (int)(-speed * Math.sin(radian(45.0)));
                }// 45 degrees
                else
		{
		    mx = (int)( speed * Math.cos(radian(30.0)));
		    my = (int)(-speed * Math.sin(radian(30.0)));
		}// else 30 degrees

                audioHit.play(); // play the audioHit
            }//

            ball.translate(mx, my);
            //move the ball to the mx, my coordinates
	    if (ball.x < 0)
            {// if the ball hits the right wall
		ball.x = 0;
		mx = -mx;
                audioHit.play();
            }

            if (ball.x > 300)
	    {// if the ball hits the left wall
            	ball.x = 300;
		mx = -mx;
                audioHit.play();
            }
            if (ball.y < 0)
	    {// if the ball hits the upper wall
            	ball.y = 0;
		my = -my;
                audioHit.play();
            }
            clear = true;
	    for (int i = 0; i < 6; i++)
	    {
                for (int j = 0; j < 10; j++)
		{
                    if (block[j][i] &&
                        ball.x > (j*30)-3 &&
                        ball.x < ((j+1)*30)-5 &&
                        ball.y > (i*16)+21 &&
                        ball.y < ((i+1)*16)+23)
                    {// if the ball hits one of the bloacks
                        my = -my;
			block[j][i] = false;
                        // set the hitted block false
                        score += incrementScore + Level;
                        // score is score plus incrementScore plus Level
                        audioHit.play();
                    }
                    if (block[j][i]) clear = false;
                }
            }
	}//end of function move

	public boolean mouseMove(Event e,int xx,int yy){
        // mouseMove function
            racket.x = xx;    //racket's x coordinates are same as mouse
	    racket.y = yy;    //racket's y coordinates are same as mouse
	    if (racket.x < -20) racket.x = -20; // racket's west border is -20
            if (racket.x > 280) racket.x = 280; // racket's east border is 280
            if (racket.y < 200) racket.y = 200; // racket's north border is 200
            if (racket.y > 305) racket.y = 305; // racket's south border is 305
            repaint(); //repaint function is called during mouseMove
            return true; // mouse is moving
        }// END OF MOUSEMOVE function

	public boolean mouseDown(Event e, int x, int y){
        // mouse is clicked on the object
            m_Wall_Destroyer.resume(); // resume the thread
            textLevel.setText("Level" + Level);
            return true;
	}

	public boolean action(Event e,Object o){
        // if an action appears
            if (e.target instanceof Button)
            {
                if ("Start".equals(o))
                {// if is the beginning
                    if (m_Wall_Destroyer == null)
                    {// if the thread is equal to null
                        score = 0;      //set score to 0
                        Level = 0;      //set Level to 0
                        Level_loop = 0; //set Level_loop to 0
                        restLive = 2;   //restlive to 2
                        textLevel.setText("Level 0");
                        Level_set();    //call Level_set
                        m_Wall_Destroyer = new Thread(this);
                        //m_Wall_Destroyer is a new Thread
                        m_Wall_Destroyer.start();//start thread
                    }
                }
                return true;
            }

            if (e.target instanceof Choice)
            {// if the cohoice is in action
                if (m_Wall_Destroyer == null)
		{// if the thread is null
		    if ("easy".equals((String)o))
		    {// if it is choosen 'easy'
		        incrementScore = 1; //incrementScore is 1
			speed = 5;          //speed is 5
                    }
                    if ("normal".equals((String)o))
                    {// if 'normal'
			incrementScore = 3; //incrementScore is 3
			speed = 7;          //speed is 7
                    }
                    if ("hard".equals((String)o))
                    {// if 'hard'
                        incrementScore = 5; //incrementScore is 5
			speed = 10;         //speed is 10
                    }
                    return true;
                }
            }
	    return false;
	}//end of action function

	//
	private double radian(double degree){
        //calculates degree to radian
		return (degree * 3.14159 / 180.0);
	}
}


    Source: geocities.com/ferudun/text

               ( geocities.com/ferudun)