jueves, 24 de junio de 2010

Laberinto en Java































Hice un laberinto aunque no me salio prefecto, me falto borrar algunas lineas,
pero básicamente tienes que llegar al lado derecho de la pantalla usando las flechas del teclado.
ahi vienen las instrucciones y se puede resetear, al hacerlo el cuadrito se puede mover en las columnas pares, y en las impares solo hay 1 brecha (1 cuadro) por el cual te dejara moverte y si al moverte te sales del cuadro te regresa, si llegas al final sale en cmd que ganaste.
Diviértanse =P


Tambien puse algunas fotos de cuando ganas y como se ve gráficamente el juego.
Aquí viene el código.
http://www.mediafire.com/?5z3k1jelmtm

Sino también esta escrito aquí.


import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.*;
import java.awt.Color;

public class Juego extends JPanel implements KeyListener, ActionListener {

public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("INS")) {
System.out.println(" Debes de mover el cuadro al lado derecho de la pantalla \n Pero si te equivocas te manda hacia atras. Aguas \n Usa las flechas de tu teclado");
} else if (cmd.equals("RESET")) {
this.reset();
this.repaint();
}
return;
}

// atributos de la clase
private double x; // posicion horizontal (0 = izq, 1 = der)
private double y; // posicion vertical (0 = arriba, 1 = abajo)
private double ancho; // tamano rel. del objeto a dibujar
private double altura; // tamano rel. del objeto a dibujar

// constante
private static final double PASO = 0.02; // desplazamiento de 3%
private static final double CAMBIO = 0.02; // cambio de 2% (tamano)
private static final double MIN = 0.001;
private static final double MAX = 0.95;

// metodo interno propio nuestro
private void dibujaObjeto(Graphics2D g, int ancho, int altura) {
g.setColor(Color.BLUE);
g.fill(new Rectangle2D.Double(this.x*ancho,
this.y*altura,
this.ancho*ancho,
this.altura*altura));
g.setColor(Color.BLACK);
g.drawLine(30, 0, 30, 320);

g.drawLine(90, 0, 90, 320);

g.drawLine(150, 0, 150, 320);

g.drawLine(210, 0, 210, 320);

g.drawLine(270, 0, 270, 320);

g.drawLine(330, 0, 330, 320);

g.drawLine(390, 0, 390, 320);

g.drawLine(450, 0, 450, 320);

g.drawLine(510, 0, 510, 320);

g.drawLine(570, 0, 570, 320);


//Lineas Horizontales

g.drawLine(0, 50, 590, 50);
g.drawLine(0, 100, 590, 100);
g.drawLine(0, 150, 590, 150);
g.drawLine(0, 200, 590, 200);
g.drawLine(0, 250, 590, 250);
g.drawLine(0, 300, 590, 300);


return;
}

// override (reescritura) del metodo paint definido en la clase JPanel
public void paint(Graphics g) {
super.paint(g); // llamar a clase padre para que ejecute lo suyo
this.dibujaObjeto((Graphics2D)g, // typecasting
super.getWidth(), // pedir el ancho en pixeles
super.getHeight()); // altura actual en pixeles
return;
}

private void reset() {
this.x = 0;
this.y = 0;
this.ancho = 0.025;
this.altura = 0.05;
return;
}

// constructor (no trae return ni tiene definido el tipo de salida)
public Juego() { // porque extiende a JPanel
super(); // llamamos al constructor de JPanel

// valores iniciales a los atributos (entre cero y uno)
this.reset();
}

public void keyPressed(KeyEvent e) {
int c = e.getKeyCode();

switch (c) {
case KeyEvent.VK_DOWN:
this.y += Juego.PASO;
break;
case KeyEvent.VK_UP:
this.y -= Juego.PASO;
break;
case KeyEvent.VK_LEFT:
this.x -= Juego.PASO;
break;
case KeyEvent.VK_RIGHT:
this.x += Juego.PASO;
break;


default: // si no fue ninguna de las flechas
System.err.println("Tecla no conocida.");
break;
}

if (this.x < x =" 0.0;"> .975) {
this.x = .975;
}
// Deja pasar 1
if ((x>=.0329 && x<=.1322) &&(y>=0 && y<=.1484)) { this.x=0.0329; } if ((x>=.0329 && x<=.1322) &&(y>=.2969 && y<=.95)) { this.x=0.0329; } // Deja pasar 2 if ((x>=.2314 && x<=.3305) &&(y>=0 && y<=.5938)) { this.x=0.2314; } if ((x>=0.2314 && x<=.3305) &&(y>=.7422 && y<=.95)) { this.x=0.2314; } // Deja pasar 3 if ((x>=.4247 && x<=.5288) &&(y>=.1484)) {
this.x=.4247;

}

// Deja pasar 4
if ((x>=.6445 && x<=.7271) &&(y>=0 && y<=.4453)) { this.x=.6445; } if ((x>=.6445 && x<=.7271) &&(y>=.5938 && y<=.95)) { this.x=.6445; } // Deja pasar 5 Entre las lineas horizontales 9 y 10 (510,570) if ((x>=.8263 && x<=.9254) &&(y>=0 && y<=.8906)) { this.x=.8263; } //Ganaste if (this.x > 0.9) {

System.out.println("GANASTEEE!!!");

}

if (this.y < y =" 0.0;"> .95) {
this.y = .95;
}




if (this.ancho < ancho =" Juego.MIN;"> Juego.MAX) {
this.ancho = Juego.MAX;
}

if (this.altura < altura =" Juego.MIN;"> Juego.MAX) {
this.altura = Juego.MAX;
}

super.repaint();

return;
}


public void keyReleased(KeyEvent e) {
return; // no se usa ahora
}

public void keyTyped(KeyEvent e) {
return; // no se usa ahora
}

public static void main(String[] args) {
JFrame ventana = new JFrame();
ventana.setTitle("Laberinto YEAH!");
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.setLocation(100, 150);
ventana.setSize(600, 400);

Juego t = new Juego();

JPanel p = new JPanel();
p.setFocusable(true);
p.setLayout(new BorderLayout());
JButton b = new JButton("RESETEAR");
b.setActionCommand("RESET");
b.addKeyListener(t); // poner escuchador
b.addActionListener(t);
p.add(b, BorderLayout.NORTH);
b = new JButton("INSTRUCCIONES");
b.setActionCommand("INS");
p.add(b, BorderLayout.SOUTH);
b.addActionListener(t);

t.setBackground(Color.WHITE);

p.addKeyListener(t); // poner escuchador
p.add(t, BorderLayout.CENTER);

ventana.setContentPane(p);
ventana.setVisible(true);

return;
}

}






Saludos

2 comentarios:

  1. como lo puedo modificar para que el automaticamente me busque la salida sin las teclas que todo sea automatico que con un boton se solucion me muestre los pasos hasta llegar al final del camino

    ResponderEliminar