/**
 * @(#)PennyPitch.java
 * Written by Eric Tedjamulia
 * August 5, 2004
 *
 */
 
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;

public class PennyPitch extends Applet implements KeyListener, MouseListener, ActionListener
{

//----------------------------------------------------------------------------	
//Declare Variables/Objects
//----------------------------------------------------------------------------
	//Title
	private Image			title, controls;
	private int				counter;
	private String[] 		intro;
	private AudioClip		music, starwars, introMusic;
	
	//Game
	private Image dcl, dcr, dsr, dsl, dwr, dwl, dar, dal, enemy, back, booth;
	private boolean right, left, jump, attack, charge, win;
	private int throwPow, move, x, y, dir=1, level, gCount, life, stun, up;
    private LevelReader l;
    private Ground[] grnd;
    private Enemies[] e;
    private Color colorGrnd;
    private Ground portal;
    private char pos;
    private ArrayList gCoins = new ArrayList();
    private Font lFont, titleFont, smallFont, commentFont;
	private AudioClip		hit, kill, thrown;
	
	//Toss
	private Button 			randomB, tossB, all; 
	private final String[]	prizes = {"Puzzle", "Poster", "Game", "Doll"};
	private	Square[][]		board;
	private int 			winningItem, phase;
	private Image 			table, hand;
	private int 			power, addPower, coinNum;
	private boolean 		aim;
	private Coin[] 			coins = new Coin[10];
	private int 			tempX, totalPrizes;
	private int[]			numPrize = {0,0,0,0};
	
	//end
	private int 			e_x=325,e_y=100,e_x2,e_y2, e_count=14;
	private char 			e_dir;
	boolean[] 				e_b = {false};
	private Image 			space;
	private Image[]			pImg=new Image[4];
	
	//db
	private Image 			dbImage;
	private Graphics 		dbg;
	private Thread 			t = new Thread();

//----------------------------------------------------------------------------
//Initialize Variables
//----------------------------------------------------------------------------
	
