-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBall.java
271 lines (243 loc) · 8.32 KB
/
Ball.java
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
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import static javax.sound.sampled.AudioSystem.*;
/**
* Created with IntelliJ IDEA.
* User: cybercser
* Date: 13-6-26
* Time: 20:48:42
* TODO: spin effect,
*/
public class Ball extends JPanel implements Runnable, PingPongConstants {
public enum Direction { UP, DOWN };
private int x = DESK_LENGTH / 2;
private int y = DESK_WIDTH / 2;
private int dx = 2;
private int dy = 2;
private Integer score1 = 0;
private Integer score2 = 0;
// the pos stands for y value of the upper-left corner of the bat rectangle
private int bat1Y = (DESK_WIDTH - BAT_LENGTH) / 2;
private int bat2Y = (DESK_WIDTH - BAT_LENGTH) / 2;
private int bat1Center = DESK_WIDTH - BAT_LENGTH / 2;
private int bat2Center = DESK_WIDTH - BAT_LENGTH / 2;
// key pressed/released
private boolean wPressed = false;
private boolean sPressed = false;
private boolean upArrowPressed = false;
private boolean downArrowPressed = false;
// for random ball direction
private int jitter = 0;
// game result
private int result = 0;
public Ball() {
new Thread(this).start();
setFont(new Font("Georgia", Font.BOLD, 30));
setFocusable(true);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_W :
wPressed = true;
break;
case KeyEvent.VK_S :
sPressed = true;
break;
case KeyEvent.VK_UP :
upArrowPressed = true;
break;
case KeyEvent.VK_DOWN :
downArrowPressed = true;
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_W :
wPressed = false;
break;
case KeyEvent.VK_S :
sPressed = false;
break;
case KeyEvent.VK_UP :
upArrowPressed = false;
break;
case KeyEvent.VK_DOWN :
downArrowPressed = false;
break;
}
}
});
}
@Override
public void run() {
try {
while (result == 0) {
processKeyEvent();
repaint();
Thread.sleep(UPDATE_INTERVAL);
}
}
catch (InterruptedException ex) {
}
}
public Dimension getPreferredSize() {
return new Dimension(DESK_LENGTH, DESK_WIDTH);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
// check game status
if (score1 == 11)
result = PLAYER1_WIN;
else if (score2 == 11)
result = PLAYER2_WIN;
bounce();
// Adjust ball position
x += dx;
y += dy;
g.fillOval(x - BALL_RADIUS, y - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2);
// draw the net
g.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
// draw the bats
g.setColor(Color.BLUE);
g.fillRect(0, bat1Y, BAT_THICKNESS, BAT_LENGTH);
g.fillRect(getWidth() - BAT_THICKNESS, bat2Y, BAT_THICKNESS, BAT_LENGTH);
// draw the score
g.drawString(score1.toString(), DESK_LENGTH / 2 - 50, 30);
g.drawString(score2.toString(), DESK_LENGTH / 2 + 30, 30);
// draw game over info
FontMetrics fm = g.getFontMetrics();
int stringWidth = fm.stringWidth("Player X wins!");
int stringAscent = fm.getAscent();
int xCoord = getWidth() / 2 - stringWidth / 2;
int yCoord = getHeight() / 2 - stringAscent / 2;
if (PLAYER1_WIN == result)
g.drawString("Player 1 wins!", xCoord, yCoord);
else if (PLAYER2_WIN == result)
g.drawString("Player 2 wins!", xCoord, yCoord);
}
private void processKeyEvent() {
if (wPressed && !sPressed)
moveBat(PLAYER1, Direction.UP);
else if (sPressed && !wPressed)
moveBat(PLAYER1, Direction.DOWN);
if (upArrowPressed && !downArrowPressed)
moveBat(PLAYER2, Direction.UP);
else if (downArrowPressed && !upArrowPressed)
moveBat(PLAYER2, Direction.DOWN);
}
public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
// The wrapper thread is unnecessary, unless it blocks on the
// Clip finishing; see comments.
public void run() {
try {
Clip clip = getClip();
AudioInputStream inputStream = getAudioInputStream(
this.getClass().getResource("sound/" + url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
private void bounce() {
if (Math.random() > 0.5)
jitter = 1;
else
jitter = -1;
// Check boundaries
if (x < BALL_RADIUS + BAT_THICKNESS) {
if ((y >= bat1Y) && (y <= bat1Y + BAT_LENGTH)) {
playSound("ball_bat.wav");
// spin effect
int distToBatCenter = Math.abs(y-bat1Center);
if ( distToBatCenter <= 5 ) {
dx = 2;
}
else if ( distToBatCenter > 5 && distToBatCenter <= 15) {
dx = 3;
}
else if ( distToBatCenter > 15) {
dx = 4;
}
}
else { // player 1 lose
score2++;
x = DESK_LENGTH / 2;
y = DESK_WIDTH / 2;
dx = 2;
dy = 2 * jitter;
}
}
if (x > getWidth() - BALL_RADIUS - BAT_THICKNESS)
{
if ((y >= bat2Y) && (y <= bat2Y + BAT_LENGTH)) {
playSound("ball_bat.wav");
// spin effect
int distToBatCenter = Math.abs(y-bat1Center);
if ( distToBatCenter <= 5 ) {
dx = -2;
}
else if ( distToBatCenter > 5 && distToBatCenter <= 15) {
dx = -3;
}
else if ( distToBatCenter > 15) {
dx = -4;
}
}
else { //player 2 lose
score1++;
x = DESK_LENGTH / 2;
y = DESK_WIDTH / 2;
dx = 2;
dy = 2 * jitter;
}
}
if (y < BALL_RADIUS) {
dy = Math.abs(dy);
playSound("ball_desk.wav");
}
if (y > getHeight() - BALL_RADIUS) {
dy = -Math.abs(dy);
playSound("ball_desk.wav");
}
}
private void moveBat(int player, Direction direction) {
if (PLAYER1 == player) {
if (Direction.UP == direction) {
bat1Y -= BAT_SPEED;
if (bat1Y < 0)
bat1Y = 0;
}
else if (Direction.DOWN == direction) {
bat1Y += BAT_SPEED;
if (bat1Y > getHeight() - BAT_LENGTH)
bat1Y = getHeight() - BAT_LENGTH;
}
}
else if (PLAYER2 == player) {
if (Direction.UP == direction) {
bat2Y -= BAT_SPEED;
if (bat2Y < 0)
bat2Y = 0;
}
else if (Direction.DOWN == direction) {
bat2Y += BAT_SPEED;
if (bat2Y > getHeight() - BAT_LENGTH)
bat2Y = getHeight() - BAT_LENGTH;
}
}
}
}