-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypingGame.java
More file actions
562 lines (438 loc) · 13.5 KB
/
TypingGame.java
File metadata and controls
562 lines (438 loc) · 13.5 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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package application;
/*
*
*
* Diego de Jesus Faria Campos 3087950
*
*
* */
//Imports
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import javax.print.DocFlavor.URL;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
public class TypingGame extends Application {
// Setting components
// Elements
Label lblInstruction, lblErrors, lblCountErrors, lblSentence, lblSentenceChecked, lblTimeLeft, lblSelectLevel, lblStartTitle;
TextField txtInput;
Button btnStart, btnInitial, btnLevel, btnHard, btnEasy, btnMenu;
ProgressBar progBar;
ProgressIndicator progIndicator;
String phraseToType;
private static int countErrors, countHits;
String phraseTyped;
//TimeLine
Timeline timeline;
private static int elapsedTime;
int elapsedTimeEasy, elapsedTimeHard;
//Icon Image
Image icon;
//ArrayList with phrases
ArrayList<String> phraselist;
boolean gameOver, displayGlame, hard;
long maxtaskHard, maxtaskEasy;
// Task
Task<Void> task;
// Stages and Scenes - Scene Navigation
private static Stage guiStage;
private static int countPhrases = 0;
private static long max;
Scene sceneGame, sceneMenu, sceneLevelConfig;
//Constructor instancing elements
public TypingGame() {
lblStartTitle = new Label("Improving your typping skills while getting fun!");
btnInitial = new Button("Start");
// Instancing components
lblInstruction = new Label("Type each sentence into the lower textfield");
lblErrors = new Label("Errors: ");
lblTimeLeft = new Label("Time Left ");
lblCountErrors = new Label("0");
lblSentence = new Label("");
lblSentenceChecked = new Label("");
lblSelectLevel = new Label("Choose the Size of your Challenge.");
txtInput = new TextField();
btnStart = new Button("Start");
btnLevel = new Button("Level");
btnHard = new Button("Hard");
btnEasy = new Button("Easy");
btnMenu = new Button("Menu");
progBar = new ProgressBar(0);
progIndicator = new ProgressIndicator(0);
countErrors = 0;
gameOver = true;
phraselist = new ArrayList<>();
hard = false;
maxtaskHard = 770000000 / 2;
maxtaskEasy = 770000000;
elapsedTimeEasy = 30;
elapsedTimeHard = 15;
// Adjusting Elements
txtInput.setDisable(true);
btnStart.setPrefHeight(40);
btnStart.setPrefWidth(120);
btnLevel.setPrefHeight(40);
btnLevel.setPrefWidth(120);
btnInitial.setPrefHeight(40);
btnInitial.setPrefWidth(120);
btnHard.setPrefHeight(40);
btnHard.setPrefWidth(120);
btnEasy.setPrefHeight(40);
btnEasy.setPrefWidth(120);
btnMenu.setId("btnMenu");
progIndicator.setMinSize(50, 50);
lblStartTitle.setStyle("-fx-font-size: 1.2em");
lblSentence.setStyle("-fx-font-size: 1.5em");
lblSentence.setTextFill(Color.BLACK);
}
//Init setting up actions
public void init() {
//Start button will provide the game start
btnStart.setOnAction(event -> {
System.out.println("State into button" + gameOver);
if (gameOver) {
startGame();
} else {
stopTimeLine();
cancelTask();
resetGame();
}
});
//SetonKeyPressed event so when the user press enter his phrase will be verified
txtInput.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
checkPhrase();
}
});
//initial Button on the scene Menu the first scene that will open and aim the level scene
btnInitial.setOnAction(event -> {
TypingGame.getStage().setScene(sceneLevelConfig);
});
//When the user is playing and click to go back to the menu
btnMenu.setOnAction(event ->{
stopTimeLine();
cancelTask();
lblCountErrors.setText("Errors: ");
lblSentenceChecked.setText(" ");
lblSentence.setText(" ");
lblTimeLeft.setText("Elapsed Time ");
TypingGame.getStage().setScene(sceneMenu);
});
//Action that will set up the level choosed by the player
btnEasy.setOnAction(event ->{
TypingGame.getStage().setScene(sceneGame);
hard = false;
});
//Action that will set up the level choosed by the player
btnHard.setOnAction(event ->{
TypingGame.getStage().setScene(sceneGame);
hard = true;
});
}
//Cancel thread binded with progress bar and indicator
private void cancelTask() {
if (task != null) {
task.cancel();
}
}
//Start methode that initialize the thread binded with progress bar and indicator
private void startTask() {
task = new Task<Void>() {
@Override
public Void call() throws Exception {
if(hard) {
max = maxtaskHard;
elapsedTime = elapsedTimeHard;
}else {
max = maxtaskEasy;
elapsedTime = elapsedTimeEasy;
}
for (long i = 1; i <= max; i++) {
if (isCancelled()) {
updateProgress(0, 0);
break;
}
updateProgress(i, max);
if (i == max) {
createScoreTable();
resetGame();
break;
}
}
return null;
}
};
//Biding and starting the thread
new Thread(task).start();
progIndicator.progressProperty().bind(task.progressProperty());
progBar.progressProperty().bind(task.progressProperty());
}
//Gettin phrases from the file A to Z on hard mode
public void generatePhrase() {
try {
// Reading file setences.csv
BufferedReader bufferedFile = new BufferedReader(new FileReader("./assets/sentences.csv"));
String phrase = "";
// Adding phrases into ArrayList
while ((phrase = bufferedFile.readLine()) != null) {
phraselist.add(phrase);
}
// Closing file
bufferedFile.close();
} catch (IOException e) {
System.out.println("Something went wrong reading the file");
e.printStackTrace();
}
}
//Get phrase to aply into the game
public String getPhrase() {
try {
countPhrases++;
Random rand = new Random();
int randomNum = rand.nextInt(phraselist.size());
String randomPhrase = phraselist.get(randomNum);
phraselist.remove(randomNum);
System.out.println(randomPhrase);
return randomPhrase;
} catch (Exception e) {
System.out.println("Probaly array list is empty");
e.printStackTrace();
return "error";
}
}
//Checking phrases that was typed
public void checkPhrase() {
phraseTyped = txtInput.getText();
if (!phraseToType.equals(phraseTyped)) {
countErrors++;
lblSentenceChecked.setText(phraseToType);
lblSentenceChecked.setTextFill(Color.RED);
lblErrors.setText("Errors: " + countErrors);
}else {
lblSentenceChecked.setText(phraseToType);
lblSentenceChecked.setTextFill(Color.GREEN);
countHits++;
}
stopTimeLine();
cancelTask();
if(!hard) {
if (countPhrases == 10) {
resetGame();
} else {
startGame();
}
}else {
startGame();
}
}
//Starting game
public void startGame() {
System.out.println("State into StarteGame()" + gameOver);
gameOver = false;
txtInput.setDisable(false);
txtInput.clear();
btnStart.setText("Stop");
if(hard) {
elapsedTime = elapsedTimeHard;
}else {
elapsedTime = elapsedTimeEasy;
}
lblTimeLeft.setText("Time Left ");
generatePhrase();
if (phraselist.isEmpty()) {
resetGame();
generatePhrase();
} else {
phraseToType = getPhrase();
lblSentence.setText(phraseToType);
}
if (elapsedTime == 0) {
resetGame();
}
startTask();
startTimeLine();
}
//Methode to start the timeline
public void startTimeLine() {
timeline = new Timeline(new KeyFrame(Duration.millis(1000), timerTick -> {
elapsedTime--;
if (elapsedTime == 0) {
timeline.stop();
}
lblTimeLeft.setText("Time Left " + elapsedTime);
}));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
//Mehode to stop the timeline
public void stopTimeLine() {
if (timeline != null) {
timeline.stop();
}
}
//Mehode to reset the game
public void resetGame() {
gameOver = true;
btnStart.setText("Start");
txtInput.setDisable(true);
countPhrases = 0;
if(hard) {
elapsedTime = elapsedTimeHard;
}else {
elapsedTime = elapsedTimeEasy;
}
countErrors = 0;
lblErrors.setText("Errors: 0");
;
lblTimeLeft.setText("Time Left ");
cancelTask();
stopTimeLine();
createScoreTable();
}
public static Stage getStage() {
/*
* When I was changing the scene into the Stage i was having the error bellow
*
* Incomplete attachment. (GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT)(FBO - 820)
* Error creating framebuffer object with Object 7).
*
* But I noticed that when I move the window it's was coming back to work
*
* So i just setWidth(601) changing the minimum of the Stage's size and works
*/
guiStage.setWidth(601);
return guiStage;
}
//Scoreboard Dialog Pane
public void createScoreTable() {
Alert scoreTable = new Alert(AlertType.INFORMATION);
scoreTable.setTitle("Score Board");
scoreTable.setHeaderText("Game Over!");
scoreTable.setContentText("Errors: " + countErrors + "\n" + "Hits: " + countHits);
scoreTable.setGraphic(null);
DialogPane dialogPane = scoreTable.getDialogPane();
dialogPane.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
dialogPane.setId("score-table-alert");
scoreTable.showAndWait();
}
public void start(Stage primaryStage) {
try {
// Setting Up Primary Stage
primaryStage.setTitle("Typing Game");
// Setting Default Size
primaryStage.setHeight(400);
primaryStage.setWidth(600);
// Setting Minimum size avoiding to broke the screen, keeping the size always
// useful to play.
primaryStage.setMinWidth(650);
primaryStage.setMinHeight(370);
// Adding Icon
icon = new Image("./assets/icon.png");
primaryStage.getIcons().add(icon);
// Creating containers
// TypingGame Layout as root
BorderPane root = new BorderPane();
// Grid Layout to put into center
GridPane grid = new GridPane();
// Containers to put into bottom
VBox vbBotton = new VBox();
// Containers to set up the bottom
VBox vbProg = new VBox();
HBox hbButton = new HBox();
// Setting up responsive functions
grid.maxHeightProperty().bind(primaryStage.widthProperty());
grid.maxWidthProperty().bind(primaryStage.widthProperty());
progBar.maxWidthProperty().bind(primaryStage.widthProperty());
txtInput.minWidthProperty().bind(primaryStage.widthProperty().divide(1.5));
// Setting up paddings and spaces
root.setPadding(new Insets(10));
grid.setPadding(new Insets(15));
grid.setVgap(10);
grid.setHgap(10);
hbButton.setPadding(new Insets(10));
vbProg.setSpacing(5);
vbProg.setPadding(new Insets(10));
// Filling up and setting positions of bottom containers
// @grid
grid.add(lblInstruction, 0, 0);
grid.add(lblSentenceChecked, 0, 4);
grid.add(lblSentence, 0, 5);
grid.add(progIndicator, 2, 5);
grid.add(txtInput, 0, 7);
grid.add(lblErrors, 2, 7);
// @Caninters
vbProg.getChildren().addAll(lblTimeLeft, progBar);
hbButton.getChildren().addAll(btnStart, btnMenu);
hbButton.setAlignment(Pos.CENTER);
hbButton.setSpacing(50);
hbButton.setPadding(new Insets(20));
vbBotton.getChildren().addAll(vbProg, hbButton);
// Setting up root layout
root.setCenter(grid);
root.setBottom(vbBotton);
// TScene Menu - A second Scene to be displayed if the user start the
// Creating new Scene and Copying Stage
BorderPane paneMenu = new BorderPane();
VBox vboxMenu = new VBox();
guiStage = primaryStage;
// Setting VBox
vboxMenu.getChildren().addAll(lblStartTitle, btnInitial);
vboxMenu.setAlignment(Pos.CENTER);
vboxMenu.setSpacing(50);
vboxMenu.setPadding(new Insets(100));
paneMenu.setCenter(vboxMenu);
//Creating Pane Level
BorderPane paneLevelConfig = new BorderPane();
VBox vboxLevelConfig = new VBox();
vboxLevelConfig.getChildren().addAll(lblSelectLevel, btnEasy, btnHard);
vboxLevelConfig.setAlignment(Pos.CENTER);
vboxLevelConfig.setSpacing(50);
vboxLevelConfig.setPadding(new Insets(100));
paneLevelConfig.setCenter(vboxLevelConfig);
// Creating setting scenes into the primaryStage and CSS
sceneGame = new Scene(root, 400, 600);
sceneMenu = new Scene(paneMenu, 400, 600);
sceneLevelConfig = new Scene(paneLevelConfig, 400, 600);
sceneGame.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
sceneMenu.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
sceneLevelConfig.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(sceneMenu);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}