	public void init() 
	{
		phase = 1;
		counter = 0;
		
		winningItem=(int)(Math.random()*4);
		
		//Images
		title = getImage(getCodeBase(), "title.JPG");
		space = getImage(getCodeBase(), "earth.GIF");
		table = getImage(getCodeBase(), "board.GIF");
		back = getImage(getCodeBase(), "backg.JPG");
		dsr = getImage(getCodeBase(), "StandRight.GIF");
		dsl = getImage(getCodeBase(), "StandLeft.GIF");
		dwr = getImage(getCodeBase(), "WalkRight.GIF");
		dwl = getImage(getCodeBase(), "WalkLeft.GIF");
		dar = getImage(getCodeBase(), "AttackR.GIF");
		dal = getImage(getCodeBase(), "AttackL.GIF");
		dcr = getImage(getCodeBase(), "ChargeR.GIF");
		dcl = getImage(getCodeBase(), "ChargeL.GIF");
		enemy = getImage(getCodeBase(), "enemy.GIF");
		booth = getImage(getCodeBase(), "booth.GIF");
		controls =  getImage(getCodeBase(), "controls.GIF");
		pImg[0] = getImage(getCodeBase(), "puzzle.GIF");
		pImg[1] = getImage(getCodeBase(), "poster.GIF");
		pImg[2] = getImage(getCodeBase(), "game.GIF");
		pImg[3] = getImage(getCodeBase(), "doll.GIF");
		
		//Sounds
		music = getAudioClip(getCodeBase(), "music.wav");
		starwars = getAudioClip(getCodeBase(), "starwars.mid");
		introMusic = getAudioClip(getCodeBase(), "HoldMe.mid");
		introMusic.loop();
		
		//Fonts
		titleFont = new Font ("Arial", Font.BOLD, 38);
		smallFont = new Font ("Arial", Font.BOLD, 18);
		commentFont = new Font("Arial", Font.PLAIN, 14);
		lFont = new Font ("Arial", Font.BOLD, 28);
		
		//int
		move = 0;
		x = 200;
		y = 300;
		life = 5;
		
		//toss
		aim = true;
		power = 0;
		addPower = 15;
		coinNum=0;
		
		//Buttons
		randomB = new Button("Random Toss");
		randomB.setBounds(57, 407, 170, 63);
		randomB.addActionListener(this);
		
		tossB = new Button("One at a time");
		tossB.setBounds(246, 407, 170, 63);
		tossB.addActionListener(this);
		
		all = new Button(); //used to clear screen
		all.setBounds(0,0,400,450);
		
		//game
		pos = 's';
		gCount = 1;
		l = new LevelReader();
		level = 0;
		switchLvl();
		
		//Initializes an empty 25 square board
		board = new Square[5][5];
		
		//Creates a Random Board
		for (int i=0; i<5; i++)
		{
			for (int j=0; j<5; j++)
			{
				board[i][j] = new Square();
			}
		}
		
		for (int i=0; i < 12; i++)
		{
			int x = (int)(Math.random()*5);
			int y = (int)(Math.random()*5);
			if (board[x][y].getName().equals("empty"))
			{
				board[x][y] = new Square(prizes[i/3]);
			}
			else
				i--;
		}
		
		//Creates an array of left and rights (for the end)
		for (int i=0; i<e_count; i++) 
		{
			boolean[] e_b2 = new boolean[2*e_b.length+1];
			for (int j=0; j<e_b.length; j++) { e_b2[j] = e_b[j]; }
			e_b2[e_b.length]=true;
			for (int j=0; j<e_b.length; j++) { e_b2[e_b2.length-j-1] = !e_b[j];}
			e_b = e_b2;
		}
		
		intro = new String[27];
		
		intro[0]="Aliens have invaded";
		intro[1]="Earth from the planet";
		intro[2]="Xalkar 7 and are ";
		intro[3]="threatening to ";
		intro[4]="destroy the Earth.";
		intro[5]="";
		intro[6]="The only thing that ";
		intro[7]="can stop the Xalkarians";
		intro[8]="from using their evil";
		intro[9]="Fractal gun to destoy ";
		intro[10]="the planet is The " + prizes[winningItem];
		intro[11]="of Ultimate Power. ";
		intro[12]="";
		intro[13]="However, the only way";
		intro[14]="of obtaining this ";
		intro[15]="" + prizes[winningItem] + " is to play ";
		intro[16]="the Legendary Game ";
		intro[17]="\"Penny Pitch\".";
		intro[18]="";
		intro[19]="As the great Penny ";
		intro[20]="Pitcher Man, you are ";
		intro[21]="Earth's only hope. ";
		intro[22]="You must work your ";
		intro[23]="way through all three ";
		intro[24]="levels, play the game ";
		intro[25]="of \"Penny Pitch\", and ";
		intro[26]="win the " + prizes[winningItem] + ".";

		
		
		addKeyListener(this);
		addMouseListener(this);
	}

	
//-----------------------------------------------------------------------------
// Paint Methods
//-----------------------------------------------------------------------------

	public void paint(Graphics g) 
	{
		switch (phase)
		{
			case 1: title(g); break;
			case 2: text(g); break;
			case 3: game(g); break;
			case 4: choice(g); break;
			case 5: randomToss(g); break;
			case 6: toss(g); break;
			case 7: results(g);break;
			case 8: end(g); break;
		}
	}
	
	//########################################	
	
	public void title(Graphics g)
	{
		counter++;
		g.drawImage(title, 0, 0, this);
		g.setColor(Color.black);
		g.setFont(new Font("Arial", Font.BOLD, 18));
		g.drawString("And the Search for the " + prizes[winningItem] + " of Ultimate Power", 30, 155);
		
		if ((counter/10)%2 == 0)
		{
			g.setColor(Color.white);
			g.fillRect(175,285,130,25);
			g.setColor(Color.red);
			g.drawString("Click to Start", 180, 305);
		}
		sleep(200);
		repaint();
	}
	
	//########################################
	
