Probleme de collision map 2D

Résolu
dadouvic Messages postés 65 Date d'inscription samedi 2 août 2008 Statut Membre Dernière intervention 29 avril 2012 - 3 avril 2010 à 17:24
dadouvic Messages postés 65 Date d'inscription samedi 2 août 2008 Statut Membre Dernière intervention 29 avril 2012 - 5 avril 2010 à 12:07
Bonjour à tous et à toute !
Voili voilou, sa fais pas tellement de temps que je fais du Java, et je me suis dis que j'allais essayer de faire quelque petits jeu, donc j'ai commencer par le celebre " PAC-MAN" je sais ...
Donc voilà, j'ai voulus faire les collisions des petits murais sur la map, mais de 1, mon personnage se coince dedans, et de 2 je me colltine une exception qui, une fois que je suis allez en arriere, m'enpeche de revenir en avant a certains endroit de ma pitite map.

Mon code :

public class Main {

public static void main(String[] args) {

Fenetre fenetre = new Fenetre();

}

}


import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class Fenetre extends JFrame implements KeyListener{

//Fields
JLabel pacMan = new JLabel(new ImageIcon("images/Pac_face.png"));
Point location = new Point();
Map map = new Map();

//Constructor
public Fenetre(){
this.setTitle("Pac-Man");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(606,628);
this.setLocationRelativeTo(null);
this.addKeyListener(this);
this.setVisible(true);

initPacMan();
 
}

//Methods
private void initPacMan() {
//Ajout de la map et du PacMan
this.setContentPane(map);	
this.getContentPane().add(pacMan);

//Mise en place du Pac Man
pacMan.setLocation(500, 260);

//Mise en place de la Map
map.currentArene = map.arene1x1;

}

//Interfaces
public void keyPressed(KeyEvent e) {
location.x = (pacMan.getY() - 5) / 20;
location.y = ((pacMan.getX() - 280) / 20) + 14;
System.out.println(location);

if(e.getKeyChar() 'Z' || e.getKeyChar() 'z'){
pacMan.setLocation(pacMan.getX(), pacMan.getY() - 20);
pacMan.setIcon(new ImageIcon("images/pac_dos.png"));
if(map.getCharTo(location) == 'X'){
pacMan.setLocation(pacMan.getX(), pacMan.getY() + 20);
}
}	
        
if(e.getKeyChar() 'Q' || e.getKeyChar() 'q'){
//pacMan.setLocation(pacMan.getX()- 20, pacMan.getY());
pacMan.setIcon(new ImageIcon("images/pac_gauche.png"));
if(map.getCharTo(location) == ' '){
pacMan.setLocation(pacMan.getX() - 20, pacMan.getY());
}
else{
pacMan.setLocation(pacMan.getX() + 20, pacMan.getY());
}
}

if(e.getKeyChar() 'D' || e.getKeyChar() 'd'){
pacMan.setLocation(pacMan.getX() + 20, pacMan.getY());
pacMan.setIcon(new ImageIcon("images/pac_droite.png"));
if(map.getCharTo(location) == 'X'){
pacMan.setLocation(pacMan.getX() - 20, pacMan.getY());
}
}

if(e.getKeyChar() 'S' || e.getKeyChar() 's'){
pacMan.setLocation(pacMan.getX(), pacMan.getY() + 20);
pacMan.setIcon(new ImageIcon("images/pac_face.png"));
if(map.getCharTo(location) == 'X'){
pacMan.setLocation(pacMan.getX(), pacMan.getY() - 20);
}
}
}

public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {

}
     
}


