-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDraw.java
More file actions
304 lines (286 loc) · 8.16 KB
/
Draw.java
File metadata and controls
304 lines (286 loc) · 8.16 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//importing packages
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class Draw extends JPanel implements KeyListener {
Ball ball = new Ball(275, 150);// creating instance of class Ball and setting the location of the ball
Paddle paddle1 = new Paddle(15, 140);// creating instance of class Paddle for player 1 and setting location to left
// side
Paddle paddle2 = new Paddle(535, 140);// creating instance of class Paddle or player 2 and setting location to right
// side
Borders border = new Borders();//creating instance of class Border called border
GameFrame game = new GameFrame();//creating instance of class GameFrame called game
public boolean firsthit = false;//variable to check the moment the ball hits the right paddle for the first time so that the ball can speed up
public boolean check = true;//boolean to enable or disable right paddle depending on which mode is selected
public boolean clicked = false;//boolean to check when a button is clicked in menu so that the game can start
public boolean win = false;//variable to check if someone won the game
public Draw()// draw constructor
{
super();
super.setFocusable(true);
super.addKeyListener(this);
}
public void paint(Graphics g)// paint procedure to paint ball, and paddles
{
g.clearRect(0, 0, getWidth(), getHeight());
border.drawBorder(g);
ball.paint(g);// painting ball
paddle1.paint(g);// painting paddle 1
paddle2.paint(g);// painting paddle 2
}
@Override
public void keyPressed(KeyEvent arg0)// procedure to make y coordinate of paddles change depending on which keys are
// pressed and then repaint paddles
{
if (arg0.getKeyCode() == KeyEvent.VK_S && paddle1.y < 290)
paddle1.y += 8;
if (arg0.getKeyCode() == KeyEvent.VK_W && paddle1.y > 10)
paddle1.y -= 8;
if (arg0.getKeyCode() == KeyEvent.VK_DOWN && paddle2.y < 290 && check)//if check is true then right paddle can move down
paddle2.y += 8;
if (arg0.getKeyCode() == KeyEvent.VK_UP && paddle2.y > 10 && check)//if check is true then right paddle can move up
paddle2.y -= 8;
repaint();
}
@Override // not used
public void keyReleased(KeyEvent arg0) {
}
@Override // not used
public void keyTyped(KeyEvent arg0) {
}
public void update(boolean x) {//update procedure to call collision and repaint procedures with boolean perameter
if (clicked)//checking if a button is clicked so that update can work (crucial so that game doesnt run in background before it starts)
{
if(x)//checking for perameter (playAi)
{
AIPaddle();//calling AI procedure
check = false;//setting check false (disables right paddle)
}
collision();//checking for any collisions and updating ball position
win();//checking to see if anyone won the game
repaint();//repainting objects
}
}
public void collision() {//procedure to check all possible collisions of the ball
if (ball.x <= 0) {
// Adds score to player 2 total
border.p2Score += 1;
// repaints and resets the objects
repaint();
resetObj();
// setting it so that it can bounce with any object again
} else if (ball.x >= 545) {
// Adds score to player 1 total
border.p1Score += 1;
// repaints and resets the objects
repaint();
resetObj();
}
Random rand = new Random();//creating instance of Random class called rand
if (ball.x == 525 && ball.y >= paddle2.y - 40 && ball.y <= paddle2.y + 40)// right paddle collision
{
firsthit = true;//setting firsthit to true so velocity can speed up
int num = rand.nextInt(0, 3);//generating random integer between 1 and 3 and storing it in variables called num
if (ball.x > 0 && ball.y > 0)// top of right paddle
{
if (num == 1) {
ball.xVel *= -1;
ball.yVel *= 0.8;
repaint();
} else if (num == 2) {
ball.xVel *= -1;
repaint();
} else {
ball.xVel *= -1;
ball.yVel *= 1.3;
repaint();
}
} else if (ball.x > 0 && ball.y < 0)// bottom of right paddle
{
if (num == 1) {
ball.xVel *= -1;
ball.yVel *= 0.8;
repaint();
} else if (num == 2) {
ball.xVel *= -1;
ball.yVel *= 1;
repaint();
} else {
ball.xVel *= -1;
ball.yVel *= 1.3;
repaint();
}
}
}
if (ball.x == 33 && ball.y >= paddle1.y - 40 && ball.y <= paddle1.y + 40)// left paddle collision
{
int num2 = rand.nextInt(0, 3);
if (ball.x > 0 && ball.y > 0)// top of left paddle
{
if (num2 == 1) {
ball.xVel *= -1;
ball.yVel *= 0.8;
repaint();
} else if (num2 == 2) {
ball.xVel *= -1;
ball.yVel*= 1;
repaint();
} else {
ball.xVel *= -1;
ball.yVel *= 1.3;
repaint();
}
} else if (ball.x > 0 && ball.y < 0)// bottom of left paddle
{
if (num2 == 1) {
ball.xVel *= -1;
ball.yVel *= 0.8;
repaint();
} else if (num2 == 2) {
ball.xVel *= -1;
ball.yVel *= 1;
repaint();
} else {
ball.xVel *= -1;
ball.yVel *= 1.3;
repaint();
}
}
}
if (ball.y >= 325 && ball.yVel > 0)// bottom border collision detection
{
int num3 = rand.nextInt(0, 3);
if (ball.xVel > 0 && ball.y > 0)// ball coming from left side
{
if (num3 == 1) {
ball.yVel *= -0.8;
repaint();
} else if (num3 == 2) {
ball.yVel *= -1;
repaint();
} else {
ball.yVel *= -1.3;
repaint();
}
} else if (ball.xVel < 0 && ball.y > 0)// ball coming from right side
{
if (num3 == 1) {
ball.yVel *= -0.8;
repaint();
} else if (num3 == 2) {
ball.yVel *= -1;
repaint();
} else {
ball.yVel *= -1.3;
repaint();
}
}
}
if (ball.y <= 10 && ball.yVel < 0)// top border collision detection
{
int num4 = rand.nextInt(0, 3);
if (ball.xVel > 0)// ball coming from left side
{
if (num4 == 1) {
ball.yVel *= -0.8;
repaint();
} else if (num4 == 2) {
ball.yVel *= -1;
repaint();
} else {
ball.yVel *= -1.3;
repaint();
}
} else if (ball.xVel < 0)// ball coming from right side
{
if (num4 == 1) {
ball.yVel *= -0.8;
repaint();
} else if (num4 == 2) {
ball.yVel *= -1;
repaint();
} else {
ball.yVel *= -1.3;
repaint();
}
}
}
if (firsthit == false)//if ball never hit the paddle then x velocity is slower
{
ball.x += ball.initialxVel;
ball.y += ball.yVel;
}
else//once ball hits the paddle speed up and add the normal x velocity
{
ball.x += ball.xVel;
ball.y += ball.yVel;
}
}
public void resetObj() {//procedure for resetting everything but the score after someone scores
try {//adds delay
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Resets ball and paddle positions and firsthit variable
ball = new Ball(275, 150);
paddle1 = new Paddle(15, 140);
paddle2 = new Paddle(535, 140);
firsthit = false;
}
public void AIPaddle()
{
for(int i = 0; i < 4; i++)
{
// Only tracks the ball within a certain range so that the game can be lose
if((paddle2.y - ball.y)>0 && ball.x>50 && ball.x <470)
{
//makes sure the paddle will not go outside the borders
if(paddle2.y <= 5)
{
}
else
{
//changes the y value of the paddle
paddle2.y -= 1;
}
}
else if((paddle2.y - ball.y)<0 && ball.x>15 && ball.x <535)
{
//makes sure the paddle will not go outside the borders
if(paddle2.y >= 290)
{
}
else
{
//changes the y value of the paddle
paddle2.y += 1;
}
}
}
}
void win()//procedure to check if anyone won the game
{
if (border.p1Score == 10)//checking for winning score for player 1
{
JOptionPane.showMessageDialog(null, "Player 1 wins");
resetObj();
border.resetScore();
win = true;
game.playAi = false;
repaint();
}
else if (border.p2Score == 10)//checking for winning score for player 2
{
JOptionPane.showMessageDialog(null, "Player 2 wins");
resetObj();
border.resetScore();
win = true;
game.playAi = false;
repaint();
}
}
}