	public void text(Graphics g)
	{
		if (counter<1500)
		{
			if (g.drawImage(space, 0, 0, this))
			{
				counter+=2;
				g.setFont(titleFont);
				g.setColor(Color.yellow);
				
				for (int i=0; i<intro.length; i++)
				{
					g.drawString(intro[i], 30, (600-counter)+(40*i));
				}
				sleep(30);
				repaint();
			}
		}
		else
		{
			g.drawImage(controls, 0, 0, this);
		}
	}
	
	//########################################
	
	public void game(Graphics g)
	{
		g.drawImage(back,0,0,this);
		
		act();
		
		if (dir==1) //right
		{
			if (pos == 'a')
				g.drawImage(dar,x,y,this);
			else
			if (pos == 's')
				g.drawImage(dsr,x,y,this);
			else
			if (pos == 'c')
				g.drawImage(dcr,x,y,this);
			else
				g.drawImage(dwr,x,y, this);		
		}
		else
		if (dir==-1) //left
		{
			if (pos == 'a')
				g.drawImage(dal,x,y,this);
			else
			if (pos == 's')
				g.drawImage(dsl,x,y,this);
			else
			if (pos == 'c')
				g.drawImage(dcl,x,y,this);
			else
				g.drawImage(dwl,x,y, this);	
		}
		
		
		//draws enemies
		for (int i=0; i < e.length; i++)
		{
			if (e[i].isAlive())
			{
				e[i].act(x, y);
				g.drawImage(enemy, e[i].getX(), e[i].getY(), this);
			}
		}
		
		gCount=0;
		//Draws ground
		for (int i=0; i < grnd.length; i++)
		{
			g.setColor(colorGrnd);
			g.fill3DRect(grnd[i].getX(), grnd[i].getY(), 50, 50, true);
			if (grnd[i].getX() > 0 && grnd[i].getX() < 500)
			{
				gCount++;
			}
		}
		
		//draws coins
		for (int i = 0; i < gCoins.size(); i++)
		{
			Coin tempC = ((Coin)(gCoins.get(i)));
			g.setColor(Color.red);
			g.fillOval(tempC.getX(), tempC.getY(), 5, 5);
		}
		
		//Draws Portal
		g.setColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
		g.fill3DRect(portal.getX(),portal.getY(),50,100,true);
		
		if (stun > 0)
		{
			g.setColor(Color.red);
			g.fillRect(0,0,500,10);
			g.fillRect(0,0,10,500);
			g.fillRect(490,0,10,500);
			g.fillRect(0,490,500,10);
		}
		
		//Life
		g.setColor(Color.yellow);
		g.setFont(lFont);
		g.drawString("Life: " + life, 0, 500);
		//Level
		g.drawString("Level " + level, 400, 500);
		
		try{
			t.sleep(10 - (gCount/10));
		}
		catch(InterruptedException e) {}
		
		if (life < 1)
		{
			sleep(2000);
			phase=8;
		}
		
		repaint();
	}
	
	//########################################
	
	public void choice(Graphics g)
	{
		if (g.drawImage(booth, 0, 0, this))
		{
	      	sleep(1000);
	      	g.setColor(Color.white);
	      	g.fillOval(320,100,200,100);
	      	g.fillRect(340,150,100,50);
	      	g.setColor(Color.black);
	      	g.setFont(smallFont);
	      	g.drawString("Hello",380,160);
			sleep(1000);
	      	g.drawImage(booth, 0, 0, this);
			sleep(500);
	      	g.setColor(Color.white);
	      	g.fillOval(320,100,200,100);
	      	g.fillRect(340,150,100,50);
	      	g.setColor(Color.black);
		    g.drawString("Would you like",340,140);
		    g.drawString("to throw the ",345,160);
		    g.drawString("pennies",350,180);
		    sleep(2000);
		    g.drawImage(booth, 0, 0, this);
			sleep(500);
		    g.setColor(Color.white);
		    g.fillOval(320,100,200,100);
		    g.fillRect(340,150,100,50);
		    g.setColor(Color.black);
		    g.drawString("one at a time",355,140);
		    g.drawString("or",390,160);
		    g.drawString("randomly?",365,180);
		    add(randomB);
			add(tossB);
		}
	}
	
