-
Notifications
You must be signed in to change notification settings - Fork 0
/
PongPanel.java
435 lines (372 loc) · 14.1 KB
/
PongPanel.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
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
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.Timer;
public class PongPanel extends JPanel implements ActionListener, KeyListener{
private boolean showTitleScreen = true;
private boolean playing = false;
private boolean gameOver = false;
private boolean upPressed = false;
private boolean downPressed = false;
private boolean wPressed = false;
private boolean sPressed = false;
private float ballX = 250;
private float ballY = 250;
private float diameter = 20;
private float ballDeltaX = -1;
private float ballDeltaY = 3;
private float playerOneX = 25;
private float playerOneY = 250;
private float playerOneWidth = 10;
private float playerOneHeight = 50;
private float playerTwoX = 465;
private float playerTwoY = 250;
private float playerTwoWidth = 10;
private float playerTwoHeight = 50;
private float paddleSpeed = 5;
private int playerOneScore = 0;
private int playerTwoScore = 0;
//Upgrade Properties
private int level = 0;
private Random rand = new Random();
private Color randomColor = Color.RED;
private Boolean startTimer = false;
private Boolean displayPowerUp = false;
private Boolean powerUpOnScreen = false;
private int currentPlayerPosession = 1;
private String currentPowerUp = "";
private String[] PowerUpOptions = {"Split","Bounce","smallPaddles", "laserShoot"};
//Power Ups
private int pwrX = 0;
private int pwrY = 0;
private Color pwrColor = Color.RED;
private int powerUpOption = 0;
private Timer pwrTimer = new Timer(500, this);
//construct a PongPanel
public PongPanel(){
setBackground(Color.WHITE);
//listen to key presses
setFocusable(true);
addKeyListener(this);
//call step() 60 fps
Timer timer = new Timer(1000/60, this);
timer.start();
level = 1;
ballDeltaX = -1f;
paddleSpeed = 5f;
System.out.println("Level: " + level);
}
public void actionPerformed(ActionEvent e){
step();
}
public void step(){
if(playing){
//move player 1
if (upPressed) {
if (playerOneY-paddleSpeed > 0) {
playerOneY -= paddleSpeed;
}
}
if (downPressed) {
if (playerOneY + paddleSpeed + playerOneHeight < getHeight()) {
playerOneY += paddleSpeed;
}
}
//move player 2
if (wPressed) {
if (playerTwoY-paddleSpeed > 0) {
playerTwoY -= paddleSpeed;
}
}
if (sPressed) {
if (playerTwoY + paddleSpeed + playerTwoHeight < getHeight()) {
playerTwoY += paddleSpeed;
}
}
//where will the ball be after it moves?
float nextBallLeft = ballX + ballDeltaX;
float nextBallRight = ballX + diameter + ballDeltaX;
float nextBallTop = ballY + ballDeltaY;
float nextBallBottom = ballY + diameter + ballDeltaY;
float playerOneRight = playerOneX + playerOneWidth;
float playerOneTop = playerOneY;
float playerOneBottom = playerOneY + playerOneHeight;
float playerTwoLeft = playerTwoX;
float playerTwoTop = playerTwoY;
float playerTwoBottom = playerTwoY + playerTwoHeight;
//ball bounces off top and bottom of screen
if (nextBallTop < 0 || nextBallBottom > getHeight()) {
ballDeltaY *= -1;
}
//will the ball go off the left side?
if (nextBallLeft < playerOneRight) {
//is it going to miss the paddle?
if (nextBallTop > playerOneBottom || nextBallBottom < playerOneTop) {
playerTwoScore ++;
incrimentLevel();
//Reset ball Speed to 1
ballDeltaX = -1;
if (playerTwoScore == 3) {
playing = false;
gameOver = true;
level = 0;
}
ballX = 250;
ballY = 250;
}
else {
incrimentLevel();
ballDeltaX *= -1;
currentPlayerPosession = 1;
}
}
//will the ball go off the right side?
if (nextBallRight > playerTwoLeft) {
//is it going to miss the paddle?
if (nextBallTop > playerTwoBottom || nextBallBottom < playerTwoTop) {
playerOneScore ++;
incrimentLevel();
//Reset ball Speed to 1
ballDeltaX = -1;
if (playerOneScore == 3) {
playing = false;
gameOver = true;
level = 0;
}
startTimer = true;
ballX = 250;
ballY = 250;
}
else {
incrimentLevel();
ballDeltaX *= -1;
currentPlayerPosession = 2;
}
}
//move the ball
ballX += ballDeltaX;
ballY += ballDeltaY;
}
//Check to see if ball will hit power up
System.out.println("X: " + ballX + " " + "Y: " + ballY);
System.out.println("pwrX: " + pwrX + " " + "pwrY: " + pwrY);
if (displayPowerUp) {
if (((ballX == pwrX) || ((ballX < pwrX + 60) && (ballX > pwrX))) && ((ballY == pwrY) || ((ballY < pwrY + 60) && (ballY > pwrY)))) {
System.out.println("Contact!!!!");
hidePowerUp();
enablePowerUp();
}
}
//stuff has moved, tell this JPanel to repaint itself
repaint();
}
public void endSuperSpeed() {
ballDeltaX = 5;
}
public void superSpeedBall() {
System.out.println("SUPER SPEED");
if (ballDeltaX > 0) {
ballDeltaX = 7;
} else {
ballDeltaX = -7;
}
pwrTimer.start();
}
public void enablePowerUp() {
powerUpOption = new Random().nextInt(2) + 1;
if(powerUpOption == 1) {
//Super Speed Ball For a Second!
superSpeedBall();
} else if (powerUpOption == 2) {
//Switch Ball Direction
ballDeltaX = ballDeltaX * -1;
} else if (powerUpOption == 3) {
//Bigger Paddle
if (ballDeltaX > 0) {
playerOneHeight = 80;
} else {
playerTwoHeight = 80;
}
}
}
public Color randomColor() {
float rC = rand.nextFloat();
float gC = rand.nextFloat();
float bC = rand.nextFloat();
return new Color(rC, gC, bC);
}
public void incrimentLevel() {
//Incriment Level
level+= 1;
System.out.println("Level: " + level);
// Java 'Color' class takes 3 floats, from 0 to 1
float rC = rand.nextFloat();
float gC = rand.nextFloat();
float bC = rand.nextFloat();
randomColor = new Color(rC, gC, bC);
//Incriment Ball Speed
if(ballDeltaX < 0) {
ballDeltaX = ballDeltaX - 0.25f;
} else {
ballDeltaX = ballDeltaX + 0.25f;
}
//Incriment Paddle Speed (every other)
if ( (level & 1) == 0 ) {
if (paddleSpeed < 10) {
paddleSpeed += 0.5f;
}
}
//Power up every 3rd
if ((level & 3) == 0) {
System.out.println("Power UP");
playerOneHeight = 50;
playerTwoHeight = 50;
displayPowerUp();
}
System.out.println("ballDeltaX: " + ballDeltaX);
System.out.println("paddleSpeed: " + paddleSpeed);
}
//Power Up
public void displayPowerUp() {
if (!(powerUpOnScreen)) {
pwrX = new Random().nextInt(200) + 200;
pwrY = new Random().nextInt(400) + 50;
System.out.println("pwrX: " + pwrX);
System.out.println("pwrY: " + pwrY);
pwrColor = randomColor();
displayPowerUp = true;
}
}
public void hidePowerUp() {
powerUpOnScreen = false;
displayPowerUp = false;
}
//paint the game screen
public void paintComponent(Graphics g){
super.paintComponent(g);
//Random Color
g.setColor(randomColor);
//Convert to Ints
int ballXInt = Math.round(ballX);
int ballYInt = Math.round(ballY);
int playerOneXInt = Math.round(playerOneX);
int playerTwoXInt = Math.round(playerTwoX);
int diameterInt = Math.round(diameter);
int playerOneYInt = Math.round(playerOneY);
int playerTwoYInt = Math.round(playerTwoY);
int playerOneWidthInt = Math.round(playerOneWidth);
int playerOneHeightInt = Math.round(playerOneHeight);
int playerTwoWidthInt = Math.round(playerTwoWidth);
int playerTwoHeightInt = Math.round(playerTwoHeight);
if (showTitleScreen) {
g.setFont(new Font(Font.DIALOG, Font.BOLD, 36));
g.setFont(new Font(Font.DIALOG, Font.BOLD, 36));
g.drawString("Pong", 165, 100);
g.setFont(new Font(Font.DIALOG, Font.BOLD, 18));
g.drawString("Press 'P' to play.", 175, 400);
}
else if (playing) {
float playerOneRight = playerOneX + playerOneWidth;
float playerTwoLeft = playerTwoX;
//draw dashed line down center
for (int lineY = 0; lineY < getHeight(); lineY += 50) {
g.drawLine(250, lineY, 250, lineY+25);
}
//draw "goal lines" on each side
int playerOneRightInt = Math.round(playerOneRight);
int playerTwoLeftInt = Math.round(playerOneRight);
g.drawLine(playerOneRightInt, 0, playerOneRightInt, getHeight());
g.drawLine(playerTwoLeftInt, 0, playerTwoLeftInt, getHeight());
//draw the scores
g.setFont(new Font(Font.DIALOG, Font.BOLD, 36));
g.drawString(String.valueOf(playerOneScore), 100, 100);
g.drawString(String.valueOf(playerTwoScore), 400, 100);
//draw the ball
g.fillOval(ballXInt, ballYInt, diameterInt, diameterInt);
//draw the paddles
g.fillRect(playerOneXInt, playerOneYInt, playerOneWidthInt, playerOneHeightInt);
g.fillRect(playerTwoXInt, playerTwoYInt, playerTwoWidthInt, playerTwoHeightInt);
}
else if (gameOver) {
g.setFont(new Font(Font.DIALOG, Font.BOLD, 36));
g.drawString(String.valueOf(playerOneScore), 100, 100);
g.drawString(String.valueOf(playerTwoScore), 400, 100);
g.setFont(new Font(Font.DIALOG, Font.BOLD, 36));
if (playerOneScore > playerTwoScore) {
g.drawString("Player 1 Wins!", 165, 200);
}
else {
g.drawString("Player 2 Wins!", 165, 200);
}
g.setFont(new Font(Font.DIALOG, Font.BOLD, 18));
g.drawString("Press space to restart.", 150, 400);
}
//Power Ups
//If we are supposed to display a power up
if (displayPowerUp) {
g.setColor(pwrColor); //Random color for power ups
g.fillRect(pwrX, pwrY, 60, 60);
powerUpOnScreen = true;
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (showTitleScreen) {
if (e.getKeyCode() == KeyEvent.VK_P) {
showTitleScreen = false;
playing = true;
}
}
else if(playing){
if (e.getKeyCode() == KeyEvent.VK_UP) {
upPressed = true;
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downPressed = true;
}
else if (e.getKeyCode() == KeyEvent.VK_W) {
wPressed = true;
}
else if (e.getKeyCode() == KeyEvent.VK_S) {
sPressed = true;
}
}
else if (gameOver) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
gameOver = false;
showTitleScreen = true;
playerOneY = 250;
playerTwoY = 250;
ballX = 250;
ballY = 250;
playerOneScore = 0;
playerTwoScore = 0;
ballDeltaX = -1;
paddleSpeed = 5;
}
}
}
public void keyReleased(KeyEvent e) {
if (playing) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
upPressed = false;
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downPressed = false;
}
else if (e.getKeyCode() == KeyEvent.VK_W) {
wPressed = false;
}
else if (e.getKeyCode() == KeyEvent.VK_S) {
sPressed = false;
}
}
}
}