Bonjour à tous.
Je suis en train de développer un jeu avec un ami graphiste et j'ai besoin de votre aide ! Voici mon problème :
Je souhaiterais créer un menu, avec deux boutons, start et garage, et lors du clic sur le bouton start ouvrir une nouvelle JFrame qui lance le jeu. La JFrame s'ouvre correctement, mais le jeu ne s'affiche pas, je redoute un conflit avec un JPanel ou ... je suis perdu
Ce problème se duplique, quand le jeu est fini, un GameOver s'affiche, et lorsque je veux rejouer, je créer une nouvelle JFrame, qui s'affiche, mais pas le jeu (contenu)
J'utilise un fichier de sauvegarde à côté.
Comment changer de fenêtre sans problème ?
Le code est plutôt lourd, et sans doute codé avec les pieds, ceci est mon premier projet.
Package def :
Main :
Game :
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354 import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Font; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class Main { // Base de donee : /* * 0 = money | 1 = lvl | 2 = XP | 3 = XPMax | 4 = id du vaisseau | 5 = * BulletLvl | 6 = LaserTourelleLvl | 7 = rocketteLvl * * Nom du vaiseau = niveau du craft - 1 */ private static double[] stats = new double[8]; private static int[] config = new int[1]; private static FileInputStream fis = null; private static FileOutputStream fos = null; private static File fichier = new File("save1"), configFile = new File("config"); private static int xFrame = 1000, yFrame = 800, scoreGlobal; private static Dimension fullScreen = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); public static String story = "Votre vaisseau s'est fait pirater ! Grâce au ciel vous avez réussi a vous échapper a bord d'une capsule de sauvetage, et avec l'objet si précieux, celui que tout le monde souhaite posséder : le Thorium. Selon la légende, cette pierre précieuse aurait le pouvoir de ramener les morts a la vie. Vous devez absolument la mettre en sécurité auprès du grand conseil des sages et sauver la galaxie ! Et c'est parti..."; public static String[] craftName = { "La patate de l'espace", "Le Frelon" }; public static int[] craftPrice = { 0, 500 }; public static boolean newPlayer; private static int nbError = 0, cleChiffrement = 125; @SuppressWarnings("deprecation") public static void main(String[] args) throws InterruptedException { System.out.println("Version 1.2"); loading(); loadingConfig(); if (newPlayer) { System.out.println("New Player !"); JFrame fen = new JFrame(); fen.setTitle("SpaceDriver - Story"); fen.setSize(300, 300); fen.setLocationRelativeTo(null); JPanel StoryPan = new JPanel(new BorderLayout()); String temp = ""; JTextArea text = new JTextArea(); Font font = new Font("ArtNoveauDecadente", 0, 16); JScrollPane scrollPan = new JScrollPane(text); scrollPan.setAutoscrolls(true); text.setEditable(false); text.setLineWrap(true); text.setWrapStyleWord(true); text.setFont(font); StoryPan.add(scrollPan, BorderLayout.CENTER); fen.setContentPane(StoryPan); fen.setVisible(true); for (int i = 0; i < story.length(); i++) { temp = temp + story.charAt(i); text.setText(temp); Thread.sleep(22); } Thread.sleep(5000); fen.disable(); } new Menu(); } public static int getStats(int i) { return (int) stats[i]; } public static void setStats(int e, int x) { stats[e] = x; } public static int getxFrame() { return xFrame; } public static int getyFrame() { return yFrame; } public static void setScore(int e) { scoreGlobal = e; } public static int getScore() { return scoreGlobal; } // --------------------- Player data --------------------- public static void loading() { try { fis = new FileInputStream(fichier); String sentence = ""; int Carac; int pos = 0; while ((Carac = fis.read()) != -1) { Carac -= cleChiffrement; if (Carac == 32) { stats[pos] = Double.valueOf(sentence); pos++; sentence = ""; } else { sentence = sentence + ((char) Carac); } } System.out.println("Loading :ok."); } catch (FileNotFoundException e) { System.out.println("Pas de fichier de sauvegarde."); System.out.println("New Player. Story ON."); newPlayer = true; try { fichier.createNewFile(); System.out.println("Fichier creer."); // Def du premier pos // 48 = 0 fos = new FileOutputStream(fichier); fos.write(48 + cleChiffrement); fos.write(32 + cleChiffrement); fos.write(49 + cleChiffrement); fos.write(32 + cleChiffrement); fos.write(48 + cleChiffrement); fos.write(32 + cleChiffrement); fos.write(51 + cleChiffrement); fos.write(32 + cleChiffrement); fos.write(49 + cleChiffrement); fos.write(32 + cleChiffrement); fos.write(49 + cleChiffrement); fos.write(32 + cleChiffrement); fos.write(48 + cleChiffrement); fos.write(32 + cleChiffrement); fos.write(48 + cleChiffrement); fos.write(32 + cleChiffrement); if (fis != null) { fis.close(); } fis = new FileInputStream(fichier); String sentence = ""; int Carac; int pos = 0; while ((Carac = fis.read()) != -1) { Carac -= cleChiffrement; if (Carac == 32) { stats[pos] = Double.valueOf(sentence); pos++; sentence = ""; } else { sentence = sentence + ((char) Carac); } } System.out.println("Loading :ok."); } catch (IOException s) { s.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } public static void save() { try { // Sauvegarde fichier.delete(); fichier.createNewFile(); fos = new FileOutputStream(fichier); char ascii; String sentence = ""; for (int i = 0; i < stats.length; i++) { sentence = String.valueOf((int) stats[i]); for (int j = 0; j < sentence.length(); j++) { ascii = sentence.charAt(j); fos.write(ascii + cleChiffrement); } fos.write(32 + cleChiffrement); } System.out.println("Save : ok."); } catch (FileNotFoundException e1) { System.out.println("Pas de sauvegarde."); } catch (IOException e) { } } // --------------------------------------------------- // --------------------- Config --------------------- public static void loadingConfig() { try { fis = new FileInputStream(configFile); String sentence = ""; int Carac; int pos = 0; while ((Carac = fis.read()) != -1) { if (Carac == 32) { config[pos] = Integer.parseInt(sentence); pos++; sentence = ""; } else { sentence = sentence + ((char) Carac); } } System.out.println("Config :ok."); } catch (FileNotFoundException e) { System.out.println("Pas de fichier de config."); try { configFile.createNewFile(); System.out.println("Fichier creer."); // Def du premier pos // 48 = 0 fos = new FileOutputStream(configFile); fos.write(49); if (fis != null) { fis.close(); } fis = new FileInputStream(configFile); String sentence = ""; int Carac; int pos = 0; while ((Carac = fis.read()) != -1) { if (Carac == 32) { config[pos] = Integer.parseInt(sentence); pos++; sentence = ""; } else { sentence = sentence + ((char) Carac); } } System.out.println("Config :ok."); } catch (IOException s) { s.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } public static void saveConfig() { try { // Sauvegarde configFile.delete(); configFile.createNewFile(); fos = new FileOutputStream(configFile); char ascii; String sentence = ""; for (int i = 0; i < config.length; i++) { sentence = String.valueOf((int) config[i]); for (int j = 0; j < sentence.length(); j++) { ascii = sentence.charAt(j); fos.write(ascii); } fos.write(32); } System.out.println("Save Config : ok."); } catch (FileNotFoundException e1) { System.out.println("Pas de Config."); } catch (IOException e) { } } // ---------------------------------------- public static void errorLog(Exception e) throws IOException { nbError++; File error = new File("ErrorLog" + nbError); error.createNewFile(); fos = new FileOutputStream(error); char ascii; String sentence = ""; sentence = e.toString(); e.printStackTrace(); for (int j = 0; j < sentence.length(); j++) { ascii = sentence.charAt(j); fos.write(ascii); } System.out.println("ErrorLogged."); if (fos != null) { fos.flush(); fos.close(); } } public static Dimension getDim() { return fullScreen; } public static int getXdim() { return (int) fullScreen.getWidth(); } public static int getYdim() { return (int) fullScreen.getHeight(); } public static void xpUp(int e) { stats[2] += e; } public static void setConfig(int place, int x) { config[place] = x; } public static int[] getConfig() { return config; } }
Menu :
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624 package def; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.io.IOException; import java.util.Timer; import java.util.TimerTask; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import animation.Destroy; public class Game extends JFrame implements MouseListener, MouseMotionListener, ActionListener, KeyListener { private static final long serialVersionUID = 1L; // Base de donee : /* * 0 = money | 1 = lvl | 2 = XP | 3 = XPMax | 4 = lvlCraft */ private static int yMouse, life; public static int backGroundSize, xBG; private static int score, moneyEarn, dif, yDir; public static int distance, timeLaser, coolDownLaser, timeForLaser; private static boolean boss, play; private static boolean laser; public static int yLaser; private JPanel game = new JPanel(new BorderLayout()); public static Panneau pan = new Panneau(); private static JLabel Info = new JLabel(); private static Timer updateMonster, spawn, backGround, updateBullet, updateBulletM, hit, outOfBorder; private static TimerTask updateMonsterCoor, spawnMonster, backGroundMove, updateBulletCoor, updateBulletMCoor, box, outOfBorderTest; public static entity.Balle[] Bul = new entity.Bullet[15]; public static entity.Balle[] MonsterB = new entity.BulletE[30]; public static entity.Balle[] Rocket = new entity.Rocket[2]; public static entity.Mob[] Monster = new entity.Monster[6]; public static entity.Mob Boss; public static entity.Craft Craft; public static animation.Destroy[] Explo = new animation.Destroy[Monster.length]; // Menu private JMenuBar menuBar = new JMenuBar(); private JMenu option = new JMenu("Option"); private JMenuItem config = new JMenuItem("Configuration"), quitter = new JMenuItem("Quitter"); // GameOver JPanel gameOver; JButton again; // Son public static Audio BossSong = new Audio("BossSong.wav"); public static Audio son = new Audio("BS.wav"); public Game() { this.setSize(Main.getDim()); this.setResizable(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setTitle("SpaceDriver"); // ------------------- GameOver ---------------------- gameOver = new JPanel(); again = new JButton("Play again"); again.addActionListener(this); gameOver.add(new JLabel(new ImageIcon("Texture/GameOver.png"))); gameOver.add(new JLabel("Score : " + def.Main.getScore() + " " + "Distance : " + Game.distance + "m")); gameOver.add(again); // --------------------------------------------------- setInfo(); game.add(pan, BorderLayout.CENTER); game.add(Info, BorderLayout.NORTH); this.setFocusable(true); this.requestFocusInWindow(true); this.addKeyListener(this); pan.addMouseListener(this); pan.addMouseMotionListener(this); config.addActionListener(this); quitter.addActionListener(this); option.add(config); option.add(quitter); menuBar.add(option); this.setJMenuBar(menuBar); this.setContentPane(game); this.setVisible(true); go(); } public static void initValue() { Craft = new entity.Craft(); life = 1; backGroundSize = 3090; xBG = backGroundSize; score = 0; dif = 1; moneyEarn = 0; distance = 0; boss = false; laser = false; if (Main.getStats(6) != 0) { coolDownLaser = (int) 2500 / Main.getStats(6); } timeForLaser = 0; play = false; } public void actionPerformed(ActionEvent e) { if (e.getSource() == config) { new gui.Config(); } if (e.getSource() == quitter) { System.exit(0); } // if (e.getSource() == again) { // play = true; // this.setSize(Main.getDim()); // this.setLocation(0, 0); // this.setContentPane(menu); // this.setVisible(true); // } } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { if (e.getButton() == 1) { for (int i = 0; i < Bul.length; i++) { if (Bul[i] == null) { Bul[i] = new entity.Bullet((int) Craft.getX() + Craft.getposTir()[0][0], (int) Craft.getY() + Craft.getposTir()[0][1]); break; } } } else if (Main.getStats(7) != 0 && e.getButton() == 3) { for (int i = 0; i < Rocket.length; i++) { if (Rocket[i] == null) { Rocket[i] = new entity.Rocket((int) Craft.getX() + Craft.getposTir()[1][0], (int) Craft.getY() + Craft.getposTir()[1][1]); break; } } } else if (Main.getStats(6) != 0 && e.getButton() == 2 && timeForLaser <= 0) { yLaser = (int) Craft.getY() + Craft.getposTir()[2][1]; laser = true; for (int i = 0; i < Monster.length; i++) { if (Monster[i] != null) { if (yLaser >= Monster[i].getY() + 23 && yLaser <= Monster[i].getY() + 102) { Monster[i] = null; score++; Main.xpUp(1); moneyEarn++; setInfo(); } } } } } public void mouseReleased(MouseEvent e) { } public void mouseDragged(MouseEvent e) { yDir = e.getY() - Craft.getPosCurseur(); } public void mouseMoved(MouseEvent e) { yDir = e.getY() - Craft.getPosCurseur(); } public static void go() { while (life > 0 && play) { if (Craft.getY() - yDir > 0.5) { Craft.upY(-Craft.getSpeed()); } else if (Craft.getY() - yDir < -0.5) { Craft.upY(Craft.getSpeed()); } try { Thread.sleep(2); } catch (InterruptedException e) { try { Main.errorLog(e); } catch (IOException e1) { e1.printStackTrace(); } } // Alternative // pan1.paintComponent(getGraphics()); // pan1.update(getGraphics()); // pan1.updateUI(); pan.repaint(); } Main.setStats(0, Main.getStats(0) + moneyEarn); Main.save(); Main.setScore(score); // this.setEnabled(false); // this.setVisible(false); // new gui.GameOver(); } public void cancelTimer() { spawn.cancel(); spawn.purge(); updateMonster.cancel(); updateMonster.purge(); backGround.cancel(); backGround.purge(); updateBullet.cancel(); updateBullet.purge(); updateBulletM.cancel(); updateBulletM.purge(); outOfBorder.cancel(); outOfBorder.purge(); hit.cancel(); hit.purge(); } public static void createTask() { // ---------------------Monster ---------------------------- spawnMonster = new TimerTask() { @Override public void run() { if (!boss && play) { for (int i = 0; i < Monster.length; i++) { if (Monster[i] == null) { Monster[i] = new entity.Monster(2 * dif, dif); break; } } } } }; updateMonsterCoor = new TimerTask() { @Override public void run() { if (play) { for (int i = 0; i < Monster.length; i++) { if (Monster[i] != null) { Monster[i].updateCoor(); Monster[i].fire(); } } if (Boss != null) { Boss.updateCoor(); Boss.fire(); } } } }; // --------------------------------------------------------- // ---------------------BackGround-------------------------- backGroundMove = new TimerTask() { @SuppressWarnings("deprecation") @Override public void run() { if (play) { timeForLaser--; xBG--; if (xBG == 0) { xBG = backGroundSize; } distance++; if (distance == 10000) { boss = true; Boss = new entity.Boss(distance / 10); // Chevauchement des deux son... son.stop(); BossSong.start(); } setInfo(); } } }; // --------------------------------------------------------- // ------------------ Bullet ------------------------------- updateBulletCoor = new TimerTask() { @Override public void run() { for (int i = 0; i < Bul.length; i++) { if (Bul[i] != null) { Bul[i].updateCoor(); if (Bul[i].getX() >= Main.getXdim()) { Bul[i] = null; } } } for (int i = 0; i < Rocket.length; i++) { if (Rocket[i] != null) { Rocket[i].updateCoor(); if (Rocket[i].getX() >= Main.getXdim()) { Rocket[i] = null; } } } } }; updateBulletMCoor = new TimerTask() { @Override public void run() { for (int i = 0; i < MonsterB.length; i++) { if (MonsterB[i] != null) { MonsterB[i].updateCoor(); } } } }; // --------------------------------------------------------- // ------------------ Hit box ------------------------------ box = new TimerTask() { @Override public void run() { for (int j = 0; j < Monster.length; j++) { for (int i = 0; i < Bul.length; i++) { if (Monster[j] != null && Bul[i] != null) { if (Bul[i].getX() >= Monster[j].getX() && Bul[i].getX() <= Monster[j].getX() + 246 && Bul[i].getY() >= Monster[j].getY() + 23 && Bul[i].getY() <= Monster[j].getY() + 102) { Monster[j].removeLife(Bul[i].getPuissance()); Bul[i] = null; if (Monster[j].getLife() <= 0) { for (int z = 0; z < Explo.length; z++) { if (Explo[z] == null) { Explo[z] = new Destroy(Monster[j].getX(), Monster[j].getY()); break; } } Monster[j] = null; score++; moneyEarn++; Main.xpUp(1); setInfo(); } } } } } for (int i = 0; i < MonsterB.length; i++) { if (MonsterB[i] != null) { // Bug if (MonsterB[i].getX() < Craft.getFrontHB() && MonsterB[i].getY() > Craft.getUpHB() && MonsterB[i].getY() < Craft.getDownHB()) { life -= MonsterB[i].getPuissance(); setInfo(); MonsterB[i] = null; } } } for (int j = 0; j < Monster.length; j++) { for (int i = 0; i < Rocket.length; i++) { if (Monster[j] != null && Rocket[i] != null) { if (Rocket[i].getX() >= Monster[j].getX() && Rocket[i].getX() <= Monster[j].getX() + 246 && Rocket[i].getY() >= Monster[j].getY() + 23 && Rocket[i].getY() <= Monster[j].getY() + 102) { Monster[j].removeLife(Rocket[i].getPuissance()); if (Monster[j].getLife() <= 0) { for (int z = 0; z < Explo.length; z++) { if (Explo[z] == null) { Explo[z] = new Destroy(Monster[j].getX(), Monster[j].getY()); break; } } Monster[j] = null; score++; moneyEarn++; Main.xpUp(1); setInfo(); } Rocket[i] = null; } } } } // Boss if (Boss != null) { for (int i = 0; i < Bul.length; i++) { if (Bul[i] != null) { if (Bul[i].getY() <= Boss.getY() + 366 && Bul[i].getY() >= Boss.getY() + 60 && Bul[i].getX() >= Boss.getX()) { Boss.removeLife(Bul[i].getPuissance()); Bul[i] = null; if (Boss.getLife() <= 0) { for (int z = 0; z < Explo.length; z++) { if (Explo[z] == null) { Explo[z] = new Destroy(Boss.getX(), Boss.getY()); break; } } Boss = null; Main.xpUp(300); score += 150; moneyEarn += 100; setInfo(); boss = false; dif *= 4; life += 15; break; } } } } for (int i = 0; i < Rocket.length; i++) { if (Rocket[i] != null) { if (Rocket[i].getY() <= Boss.getY() + 366 && Rocket[i].getY() >= Boss.getY() + 60 && Rocket[i].getX() >= Boss.getX()) { Boss.removeLife(Rocket[i].getPuissance()); Rocket[i] = null; if (Boss.getLife() <= 0) { for (int z = 0; z < Explo.length; z++) { if (Explo[z] == null) { Explo[z] = new Destroy(Boss.getX(), Boss.getY()); break; } } Boss = null; Main.xpUp(300); score += 150; moneyEarn += 100; setInfo(); boss = false; dif *= 4; life += 15; break; } } } } } } }; // --------------------------------------------------------- // ----------------- Out of border ------------------------- outOfBorderTest = new TimerTask() { @Override public void run() { for (int i = 0; i < Bul.length; i++) { if (Bul[i] != null) { if (Bul[i].getX() > Main.getXdim() + 5) Bul[i] = null; } } for (int i = 0; i < MonsterB.length; i++) { if (MonsterB[i] != null) { if (MonsterB[i].getX() < -5) MonsterB[i] = null; } } for (int i = 0; i < Explo.length; i++) { if (Explo[i] != null) { if (Explo[i].state >= 7) { Explo[i] = null; } } } } }; // --------------------------------------------------------- } public static void startTimer() { spawn = new Timer(); spawn.schedule(spawnMonster, 1, 500); updateMonster = new Timer(); updateMonster.schedule(updateMonsterCoor, 1, 4); updateBullet = new Timer(); backGround = new Timer(); backGround.schedule(backGroundMove, 1, 5); updateBullet.schedule(updateBulletCoor, 1, 1); updateBulletM = new Timer(); updateBulletM.schedule(updateBulletMCoor, 1, 2); hit = new Timer(); hit.schedule(box, 1, 1); outOfBorder = new Timer(); outOfBorder.schedule(outOfBorderTest, 1, 1); } public static int posMouseY() { return yMouse; } public static void removeLife(int power) { life -= power; } public static int getScore() { return score; } public static void setInfo() { Info.setText("Score = " + String.valueOf(score) + " Vie =" + String.valueOf(life) + " Distance : " + distance + "m"); } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); // System de pause ------ A FINIR // if (e.getKeyCode() == KeyEvent.VK_SPACE) { // if (pause) { // pause = false; // startTimer(); // System.out.println("Pause"); // } else { // System.out.println("Play"); // pause = true; // cancelTimer(); // } // } if (key == KeyEvent.VK_1) { for (int i = 0; i < Bul.length; i++) { if (Bul[i] == null) { Bul[i] = new entity.Bullet((int) Craft.getX() + Craft.getposTir()[0][0], (int) Craft.getY() + Craft.getposTir()[0][1]); break; } } } if (key == KeyEvent.VK_2) { for (int i = 0; i < Rocket.length; i++) { if (Rocket[i] == null) { Rocket[i] = new entity.Rocket((int) Craft.getX() + Craft.getposTir()[1][0], (int) Craft.getY() + Craft.getposTir()[1][1]); break; } } } if (key == KeyEvent.VK_3) { if (Main.getStats(6) != 0 && timeForLaser <= 0) { yLaser = (int) Craft.getY() + Craft.getposTir()[2][1]; laser = true; for (int i = 0; i < Monster.length; i++) { if (Monster[i] != null) { if (yLaser >= Monster[i].getY() + 23 && yLaser <= Monster[i].getY() + 102) { Monster[i] = null; score++; Main.xpUp(1); moneyEarn++; setInfo(); } } } } } if (key == KeyEvent.VK_UP) { if (yDir > -Craft.getposTir()[0][1]) yDir -= 25; } if (key == KeyEvent.VK_DOWN) { if (yDir < Main.getYdim() - Craft.getposTir()[0][1]) yDir += 25; } if (key == KeyEvent.VK_W) { if (yDir > -Craft.getposTir()[0][1]) yDir -= 25; } if (key == KeyEvent.VK_S) { if (yDir < Main.getYdim() - Craft.getposTir()[0][1]) yDir += 25; } } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } public static boolean getLaser() { return laser; } public static void setLaser(boolean i) { laser = i; } }
MenuPan :
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 package def; import javax.swing.JFrame; public class Menu extends JFrame{ private static final long serialVersionUID = 1L; public Menu(){ this.setTitle("Menu"); this.setSize(Main.getDim()); this.setResizable(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setContentPane(new MenuPan()); this.setVisible(true); } }
Panneau :
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 package def; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; public class MenuPan extends JPanel implements ActionListener { private static final long serialVersionUID = 1L; private JPanel centerMenu = new JPanel(new GridLayout(3, 1)); private JButton start, garage; private JLabel imgDroite = new JLabel(new ImageIcon("Texture/bannerDroite.png")), imgGauche = new JLabel( new ImageIcon("Texture/bannerDroite.png")), imgHaut = new JLabel(new ImageIcon( "Texture/SpaceDriverBanner.png")), imgBas = new JLabel(new ImageIcon("Texture/skull1.png")); public MenuPan() { this.setLayout(new BorderLayout()); start = new JButton("Start!"); start.addActionListener(this); garage = new JButton("Garage"); garage.addActionListener(this); centerMenu.add(start); centerMenu.add(garage); JLabel by = new JLabel("Jeu imagé par Vivien F. et développé par Maxence M. (5 Dec. 2016)"); by.setHorizontalAlignment(JLabel.CENTER); centerMenu.add(by); centerMenu.setBackground(Color.white); this.add(centerMenu, BorderLayout.CENTER); this.add(imgDroite, BorderLayout.EAST); this.add(imgGauche, BorderLayout.WEST); this.add(imgHaut, BorderLayout.NORTH); this.add(imgBas, BorderLayout.SOUTH); this.setBackground(Color.white); } public void actionPerformed(ActionEvent e) { if (e.getSource() == start) { super.setVisible(false); super.setEnabled(false); new Game(); } if (e.getSource() == garage) { new gui.Garage(); } } }
Voila voila,
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 package def; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JPanel; import animation.Destroy; import entity.Balle; import entity.Mob; public class Panneau extends JPanel { private static final long serialVersionUID = 1L; private Image imgBG0, imgCloud, imgCloud2, imgCloud3, imgLaser; private Image[] Explo = new Image[8]; public Panneau() { load(); } public void paintComponent(Graphics g) { // Background if (imgBG0 == null) { g.setColor(Color.white); g.fillRect(0, 0, Main.getXdim(), def.Main.getYdim()); } else { g.drawImage(imgBG0, Game.xBG, 0, this); g.drawImage(imgBG0, Game.xBG - Game.backGroundSize, 0, this); } // Craft g.drawImage(Game.Craft.getImg(), (int) Game.Craft.getX(), (int) Game.Craft.getY(), this); // bullet for (Balle bul : Game.Bul) { if (bul != null) { g.drawImage(bul.getImg(), bul.getX(), bul.getY(), this); } } // Monster for (Mob monster : Game.Monster) { if (monster != null) { g.drawImage(monster.getImg(), monster.getX(), monster.getY(), this); } } // bullet Monster for (Balle monster : Game.MonsterB) { if (monster != null) { g.drawImage(monster.getImg(), monster.getX(), monster.getY(), this); } } // cloud & Rocket for (Balle rocket : Game.Rocket) { if (rocket != null) { g.drawImage(imgCloud, rocket.getCloudPos()[0], rocket.getY() + 8, this); g.drawImage(imgCloud2, rocket.getCloudPos()[1], rocket.getY() + 8, this); g.drawImage(imgCloud3, rocket.getCloudPos()[2], rocket.getY() + 8, this); g.drawImage(rocket.getImg(), rocket.getX(), rocket.getY(), this); } } // Explosion animation for (int i = 0; i < Game.Explo.length; i++) { if (Game.Explo[i] != null) { Game.Explo[i].updateState(); g.drawImage(Explo[Game.Explo[i].state], Destroy.x, Destroy.y, this); } } // boss if (Game.Boss != null) { g.drawImage(Game.Boss.getImg(), Game.Boss.getX(), Game.Boss.getY(), this); } // Laser if (def.Game.getLaser()) { g.drawImage(imgLaser, Game.Craft.getposTir()[2][0], Game.yLaser + 3, this); Game.timeLaser++; if (Game.timeLaser >= 50) { Game.setLaser(false); Game.timeLaser = 0; Game.timeForLaser = Game.coolDownLaser; } } } public void load() { /* * Chargement des images */ try { imgBG0 = ImageIO.read(new File("Texture/BG0.jpg")); // clouds imgCloud = ImageIO.read(new File("Texture/cloud.png")); imgCloud2 = ImageIO.read(new File("Texture/cloud2.png")); imgCloud3 = ImageIO.read(new File("Texture/cloud3.png")); // Explosion for (int i = 0; i < Explo.length; i++) { Explo[i] = ImageIO.read(new File("Texture/Explosion" + (i + 1) + ".png")); } // Laser imgLaser = ImageIO.read(new File("Texture/Laser.png")); } catch (IOException e) { try { Main.errorLog(e); } catch (IOException e1) { e1.printStackTrace(); } } } }
Merci pour votre temps,
Lumix.
Partager