	//########################################
	
	public void randomToss(Graphics g)
	{
		g.setFont(commentFont);
		for(int i=0; i<10; i++)
			coins[i]=new Coin((int)(Math.random()*380)+40, (int)(Math.random()*380)+40);
		
		if (g.drawImage(table, 0, 0, this))
		{
			for (int i=0; i<10; i++)
			{
				g.setColor(new Color(128,128,64));
				g.fillOval(coins[i].getX(), coins[i].getY(), 40,40);
				g.setColor(Color.black);
				g.fillOval(coins[i].getX()+20, coins[i].getY()+20, 1,1);
			}
			for (int i=0; i<5; i++)
			{
				for (int j=0; j<5; j++)
				{
					g.setColor(Color.black);
					if (!board[i][j].getName().equals("empty"))
						g.drawString(board[i][j].getName(), (i+1)*80, (j+1)*85);
				}
			}
		}
	}
	
	//########################################
	
	public void toss(Graphics g)
	{
		g.drawImage(table, 0, 0, this);
		g.setFont(commentFont);
		
		if (coinNum >= 10)
		{
			check();
			phase=7;
			repaint();
		}
		
		for (int i=0; i<coinNum; i++)
		{
			g.setColor(new Color(128,128,64));
			g.fillOval(coins[i].getX(), coins[i].getY(), 40,40);
			g.setColor(Color.black);
			g.fillOval(coins[i].getX()+20, coins[i].getY()+20, 1,1);
		}
		
		for (int i=0; i<5; i++)
		{
			for (int j=0; j<5; j++)
			{
				g.setColor(Color.black);
				if (!board[i][j].getName().equals("empty"))
					g.drawString(board[i][j].getName(), (i+1)*80, (j+1)*85);
			}
		}
		
		if (aim) {
			g.setColor(new Color(128,128,64));
			g.fillOval(power, 470, 40,40);
		}
		else
		{
			g.setColor(Color.white);
			g.fillRect(125, 460, 250, 30);
			g.setColor(Color.red);
			g.drawString("Power", 230, 455);
			g.fillRect(125, 460, (power/2), 30);
			g.setColor(Color.black);
			g.drawRect(125, 460, 250, 40);
		}
		
		power+=addPower;
		
		if (power>500 || power<0) 
			addPower*=-1;
		
        sleep(10);
		
		repaint();
	}
	
