-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTennisPanel.java
More file actions
289 lines (260 loc) · 8.83 KB
/
TennisPanel.java
File metadata and controls
289 lines (260 loc) · 8.83 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
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class TennisPanel extends JPanel implements KeyListener, ActionListener {
/* WINDOW VARS */
private int width = 1800; // X dimension for window
private int height = 750; // Y dimension for window
/* PADDLE VARS */
private int paddleWidth = 20; // X dimension for paddles
private int paddleHeight = 110; // Y dimension for paddles
private int paddleSpeed = 5;
private int paddleOnePos = 0; // X dimension for paddles
private int paddleTwoPos = 0; // X dimension for paddles
/* BALL VARS */
private int x = width / 2; // x position
private int y = height / 2; // y position
private int radius = 10; // ball radius
private int dx = 6; // increment amount (x coord)
private int dy = 6; // increment amount (y coord)
/* BRICK VARS */
ArrayList<Block> playerOneBricks = new ArrayList<>();
ArrayList<Block> playerTwoBricks = new ArrayList<>();
/* TIMER VARS */
private Timer timer;
private int delay = 5;
/* KEY VARS */
ArrayList<Integer> activeKeys = new ArrayList<Integer>();
/* BACKGROUND IMAGE */
private ImageIcon courtIcon;
// private JLabel courtLabel;
private int level;
public TennisPanel() {
JOptionPane.showMessageDialog(null, "Welcome to Block Tennis! \nPlayer 1 moves with Q and A\nPlayer 2 moves with P and L\nFirst to break all the other player's blocks wins!");
courtIcon = new ImageIcon("game.png");
level = levelSelect();
timer = new Timer(delay, this);
timer.start(); // start the timer
}
public int levelSelect() {
int option = Integer.parseInt(JOptionPane.showInputDialog("Select Level (1) (2) (3)"));
if (option < 1) {
option = 1;
} else if (option > 3) {
option = 3;
}
switch (option) {
case 1:
for (int i = 200; i <= 500; i += 150) {
playerOneBricks.add(new Block(200, i, 20, 100));
playerTwoBricks.add(new Block(width - 200, i, 20, 100));
}
dx = 5;
dy = 5;
paddleSpeed = 5;
break;
case 2:
for (int i = 150; i <= 550; i += 100) {
playerOneBricks.add(new Block(200, i, 50, 50));
playerTwoBricks.add(new Block(width - 200, i, 50, 50));
}
dx = 6;
dy = 6;
paddleSpeed = 6;
break;
case 3:
for (int i = 200; i <= 400; i += 100) {
playerOneBricks.add(new Block(i, 350, 50, 100));
playerTwoBricks.add(new Block(width - i, 350, 50, 100));
}
dx = 7;
dy = 7;
paddleSpeed = 7;
break;
default:
System.out.println("Dang we should never have executed this");
}
return option;
}
public void actionPerformed(ActionEvent arg) {
// Search through arraylist for active keys and move paddles
for (int i = 0; i < activeKeys.size(); i++) {
if (activeKeys.get(i) == KeyEvent.VK_P) {
if (paddleTwoPos > (-height/2) + (paddleHeight/2)) {
paddleTwoPos = paddleTwoPos - paddleSpeed;
}
} else if (activeKeys.get(i) == KeyEvent.VK_L) {
if (paddleTwoPos < (height/2) - (paddleHeight - 5)) {
paddleTwoPos = paddleTwoPos + paddleSpeed;
}
} else if (activeKeys.get(i) == KeyEvent.VK_Q) {
if (paddleOnePos > (-height/2) + (paddleHeight/2)) {
paddleOnePos = paddleOnePos - paddleSpeed;
}
} else if (activeKeys.get(i) == KeyEvent.VK_A) {
if (paddleOnePos < (height/2) - (paddleHeight - 5)) {
paddleOnePos = paddleOnePos + paddleSpeed;
}
}
}
repaint();
}
// check for if the ball is within a certain x range
public int getBlockX(int blockWidth, int blockPosition, String loc){
if (loc.equals("left")) {
return blockPosition-(blockWidth/2);
} else if(loc.equals("right")) {
return blockPosition+(blockWidth/2);
}
return 0;
}
//check for if the ball is within a certan y range
public int getBlockY(int blockHeight, int blockPosition, String loc) {
if (loc.equals("top")) {
return blockPosition-(blockHeight/2);
} else if (loc.equals("bottom")) {
return blockPosition+(blockHeight/2);
}
return 0;
}
// checks if a certain block has been hit
public boolean hitBlock(Block b) {
return x >= getBlockX(b.width, b.centerX, "left") && x <= getBlockX(b.width, b.centerX, "right") &&
y >= getBlockY(b.height, b.centerY, "top") && y <= getBlockY(b.height, b.centerY, "bottom");
}
// changes the motion of the ball (due to a collision)
public void ballCollision() {
if (dx >= 0) {
dx = -Math.abs(dx);
} else {
dx = Math.abs(dx);
}
if (dy >= 0) {
dy = Math.abs(dx);
} else {
dy = -Math.abs(dx);
}
}
public void paintComponent( Graphics g ) {
super.paintComponent(g);
switch (level) {
case 1:
g.setColor(new Color(60, 179, 113));
break;
case 2:
g.setColor(new Color(109, 97, 147));
break;
default:
g.setColor(new Color(214, 100, 102));
}
g.fillRect(0, 0, width, height);
// drawing all bricks out onto the screen
g.setColor(Color.WHITE);
// here we draw the lines of the tennis court
g.fillRect(width / 2 - 2, 0, 4, height);
g.fillRect(width / 2 - 400, 0, 4, height);
g.fillRect(width / 2 + 400, 0, 4, height);
// horizontal lines
g.fillRect(0, 75, width, 4);
g.fillRect(0, height - 75, width, 4);
g.fillRect(width / 2 - 400, height / 2, 800, 4);
g.setColor(Color.BLACK);
for (int i = 0; i < playerOneBricks.size(); i++) {
g.fillRect(playerOneBricks.get(i).topLeftX, playerOneBricks.get(i).topLeftY, playerOneBricks.get(i).width, playerOneBricks.get(i).height);
}
for (int i = 0; i < playerTwoBricks.size(); i++) {
g.fillRect(playerTwoBricks.get(i).topLeftX, playerTwoBricks.get(i).topLeftY, playerTwoBricks.get(i).width, playerTwoBricks.get(i).height);
}
//g.drawRect(paddleSizeX, paddleSizeY, (int) (width*.40), height/2);
g.setColor(new Color(140, 69, 2));
g.fillRect((int) (width*.30), ((int) (height/2))-(paddleHeight/2)+paddleOnePos, paddleWidth, paddleHeight);
g.fillRect((int) (width*.70)-paddleWidth, ((int) (height/2))-(paddleHeight/2)+paddleTwoPos, paddleWidth, paddleHeight);
g.setColor(Color.GREEN);
// checking for if the ball is hitting the game boundaries
if (x < radius)
dx = Math.abs(dx);
if (x > getWidth() - radius)
dx = -Math.abs(dx);
if (y < radius)
dy = Math.abs(dy);
if (y > getHeight() - radius)
dy = -Math.abs(dy);
//Player 1 Paddle collision
if ((x >= getBlockX(paddleWidth, (int) (width*.30)-paddleWidth, "left")) && (x <= getBlockX(paddleWidth, (int) (width*.30)+paddleWidth, "right")) &&
(y >= getBlockY(paddleHeight, ((height/2)+paddleOnePos), "top")) && (y <= getBlockY(paddleHeight, ((height/2)+paddleOnePos), "bottom"))) {
ballCollision();
}
//Player 2 Paddle collision
if ((x >= getBlockX(paddleWidth, (int) (width*.70)-paddleWidth, "left")) && (x <= getBlockX(paddleWidth, (int) (width*.70), "right")) &&
(y >= getBlockY(paddleHeight, ((height/2)+paddleTwoPos), "top")) && (y <= getBlockY(paddleHeight, ((height/2)+paddleTwoPos), "bottom"))) {
ballCollision();
}
// check block colision
for (int i = 0; i < playerOneBricks.size(); i++) {
if (hitBlock(playerOneBricks.get(i))) {
g.clearRect(playerOneBricks.get(i).topLeftX, playerOneBricks.get(i).topLeftY, playerOneBricks.get(i).width, playerOneBricks.get(i).height);
System.out.println("Player 2 hit a brick!");
playerOneBricks.remove(i);
ballCollision();
}
}
for (int i = 0; i < playerTwoBricks.size(); i++) {
if (hitBlock(playerTwoBricks.get(i))) {
g.clearRect(playerTwoBricks.get(i).topLeftX, playerTwoBricks.get(i).topLeftY, playerTwoBricks.get(i).width, playerTwoBricks.get(i).height);
System.out.println("Player 1 hit a brick!");
playerTwoBricks.remove(i);
ballCollision();
}
}
// check if either player has won
if (playerOneBricks.isEmpty()) {
playAgain("2");
} else if (playerTwoBricks.isEmpty()) {
playAgain("1");
}
// adjust ball position
x += dx;
y += dy;
g.fillOval(x - radius, y - radius, radius*2, radius*2);
}
public void playAgain(String winner) {
timer.stop();
JOptionPane.showMessageDialog(null, "Player " + winner + " has won!" );
int playAgain = JOptionPane.showConfirmDialog(null, "Would you like to play Again?", "Game Over", JOptionPane.YES_NO_OPTION);
switch (playAgain) {
case JOptionPane.YES_OPTION:
playerOneBricks.clear();
playerTwoBricks.clear();
level = levelSelect();
x = width / 2;
y = height / 2;
timer.start();
break;
default:
JOptionPane.showMessageDialog(null, "Thanks for Playing!");
System.exit(0);
}
}
public void keyPressed(KeyEvent e) {
boolean foundKey = false;
for (int i = 0; i < activeKeys.size(); i++) {
if (activeKeys.get(i) == e.getKeyCode()) {
foundKey = true;
}
}
if (foundKey == false) {
activeKeys.add(e.getKeyCode());
}
}
public void keyReleased(KeyEvent e) {
for (int i = 0; i < activeKeys.size(); i++) {
if (activeKeys.get(i) == e.getKeyCode()) {
activeKeys.remove(i);
}
}
}
public void keyTyped(KeyEvent e) {
//System.out.print(e.getKeyCode());
}
}