Bonjour,
je suis en train de faire un pacman dans le cadre d'un projet de programmation, et j'ai un souci à propos des Keylistener sur la Frame qui doivent écouter les directions que je rentre au clavier pour diriger le pacman.
En effet, lorsque j'appuie sur le clavier, j'ai le code clavier correspondant à la touche appuyée qui apparait sur le terminal, mais cela n'est pas pris en compte par le jeu, et le pacman continue a avancer tout droit.
De plus, qui je clique autre part que sur la fenetre de jeu, et que je reviens ds cette derniere, les keylisteners ne sont plus actifs!
par ailleurs, la fenetre a un probleme de clipping, j'ai bien essayé d'y intégrer un double buffer(maladroitement je suppose) mais n'a pas sensiblement amélioré le rendu.
Pouvez vous m'aider?
et aussi
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 import fr.jussieu.script.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.awt.*; import java.awt.image.*; class Pacman implements Runnable { public int[][] lab; public int i,j; public int x,y; public char direction; public Thread t; public decor d; public KeyListener k; public Pacman(int[][] v, decor d) { this.i=0; this.j=0; this.lab=v; this.direction='d'; this.t = new Thread(); this.d = d; this.run(); } public void run() { this.d.start("Pacman"); this.d.getf().addKeyListener(this.d); while(this.t.isAlive() || !this.t.isInterrupted()){ Deug.println(this.d.directionPressed); try { Thread.sleep(500); } catch (InterruptedException e) {} switch(this.direction) { case 'h': for (int x=this.i+1;x<0;x--){ if(!this.updatePacman(x,this.j) || this.updateDirection()) break; Deug.println("haut"); try { Thread.sleep(10); } catch (InterruptedException e) {} } break; case 'b': for (int x=this.i+1;x<this.lab.length;x++){ if(!this.updatePacman(x,this.j) || this.updateDirection()) break; Deug.println("bas"); try { Thread.sleep(500); } catch (InterruptedException e) {} } break; case 'd': for (int y=this.j+1;y<this.lab[0].length;y++){ if(!this.updatePacman(this.i,y) || this.updateDirection()) break; Deug.println("droite"); try { Thread.sleep(500); } catch (InterruptedException e) {} } break; case 'g': for (int y=this.j+1;y<0;y--){ if(!this.updatePacman(this.i,y) || this.updateDirection()) break; Deug.println("haut"); try { Thread.sleep(500); } catch (InterruptedException e) {} } break; default: } if( this.d.directionPressed != 's') { this.updateDirection(); //change direction } try { Thread.sleep(10); } catch (InterruptedException e) {} } } public boolean updatePacman(int w,int t) { if (this.direction=='g'){ if (this.lab[w][t] != 1 || this.lab[w][t] != 2 ) { this.x=this.i; this.y=this.j; this.d.traceVide(this.i,this.j); this.j--; this.d.tracePacman(this.i,this.j); return true; } else { this.d.tracePacman(this.i,this.j); return false;} } else if (this.direction=='d') { if (this.lab[w][t] != 1 || this.lab[w][t] != 2 ) { this.x=this.i; this.y=this.j; this.d.traceVide(this.i,this.j); this.j++; this.d.tracePacman(this.i,this.j); return true; } else { this.d.tracePacman(this.i,this.j); return false;} } else if (this.direction=='h') { if (this.lab[w][t] != 1 || this.lab[w][t] != 2 ) { this.x=this.i; this.y=this.j; this.d.traceVide(this.i,this.j); this.i--; this.d.tracePacman(this.i,this.j); return true; } else { this.d.tracePacman(this.i,this.j); return false;} } else if (this.direction=='b') { if (this.lab[w][t] != 1 || this.lab[w][t] != 2 ) { this.x=this.i; this.y=this.j; this.d.traceVide(this.i,this.j); this.i++; this.d.tracePacman(this.i,this.j); return true; } else { this.d.tracePacman(this.i,this.j);return false;} } return false; } public boolean updateDirection() { Deug.println(this.d.directionPressed); if(this.d.directionPressed == 'h' && this.lab[this.i-1][this.j] != 1 && this.lab[this.i-1][this.j] != 2 && this.i>-1 && this.i<this.lab[0].length && this.j>-1 && this.j<this.lab.length) { this.direction = 'h'; this.d.directionPressed = 's'; return true; } if(this.d.directionPressed == 'b' && this.lab[this.i+1][this.j] != 1 && this.lab[this.i+1][this.j] != 2 && this.i>-1 && this.i<this.lab[0].length && this.j>-1 && this.j<this.lab.length) { this.direction = 'b'; this.d.directionPressed = 's'; return true; } if(this.d.directionPressed == 'g' && this.lab[this.i][this.j-1] != 1 && this.lab[this.i][this.j-1] != 2 && this.i>-1 && this.i<this.lab[0].length && this.j>-1 && this.j<this.lab.length) { this.direction = 'g'; this.d.directionPressed = 's'; return true; } if(this.d.directionPressed == 'd' && this.lab[this.i][this.j+1] != 1 && this.lab[this.i][this.j+1] != 2 && this.i>-1 && this.i<this.lab[0].length && this.j>-1 && this.j<this.lab.length) { this.direction = 'd'; this.d.directionPressed = 's'; return true; } return false; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 import fr.jussieu.script.*; import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; /** * @author humel * */ public class decor extends Canvas implements KeyListener { public int[][] lab; // private static Drawable d = null; private static JFrame f=null; private int width, height, x, y; private Image Im; private Graphics Gr; private Image[][] Images; public char directionPressed; // private TexturePaint texture; private Pacman pacman; public decor(int[][] t,int h, int l){ this.lab=t; this.width=l; this.height=h; Images = new Image[t.length][t[0].length]; } public void setDirectionPressed(int i) { if (i == 37) this.directionPressed='g'; if (i == 38) this.directionPressed='h'; if (i == 39) this.directionPressed='d'; if (i == 40) this.directionPressed='b'; } public void keyPressed(KeyEvent e){ this.setDirectionPressed(e.getKeyCode()); Deug.println(e.getKeyCode()); } public void keyReleased(KeyEvent e){ } public void keyTyped(KeyEvent e){ } public Dimension getPreferredSize() { return new Dimension(width,height); } public JFrame getf() { return f;} public void paint(Graphics g) { if (this.Im==null) { Im = createImage(this.width,this.height); this.Gr = Im.getGraphics(); } g.drawImage(Im,this.x,this.y,this); } public void update(Graphics g) { paint(g); } public void start(String s) { if (f!=null) return; f = new JFrame(s); f.setResizable(false); f.getContentPane().add(this); f.pack(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); try { Thread.sleep(3000); } catch(Exception e) {} for(int j=0;j<lab.length;j++){ for(int i=0;i<lab[0].length;i++){ if(lab[i][j]==1){ traceBloc(i,j); } if (lab[i][j]==0){ traceVide(i,j); } f.getContentPane().add(this); } f.getContentPane().add(this); } // this.f.setVisible(true); // this.t.start(); } public void traceBloc(int i,int j){ this.Images[i][j]= f.getToolkit().getImage("brick.gif"); this.Gr.drawImage(this.Images[i][j],25*i,25*j,this.f); /*// d.getGr().drawImage(f.getToolkit().getImage("brick.gif"),x,y,this.f); this.bufferedImage = this.toBufferedImage(Toolkit.getDefaultToolkit().getImage("brick.gif")); this.texture = new TexturePaint(Image,new Rectangle(0, 0, 25, 25)); Graphics2D g2d = (Graphics2D)this.Gr; g2d.setPaint(texture); g2d.fillRect(x,y,25,25); f.getContentPane().add(); */ f.getContentPane().add(this); } public void traceVide(int i,int j){ this.Images[i][j]= f.getToolkit().getImage("vide.gif"); this.Gr.drawImage(this.Images[i][j],i*25,j*25,this.f); // d.getGr().drawImage(f.getToolkit().getImage("brick.gif"),x,y,this.f); /* this.x=x; this.y=y; this.Im = f.getToolkit().getImage("vide.gif"); this.Gr.drawImage(Im,x,y,this.f); // this.Gr.drawImage(b,x,y,this.f); */ f.getContentPane().add(this); } public void tracePacman(int i,int j){ this.Images[i][j]= f.getToolkit().getImage("pacman.gif"); this.Gr.drawImage(this.Images[i][j],i*25,j*25,this.f); f.getContentPane().add(this); } }
Partager