	//########################################
	public void results(Graphics g)
	{
		if (g.drawImage(booth, 0, 0, this))
		{
			if (totalPrizes == 0)
			{
				sleep(1000);
		      	g.setColor(Color.white);
		      	g.fillOval(320,100,200,100);
		      	g.fillRect(340,150,100,50);
		      	g.setColor(Color.black);
		      	g.setFont(smallFont);
		      	g.drawString("Wow...",380,160);
				sleep(1000);
		      	g.drawImage(booth, 0, 0, this);
				sleep(500);
		      	g.setColor(Color.white);
		      	g.fillOval(320,100,200,100);
		      	g.fillRect(340,150,100,50);
		      	g.setColor(Color.black);
			    g.drawString("You didnt win",340,140);
			    g.drawString("a single prize",345,160);
			    sleep(1000);
			    g.drawImage(booth, 0, 0, this);
				sleep(500);
		      	g.setColor(Color.white);
		      	g.fillOval(277,177,20,20);
		      	g.fillOval(305,179,20,20);
		      	g.fillOval(320,100,200,100);
		      	g.fillRect(340,150,100,50);
		      	g.setColor(Color.black);
		      	g.fillOval(312,185,9,9);
		      	g.fillOval(284,184,9,9);
		      	g.fillOval(285,215,35,20);
			    g.drawString("NOW THE WORLD ",340,140);
			    g.drawString("IS DOOMED!!!!!",345,160);
			    
			}
			else
			if (totalPrizes >= 1)
			{
				sleep(500);
		      	g.setColor(Color.white);
		      	g.fillOval(320,100,200,100);
		      	g.fillRect(340,150,100,50);
		      	g.setColor(Color.black);
			    g.drawString("You won a",340,140);
			    for (int i = 0; i < 4; i++)
			    {
			    	if (numPrize[i]>=3)
				    {
				    	g.setColor(Color.black);
				    	if (i == totalPrizes && totalPrizes  != 1)
				    		g.drawString("and a",350,140);
				    	g.drawString(prizes[i],345,160);
				    
					    sleep(1000);
					    g.drawImage(booth, 0, 0, this);
					    sleep(500);
				      	g.setColor(Color.white);
				      	g.fillOval(320,100,200,100);
				      	g.fillRect(340,150,100,50);
				      	g.setColor(Color.black);
				    }
				}
				    	
			    if (numPrize[winningItem] < 3)
			    {
			    	g.drawString("But you DIDNT ",340,140);
			   		g.drawString("win the " + prizes[winningItem] + "!!",345,160);
			   		sleep(1000);
				    g.drawImage(booth, 0, 0, this);
				    sleep(500);
			      	g.setColor(Color.white);
			      	g.fillOval(320,100,200,100);
			      	g.fillRect(340,150,100,50);
			      	g.fillOval(277,177,20,20);
			      	g.fillOval(305,179,20,20);
			      	g.setColor(Color.black);
			      	g.fillOval(312,185,9,9);
			      	g.fillOval(284,184,9,9);
			      	g.fillOval(285,215,35,20);
				    g.drawString("NOW THE WORLD ",340,140);
				    g.drawString("IS DOOMED!!!!!",345,160);
				}
				else
				{
					win = true;
					g.drawString("You have saved",340,140);
			   		g.drawString("us all",345,160);
				}
			}
			
			sleep(4000);
			phase=8;
			repaint();
		}
	}
	
	//########################################
	public void end(Graphics g)
	{
		int e_numSeg = e_b.length, e_r = 2;
		e_dir = 'N';
		e_x=325; e_y=100;
		e_x2=e_x; e_y2=e_y;
		if (g.drawImage(space, 0, 0, this))
		{
			if (!win)
			{
				for (int i = 0; i < e_numSeg; i++) 
				{
					g.setColor(new Color ((i/50)%110+140, (i)%110+100, 0));
					g.drawLine(e_x,e_y,e_x2,e_y2);
					e_x=e_x2;
					e_y=e_y2;
					if (e_b[i]) { //turn right
						if (e_dir == 'N') {e_dir = 'E'; e_x2+=e_r;}
						else if (e_dir == 'E') {e_dir = 'S'; e_y2+=e_r;}
						else if (e_dir == 'S') {e_dir = 'W'; e_x2-=e_r;}
						else {e_dir = 'N'; e_y2-=e_r;}
					}
					else { //turn left
						if (e_dir == 'N') {e_dir = 'W'; e_x2-=e_r;}
						else if (e_dir == 'W') {e_dir = 'S'; e_y2+=e_r;}
						else if (e_dir == 'S') {e_dir = 'E'; e_x2+=e_r;}
						else {e_dir = 'N'; e_y2-=e_r;}				
					}
					if (i%10==0)
						sleep(1);
				}
				sleep(3000);
				g.setColor(Color.black);
				g.fillRect(0,0,500,500);
				g.setFont(titleFont);
				g.setColor(Color.red);
				g.drawString("Game Over", 140, 220);
				g.drawString("You Lose!", 145, 270);
			}
			else
			{
				counter+=2;
				if (counter<140)
				{
					g.drawImage(pImg[winningItem], counter+220, (0-counter)+220, this);
				}
				else
				if (counter<256)
				{
					g.setColor(new Color(counter, 150, 0));
					g.fillOval(285,0,counter,counter);
				}
				
				if (counter>=256)
				{
					sleep(2000);
					g.setColor(Color.black);
					g.fillRect(0,0,500,500);
					g.setFont(titleFont);
					g.setColor(Color.red);
					g.drawString("Game Over", 140, 220);
					g.drawString("You WIN!", 150, 270);
					return;
					
				}
				sleep(30);
				repaint();
			}
		}
	}
	
	
//-----------------------------------------------------------------------------
// Game Methods
//-----------------------------------------------------------------------------