import java.awt.Graphics;
import java.awt.Point;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Map extends JPanel{

public char[][] currentArene;
public char[][] arene1x1 = {{'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
   	 	{'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
   	 	{'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
   	 	{'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
   	 	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
   	 	{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}};

//Constructors
public Map(){

}

//Methods
public void paintComponent(Graphics g){
try {
g.drawImage(ImageIO.read(new File("images/Maps/PacMap_1x1.png")), 0, 0, this);
} catch (IOException e) {
e.printStackTrace();
}
}

public char getCharTo(Point location) {
return currentArene[location.x][location.y];
}

}


voici l'exception donner avec :
java.lang.ArrayIndexOutOfBoundsException

Merci d'avance.Dadouvic

7 réponses

uhrand Messages postés 491 Date d'inscription samedi 20 mai 2006 Statut Membre Dernière intervention 15 juillet 2012 9
3 avril 2010 à 18:09
Pour éliminer les exceptions, essaie ceci
public char getCharTo(Point location) {
    int x1 = location.x;
    int y1 = location.y;
    if(x1 < 0){
        x1 = 0;
    }
    if(x1 > 14){
        x1 = 14;
    }
    if(y1 < 0){
        y1 = 0;
    }
    if(y1 > 14){
        y1 = 14;
    }
    location.x = x1;
    location.y = y1;
    return currentArene[location.x][location.y];
}
3
dadouvic Messages postés 65 Date d'inscription samedi 2 août 2008 Statut Membre Dernière intervention 29 avril 2012
3 avril 2010 à 19:49
Plus qu'un grand merci à toi ... un Hourra !!!!!!!! merci beaucoup
sa m'aide vachement ! merci merci
0
dadouvic Messages postés 65 Date d'inscription samedi 2 août 2008 Statut Membre Dernière intervention 29 avril 2012
3 avril 2010 à 19:54
Bon ... juste je trouve vraiment pas comment en fait, quand je tape le mur de collision, qu'il recule sans etre bloqué mon petit perso, par contre uhrand, ta reponse ma tellement aidée :) .
0
uhrand Messages postés 491 Date d'inscription samedi 20 mai 2006 Statut Membre Dernière intervention 15 juillet 2012 9
4 avril 2010 à 03:53
Quand je tape 16 fois 'Q', il se déplace vers la gauche, la 17e fois il recule. Où est le problème?
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
dadouvic Messages postés 65 Date d'inscription samedi 2 août 2008 Statut Membre Dernière intervention 29 avril 2012
4 avril 2010 à 16:43
Je voudrais que mon pac-man ne puisse meme pas du tout bouger une fois quil aura atteint la 16em, il ne peut plus bouger, car avec ma technique la, le pac-man reste bloquer dans le mur si l'on ne rappuie pas sur "q" ce qui est lourd pour un jouer
0
uhrand Messages postés 491 Date d'inscription samedi 20 mai 2006 Statut Membre Dernière intervention 15 juillet 2012 9
4 avril 2010 à 23:27
Désolé, j'avais compris que tu voulais "quand je tape le mur de collision, qu'il recule", voila autre chose. Essaie cette demo pour voir le principe:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class PacmanDemo {
    public static void main(final String[] args) {
        Runnable gui = new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        };
        SwingUtilities.invokeLater(gui);
    }
}
class MainFrame extends JFrame implements KeyListener {
    private JLabel pacMan;
    private Point location = new Point();
    private Point next = new Point();
    private PacmanMap map = new PacmanMap();
    private Pacman pacRight, pacLeft, pacUp, pacDown;
    public MainFrame() {
        this.setTitle("Pac-Man Demo:  use arrow keys to play");
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(350, 350);
        this.setLocationRelativeTo(null);
        this.addKeyListener(this);
        initPacMan();
    }
    private void initPacMan() {
        pacRight = new Pacman("right");
        pacLeft = new Pacman("left");
        pacUp = new Pacman("up");
        pacDown = new Pacman("down");
        pacMan = new JLabel(pacLeft);
        //Ajout de la map et du PacMan
        this.setContentPane(map);
        map.add(pacMan);
        //Mise en place du Pac Man
        pacMan.setBounds(200, 40, 20, 20);
        //Mise en place de la Map
        map.currentArene = map.arene1x1;
    }
    public void keyPressed(final KeyEvent e) {
        location.x = pacMan.getX() / 20;
        location.y = pacMan.getY() / 20;
        next.x = location.x;
        next.y = location.y;
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            pacMan.setIcon(pacUp);
            next.y -= 1;
            if (map.getCharTo(next) == ' ') {
                pacMan.setLocation(pacMan.getX(), pacMan.getY() - 20);
            }
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            pacMan.setIcon(pacLeft);
            next.x -= 1;
            if (map.getCharTo(next) == ' ') {
                pacMan.setLocation(pacMan.getX() - 20, pacMan.getY());
            }
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            pacMan.setIcon(pacRight);
            next.x += 1;
            if (map.getCharTo(next) == ' ') {
                pacMan.setLocation(pacMan.getX() + 20, pacMan.getY());
            }
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            pacMan.setIcon(pacDown);
            next.y += 1;
            if (map.getCharTo(next) == ' ') {
                pacMan.setLocation(pacMan.getX(), pacMan.getY() + 20);
            }
        }
    }
    public void keyReleased(final KeyEvent e) {
    }
    public void keyTyped(final KeyEvent e) {
    }
}
class PacmanMap extends JPanel {
    public char[][] currentArene;
    public char[][] arene1x1 = {
        {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
        {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
        {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
        {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}
    };
    private BufferedImage image;
    public PacmanMap() {
        setLayout(null);
    }
    @Override
    public void paintComponent(final Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, this);
    }
    public char getCharTo(final Point location) {
        return currentArene[location.x][location.y];
    }
}
class Pacman implements Icon {
    private int[] xpoints;
    private int[] ypoints;
    private Polygon bouche;
    private Color color = Color.ORANGE;
    public Pacman(final String dir) {
        if (dir.equals("right")) {
            xpoints = new int[]{10, 20, 20};
            ypoints = new int[]{10, 0, 20};
        } else if (dir.equals("left")) {
            xpoints = new int[]{10, 0, 0};
            ypoints = new int[]{10, 0, 20};
        } else if (dir.equals("up")) {
            xpoints = new int[]{10, 0, 20};
            ypoints = new int[]{10, 0, 0};
        } else if (dir.equals("down")) {
            xpoints = new int[]{10, 0, 20};
            ypoints = new int[]{10, 20, 20};
        }
        bouche = new Polygon(xpoints, ypoints, 3);
    }
    public void paintIcon(final Component c, final Graphics g, final int x, final int y) {
        g.setColor(color);
        g.fillOval(0, 0, 20, 20);
        g.setColor(c.getBackground());
        g.fillPolygon(bouche);
    }
    public int getIconWidth() {
        return 20;
    }
    public int getIconHeight() {
        return 20;
    }
}
0
dadouvic Messages postés 65 Date d'inscription samedi 2 août 2008 Statut Membre Dernière intervention 29 avril 2012
5 avril 2010 à 12:07
Tu me sauves une nouvelle fois merci beaucoup frenchement :)
0
Rejoignez-nous