-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaddle.java
More file actions
117 lines (110 loc) · 4.49 KB
/
Paddle.java
File metadata and controls
117 lines (110 loc) · 4.49 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
import java.awt.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
/**
* Class that implements a paddle with a given position and responds to the movement of the mouse.
*/
class Paddle extends CollidableRect
{
/*
*****************************************************************************************************************************************************
***** CONSTANTS ************************************************************************************************************************************
*****************************************************************************************************************************************************
*/
/**
* The width of the paddle.
*/
public static final int PADDLE_WIDTH = 100;
/**
* The height of the paddle.
*/
public static final int PADDLE_HEIGHT = 5;
/**
* The initial position (specified as a fraction of the game height) of center of the paddle.
*/
public static final double INITIAL_Y_LOCATION_FRAC = 0.8;
/**
* The minimum position (specified as a fraction of the game height) of center of the paddle.
*/
public static final double MIN_Y_LOCATION_FRAC = 0.7;
/**
* The maximum position (specified as a fraction of the game height) of center of the paddle.
*/
public static final double MAX_Y_LOCATION_FRAC = 0.9;
/*
*****************************************************************************************************************************************************
***** INSTANCE VARIABLES / CONSTRUCTOR **************************************************************************************************************
*****************************************************************************************************************************************************
*/
private Rectangle rectangle;
/**
* Constructs a new Paddle whose vertical center is at INITIAL_Y_LOCATION_FRAC * GameImpl.HEIGHT.
*/
public Paddle ()
{
final double x = PADDLE_WIDTH / 2;
final double y = INITIAL_Y_LOCATION_FRAC * GameImpl.HEIGHT;
rectangle = new Rectangle(0, 0, PADDLE_WIDTH, PADDLE_HEIGHT);
rectangle.setLayoutX(x - PADDLE_WIDTH / 2);
rectangle.setLayoutY(y - PADDLE_HEIGHT / 2);
rectangle.setStroke(Color.GREEN);
rectangle.setFill(Color.GREEN);
setX1(getX() - PADDLE_WIDTH / 2);
setY1(getY() - PADDLE_HEIGHT / 2);
setX2(getX() + PADDLE_WIDTH / 2);
setY2(getY() + PADDLE_HEIGHT / 2);
}
/*
*****************************************************************************************************************************************************
***** PUBLIC METHODS ********************************************************************************************************************************
*****************************************************************************************************************************************************
*/
/**
* @return the x coordinate of the center of the paddle.
*/
public double getX ()
{
return rectangle.getLayoutX() + rectangle.getTranslateX() + PADDLE_WIDTH / 2;
}
/**
* @return the y coordinate of the center of the paddle.
*/
public double getY()
{
return rectangle.getLayoutY() + rectangle.getTranslateY() + PADDLE_HEIGHT/2;
}
/**
* @return the Rectangle object that represents the paddle on the game board.
*/
public Rectangle getRectangle ()
{
return rectangle;
}
/**
* Moves the paddle so that its center is at (newX, newY), subject to
* the horizontal constraint that the paddle must always be completely visible
* and the vertical constraint that its y coordinate must be between MIN_Y_LOCATION_FRAC
* and MAX_Y_LOCATION_FRAC times the game height.
* @param newX the newX position to move the center of the paddle.
* @param newY the newX position to move the center of the paddle.
*/
public void moveTo (double newX, double newY)
{
if (newX < PADDLE_WIDTH/2) {
newX = PADDLE_WIDTH/2;
} else if (newX > GameImpl.WIDTH - PADDLE_WIDTH/2) {
newX = GameImpl.WIDTH - PADDLE_WIDTH/2;
}
if (newY < MIN_Y_LOCATION_FRAC * GameImpl.HEIGHT) {
newY = MIN_Y_LOCATION_FRAC * GameImpl.HEIGHT;
} else if (newY > MAX_Y_LOCATION_FRAC * GameImpl.HEIGHT) {
newY = MAX_Y_LOCATION_FRAC * GameImpl.HEIGHT;
}
rectangle.setTranslateX(newX - (rectangle.getLayoutX() + PADDLE_WIDTH / 2));
rectangle.setTranslateY(newY - (rectangle.getLayoutY() + PADDLE_HEIGHT / 2));
setX1(newX - PADDLE_WIDTH / 2);
setY1(newY - PADDLE_HEIGHT / 2);
setX2(newX + PADDLE_WIDTH / 2);
setY2(newY + PADDLE_HEIGHT / 2);
}
}