    public boolean hit(int _x, int _y)
    {
    	for (int i=0; i<grnd.length; i++)
    	{
    		if (grnd[i].hit(_x,_y))
    			return true;
    	}
    	return false;
    }
    
    private void switchLvl()
    {
    	if (level == 3)
    	{
    		phase++;
    	}
    	level++;
    	y=100;
    	grnd = l.getLevel(level);
		e = l.getEnemies(level);
		colorGrnd = l.getColor(level);
		portal = l.getPortal(level);
    }
    
    public void attack(int pow)
    {
    	gCoins.add(new Coin(x,y,pow*dir));
    }
    
    public void act()
	{
		move++;
		stun--;
		
		//moves coin
		for (int i = 0; i < gCoins.size(); i++)
		{
			((Coin)(gCoins.get(i))).move();
			if (((Coin)(gCoins.get(i))).getY() > 500)
			{
				gCoins.remove(i);
				i--;
			}
		}
		
		//checks for fall
		if (y>550)
		{
			life--;
			level--;
			switchLvl();
		}
		
		//checks for hits
		for (int i = 0; i < e.length; i++)
		{
			e[i].hit(gCoins);
			
			if (e[i].getX()<=x+40 && e[i].getX()+40>=x && e[i].getY()<=y+90 && e[i].getY()+90>=y && stun < 0 && e[i].isAlive())
			{
				life--;
				stun = 100;
			}
		}
		
		//Standing pos
		if (move > 10)
		{	
			pos = 's';
			attack = false;
		}
		
		//Checks to see if in portal
		if (portal.getX() <= x+30 && y+100 >= portal.getY() && y <= portal.getY()+100)
			switchLvl(); 
				
		//moves everything right
		if (right)
		{
			if (!hit(x+3,y))
			{
				for (int i=0; i < e.length; i++)
					e[i].moveRight();
				for (int i=0; i < grnd.length; i++)
					grnd[i].moveRight();
				portal.moveRight();
			}
			if (move/10%2 == 0)
				pos = 'w';
		}
		
		//moves todos left
		if (left)
		{
			if (!hit(x-3,y))
			{
				for (int i=0; i < e.length; i++)
					e[i].moveLeft();
				for (int i=0; i < grnd.length; i++)
					grnd[i].moveLeft();
				portal.moveLeft();
			}
			if (move/10%2 == 0)
				pos = 'w';
		}
		
		//Jump
		if (jump && up > 0) 
		{
			up--;
			if(!hit(x, y-4))
				y-=4;
			else
				jump = false;
		}
		else
		{
			if (!(y>550) && !hit(x, y+4))
				y+=4;
			else
				up = 30;
		}
		
		if (charge)
		{
			throwPow++;
			pos = 'c';
			move=0;
		}
		
		if (attack)
			pos = 'a';
	}
	
//-----------------------------------------------------------------------------
// Toss Methods
//-----------------------------------------------------------------------------
	
	public void check()
	{
		for (int i=0; i<5; i++)
		{
			for (int j=0; j<5; j++)
			{
				for (int k=0; k<10; k++)
				{
					if (coins[k].getX()+20 > i*90+25 && coins[k].getX()+20 < (i+1)*90+25 && coins[k].getY()+20 > j*90+25 && coins[k].getY()+20 < (j+1)*90+25)
					{
						board[i][j].addValue();
					}
				}
			}
		}
		for (int i=0; i<5; i++)
		{
			for (int j=0; j<5; j++)
			{
				if (board[i][j].getName().equals("Puzzle") && board[i][j].getValue()>0)
					numPrize[0]++;
				if (board[i][j].getName().equals("Poster") && board[i][j].getValue()>0)
					numPrize[1]++;
				if (board[i][j].getName().equals("Game") && board[i][j].getValue()>0)
					numPrize[2]++;
				if (board[i][j].getName().equals("Doll") && board[i][j].getValue()>0)
					numPrize[3]++;				
			}
		}
		for(int i=0; i<4; i++)
		{
			if (numPrize[i]>=3)
				totalPrizes++;
		}
	}

//----------------------------------------------------------------------------
// End Methods
//----------------------------------------------------------------------------



//----------------------------------------------------------------------------
// Event Methods
//----------------------------------------------------------------------------

