-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.java
196 lines (162 loc) · 5.47 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
import javax.swing.*;
import java.awt.*;
import java.util.Random;
/**
* Creates a ball for the pong game
*
* @author Ammar Malik, Amal Thomas, Daniel Graham, Bailey Cross, Aimen HARIZI
* @version Spring 2021
*/
public class Ball extends Thread{
//Constants for ball objects
public final int BALL_SIZE = 30;
public final int DELAY_TIME = 33;
public static final double ALMOST_STOPPED = 0.4;
public static final double GRAVITY = 0.35;
public static final double DAMPING = 1.02;
//Speeds of the current ball
public double speedX, speedY;
//Top left points of the circle
public int circleX, circleY;
//The max x and y value for the current container
private int xMax, yMax;
//See if the current ball no longer needs to be drawn
public boolean done;
//The current container
private JComponent container;
//Check if the ball hit the plank
private boolean bouncedPlank;
//The top left point of the plank
private Point plankLoc;
//Check if the game is currently playing
private boolean gameStarted = false;
//The max speed for the ball
private double maxSpeedY = 35;
private double maxSpeedX = 25;
//Used to get the current color of the game ball
public Color curColor = Color.BLACK;
/**
* public constructor for the Ball class
*
* @param drawLocation, double speedX, double speedY, JComponent container
* @param speedX the x speed of the ball
* @param speedY the y speed of the ball
* @param container the panel of which the ball is being drawn
*/
public Ball(Point drawLocation, double speedX, double speedY, JComponent container) {
this.speedX = speedX;
this.speedY = speedY;
circleX = drawLocation.x - BALL_SIZE / 2;
circleY = drawLocation.y - BALL_SIZE / 2;
this.yMax = container.getHeight() - BALL_SIZE;
this.xMax = container.getWidth() - BALL_SIZE;
this.container = container;
}
/**
* Paint method for the Ball class, draw the ball
*
* @param Graphics g
*/
public void paint(Graphics g) {
//Draw the current game ball
g.setColor(curColor);
g.fillOval((int) circleX, (int) circleY, BALL_SIZE, BALL_SIZE);
}
/**
* Run method for the Ball class, the main logic
*/
@Override
public void run() {
//Check if the ball is done running
if(!done){
while (!done) {
try {
sleep(DELAY_TIME);
} catch (InterruptedException e) {
}
//Check if the speeds are too fast
if (speedX > maxSpeedX){
speedX = maxSpeedX;
}
if (speedY > maxSpeedY){
speedY = maxSpeedY;
}
//Update the current position of the ball
circleX += speedX;
circleY += speedY;
boolean bounced = false;
//Used to give the ball a random xSpeed
Random xSpeed = new Random();
double test;
//Check how the ball bounced, update its velocity accordingly
if (circleX < 0) {
circleX = 0;
bounced = true;
speedX = -speedX;
}
if (circleX > xMax) {
circleX = xMax;
bounced = true;
speedX = -speedX;
}
if (circleY < 0) {
circleY = 0;
bounced = true;
speedY = -speedY;
}
if (circleY > yMax) {
circleY = yMax;
bounced = true;
speedY = -speedY;
}
//Check if the ball hit the plank.
//If it did, make it somewhat randomly change velocity
if (gameStarted) {
if (Collision.circleOverlapsRectangle(circleX + BALL_SIZE / 2, circleY + BALL_SIZE / 2, BALL_SIZE / 2, plankLoc.x - 40, 690, 80, 20)) {
bounced = true;
speedY = -Math.abs(speedY);
test = (double) (xSpeed.nextInt(10) - 5);
speedX = test;
}
}
//If we bounced, update the speed aswell
if (bounced) {
speedY *= DAMPING;
speedX *= DAMPING;
}
speedY += GRAVITY;
container.repaint();
done();
}
}
}
/**
* Check if the ball should stop running
*
* @return boolean true or false, based on whether or not the Ball's y
* plus the Ball's size divided by 2 is greater than or equal to 750
*/
public boolean done() {
if((circleY + BALL_SIZE / 2) >= 750) {
done = true;
} else {
done = false;
}
return done;
}
/**
* Sets the current plank location for the Ball class to use
*
* @param Point plankLocation
*/
public void setPlankLocation(Point plankLocation){
gameStarted = true;
plankLoc = new Point(plankLocation.x, 700);
}
/**
* Resets the Ball's done variable to false
*/
public void resetBall(){
done = false;
}
}