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) {
}
}
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 :) .
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