-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathController.java
383 lines (344 loc) · 13.1 KB
/
Controller.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package com.example.javafxproject.CarGame;
import javafx.animation.*;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
//import java.lang.classfile.Label;
import java.awt.event.ActionEvent;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Timer;
//import java.awt.event.KeyEvent;
public class Controller implements Initializable {
private double speed = 0;
@FXML
ImageView myCar;
@FXML
ImageView road;
@FXML
ImageView road2;
@FXML
ImageView car1,car2,car3,car4,car5,car6,car7,car8;
@FXML
Pane pane;
private Parent root;
private Scene scene;
Stage stage;
@FXML
Label score,maxScore;
private int sc,maxsc;
private Timeline roadTimeline;
private Timeline setEnemyCars;
private Timeline collisionTimeline;
// private Image[] imageArr = {new Image("/Car1.png") , new Image("Car2.png") , new Image("Car3.jpeg"),
// new Image("Car4.jpeg"),
// new Image("Car5.jpeg")
// };
void left(KeyEvent keyEvent){
if(myCar.getLayoutX()>230){
myCar.setLayoutX(myCar.getLayoutX()-myCar.getFitWidth()-15);
// myCar.setLayoutX(myCar.getLayoutX()-10);
}
}
void right(KeyEvent keyEvent){
if(myCar.getLayoutX()<350){
// myCar.setLayoutX(myCar.getLayoutX()+10);
myCar.setLayoutX(myCar.getLayoutX()+myCar.getFitWidth()+15);
}
}
void increasingSpeed(KeyEvent keyEvent){
if(speed<50)
speed+=.5;
System.out.println(speed);
}
void decreaseSpeed(KeyEvent keyEvent){
if(speed>0 && speed>=5){
speed-=5;
}
else{
speed=0;
}
System.out.println(speed);
}
void fraction(){
if(speed>0){
speed-=.5;
}
else{
speed=0;
}
System.out.println(speed);
}
private Timeline fraction;
private void startFrictionTimer() {
fraction = new Timeline(
new KeyFrame(Duration.seconds(1), event -> fraction())
);
fraction.setCycleCount(Timeline.INDEFINITE);
fraction.play();
}
private void addKeyEvent(){
// FXMLLoader loader = new FXMLLoader(getClass().getResource("/CarGame.fxml"));
// try{
// loader.load();
// }
// catch(Exception e){System.out.println("Error Occured");}
// Controller controller = loader.getController();
scene = pane.getScene();
scene.setOnKeyPressed(event ->{
//scene.setFill(Color.BLACK);
switch(event.getCode()){
case KeyCode.UP :
increasingSpeed(event);
break;
case KeyCode.DOWN:
decreaseSpeed(event);
break;
case KeyCode.LEFT:
left(event);
break;
case KeyCode.RIGHT:
right(event);
break;
case KeyCode.ESCAPE:
stopEverything();
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Exit");
alert.setContentText("Do you want to Continue");
ButtonType exit = new ButtonType("Exit");
ButtonType play = new ButtonType("Continue/Play");
alert.getButtonTypes().setAll(exit,play);
if(alert.showAndWait().get() == exit){
stage.close();
}
else{
startAgain();
}
}
});
}
private void startRoadAnimation() {
road.setLayoutY(0);
road2.setLayoutY(-road.getFitHeight());
roadTimeline = new Timeline(new KeyFrame(Duration.millis(16), event -> {
if(timer.getStatus() == Animation.Status.STOPPED) {
if (speed > 0) {
road.setLayoutY(road.getLayoutY() + speed);
road2.setLayoutY(road2.getLayoutY() + speed);
}
if (road.getLayoutY() >= road.getFitHeight()) {
road.setLayoutY(road2.getLayoutY() - road.getFitHeight());
}
if (road2.getLayoutY() >= road.getFitHeight()) {
road2.setLayoutY(road.getLayoutY() - road2.getFitHeight());
}
}
}));
roadTimeline.setCycleCount(Timeline.INDEFINITE);
roadTimeline.play();
}
void setEnemyCar(){
car1.setLayoutY(-100);
car2.setLayoutY(-road.getFitHeight());
car3.setLayoutY(-100);
car4.setLayoutY(-road.getFitHeight());
car5.setLayoutY(-100);
car6.setLayoutY(-road.getFitHeight());
car7.setLayoutY(-100);
car8.setLayoutY(-road.getFitHeight());
setEnemyCars = new Timeline(new KeyFrame(Duration.millis(20),event->{
if(timer.getStatus() == Animation.Status.STOPPED) {
car1.setLayoutY(car1.getLayoutY() + .6 + speed);
car2.setLayoutY(car2.getLayoutY() + .6 + speed);
car3.setLayoutY(car3.getLayoutY() + 1.5 + speed);
car4.setLayoutY(car4.getLayoutY() + 1.5 + speed);
car5.setLayoutY(car5.getLayoutY() + 2.5 + speed);
car6.setLayoutY(car6.getLayoutY() + 2.5 + speed);
car7.setLayoutY(car7.getLayoutY() + .9 + speed);
car8.setLayoutY(car8.getLayoutY() + .9 + speed);
if (car1.getLayoutY() >= 348 + car1.getFitHeight() && car2.getLayoutY() >= 348 + car2.getFitHeight()) {
car1.setLayoutY(-500);
}
if (car2.getLayoutY() >= 348 + car2.getFitHeight()) {
car2.setLayoutY(-340);
}
if (car3.getLayoutY() >= 348 + car3.getFitHeight() && car4.getLayoutY() >= 348 + car4.getFitHeight()) {
car3.setLayoutY(-200);
}
if (car4.getLayoutY() >= 348 + car4.getFitHeight()) {
car4.setLayoutY(-340);
}
if (car5.getLayoutY() >= 348 + car5.getFitHeight() && car6.getLayoutY() >= 348 + car6.getFitHeight()) {
car5.setLayoutY(-700);
}
if (car6.getLayoutY() >= 348 + car6.getFitHeight()) {
car6.setLayoutY(-340);
}
if (car7.getLayoutY() >= 348 + car7.getFitHeight() && car8.getLayoutY() >= 348 + car8.getFitHeight()) {
car7.setLayoutY(-420);
}
if (car8.getLayoutY() >= 348 + car8.getFitHeight()) {
car8.setLayoutY(-340);
}
}
}));
setEnemyCars.setCycleCount(Timeline.INDEFINITE);
setEnemyCars.play();
}
private void scoring() {
Timeline tl = new Timeline(new KeyFrame(Duration.millis(50), event -> {
// Update score based on each car's position
checkAndScore(car1);
checkAndScore(car2);
checkAndScore(car3);
checkAndScore(car4);
checkAndScore(car5);
checkAndScore(car6);
checkAndScore(car7);
checkAndScore(car8);
}));
tl.setCycleCount(Timeline.INDEFINITE);
tl.play();
}
// Helper method to check each car's position and update score
private void checkAndScore(ImageView car) {
if (car.getLayoutY() >= myCar.getLayoutY() + myCar.getFitHeight() &&
car.getLayoutY() <= myCar.getLayoutY() + myCar.getFitHeight() + speed) {
// Increment the score
sc++;
if (sc > maxsc) maxsc = sc;
// Update score display
score.setText(String.valueOf(sc));
maxScore.setText(String.valueOf(maxsc));
// Reset the car's position for another cycle
resetEnemyPosition(car);
}
}
// Method to reset the car's position to the top for replaying
private void resetEnemyPosition(ImageView car) {
car.setLayoutY(-road.getFitHeight()); // Adjust as per your initial starting height
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
stopEverything();
startTimer();
startRoadAnimation();
startFrictionTimer();
setEnemyCar();
outCondition();
scoring();
pane.setBackground(new Background(new BackgroundFill(Color.BLACK,null,null)));
Platform.runLater(this::addKeyEvent);
}
private Timeline tl;
@FXML
ImageView gameOver;
private void outCondition() {
tl = new Timeline(new KeyFrame(Duration.millis(100), event -> {
if (myCar.getBoundsInParent().intersects(car1.getBoundsInParent()) ||
myCar.getBoundsInParent().intersects(car2.getBoundsInParent()) ||
myCar.getBoundsInParent().intersects(car3.getBoundsInParent()) ||
myCar.getBoundsInParent().intersects(car4.getBoundsInParent()) ||
myCar.getBoundsInParent().intersects(car5.getBoundsInParent()) ||
myCar.getBoundsInParent().intersects(car6.getBoundsInParent()) ||
myCar.getBoundsInParent().intersects(car7.getBoundsInParent()) ||
myCar.getBoundsInParent().intersects(car8.getBoundsInParent())) {
gameOver.setImage(new Image("/GameOver.png"));
ScaleTransition transition = new ScaleTransition(Duration.seconds(1),gameOver);
transition.setByX(1.2);
transition.setByY(1.2);
transition.play();
switchToGameOver();
}
}));
tl.setCycleCount(Timeline.INDEFINITE);
tl.play();
}
void switchToGameOver(){
stopEverything();
PauseTransition pause = new PauseTransition(Duration.seconds(2.3));
pause.setOnFinished(tempevent-> {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/carGame2.fxml"));
root = loader.load();
Controller2 controller = loader.getController();
controller.score.setText(sc + "");
controller.maxScore.setText(maxsc + "");
scene = new Scene(root);
// stage = new Stage();
if(stage==null)
stage = (Stage)pane.getScene().getWindow();
stage.setScene(scene);
// stage.show();
} catch (Exception e) {
System.out.println("Exception Occured");
System.out.println(e);
}//System.out.println("Game Over!");
//remainTime.setText("Game Over!");
});
pause.play();
}
private double prevSpeed;
void stopEverything(){
prevSpeed=speed;
speed=0;
if(roadTimeline!=null)
roadTimeline.stop();
if(setEnemyCars!=null)
setEnemyCars.stop();
if(tl!=null)
tl.stop();
if(fraction!=null)
fraction.stop();
}
@FXML
Label remainTime;
// Timeline breakTimeLine;
void startAgain(){
//prevSpeed=speed;
// breakTimeLine = new Timeline(new KeyFrame(Duration.seconds(5),event->{
startTimer();
// }));
// breakTimeLine.setCycleCount(1);
// breakTimeLine.play();
}
Timeline timer;
void startTimer() {
remainTime.setText("3");
timer = new Timeline(new KeyFrame(Duration.seconds(1),event->{
if(remainTime.getText().equals("GO")){
remainTime.setText("");
}
else if(remainTime.getText().equals("1")){
remainTime.setText("GO");
speed = prevSpeed;
roadTimeline.playFromStart();
setEnemyCars.playFromStart();
tl.play();
}
else {
remainTime.setText(Integer.parseInt(remainTime.getText()) - 1 + "");
}
}));
timer.setCycleCount(4);
timer.play();
}
}