	public void actionPerformed(ActionEvent event)
	{
		String command = event.getActionCommand(); 
		if (command.equals("Random Toss"))
			phase=5;
		else
			phase=6;
		
		remove(randomB);
		remove(tossB);
		add(all);
		remove(all);
		repaint();
	}
		

	public void keyPressed(KeyEvent e)
	{
		if (phase == 6)
		{
			
			if (aim)
				tempX = power-addPower;
			else
			{
				coins[coinNum]=new Coin(tempX, 500-power);
				coinNum++;
			}
			aim=!aim;
		   	sleep(400);
		   	
		
		}
		
		if (phase == 3)
		{
			if (e.getKeyCode() == KeyEvent.VK_RIGHT)
			{
				right = true;
				left = false;
				dir = 1;
				pos = 'w';

			}
			if (e.getKeyCode() == KeyEvent.VK_LEFT)
			{
				right = false;
				left = true;
				dir = -1;
				pos = 'w';
			}
			if (e.getKeyCode() == KeyEvent.VK_UP)
			{
				jump = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_SPACE)
			{
				charge=true;
				move = 0;
			}
		
			if (e.getKeyCode() == KeyEvent.VK_N)
			{
				switchLvl();
			}
			repaint();
		}
	}
	
	
	public void mouseClicked(MouseEvent e)
	{
		if (phase == 1)
		{
			introMusic.stop();
			starwars.loop();
			phase++;
			counter=0;
			repaint();
		}
		else
		if (phase == 2)
		{
			starwars.stop();
			music.loop();			
			phase++;
			counter=0;
			repaint();
		}
		if (phase == 5)
		{
			check();
			phase=7;
			repaint();
		}
	}
	
	public void keyTyped(KeyEvent e){}
	public void keyReleased(KeyEvent e)
	{
		if (phase == 3)
		{
			if (e.getKeyCode() == KeyEvent.VK_UP)
			{
				jump = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_SPACE)
			{
				charge=false;
				attack = true;
				attack(throwPow);
				throwPow=0;
				pos = 'a';
			}
			if (e.getKeyCode() == KeyEvent.VK_RIGHT)
			{
				right = false;
				pos = 's';
			}
			if (e.getKeyCode() == KeyEvent.VK_LEFT)
			{
				left = false;
				pos = 's';
			}
			repaint();
		}
	}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mousePressed(MouseEvent e){}
	public void mouseReleased(MouseEvent e) {}
	
//----------------------------------------------------------------------------
// Misc Methods
//----------------------------------------------------------------------------
		
	//executes Thread sleep for a given amount of time
	public void sleep(int time)
	{
		try 
		{
        	t.sleep(time);
      	}
      	catch(InterruptedException e) {}
	}
	
	//Double Buffering
	public void update (Graphics g)
	{
		if (phase!=8 && phase!=4 && phase!=7)
		{
			// initialize buffer
			if (dbImage == null)
			{
				dbImage = createImage (this.getSize().width, this.getSize().height);
				dbg = dbImage.getGraphics ();
		
			}
			// clear screen in background
			dbg.setColor (getBackground ());
			dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
			
			// draw elements in background
			dbg.setColor (getForeground());
			paint (dbg);
			
			// draw image on the screen
			g.drawImage (dbImage, 0, 0, this);
		}
		else
		{
			g.setColor (getBackground ());
			g.fillRect (0, 0, this.getSize().width, this.getSize().height);
			paint(g);
		}
		
	}
}
