Bonjour, je me suis récemment lancé dans un projet de casse brique en java et je suis en proie à un bug que je n'arrive pas à identifier/réctifier. (Au passage, je commence tout juste la programmation par moi-même, je n'ai pas pris de cours en java).
Je vous présente mon problème :
Lorsque la balle arrive en dessous d'une brique, au milieu de celle-ci, elle réagit normalement: casse la brique et repart. Mais lorsqu'elle atteint une brique dans l'extrémité, elle casse la brique mais ne repart pas donc casse une deuxième brique et il se peut qu'elle se fasse un chemin à travers les briques alors que ce n'est normalement pas possible.
voici mon code :
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.KeyListener;
 
public class Game extends JPanel{
	//int mapNbBriqueW = 15;
	//int mapNbBriqueH = 30;
	int mapNbBriqueW = 30;						//Nombre briques en longueur
	int mapNbBriqueH = 45;						//Nombre briques en hauteur
	int mapBriqueW = 45;						//Longueur d'une brique
	int mapBriqueH = 15;						//Hauteur d'une brique
	int mapW = mapNbBriqueW*mapBriqueW;			//Longueur de la fenêtre
	int mapH = mapNbBriqueH*mapBriqueH;			//Hauteur de la fenêtre
	int longT = 100;							//Longueur de la raquette
	int xr = mapW/2-longT/2;						//Placement raquette
	int xr2 = xr;
	//int yr = mapH/2;
	int x = 100;									//Initialisation de la balle en (0; 0)
	int y = 150;
	int incX = 1;								//Incrément des vitesses
	int incVitesse = (int)(mapW/600*1.2);
	//int incVitesse = 1;
	int incXVitesse = incVitesse;
	int incY = 1;
	int incYVitesse = incVitesse;
	int gameOver = 0;
	int trait2 = 0;
	int touche = KeyEvent.VK_UNDEFINED;
	int map[][]={								//"dessin" de la carte
 
			//map #1
			{1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
			{1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
			{1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
			{1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
			{1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
			{1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,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
			{1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
			{1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
	};
 
 
	public Game(JFrame pFrame) {				//Réception des touches
		pFrame.setSize(mapW+19, mapH+48+50);
		addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {
			}
 
			@Override
			public void keyReleased(KeyEvent e) {
				touche = KeyEvent.VK_UNDEFINED;
			}
 
			@Override
			public void keyPressed(KeyEvent e) {
				touche = e.getKeyCode();
			}
		});
		setFocusable(true);
	}
 
	public void trait_move() {					//Propriétés raquette
		if (touche == KeyEvent.VK_LEFT)
			xr-= 10*incVitesse/4;
		if (touche == KeyEvent.VK_RIGHT)
			xr+= 10*incVitesse/4;
		/*if (touche == KeyEvent.VK_UP)
			yr-= 10*incVitesse/4;
		if (touche == KeyEvent.VK_DOWN)
			yr+= 10*incVitesse/4;*/
		if(xr >= mapW-longT)
			xr = mapW-longT;
		if(xr <= 0)
			xr = 0;
	}
 
	public void trait2_move() {					
		if (touche == KeyEvent.VK_RIGHT)
			xr2-= 10*incVitesse/4;
		if (touche == KeyEvent.VK_LEFT)
			xr2+= 10*incVitesse/4;
		/*if (touche == KeyEvent.VK_UP)
			yr-= 10*incVitesse/4;
		if (touche == KeyEvent.VK_DOWN)
			yr+= 10*incVitesse/4;*/
		if(xr2 >= mapW-longT)
			xr2 = mapW-longT;
		if(xr2 <= 0)
			xr2 = 0;
	}
 
	@Override
	public void paint(Graphics g) {				//Formes et couleurs
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		for (int i=0; i<map.length; i++) {
			for (int j=0; j<map[i].length; j++) {
				Color briqueColor = Color.RED;
				switch (map[i][j]) {
					case 1:
						briqueColor = Color.RED;
						break;
					case 2:
						briqueColor = Color.YELLOW;
						break;
					case 3:
						briqueColor = Color.BLUE;
						break;
					case 4:
						briqueColor = Color.GRAY;
						break;
					case 5:
						briqueColor = Color.MAGENTA;
						break;
					case 6:
						briqueColor = Color.GREEN;
						break;
				}
				g2d.setColor(briqueColor);				//Briques
				if (map[i][j]>0)
					g2d.fillRect(i*mapBriqueW, j*mapBriqueH, mapBriqueW-1, mapBriqueH-1);
				}
		}		
 
		g2d.setColor(Color.BLUE);				//Balle
		g2d.fillOval(x, y, 15, 15);
		g2d.setColor(Color.DARK_GRAY);				//Raquette
		g2d.fillRect(xr, /*yr*/mapH-15, longT, 3);
		g2d.setColor(Color.MAGENTA);			//Contours de la map
		g2d.drawRect(0, 0, mapW, mapH);
		g2d.setColor(Color.BLACK);
 
		//Légende
		g2d.setColor(Color.RED);
		g2d.fillRect(500, mapH+48-40, mapBriqueW, mapBriqueH);
		g2d.setColor(Color.YELLOW);
		g2d.fillRect(500, mapH+48-25/2, mapBriqueW, mapBriqueH);
		g2d.setColor(Color.BLUE);
		g2d.fillRect(750, mapH+48-40, mapBriqueW, mapBriqueH);
		g2d.setColor(Color.DARK_GRAY);
		g2d.fillRect(750, mapH+48-25/2, mapBriqueW, mapBriqueH);
		g2d.setColor(Color.MAGENTA);
		g2d.fillRect(1000, mapH+48-40, mapBriqueW, mapBriqueH);
		g2d.setColor(Color.GREEN);
		g2d.fillRect(1000, mapH+48-25/2, mapBriqueW, mapBriqueH);
		g2d.setColor(Color.DARK_GRAY);
		g2d.drawString("Brique '1'", 500+mapBriqueW, mapH+48-40+10);
		g2d.drawString("Brique '2'", 500+mapBriqueW, mapH+48-25/2+10);
		g2d.drawString("Brique '3'", 750+mapBriqueW, mapH+48-40+10);
		g2d.drawString("Malus (Raquette /2)", 750+mapBriqueW, mapH+48-25/2+10);
		g2d.drawString("Bonus (Raquette *2)", 1000+mapBriqueW, mapH+48-40+10);
		g2d.drawString("Fin du niveau", 1000+mapBriqueW, mapH+48-25/2+10);
		String result = String.format("coordonnées de la balle : %d,%d", x, y);
		g2d.drawString(result, 10, mapH+48-25);
		result = String.format("coordonnées de la balle (briques) : %d,%d", x/mapBriqueW, y/mapBriqueH);
		g2d.drawString(result, 10, mapH+48-25/2);
		/*result = String.format("map.length = %d, map[0].length = %d", map.length, map[0].length);
		g2d.drawString(result, 200, mapH+48-25);*/
 
		if (gameOver==1) {
			g2d.setColor(Color.DARK_GRAY);
			g.setFont(new Font("TimesRoman", Font.BOLD, 50)); 
			g2d.drawString("Fin du jeu", mapW/2-75, mapH/2);
		}
 
		if (gameOver==2) {
			g2d.setColor(Color.DARK_GRAY);
			g.setFont(new Font("TimesRoman", Font.PLAIN, 50)); 
			g2d.drawString("Niveau terminé", mapW/2-125, mapH/2);
		}
 
		if (trait2==0) {
		}
 
		if (trait2==1) {
			g2d.setColor(Color.RED);
			g2d.fillRect(xr2, /*yr*/mapH-15, longT, 3);
		}
 
	}
 
	public void balle_move() throws InterruptedException {					//Propriétés balle
		if(y < mapH) {
			if(map[x/mapBriqueW][y/mapBriqueH]>0 && map[x/mapBriqueW][y/mapBriqueH]<4) {
				map[x/mapBriqueW][y/mapBriqueH]--;
				incY = incY*-1;
			}
			else if(map[x/mapBriqueW][y/mapBriqueH]==4) {
				map[x/mapBriqueW][y/mapBriqueH]=0;
				if (trait2==0) {
					longT = longT/2;
					incY = incY*-1;
					//Thread.sleep(5000);
					//longT = 100;
				}
				else {
					trait2 = 0;
				}
			}
			else if(map[x/mapBriqueW][y/mapBriqueH]==5) {
				map[x/mapBriqueW][y/mapBriqueH]=0;
				trait2 = 1;
				incY = incY*-1;
			}
			else if(map[x/mapBriqueW][y/mapBriqueH]==6) {
				map[x/mapBriqueW][y/mapBriqueH]=0;
				incVitesse = 0;
				gameOver=2;
			}
			if(x+incX*incXVitesse*incVitesse <= 0 || x+incX*incXVitesse*incVitesse >= mapW-15)
				incX = incX*-1;
			if(y+incY*incYVitesse*incVitesse <= 0 /*|| y+incYVitesse*incVitesse >= mapH-15*/)
				incY = incY*-1;
			if(y+incY*incYVitesse*incVitesse >= mapH-30+3 && (x+incX*incXVitesse*incVitesse+15/2 >= xr && x+incX*incXVitesse*incVitesse+15/2 <= xr+longT)) {
				if(x+incX*incXVitesse*incVitesse+15/2 >= xr+longT/4 && x+incX*incXVitesse*incVitesse+15/2 <= xr+longT/2+longT/4) {
					incXVitesse = incVitesse;
					System.out.print("vitesse normale\nok\n");
				}
				else if(x+incX*incXVitesse*incVitesse+15/2 <= xr+longT/10 || x+incX*incXVitesse*incVitesse+15/2 >= xr+longT/2+4*longT/10) {
					if(incX<0 && x+incX*incXVitesse*incVitesse+15/2 >= xr+longT/2+4*longT/10) {
						incXVitesse = 2*incVitesse;
						incX = -1;
						System.out.print("vitesse x2\nretour\nok\n");
					}
					else if(incX<0 && x+incX*incXVitesse*incVitesse+15/2 <= xr+longT/10) {
						incXVitesse = 2*incVitesse;
						System.out.print("vitesse x2\nretour\nok\n");
					}
					else if(incX>0 && x+incX*incXVitesse*incVitesse+15/2 >= xr+longT/2+4*longT/10) {
						incXVitesse = 2*incVitesse;
						System.out.print("vitesse x2\nretour\nok\n");
					}
					else if(incX>0 && x+incX*incXVitesse*incVitesse+15/2 <= xr+longT/10) {
						incXVitesse = 2*incVitesse;
						incX = -1;
						System.out.print("vitesse x2\nretour\nok\n");
					}
				}
				else{
					incXVitesse = 2*incVitesse;
					System.out.print("vitesse x2\nok\n");
				}
				incY = -1;
			}
			if (trait2==1) {
				if(y+incY*incYVitesse*incVitesse >= mapH-30+3 && (x+incX*incXVitesse*incVitesse+15/2 >= xr2 && x+incX*incXVitesse*incVitesse+15/2 <= xr2+longT)) {
					if(x+incX*incXVitesse*incVitesse+15/2 >= xr2+longT/4 && x+incX*incXVitesse*incVitesse+15/2 <= xr2+longT/2+longT/4) {
						incXVitesse = incVitesse;
						System.out.print("vitesse normale\nok\n");
					}
					else if(x+incX*incXVitesse*incVitesse+15/2 <= xr2+longT/10 || x+incX*incXVitesse*incVitesse+15/2 >= xr2+longT/2+4*longT/10) {
						if(incX<0 && x+incX*incXVitesse*incVitesse+15/2 >= xr2+longT/2+4*longT/10) {
							incXVitesse = 2*incVitesse;
							incX = -1;
							System.out.print("vitesse x2\nretour\nok\n");
						}
						else if(incX<0 && x+incX*incXVitesse*incVitesse+15/2 <= xr2+longT/10) {
							incXVitesse = 2*incVitesse;
							System.out.print("vitesse x2\nretour\nok\n");
						}
						else if(incX>0 && x+incX*incXVitesse*incVitesse+15/2 >= xr2+longT/2+4*longT/10) {
							incXVitesse = 2*incVitesse;
							System.out.print("vitesse x2\nretour\nok\n");
						}
						else if(incX>0 && x+incX*incXVitesse*incVitesse+15/2 <= xr2+longT/10) {
							incXVitesse = 2*incVitesse;
							incX = -1;
							System.out.print("vitesse x2\nretour\nok\n");
						}
					}
					else{
						incXVitesse = 2*incVitesse;
						System.out.print("vitesse x2\nok\n");
					}
					incY = -1;
				}
			}
			x = x+incX*incXVitesse*incVitesse;
			y = y+incY*incYVitesse*incVitesse;
		}
		else {									//Perdu
			gameOver = 1;
			incVitesse = 0;
			//System.out.println("Fin du jeu");
		}
	}
 
	public static void main(String[] args) throws InterruptedException {
		JFrame frame = new JFrame("Jeu");
		Game jeu = new Game(frame);
		frame.add(jeu);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
		while(true){
			jeu.balle_move();
			jeu.trait_move();
			jeu.trait2_move();
			jeu.repaint();
			Thread.sleep(10);
		}
	}
}
Les collisions de ma balle sont codés à la ligne 213 jusqu'à la ligne 317.
Voici mon problème :
Nom : Capture.PNG
Affichages : 414
Taille : 27,7 Ko

Merci d'avance pour votre aide et votre temps.
Cordialement
Paul