-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPUPaddle.java
More file actions
49 lines (32 loc) · 1.69 KB
/
CPUPaddle.java
File metadata and controls
49 lines (32 loc) · 1.69 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
import java.awt.Color;
//A CPU controlled JavaPong paddle
public abstract class CPUPaddle extends Paddle{
//**************** CONSTRUCTOR *************
public CPUPaddle(double length, double speed, Color color){
super(length, speed, color);
}
//determines if the Paddle should move up, down, or stay in its current position
//Returns one of three int values, telling the game that, per the key being
//pressed, if the Paddle should move up, down, or not move at all (neutral)
//
//arguments include (in order):
//bX, bY: the ball's current x and y coordinates
//bXVel, bYVel: the ball's current x and y velocities
public int calcMove(double bX, double bY, double bXVel, double bYVel){
if (bXVel > 0)
return reactBallMovingRight(bX, bY, bXVel, bYVel);
else
return reactBallMovingLeft(bX, bY, bXVel, bYVel);
}
//**************** ABSTRACT METHODS *************
//helpers used by calcMove(...) to determine the Paddle's move when ball is travelling left/right
//like calcMove(...), returns one of three int values to dictate move up, down, nor eutral
protected abstract int reactBallMovingLeft(double bX, double bY, double bXVel, double bYVel);
protected abstract int reactBallMovingRight(double bX, double bY, double bXVel, double bYVel);
//Called automatically whenver the ball collides with this paddle
public abstract void ballVolleyed();
//Called automatically whenever EITHER paddle scores a point.
//argument boolean indicates if it was the human who scored (true) or
//the CPU (false).
public abstract void pointScored(boolean didCPUScore);
}