-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBall.java
More file actions
109 lines (81 loc) · 2.02 KB
/
Ball.java
File metadata and controls
109 lines (81 loc) · 2.02 KB
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
package pong;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineEvent;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Ball extends JPanel {
Pong pon;
public int x = 210;
public int y = 300;
private boolean deplace = true;
int directionX = -3;
int directionY = -3; // -1 pour monter, 1 pour descendre
public Rectangle rectBall;
public Ball(Pong pon) {
this.pon = pon;
Timer time = new Timer(12, new ActionListener() {
public void actionPerformed(ActionEvent e) {
update();
repaint();
}
});
time.start();
rectBall = new Rectangle(x, y, 30, 30);
}
public void update() {
if(this.deplace) {
x += directionX;
if(x < 65) {
song();
x = 300;
pon.score1++;
x++;
directionX = 3; //Change de direction vers la droite.
}
else if(x > 700) {
song();
x = 300;
pon.score2++;
directionX = -3; //Change de direction vers la gauche
}
y += directionY;
if (y < 107) {
y = 107;
directionY = 3; // Change de direction vers le bas
} else if (y > 420) {
y = 420;
directionY = -3; // Change de direction vers le haut
}
}
rectBall.setBounds(x, y, 30, 30);
}
public void song() {
try {
File audio = new File("song/audio/ping.wav");
AudioInputStream sc = AudioSystem.getAudioInputStream(audio);
Clip clip = AudioSystem.getClip();
clip.open(sc);
clip.start();
//Stopper la musique apres sa lecture
clip.addLineListener(event ->{
if(event.getType() == LineEvent.Type.STOP) {
clip.close();
}
});
}catch(Exception e) {
e.printStackTrace();
}
}
public void draw(Graphics2D g) {
g.setColor(Color.pink);
g.fillOval(x, y, 30, 30);
}
}