<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/**
 * Esta clase es la de Background, es la clase principal
 * y aqui se agrega toda la interfaz. Tambien se usa
 * la de Nivel para saber donde va a crear y agregar
 * todos los objetos.
 * @author ByteCode
 * @version Beta
 */

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import java.applet.*;
import java.net.URL;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

public class Background extends Applet implements ActionListener, KeyListener{
	private static final long serialVersionUID = 1L;
	
	Image pantalla;
	Image instruccionesp;
	Image loadGamep;
	Image playNowp;
	Image highScoresp;
	Image levelcomplete;
	Image levelcompleteHS;
	Image barra;
	Image sakura;
	Image mikau;
	Image rainer;
	Image bingo;
	Image personaje;
	Image objeto;
	Image fondo;
	Image border;
	Image fondoD;
	Image extra;
	
	AudioClip punch;
	AudioClip lcomplete;
	AudioClip intro;
	AudioClip au;
	
	private Image dbImage; 
	private Graphics dbg; 
	
	TextField levelHS;
	TextField name;
	JButton levelSelect;
	JButton playNow;
	JButton instructions;
	JButton loadlevel;
	JButton quitGame;
	JButton highScores;
	JButton qmarkboton;
	JButton ninaboton;
	JButton ninoboton;
	JButton perroboton;
	JButton pandaboton;
	JButton back;
	JButton startgame;
	JButton retry;
	JButton next;
	ImageIcon playNowi;
	ImageIcon levelS;
	ImageIcon inst;
	ImageIcon lole;
	ImageIcon hisc;
	ImageIcon quit;
	ImageIcon backi;
	ImageIcon ninab;
	ImageIcon ninob;
	ImageIcon pandab;
	ImageIcon startgamei;
	ImageIcon nexti; 
	ImageIcon retryi;
	ImageIcon perrob;
	ImageIcon qmark;
	static String imagen[]={"0.gif","1.gif","2.gif","3.gif", "4.gif","5.gif", "6.gif", "7.gif", "8.gif","9.gif","10.gif",
	"11.gif", "12.gif", "13.gif", "14.gif", "15.gif","16.gif", "17.gif","18.gif", "19.gif", "20.gif", "21.gif", "22.gif",
	"23.gif", "24.gif", "25.gif", "26.gif", "27.gif","28.gif", "29.gif","30.gif"};
	static int valor = 0;
	static int valoranterior;
	static int nivel = 1;
	static int matriz[][] = new int[17][25];
	static int arrX[] = new int[50];
	static int arrY[] = new int[50];
	static int cont = 0;
	static boolean d = false;
	static boolean f = true;
	static boolean h = false;
	static String[][] records = new String[22][5];
	boolean movimientou=false;
	boolean movimientod=false;
	boolean movimientor=false;
	boolean movimientol=false;
	int contmovimientou=0;
	int contmovimientod=0;
	int contmovimientor=0;
	int contmovimientol=0;
	LinkedList lista;
	
	/**
  	 * El metodo paint, en donde se van 
  	 * cambiando todas las pantallas dependiendo
  	 * de lo que vaya escogiendo el usuario
  	 * @param Graphics g 
  	 */
	public void paint(Graphics g){
		switch(valor){
		case 0: {g.drawImage(pantalla, 0,0 , this); break;}
		case 1: {g.drawImage(playNowp, 0,0 , this); break;}
		case 2: {g.drawImage(instruccionesp, 0,0 , this); break;}
		case 3: {g.drawImage(loadGamep, 0,0 , this); break;}
		case 4: {g.drawImage(highScoresp, 0,0 , this); despliegaRecords(g); break;}
		case 5: {g.drawImage(rainer, 0,0 , this); break;}
		case 6: {g.drawImage(sakura, 0,0 , this); break;}
		case 7: {g.drawImage(mikau, 0,0 , this); break;}
		case 8: {g.drawImage(bingo, 0,0 , this); break;}
		case 9: {g.drawImage(barra, 0,0 , this); actualizaPantalla(g, valor); break;}
		case 10: {g.drawImage(barra, 0,0 , this); actualizaPantalla(g, valor); break;}
		case 11: {g.drawImage(barra, 0,0 , this); actualizaPantalla(g, valor); break;}
		case 12: {g.drawImage(barra, 0,0 , this); actualizaPantalla(g, valor); break;}
		case 13: {
			if(verificarHS() == false){g.drawImage(levelcomplete, 0, 0, this);}
			else{g.drawImage(levelcompleteHS, 0, 0, this);}
			despliegaRecordsNivel(g);}
		}}
	
      
	
	  public void stop(){
		  destroy();
		 
	  }
	
	/**
  	 * Este metodo es cuando termina el usuario
  	 * algun nivel. Se ejecutan sonidos donde se  
  	 * muestra que gano el juego y el 
  	 * "Score" del usuario. 
  	 */
	public void pantallaLevelComplete(){
		au.stop();
		removeAll();
		add(retry); retry.setLocation(190,406); retry.setSize(360,31); retry.setVisible(true);
		add(next); next.setLocation(190,457); next.setSize(360,31); next.setVisible(true);
		if(verificarHS()==true)
			add(name); name.setLocation(565, 344); name.setSize(80,20); name.setVisible(true);
		lcomplete=getAudioClip(getDocumentBase(),"BattleFinish.mid");
	    lcomplete.play();
	    valoranterior = valor;
		valor = 13;
		repaint();
	}
	
