Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Université Cadi Ayyad P.O.

O (JAVA) Année universitaire


EST-Safi S.E. Abdellaoui & I. Kachbal 2023-2024

TP N°9 : Jeu de tir en Java Swing avec threads - Correction

Classe Target :
1 class Target {
2 private int x;
3 private int y;
4

5 public Target() {
6 this.x = (int) (Math.random() * 760);
7 this.y = 0;
8 }
9

10 public void move() {


11 y += 2;
12 }
13

14 public int getX() {


15 return x;
16 }
17

18 public int getY() {


19 return y;
20 }
21

22 public boolean collidesWith(Projectile projectile) {


23 int projX = projectile.getX();
24 int projY = projectile.getY();
25

26 return x < projX + 5 && x + 20 > projX && y < projY + 5 && y + 20 > projY;
27 }
28 }

Classe Projectile :
1 class Projectile
2 { private int
3 x; private
4 int y;
5

6 public Projectile(int x) {
7 this.x = x + 18; // Adjust for cannon width
8 this.y = 500;
9 }
10

11 public void move() {


12 y -= 5;
13 }
14

15 public int getX() {


16 return x;
17 }
18

19 public int getY() {


20 return y;
}

S.E. Abdellaoui & I. Kachbal 1 P.O.O


(JAVA)
Université Cadi Ayyad P.O.O (JAVA) Année universitaire
EST-Safi S.E. Abdellaoui & I. Kachbal 2023-2024

21 }

Classe ShootingGame :
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import java.util.ArrayList;
5 import java.util.Iterator;
6

7 public class ShootingGame extends JFrame {


8 private GamePanel gamePanel;
9 private ArrayList<Projectile> projectiles;
10 private ArrayList<Target> targets;
11 private int score;
12 private boolean isRunning;
13 private int cannonX;
14

15 public ShootingGame() {
16 setTitle("Shooting Game");
17 setSize(800, 600);
18 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
19

20 gamePanel = new GamePanel();


21 add(gamePanel);
22

23 projectiles = new ArrayList<>();


24 targets = new ArrayList<>();
25 score = 0;
26 isRunning = true;
27 cannonX = 400;
28

29 addKeyListener(new KeyAdapter() {
30 @Override
31 public void keyPressed(KeyEvent e) {
32 if (e.getKeyCode() == KeyEvent.VK_SPACE) {
33 projectiles.add(new Projectile(cannonX));
34 }
35 if (e.getKeyCode() == KeyEvent.VK_P) {
36 isRunning = !isRunning;
37 }
38 if (e.getKeyCode() == KeyEvent.VK_LEFT) {
39 if (cannonX > 0) {
40 cannonX -= 10;
41 }
42 }
43 if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
44 if (cannonX < 760) {
45 cannonX += 10;
46 }
47 }
48 }
49 });
50

51 Timer targetTimer = new Timer(1000, new ActionListener() {


52 public void actionPerformed(ActionEvent e) {

S.E. Abdellaoui & I. Kachbal 2 P.O.O


(JAVA)
Université Cadi Ayyad P.O.O (JAVA) Année universitaire
EST-Safi S.E. Abdellaoui & I. Kachbal 2023-2024

53 if (isRunning) {
54 targets.add(new Target());
55 }
56 }
57 });
58 targetTimer.start();
59

60 Timer gameTimer = new Timer(10, new ActionListener() {


61 public void actionPerformed(ActionEvent e) {
62 if (isRunning) {
63 Iterator<Projectile> iteratorP = projectiles.iterator();
64 while (iteratorP.hasNext()) {
65 Projectile p = iteratorP.next();
66 p.move();
67 if (p.getY() < 0) {
68 iteratorP.remove();
69 }
70 }
71

72 Iterator<Target> iteratorT = targets.iterator();


73 while (iteratorT.hasNext()) {
74 Target t = iteratorT.next();
75 t.move();
76 for (Projectile p : projectiles) {
77 if (t.collidesWith(p)) {
78 iteratorT.remove();
79 score += 10;
80 }
81 }
82 }
83 }
84

85 gamePanel.repaint();
86 }
87 });
88 gameTimer.start();
89 }
90

91 private class GamePanel extends JPanel {


92 public int getCanonX() {
93 return cannonX;
94 }
95

96 @Override
97 protected void paintComponent(Graphics g) {
98 super.paintComponent(g);
99

100 g.setColor(Color.BLACK);
101 g.fillRect(cannonX, 500, 40, 20);
102

103 g.setColor(Color.RED);
104 for (Projectile p : projectiles) {
105 g.fillRect(p.getX(), p.getY(), 5, 5);
106 }
107

S.E. Abdellaoui & I. Kachbal 3 P.O.O


(JAVA)
Université Cadi Ayyad P.O.O (JAVA) Année universitaire
EST-Safi S.E. Abdellaoui & I. Kachbal 2023-2024

108 g.setColor(Color.BLUE);
109 for (Target t : targets) {
110 g.fillRect(t.getX(), t.getY(), 20, 20);
111 }
112

113 g.setColor(Color.BLACK);
114 g.drawString("Score: " + score, 10, 20);
115 if (!isRunning) {
116 g.drawString("Paused (Press P to Resume)", 330, 300);
117 }
118 }
119 }
120

121 public static void main(String[] args) {


122 SwingUtilities.invokeLater(() -> {
123 ShootingGame game = new ShootingGame();
124 game.setVisible(true);
125 });
126 }
127 }

S.E. Abdellaoui & I. Kachbal 4 P.O.O


(JAVA)

You might also like