¿Cómo mover un objeto por un tablero (grid)?

Este es el código del main que tengo hasta ahora. Quiero hacer que el personaje de la esquina superior izquierda se pueda mover respetando los muros y actuando dependiendo del tipo de objeto con el que se tope. Si pudieran incluir el código específico se los agradecería. Gracias.

Esto es lo que aparecería si se ejecutara el código

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pruebaproyectojuego;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
 *
 * @author usuario
 */
public class Table extends javax.swing.JFrame {
 private static int numFilas = 15;
 private static int numColumnas = 10;
 private JLabel[][] casillas; // matriz grafica
 private int[][] mLogica = new int[numFilas][numColumnas]; // matriz para almacenar las entidades
 private Entidad[][] entidades = new Entidad[numFilas][numColumnas];
 //private Heroe heroe;
 public static final int tamañoCuadrito = 60;
 private Entidad x;
 public Table() {
 initComponents();
 crearMapaLogico();
 DesplegarMatriz();
 }
 public void crearMapaLogico() {
 int[][] mapaLogico = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
 {1, 7, 0, 1, 0, 0, 0, 0, 0, 1},
 {1, 0, 0, 1, 0, 0, 0, 0, 0, 1},
 {1, 0, 0, 1, 1, 1, 0, 4, 0, 1},
 {1, 0, 0, 6, 0, 1, 0, 0, 0, 1},
 {1, 0, 0, 3, 0, 1, 0, 0, 0, 1},
 {1, 0, 0, 1, 1, 1, 0, 3, 0, 1},
 {1, 6, 0, 0, 0, 1, 0, 0, 0, 1},
 {1, 0, 0, 0, 5, 1, 0, 0, 0, 1},
 {1, 0, 0, 0, 0, 1, 1, 1, 0, 1},
 {1, 2, 0, 0, 0, 0, 0, 3, 0, 1},
 {1, 0, 0, 6, 0, 0, 0, 0, 0, 1},
 {1, 0, 0, 0, 0, 0, 0, 1, 1, 1},
 {1, 0, 0, 0, 0, 0, 0, 0, 8, 1},
 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
 mLogica = mapaLogico;
 }
 public void DesplegarMatriz() {
 casillas = new JLabel[numFilas][numColumnas]; // se instancia la matriz para dibujar entidades y pasto
// javax.swing.SwingUtilities.invokeLater(new Runnable() {
// @Override
// public void run() {
// // JFrame tablero = new JFrame(); 
// // tablero.setLayout(new GridLayout(numFilas, numColumnas));
 for (int i = 0; i < numFilas; i++) {
 for (int j = 0; j < numColumnas; j++) {
 casillas[j] = new JLabel();
 casillas[j].setOpaque(true);
 casillas[j].setBounds((i * tamañoCuadrito) + 30, (j * tamañoCuadrito) + 30, tamañoCuadrito, tamañoCuadrito);
 casillas[j].setVisible(true);
 //casillas[j].setIcon(dibujar(mLogica[j]));
 casillas[j].setIcon(dibujar(mLogica[j]));
 panelContenedor.add(casillas[j]);
 }
 }
 }
 public ImageIcon dibujar(int tipo) {
 switch (tipo) {
 case 0:
 //return new ImageIcon(getClass().getResource("/imagenes/green.jpg"));
 return new ImageIcon(((new ImageIcon(getClass().getResource("/imagenes/pasto.jpg")).getImage().getScaledInstance(80, 80, java.awt.Image.SCALE_SMOOTH))));
 case 1:
 Entidad x1 = new Muro();
 return ((Muro) x1).dibujar();
 case 2:
 Entidad x2 = new Bomba();
 return ((Bomba) x2).dibujar();
 case 3:
 Entidad x3 = new Pistola();
 return ((Pistola) x3).dibujar();
 case 4:
 Entidad x4 = new Ballesta();
 return ((Ballesta) x4).dibujar();
 case 5:
 Entidad x5 = new Espada();
 return ((Espada) x5).dibujar();
 case 6:
 Entidad x6 = new Enemigo();
 return ((Enemigo) x6).dibujar();
 case 7:
 Entidad x7 = new Heroe();
 return ((Heroe) x7).dibujar();
 case 8:
 return new ImageIcon(getClass().getResource("/imagenes/salida.jpg"));
 default:
 return new ImageIcon(getClass().getResource("/imagenes/pasto.jpg"));
 }
 }
 /**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
 @SuppressWarnings("unchecked")
 // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
 private void initComponents() {
 panelContenedor = new javax.swing.JPanel();
 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 javax.swing.GroupLayout panelContenedorLayout = new javax.swing.GroupLayout(panelContenedor);
 panelContenedor.setLayout(panelContenedorLayout);
 panelContenedorLayout.setHorizontalGroup(
 panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGap(0, 1000, Short.MAX_VALUE)
 );
 panelContenedorLayout.setVerticalGroup(
 panelContenedorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGap(0, 600, Short.MAX_VALUE)
 );
 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
 getContentPane().setLayout(layout);
 layout.setHorizontalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addComponent(panelContenedor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
 );
 layout.setVerticalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addComponent(panelContenedor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
 );
 pack();
 }// </editor-fold> 
 /**
 * @param args the command line arguments
 */
 public static void main(String args[]) {
 /* Set the Nimbus look and feel */
 //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
 /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
 */
 try {
 for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
 if ("Nimbus".equals(info.getName())) {
 javax.swing.UIManager.setLookAndFeel(info.getClassName());
 break;
 }
 }
 } catch (ClassNotFoundException ex) {
 java.util.logging.Logger.getLogger(Table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
 } catch (InstantiationException ex) {
 java.util.logging.Logger.getLogger(Table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
 } catch (IllegalAccessException ex) {
 java.util.logging.Logger.getLogger(Table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
 } catch (javax.swing.UnsupportedLookAndFeelException ex) {
 java.util.logging.Logger.getLogger(Table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
 }
 //</editor-fold>
 /* Create and display the form */
 java.awt.EventQueue.invokeLater(new Runnable() {
 public void run() {
 new Table().setVisible(true);
 }
 });
 }
 // Variables declaration - do not modify 
 private javax.swing.JPanel panelContenedor;
 // End of variables declaration 
}

Añade tu respuesta

Haz clic para o