	public void despliegaRecordsNivel(Graphics g){
		g.setFont(new Font("Bradley Hand ITC",Font.BOLD, 20));
		g.setColor(new Color(155, 255, 255));
		for(int i = 0; i&lt;5; i++){
		String s = records[nivel-1][i];
		StringTokenizer st = new StringTokenizer(s, "*");
		g.drawString(st.nextToken()+ " by player "+st.nextToken(), 100, 225 + 30* i);}
		g.drawString(""+cont, 595, 184);}

	
	public void despliegaRecords(Graphics g){
		g.setFont(new Font("Bradley Hand ITC",Font.BOLD, 20));
		g.setColor(new Color(155, 255, 255));
		int m;
		try{
			Integer.parseInt(levelHS.getText());
		}catch(NumberFormatException nfe){g.drawString("That level does not exist! ",310, 180); return;}
		if((Integer.parseInt(levelHS.getText())&lt;1)||(Integer.parseInt(levelHS.getText())&gt;22)){
			g.drawString("That level does not exist! ",310, 180); 
		}
		else{
		m = Integer.parseInt(levelHS.getText());
		for(int i = 0; i&lt;5; i++){
	    String s = records[m-1][i];
		StringTokenizer st = new StringTokenizer(s, "*");
		g.drawString(st.nextToken()+ " by player "+st.nextToken(), 310, 180 + 42 * i);}}
	}
	
	public boolean verificarHS(){
		String s = records[nivel-1][4];
		StringTokenizer st = new StringTokenizer(s,"*");
		if(Integer.parseInt(st.nextToken())&gt;cont){
			if(h==false){
			records[nivel-1][4]=cont+"*???";
			records[nivel-1] = bubbleSort();
			h = true;}
			return true;}
		return false;
	}
	
	public String[] bubbleSort(){
		String temp;
		String s; 
		String z;
		StringTokenizer st;
		StringTokenizer zt;
		for(int i = 0; i&lt;4; i++){
			for(int j = 0; j&lt;4-i; j++){
				 s = records[nivel-1][j];
				 z = records[nivel-1][j+1];
				 st = new StringTokenizer(s,"*");
				 zt = new StringTokenizer(z,"*");
				if(Integer.parseInt(st.nextToken())&gt;Integer.parseInt(zt.nextToken())){
					temp = records[nivel-1][j];
					records[nivel-1][j] = records[nivel-1][j+1];
					records[nivel-1][j+1] = temp;
				}}}
		return records[nivel-1];}
	
	public void actualizaArchivo(String name){
		String s;
		String z;
		StringTokenizer st;
		for(int j = 0; j&lt;5; j++){
			s = records[nivel-1][j];
			st = new StringTokenizer(s,"*");
			z = st.nextToken();
			if(st.nextToken().equals("???"))
				records[nivel-1][j]=z+"*"+name;
		}
	}
	
	/**
  	 * Este metodo declara todos los botones, sonidos,
  	 * pantallas, imagenes, etc. Y los agrega respectivamente.
  	 * El metodo init es lo que va haciendo toda la interfaz
  	 * grafica agregando los objetos y definiendo el tamaño de la
  	 * pantalla. 
  	 */
     public void start(){ 
    	punch=getAudioClip(getDocumentBase(),"punch.wav");
    	intro=getAudioClip(getDocumentBase(),"TitleScreen.mid");
    	lista=new LinkedList();
    	intro.loop();
		setSize(743,553);
		matriz = Nivel.generaNiv(1);
		pantalla = getImage( getCodeBase(), "asobus.gif" );
		instruccionesp = getImage( getCodeBase(), "instructions.gif" );
		loadGamep = getImage( getCodeBase(), "loadlevel.gif" );
		playNowp = getImage( getCodeBase(), "characterselect.gif" );
		highScoresp = getImage( getCodeBase(), "highscoresp.gif" );
		levelcomplete = getImage(getCodeBase(), "levelcomplete.gif");
		levelcompleteHS = getImage(getCodeBase(), "levelcompleteHS.gif");
		barra = getImage(getCodeBase(), "rainertable.gif");
		sakura = getImage( getCodeBase(), "sakura.gif" );
		mikau = getImage( getCodeBase(), "mikau.gif" );
		bingo = getImage( getCodeBase(), "bingo.gif" );
		rainer = getImage( getCodeBase(), "rainer.gif" );
		
		playNowi=new ImageIcon(getURL(".\\Images\\"+"playnow.gif"));
		levelS = new ImageIcon(getURL(".\\Images\\"+"levelS.gif"));
		inst=new ImageIcon(getURL(".\\Images\\"+"inb.gif"));
		lole=new ImageIcon(getURL(".\\Images\\"+"loadlevelb.gif"));
		hisc=new ImageIcon(getURL(".\\Images\\"+"hi.gif"));
		quit=new ImageIcon(getURL(".\\Images\\"+"quitgame.gif"));
		backi=new ImageIcon(getURL(".\\Images\\"+"back.gif"));
		startgamei=new ImageIcon(getURL(".\\Images\\"+"startgame.gif"));
		nexti = new ImageIcon(getURL(".\\Images\\"+"next.gif"));
		retryi = new ImageIcon(getURL(".\\Images\\"+"retry.gif"));
		
		
		ninab=new ImageIcon(getURL(".\\Images\\"+"botonNina.gif"));
		ninob=new ImageIcon (getURL(".\\Images\\"+"botonCab.gif"));
		pandab=new ImageIcon (getURL(".\\Images\\"+"botonPanda.gif"));
		perrob=new ImageIcon (getURL(".\\Images\\"+"botonVeemon.gif"));
		qmark = new ImageIcon(getURL(".\\Images\\"+"botonQmark.gif"));
		
		ninoboton=new JButton(ninob);
		ninaboton=new JButton(ninab);
		pandaboton=new JButton(pandab);
		perroboton=new JButton(perrob);
		qmarkboton= new JButton(qmark);
		
		playNow=new JButton (playNowi);
		levelSelect = new JButton(levelS);
		instructions=new JButton (inst);
		loadlevel=new JButton (lole);
		quitGame=new JButton (quit);
		highScores=new JButton (hisc);
		back=new JButton (backi);
		startgame=new JButton (startgamei);
		next = new JButton(nexti);
		retry = new JButton(retryi);
		levelSelect = new JButton(levelS);
		
		levelHS = new TextField();
		name = new TextField(8);
		
		setLayout(null);
		add(playNow);
		
		playNow.setLocation(198,156);
		playNow.setSize(354,31);
		playNow.setVisible(true);
		add(instructions);
		
		instructions.setLocation(198,214);
		instructions.setSize(354,31);
		instructions.setVisible(true);
		add(quitGame);
		
		quitGame.setLocation(198,382);
		quitGame.setSize(354,31);
		quitGame.setVisible(true);
		
		add(loadlevel);
		loadlevel.setLocation(198,272);
		loadlevel.setSize(354,31);
		loadlevel.setVisible(true);
	
		add(highScores);
		highScores.setLocation(198,330);
		highScores.setSize(354,31);
		highScores.setVisible(true);

		playNow.addActionListener(this);
		instructions.addActionListener(this);
		loadlevel.addActionListener(this);
		quitGame.addActionListener(this);
		highScores.addActionListener(this);
		back.addActionListener(this);
		startgame.addActionListener(this);
		ninoboton.addActionListener(this);
		ninaboton.addActionListener(this);
		pandaboton.addActionListener(this);
		perroboton.addActionListener(this);
		qmarkboton.addActionListener(this);
		next.addActionListener(this);
		retry.addActionListener(this);
		levelSelect.addActionListener(this);
		 
		if(d==false){
		records = Nivel.generaRecords();
		addKeyListener(this);
    	setFocusable(true); } 
	}
     
