-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBallGame.java
More file actions
251 lines (224 loc) · 6.97 KB
/
BallGame.java
File metadata and controls
251 lines (224 loc) · 6.97 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
// ********** BONUS: ***********
// Ball gets smaller each time it gets hit
// Ball changes color each time it gets hit
// 2 Balls at the same time
// *****************************
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
import javafx.event.EventHandler;
import java.util.Random;
public class BallGame extends Application {
private Pane graphicsPane;
private BorderPane root;
private Circle ball;
private Circle ball2;
private int ballRadius = 28;
private Button pauseButton;
private Button resetButton;
private Text hitsText;
private Text missesText;
private HBox top;
private int hits = 0;
private int misses = 0;
private int xVelocity = 1;
private BallAnimation animation;
private ButtonHandler buttonHandler;
private Text stop;
private int pauseCount = 0;
private boolean isPaused = false;
private boolean isEnded = false;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
root = new BorderPane();
graphicsPane = new Pane();
graphicsPane.setPrefWidth(400);
graphicsPane.setPrefHeight(400);
setBackground();
setText();
setBall();
setBall2();
setButtons();
buttonHandler = new ButtonHandler();
resetButton.setOnAction(buttonHandler);
pauseButton.setOnAction(buttonHandler);
ball.setOnMouseClicked(new ClickHandler());
ball2.setOnMouseClicked(new ClickHandler());
animation = new BallAnimation();
animation.start();
root.setCenter(graphicsPane);
Scene scene = new Scene(root);
primaryStage.setTitle("Ball Game");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* Handles the reset and pause buttons.
*/
private class ButtonHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
if (e.getSource() == resetButton) {
resetGame();
}
if (e.getSource() == pauseButton) {
animation.stop();
if (pauseCount > 0) {
animation.start();
pauseCount = 0;
isPaused = false;
}
else {
pauseCount++;
isPaused = true;
}
}
}
}
/**
* Handles the clicking of a Circle.
* Circles decrease in size
* Circles' speed increases
* Circles change colour
*/
private class ClickHandler implements EventHandler<MouseEvent> {
@Override
public void handle(MouseEvent e) {
if ((e.getSource() == ball || e.getSource() == ball2) && !(isPaused) && !(isEnded)) {
hits++;
setText();
xVelocity++;
ball.setCenterX(0);
ball.setRadius(ballRadius--);
ball.setFill(randomColor());
ball2.setCenterX(0);
ball2.setRadius(ballRadius--);
ball2.setFill(randomColor());
}
}
}
/**
* Resets game when reset button is pressed.
*/
public void resetGame() {
graphicsPane.getChildren().remove(ball);
graphicsPane.getChildren().remove(ball2);
graphicsPane.getChildren().remove(stop);
ballRadius = 28;
setBall();
setBall2();
ball.setOnMouseClicked(new ClickHandler());
ball2.setOnMouseClicked(new ClickHandler());
hits = 0;
misses = 0;
setText();
xVelocity = 1;
isEnded = false;
isPaused = false;
animation.start();
}
/**
* Animates a circle moving across the screen.
* When the number of misses reaches 5, animation stops and displayed message
*/
private class BallAnimation extends AnimationTimer {
@Override
public void handle(long now) {
isEnded = false;
double x = ball.getCenterX();
if (x + xVelocity >= 395 && x + xVelocity <= 400) {
misses++;
setText();
ball.setCenterX(0);
ball2.setCenterX(0);
}
else if (misses == 5) {
animation.stop();
isEnded = true;
getStopMessage();
}
else {
x += xVelocity;
ball.setCenterX(x);
ball2.setCenterX(x);
}
}
}
/**
* Colour randomizer.
* @return a random colour
*/
private Color randomColor() {
Random random = new Random();
return Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}
/**
* Sets the black background.
*/
public void setBackground() {
Rectangle background = new Rectangle(400, 400);
background.setFill(Color.BLACK);
root.getChildren().add(background);
}
/**
* Sets the text for number of hits and misses.
*/
public void setText() {
hitsText = new Text("Hits: " + hits);
hitsText.setFill(Color.WHITE);
missesText = new Text("Misses: " + misses);
missesText.setFill(Color.WHITE);
top = new HBox(10, hitsText, missesText);
top.setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY)));
top.setAlignment(Pos.TOP_LEFT);
graphicsPane.getChildren().add(top);
}
/**
* Adds the first ball to the pane.
*/
public void setBall() {
ball = new Circle(100, 200, ballRadius, Color.WHITE);
graphicsPane.getChildren().add(ball);
}
/**
* Adds the second ball to the pane.
*/
public void setBall2() {
ball2 = new Circle(100, 100, ballRadius, Color.WHITE);
graphicsPane.getChildren().add(ball2);
}
/**
* Adds the pause and reset button.
*/
public void setButtons() {
pauseButton = new Button("Pause");
resetButton = new Button("Reset");
HBox bottom = new HBox(10, pauseButton, resetButton);
bottom.setAlignment(Pos.BOTTOM_RIGHT);
root.setBottom(bottom);
}
/**
* Sets the "Game Over" message for when number of misses reaches 5.
*/
public void getStopMessage() {
stop = new Text(120, 200, "Game Over");
stop.setFill(Color.WHITE);
stop.setFont(Font.font(40));
graphicsPane.getChildren().add(stop);
}
}