     protected URL getURL(String filename) {
 	    URL codeBase = this.getCodeBase();
 	    URL url = null;

 	    try {
 	        url = new URL(codeBase, filename);
 	    } catch (java.net.MalformedURLException e) {
 	        System.out.println("No se pudo cargar la imagen");
 	        return null;
 	    }
 	    return url;
 	}
    
     /**
      * Método que actualiza las pantallas dependiendo del personaje con el que se escoja
      * jugar dentro del juego, de manera que despliegue las imágenes y gráficas correspondientes
      * al personaje elegido. 
      * @param g
      * @param select
      */
     public void actualizaPantalla(Graphics g, int select){
    	int h = 3;
    	int w = 3;
    	switch(select){
    	case 9:{personaje = getImage(getDocumentBase(), imagen[1]);
     			objeto = getImage(getDocumentBase(), imagen[5]); 
     			fondo = getImage(getDocumentBase(), imagen[25]);
     			border = getImage(getDocumentBase(), imagen[7]);
     			fondoD = getImage(getDocumentBase(), imagen[26]);
     			switch(nivel){
     			case 1: extra = getImage(getDocumentBase(), imagen[13]); h = 2; w = 2; break;
     			case 2: extra = getImage(getDocumentBase(), imagen[14]); break;
     			case 3: extra = getImage(getDocumentBase(), imagen[12]); break;
     			case 4: extra = getImage(getDocumentBase(), imagen[15]); break;
     			case 5: extra = getImage(getDocumentBase(), imagen[14]); break;
     			case 6: extra = getImage(getDocumentBase(), imagen[13]); h = 2; w = 2; break;
     			case 7: extra = getImage(getDocumentBase(), imagen[15]); break;
     			case 8: extra = getImage(getDocumentBase(), imagen[12]); break;
     			case 9: extra = getImage(getDocumentBase(), imagen[13]); h = 2; w = 2; break;
     			case 10: extra = getImage(getDocumentBase(), imagen[14]); break;
     			case 11: extra = getImage(getDocumentBase(), imagen[12]); break;
     			case 12: extra = getImage(getDocumentBase(), imagen[15]); break;
     			case 13: extra = getImage(getDocumentBase(), imagen[14]); break;
     			case 14: extra = getImage(getDocumentBase(), imagen[13]); h = 2; w = 2; break;
     			case 15: extra = getImage(getDocumentBase(), imagen[15]); break;
     			case 16: extra = getImage(getDocumentBase(), imagen[12]); break;
     			case 17: extra = getImage(getDocumentBase(), imagen[12]); break;
     			case 18: extra = getImage(getDocumentBase(), imagen[15]); break;
     			case 19: extra = getImage(getDocumentBase(), imagen[14]); break;
     			case 20: extra = getImage(getDocumentBase(), imagen[13]); h = 2; w = 2; break;
     			case 21: extra = getImage(getDocumentBase(), imagen[15]); break;
     			case 22: extra = getImage(getDocumentBase(), imagen[12]); break;
     			}
     			break;}
    	case 10:{personaje = getImage(getDocumentBase(), imagen[0]);
 				objeto = getImage(getDocumentBase(), imagen[6]); 
 				fondo = getImage(getDocumentBase(), imagen[23]);
 				border = getImage(getDocumentBase(), imagen[16]);
 				fondoD = getImage(getDocumentBase(), imagen[24]);
 				switch(nivel){
     			case 1: extra = getImage(getDocumentBase(), imagen[17]); break;
     			case 2: extra = getImage(getDocumentBase(), imagen[18]); break;
     			case 3: extra = getImage(getDocumentBase(), imagen[21]); break;
     			case 4: extra = getImage(getDocumentBase(), imagen[22]); break;
     			case 5: extra = getImage(getDocumentBase(), imagen[17]); break;
     			case 6: extra = getImage(getDocumentBase(), imagen[21]); break;
     			case 7: extra = getImage(getDocumentBase(), imagen[18]); break;
     			case 8: extra = getImage(getDocumentBase(), imagen[22]); break;
     			case 9: extra = getImage(getDocumentBase(), imagen[17]); break;
     			case 10: extra = getImage(getDocumentBase(), imagen[18]); break;
     			case 11: extra = getImage(getDocumentBase(), imagen[21]); break;
     			case 12: extra = getImage(getDocumentBase(), imagen[22]); break;
     			case 13: extra = getImage(getDocumentBase(), imagen[17]); break;
     			case 14: extra = getImage(getDocumentBase(), imagen[21]); break;
     			case 15: extra = getImage(getDocumentBase(), imagen[18]); break;
     			case 16: extra = getImage(getDocumentBase(), imagen[22]); break;
     			case 17: extra = getImage(getDocumentBase(), imagen[21]); break;
     			case 18: extra = getImage(getDocumentBase(), imagen[22]); break;
     			case 19: extra = getImage(getDocumentBase(), imagen[17]); break;
     			case 20: extra = getImage(getDocumentBase(), imagen[21]); break;
     			case 21: extra = getImage(getDocumentBase(), imagen[18]); break;
     			case 22: extra = getImage(getDocumentBase(), imagen[22]); break;
     			}
 				break;}
    	case 11:{personaje = getImage(getDocumentBase(), imagen[3]);
 				objeto = getImage(getDocumentBase(), imagen[7]); 
 				fondo = getImage(getDocumentBase(), imagen[27]);
 				border = getImage(getDocumentBase(), imagen[22]);
 				fondoD = getImage(getDocumentBase(), imagen[28]);
 				switch(nivel){
     			case 1: extra = getImage(getDocumentBase(), imagen[22]); h = 4; w = 4; break;
     			case 2: extra = getImage(getDocumentBase(), imagen[18]); break;
     			case 3: extra = getImage(getDocumentBase(), imagen[17]); break;
     			case 4: extra = getImage(getDocumentBase(), imagen[21]); break;
     			case 5: extra = getImage(getDocumentBase(), imagen[17]); break;
     			case 6: extra = getImage(getDocumentBase(), imagen[21]); break;
     			case 7: extra = getImage(getDocumentBase(), imagen[18]); break;
     			case 8: extra = getImage(getDocumentBase(), imagen[22]); h = 4; w = 4; break;
     			case 9: extra = getImage(getDocumentBase(), imagen[22]); h = 4; w = 4; break;
     			case 10: extra = getImage(getDocumentBase(), imagen[18]); break;
     			case 11: extra = getImage(getDocumentBase(), imagen[17]); break;
     			case 12: extra = getImage(getDocumentBase(), imagen[21]); break;
     			case 13: extra = getImage(getDocumentBase(), imagen[17]); break;
     			case 14: extra = getImage(getDocumentBase(), imagen[21]); break;
     			case 15: extra = getImage(getDocumentBase(), imagen[18]); break;
     			case 16: extra = getImage(getDocumentBase(), imagen[22]); h = 4; w = 4; break;
     			case 17: extra = getImage(getDocumentBase(), imagen[22]); h = 4; w = 4; break;
     			case 18: extra = getImage(getDocumentBase(), imagen[18]); break;
     			case 19: extra = getImage(getDocumentBase(), imagen[17]); break;
     			case 20: extra = getImage(getDocumentBase(), imagen[21]); break;
     			case 21: extra = getImage(getDocumentBase(), imagen[17]); break;
     			case 22: extra = getImage(getDocumentBase(), imagen[21]); break;
     			}
 				break;}
    	case 12:{personaje = getImage(getDocumentBase(), imagen[2]);
 				objeto = getImage(getDocumentBase(), imagen[4]); 
 				fondo = getImage(getDocumentBase(), imagen[29]);
 				border = getImage(getDocumentBase(), imagen[9]);
 				fondoD = getImage(getDocumentBase(), imagen[30]);
 				switch(nivel){
     			case 1: extra = getImage(getDocumentBase(), imagen[19]); h = 2; w = 3; break;
     			case 2: extra = getImage(getDocumentBase(), imagen[22]); break;
     			case 3: extra = getImage(getDocumentBase(), imagen[20]); break;
     			case 4: extra = getImage(getDocumentBase(), imagen[8]);  h= 2; w = 2; break;
     			case 5: extra = getImage(getDocumentBase(), imagen[22]); break;
     			case 6: extra = getImage(getDocumentBase(), imagen[8]); h = 2; w = 2; break;
     			case 7: extra = getImage(getDocumentBase(), imagen[19]); h = 2; w = 3; break;
     			case 8: extra = getImage(getDocumentBase(), imagen[20]); break;
     			case 9: extra = getImage(getDocumentBase(), imagen[19]); h = 2; w = 3; break;
     			case 10: extra = getImage(getDocumentBase(), imagen[22]); break;
     			case 11: extra = getImage(getDocumentBase(), imagen[20]); break;
     			case 12: extra = getImage(getDocumentBase(), imagen[8]);  h= 2; w = 2; break;
     			case 13: extra = getImage(getDocumentBase(), imagen[22]); break;
     			case 14: extra = getImage(getDocumentBase(), imagen[8]); h = 2; w = 2; break;
     			case 15: extra = getImage(getDocumentBase(), imagen[19]); h = 2; w = 3; break;
     			case 16: extra = getImage(getDocumentBase(), imagen[20]); break;
     			case 17: extra = getImage(getDocumentBase(), imagen[20]); break;
     			case 18: extra = getImage(getDocumentBase(), imagen[8]);  h= 2; w = 2; break;
     			case 19: extra = getImage(getDocumentBase(), imagen[22]); break;
     			case 20: extra = getImage(getDocumentBase(), imagen[8]); h = 2; w = 2; break;
     			case 21: extra = getImage(getDocumentBase(), imagen[19]); h = 2; w = 3; break;
     			case 22: extra = getImage(getDocumentBase(), imagen[20]); break;
     			}
 				break;}
    	}
 		for(int i = 0; i&lt;17; i++){ //fila
 			for(int j = 0; j&lt;25; j++){ //Columna
 				switch(matriz[i][j]){
 				case 1: {g.drawImage(fondo,(j*(743/30)),(i*(553/20)),(743/30),(553/20),this); 
 					g.drawImage(personaje,(j*(743/30)),(i*(553/20)),(743/30),(553/20),this); break;}
 				case 2: {g.drawImage(fondo,(j*(743/30)),(i*(553/20)),(743/30),(553/20),this);  
 					g.drawImage(objeto,(j*(743/30)),(i*(553/20)),(743/30),(553/20),this); break;}
 				case 3: {g.drawImage(fondo,(j*(743/30)),(i*(553/20)),(743/30),(553/20),this);  
 					g.drawImage(border,(j*(743/30)),(i*(553/20)),(743/30), (553/20), this); break;}
 				case 4: {g.drawImage(fondoD,(j*(743/30)),(i*(553/20)),(743/30),(553/20),this); break;}
 				default: {g.drawImage(fondo,(j*(743/30)),(i*(553/20)),(743/30),(553/20),this); break;}
 				}}}
 		
 		switch(nivel){
 		case 1: g.drawImage(extra,(3*(743/30)),(3*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 2: g.drawImage(extra,(20*(743/30)),(6*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 3: g.drawImage(extra,(20*(743/30)),(8*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 4: g.drawImage(extra,(16*(743/30)),(3*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 5: g.drawImage(extra,(4*(743/30)),(2*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 6: g.drawImage(extra,(19*(743/30)),(4*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 8: g.drawImage(extra,(19*(743/30)),(10*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 7: g.drawImage(extra,(18*(743/30)),(8*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 9: g.drawImage(extra,(20*(743/30)),(12*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 10: g.drawImage(extra,(20*(743/30)),(6*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 11: g.drawImage(extra,(11*(743/30)),(10*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 12: g.drawImage(extra,(17*(743/30)),(7*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 13: g.drawImage(extra,(18*(743/30)),(8*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 14: g.drawImage(extra,(18*(743/30)),(10*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 15: g.drawImage(extra,(17*(743/30)),(7*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 16: g.drawImage(extra,(17*(743/30)),(4*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 17: g.drawImage(extra,(19*(743/30)),(8*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 18: g.drawImage(extra,(18*(743/30)),(5*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 19: g.drawImage(extra,(20*(743/30)),(12*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 20: g.drawImage(extra,(19*(743/30)),(4*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 21: g.drawImage(extra,(20*(743/30)),(12*(553/20)),w*(743/30),h*(553/20),this); break;
 		case 22: g.drawImage(extra,(20*(743/30)),(12*(553/20)),w*(743/30),h*(553/20),this); break;
 		}
 		
 		switch(valor){
 		case 9: g.setColor(Color.green); break;
 		case 10: g.setColor(Color.yellow); break;
 		case 11: g.setColor(Color.blue); break;
 		case 12: g.setColor(Color.red); break;
 		}
 		g.setFont(new Font("Arial", Font.PLAIN, 12));
 		g.fillRect(508, 40, 50, 10);
 		g.fillRect(34, 40, 50, 10);
 		g.setColor(Color.white);
 		g.fillRect(508, 50, 50, 20);
 		g.fillRect(34, 50, 50, 20);
 		g.setColor(Color.black);
 		g.drawString("NIVEL", 42, 50);
 		g.drawString("MOVES", 513, 50);
 		g.drawString(""+cont, 508, 60);
 		g.drawString(""+nivel, 34, 60);
 		
 		arrX = Nivel.ganasteY(nivel);
 		arrY = Nivel.ganasteX(nivel);
 		for(int i = 0; i&lt;Nivel.numMinas(nivel); i++){
 			if(matriz[arrX[i]][arrY[i]]==1){
 				g.drawImage(fondoD,(arrY[i]*(743/30)),(arrX[i]*(553/20)),(743/30),(553/20),this); 
				g.drawImage(personaje,(arrY[i]*(743/30)),(arrX[i]*(553/20)),(743/30),(553/20),this);}
 			if(matriz[arrX[i]][arrY[i]]==2){
 				g.drawImage(fondoD,(arrY[i]*(743/30)),(arrX[i]*(553/20)),(743/30),(553/20),this);
 				g.drawImage(objeto,(arrY[i]*(743/30)),(arrX[i]*(553/20)),(743/30),(553/20),this);}
 			if(matriz[arrX[i]][arrY[i]]==0){
 				g.drawImage(fondoD,(arrY[i]*(743/30)),(arrX[i]*(553/20)),(743/30),(553/20),this); 
 			}}
    	}
     
     /**
      * Método que actualiza las pantallas. 
      * @param g
      */
     public void update(Graphics g){ 
    	    //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); }

     /**
      * Método que maneja todas las acciones que realizan los botones 
      * para las distintas pantallas
      * @param ActionEvent e
      */
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==playNow){
			valor = 1;
			removeAll();
			setLayout(null);
			
			add(back);
			back.setLocation(187,450);
			back.setSize(354,31);
			back.setVisible(true);
			
			add(ninaboton);
			ninaboton.setLocation(440,133);
			ninaboton.setSize(106,124);
			ninaboton.setVisible(true);
		
			add(ninoboton);
			ninoboton.setLocation(150,130);
			ninoboton.setSize(106,124);
			ninoboton.setVisible(true);
			
			add(perroboton);
			perroboton.setLocation(448,299);
			perroboton.setSize(100,125);
			perroboton.setVisible(true);
			
			add(pandaboton);
			pandaboton.setLocation(144,299);
			pandaboton.setSize(112,117);
			pandaboton.setVisible(true);
			
			add(qmarkboton);
			qmarkboton.setLocation(296,228);
			qmarkboton.setSize(100,100);
			qmarkboton.setVisible(true);
			repaint();
		}
		else if(e.getSource()==instructions){
			valor = 2;
			removeAll();
			add(playNow);
			playNow.setLocation(190,405);
			playNow.setSize(354,31);
			playNow.setVisible(true);
			
			add(back);
			back.setLocation(190,455);
			back.setSize(354,31);
			back.setVisible(true);
			
			repaint();
		}
		else if(e.getSource()==loadlevel){
			valor = 3;
			removeAll();
			add(playNow);
			playNow.setLocation(190,407);
			playNow.setSize(354,31);
			playNow.setVisible(true);
			
			add(back);
			back.setLocation(190,459);
			back.setSize(354,31);
			back.setVisible(true);
		
			repaint();

		}
		else if(e.getSource()==highScores){
			valor = 4;
			removeAll();
			
			add(levelSelect);
			levelSelect.setLocation(69,218);
			levelSelect.setSize(222,33);
			levelSelect.setVisible(true);
			
			add(levelHS);
			levelHS.setLocation(208, 170);
			levelHS.setSize(60,30);
			levelHS.setText(""+1);
			levelHS.setVisible(true);
			
			add(back);
			back.setLocation(192,456);
			back.setSize(354,31);
			back.setVisible(true);
		
			repaint();
		}
		else if(e.getSource()==quitGame){
			intro.stop();
			removeAll();
			setVisible(false);
			destroy();
			
			
		}
		else if (e.getSource()==back){
			valor = 0;
			removeAll();
			setLayout(null);
			
			add(playNow);
			playNow.setLocation(198,156);
			playNow.setSize(354,31);
			playNow.setVisible(true);
			
			add(instructions);
			instructions.setLocation(198,214);
			instructions.setSize(354,31);
			instructions.setVisible(true);
			
			add(quitGame);
			quitGame.setLocation(198,382);
			quitGame.setSize(354,31);
			quitGame.setVisible(true);
			
			add(loadlevel);
			loadlevel.setLocation(198,272);
			loadlevel.setSize(354,31);
			loadlevel.setVisible(true);
			
			add(highScores);
			highScores.setLocation(198,330);
			highScores.setSize(354,31);
			highScores.setVisible(true);
			
			playNow.addActionListener(this);
			instructions.addActionListener(this);
			loadlevel.addActionListener(this);
			quitGame.addActionListener(this);
			highScores.addActionListener(this);
			back.addActionListener(this);
			startgame.addActionListener(this);
			repaint();
		}
		else if(e.getSource()==qmarkboton){
			intro.stop();
			int arr[] = {5,6,7,8};
			int n =(int)(Math.random()*4);
			valor = arr[n];
			removeAll();
			add(startgame);
			switch(arr[n]){
			case 5: {au=getAudioClip(getDocumentBase(),"Rainer.mid"); au.loop(); break;}
			case 6: {au=getAudioClip(getDocumentBase(),"Sakura.mid"); au.loop(); break;}
			case 7: {au=getAudioClip(getDocumentBase(),"Mikau.mid"); au.loop(); break;}    
			case 8: {au=getAudioClip(getDocumentBase(),"Perro.mid"); au.loop(); break;}
			}
			
			switch(arr[n]){
			case 5:{startgame.setLocation(194,508); startgame.setSize(360,35); break;}
			case 6:{startgame.setLocation(202,466); startgame.setSize(354,31); break;}
			case 7:{startgame.setLocation(203,471); startgame.setSize(354,31); break;}
			case 8:{startgame.setLocation(204,464); startgame.setSize(354,31); break;}
			}
			startgame.setVisible(true);
			repaint();
		}
		else if(e.getSource()==ninoboton){
			intro.stop();
			au=getAudioClip(getDocumentBase(),"Rainer.mid");
			au.loop();
			valor = 5;
			removeAll();
			add(startgame);
			startgame.setLocation(194,508);
			startgame.setSize(360,35);
			startgame.setVisible(true);
			repaint();
		}
		else if(e.getSource()==ninaboton){
			au=getAudioClip(getDocumentBase(),"Sakura.mid");
			au.loop();
			intro.stop();
			valor = 6;
			removeAll();
			add(startgame);
			startgame.setLocation(202,466);
			startgame.setSize(354,31);
			startgame.setVisible(true);
			repaint();
		}
	
		else if(e.getSource()==pandaboton){
			au=getAudioClip(getDocumentBase(),"Mikau.mid");
			au.loop();
			intro.stop();
			valor = 7;
			removeAll();
			add(startgame);
			startgame.setLocation(203,471);
			startgame.setSize(354,31);
			startgame.setVisible(true);
			repaint();
		}
		else if(e.getSource()==perroboton){
			au=getAudioClip(getDocumentBase(),"Perro.mid");
			au.loop();
			intro.stop();
			valor = 8;
			removeAll();
			add(startgame);
			startgame.setLocation(204,464);
			startgame.setSize(354,31);
			startgame.setVisible(true);
			repaint();
		}
		else if(e.getSource()==startgame){
			removeAll();
			switch(valor){
			case 5: valor = 9; break;
			case 6: valor = 10; break;
			case 7: valor = 11; break;
			case 8: valor = 12; break;
			}
			repaint();
		}
		else if(e.getSource()==next){
			if((name.getText().length()&gt;8)||(name.getText().length()==0)){
				actualizaArchivo("Bytecode");}
			actualizaArchivo(name.getText());
			lcomplete.stop();
			au.loop();
			nivel++;
			cont = 0;
			h = false;
			valor = valoranterior;
			matriz = Nivel.generaNiv(nivel);
			name.setText("");
			removeAll();
			repaint();
		}
		else if(e.getSource()==retry){
			if((name.getText().length()&gt;8)||(name.getText().length()==0)){
				actualizaArchivo("Bytecode");}
			actualizaArchivo(name.getText());
			lcomplete.stop();
			au.loop();
			cont = 0;
			h = false;
			valor = valoranterior;
			matriz = Nivel.generaNiv(nivel);
			name.setText("");
			removeAll();
			repaint();
		}
		else if (e.getSource()==levelSelect){
			valor = 4;
			repaint();}

	}
	
	/**
	 * Método que determina si el jugador ha ganado el juego
	 * conociendo de antemano que quien gana el juego es quien logra
	 * completar un nivel acomodando las cajitas en su posición correspondiente.
	 * @return true, si se completa el nivel
	 */
	public boolean winner(){
		int z;
		arrX = Nivel.ganasteY(nivel); 	
		arrY = Nivel.ganasteX(nivel); 
		z = Nivel.numMinas(nivel); 
		int acum = 0;
		for(int i = 0; i&lt;z; i++){
			if(matriz[arrX[i]][arrY[i]]==2)
				acum++;
		}
		if(acum==z){
			return true;
		}
		return false;
	}
	
	/**
	 * Método que hace posible el movimiento de los personajes en la pantalla de nivel.
	 * Maneja matriz
	 * @param k
	 * @exception e 
	 */
	public void muevete(int k){
		
		for(int i = 0; i&lt;17; i++){
 			for(int j = 0; j&lt;25; j++){
 				if(matriz[i][j]==1){
 				switch(k){
 				case 1:{
 					if(matriz[i+1][j]==0){
 						matriz[i+1][j]= 1; 
 						matriz[i][j]=0;
 						cont++;}
 					else if((matriz[i+1][j]==2)&amp;&amp;(matriz[i+2][j]==0)){
 						
 				    	matriz[i+2][j]=2;
 				    	matriz[i+1][j]=1;
 				    	matriz[i][j]=0;
 				    	contmovimientod++;
 				    	movimientod=true;
 				    	cont++;}
 					else{
 					punch.play();
 					}
 				    return;}
 			    case 2:{ 
 			    	if(matriz[i-1][j]==0){
 			    		
						 matriz[i-1][j]= 1; 
						 matriz[i][j]=0;
					
						 cont++;}
 			    	else if((matriz[i-1][j]==2)&amp;&amp;(matriz[i-2][j]==0)){
  				    	matriz[i-2][j]=2;
  				    	matriz[i-1][j]=1;
  				    	matriz[i][j]=0;
  				    	contmovimientou++;
  				    	movimientou=true;
  				    	cont++;}
 			    	else{
 			    	
 					punch.play();
 			    	}
 					return;}
 				case 3:{ 
 					if(matriz[i][j+1]==0){
						 matriz[i][j+1]= 1; 
						 matriz[i][j]=0;
						 cont++;}
 					else if((matriz[i][j+1]==2)&amp;&amp;(matriz[i][j+2]==0)){
 						
  				    	matriz[i][j+2]=2;
  				    	matriz[i][j+1]=1;
  				    	matriz[i][j]=0;
  				    	contmovimientor++;
  				    	movimientor=true;
  				    	cont++;}
 					else{	
 	 					punch.play();
 			    	}
 					return;} 
 				case 4:{ 
 					if(matriz[i][j-1]==0){
 					
					   matriz[i][j-1]= 1; 
					   matriz[i][j]=0;
					   
					   cont++;}
 					else if((matriz[i][j-1]==2)&amp;&amp;(matriz[i][j-2]==0)){
 					
				    	matriz[i][j-2]=2;
				    	matriz[i][j-1]=1;
				    	matriz[i][j]=0;
				    	contmovimientol++;
				    	movimientol=true;
				    	cont++;}
 					else{
 						punch.play();
 					}
					return;} 
 				
 				}
 				}
 			}
 				
 		}
	}
	
public void mueveteUndo(int k){
		
		for(int i = 0; i&lt;17; i++){
 			for(int j = 0; j&lt;25; j++){
 				if(matriz[i][j]==1){
 				switch(k){
 				case 1:{
 					if(matriz[i-1][j]==0){
						 matriz[i-1][j]= 1; 
						 matriz[i][j]=0;
						 if(matriz[i+1][j]==2&amp;&amp;movimientod==true){
							 matriz[i][j]=2;
							 matriz[i+1][j]=0;
							 contmovimientod--;
							 if(contmovimientod==0){
							 movimientod=false;
							 }
						 }
						
						 }
			    	
			    	else{
			    	
					punch.play();
			    	}
					return;}
 			    case 2:{ 
 			    	if(matriz[i+1][j]==0){
 						matriz[i+1][j]= 1; 
 						matriz[i][j]=0;
 						if(matriz[i-1][j]==2&amp;&amp;movimientou==true){
 							matriz[i][j]=2;
 							matriz[i-1][j]=0;
 							contmovimientou--;
 							 if(contmovimientou==0){
 								 movimientou=false;
 								 }
 							
 							
 						}
 						}
 			    	
 				
 					else{
 					punch.play();
 					}
 				    return;}
 				case 3:{ 
 					if(matriz[i][j-1]==0){
 					   matriz[i][j-1]= 1; 
 					   matriz[i][j]=0;
 					if(matriz[i][j+1]==2&amp;&amp;movimientor==true){
 						matriz[i][j]=2;
 						matriz[i][j+1]=0;
 						contmovimientor--;
 						 if(contmovimientor==0){
							 movimientor=false;
							 }
 					}
 					   
 					  }
  			
  					else{
  						punch.play();
  					}
 					return;} 
 				case 4:{ 
 					if(matriz[i][j+1]==0){
						 matriz[i][j+1]= 1; 
						 matriz[i][j]=0;
						 if(matriz[i][j-1]==2&amp;&amp;movimientol==true){
							 matriz[i][j]= 2; 
							 matriz[i][j-1]=0;
							 contmovimientol--;
							 if(contmovimientol==0){
								 movimientol=false;
								 }
						 }
 			
					else{	
	 					punch.play();
			    	}
					return;} 
 				
 				}
 				
 
 				}
 				}
 			}}}
	
	/**
	 * Metodo que hace posible la reproducción de
	 * sonidos
	 */
	public void activaSonido(){
		if(f==false){
			au.loop();
		   f = true;
			}
			else{
			au.stop();
			f = false;
			
			}
	}
	
	/**
	 * Método que maneja las acciones, en este caso de movimiento
	 * para cuando se presionan las teclas el personaje se mueva en las
	 * distintas posiciones en la pantalla, hacia arriba, abajo, izquierda y derecha
	 * @param KeyEvent e
	 */
	public void keyPressed(KeyEvent e){
		String uno="1";
		String dos="2";
		String tres="3";
		String cuatro="4";
	 if((valor==12)||(valor==11)||(valor==10)||(valor==9)){
		switch(e.getKeyCode()){
	    case KeyEvent.VK_S:{activaSonido(); break;}
	    case KeyEvent.VK_RIGHT:{muevete(3);lista.addLast(tres);break;}
		case KeyEvent.VK_LEFT:{muevete(4);lista.addLast(cuatro);break;}
		case KeyEvent.VK_UP:{muevete(2);lista.addLast(dos);break;}
		case KeyEvent.VK_DOWN:{muevete(1);lista.addLast(uno);break;}
		case KeyEvent.VK_SPACE:{matriz = Nivel.generaNiv(nivel) ;cont = 0; break;}
		case KeyEvent.VK_ESCAPE:{valor = 0; nivel = 1; cont = 0; d = true; start(); au.stop(); break;}
		case KeyEvent.VK_U:{
			if(cont==0){
				return;
			}
			else{
			try{
			
			cont--;
		
			mueveteUndo(Integer.parseInt((String)lista.removeLast()));
			break;
			}
			catch(NoSuchElementException nse){
				int auxnivel=nivel;
				matriz = Nivel.generaNiv(auxnivel--) ;cont = 0; 
				
			}
			}
			}
			
		case KeyEvent.VK_N:{
			if((nivel&lt;10))
				nivel++; 
			matriz = Nivel.generaNiv(nivel); cont = 0; break;}
		case KeyEvent.VK_P:{
			if((nivel&gt;1))
				nivel--; 
				matriz = Nivel.generaNiv(nivel); cont = 0; break;}
		}
		
	
		repaint();
		}
	 	if(winner()==true){
		pantallaLevelComplete();}
		}
	
	public void keyReleased(KeyEvent e){
		if((valor==12)||(valor==11)||(valor==10)||(valor==9)){
		 if(winner()==true){
			 pantallaLevelComplete();}}
	}

	public void keyTyped(KeyEvent e) {}
	}

</pre></body></html>