-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamePane.java
More file actions
235 lines (206 loc) · 8.42 KB
/
GamePane.java
File metadata and controls
235 lines (206 loc) · 8.42 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
package com.example.ballpane;
import javafx.animation.*;
import javafx.beans.property.DoubleProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.util.Duration;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
public class GamePane extends Pane {
private final int radius = 40;
private int count = 0;
private double defaultRate ;
private int score = 0;
private myCircle[] myCirclesArr = new myCircle[50];
private Timeline animation;
private Label label = new Label();
private Font font = new Font(20);
private GridPane topScores = new GridPane();
private VBox vBox = new VBox();
public int getScore() {
return score;
}
public GamePane() throws IOException {
Label welcomeLabel = new Label("Welcome to Speed Click Game");
welcomeLabel.setFont(new Font(54));
welcomeLabel.setTextFill(Color.WHITESMOKE);
getChildren().add(welcomeLabel);
TranslateTransition translateTransition = new TranslateTransition(Duration.seconds(5), welcomeLabel);
translateTransition.setFromX(100);
translateTransition.setFromY(-100);
translateTransition.setToY(300);
FadeTransition fadeTransition = new FadeTransition(Duration.seconds(1), welcomeLabel);
fadeTransition.setFromValue(1.0);
fadeTransition.setToValue(0.0);
fadeTransition.setDelay(Duration.seconds(3));
fadeTransition.setOnFinished(event -> {getChildren().remove(welcomeLabel);
label.setText("To Start, Click the play button\n" +
"Your score will be shown at the bottom and the values of the \n" +
"objects are shown on the right side.");
label.setTextFill(Color.WHITESMOKE);
label.setFont(font);
try {
getScores();
} catch (IOException e) {
throw new RuntimeException(e);
}
label.setAlignment(Pos.CENTER);
topScores.setAlignment(Pos.CENTER);
vBox.getChildren().addAll(label,topScores);
getChildren().add(vBox);});
translateTransition.play();
fadeTransition.play();
}
public void startGame(){
reset();
getChildren().add(this.myCirclesArr[this.count]);
this.animation = new Timeline( new KeyFrame(Duration.millis(50), e -> {
try {
moveBallDownAndRemove();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}));
this.animation.setCycleCount(Timeline.INDEFINITE);
this.animation.play();
this.defaultRate = this.animation.getRate();
}
private void reset(){
this.score = 0;this.count = 0;
getChildren().remove(vBox);
createObjects();
}
private void createObjects(){
for (int i = 0 ; i < myCirclesArr.length ; i++){
myCirclesArr[i] = new myCircle(i,radius,(int)getWidth());}
}
private void add(boolean shouldAdd) {
if (shouldAdd && count != myCirclesArr.length -1){
getChildren().add(myCirclesArr[count + 1]);count++;
increaseSpeed();
}
}
private void roundOver() throws IOException {animation.setRate(this.defaultRate);animation.stop(); endRoundDisplay();}
private void saveScores() throws IOException {
File file = new File("High_Scores.txt");
Scanner input = new Scanner(file);
ArrayList<Integer> highScores = new ArrayList<>();
while(input.hasNext()){
highScores.add(input.nextInt());
} input.close();
highScores.add(score);
highScores.sort(Collections.reverseOrder());
highScores.remove(highScores.size()-1);
PrintWriter output = new PrintWriter(file);
highScores.forEach(output::println);
output.close();
}
private void getScores() throws IOException {
File file = new File("High_Scores.txt");
Scanner input = new Scanner(file);
ArrayList<Integer> highScores = new ArrayList<>();
while(input.hasNext()){
highScores.add(input.nextInt());
} input.close();
topScores.setPadding(new Insets(10));
topScores.setHgap(10);
topScores.setVgap(10);
Label nameLabel = new Label("Rank");
Label scoreLabel = new Label("Top Scores");
nameLabel.setTextFill(Color.WHITESMOKE);
nameLabel.setFont(font);
scoreLabel.setTextFill(Color.WHITESMOKE);
scoreLabel.setFont(font);
topScores.add(nameLabel, 0, 0);
topScores.add(scoreLabel, 1, 0);
String[] ranks = { "1", "2", "3", "4", "5" };
for (int i = 0; i < ranks.length; i++) {
Label nameLabelEntry = new Label(ranks[i]);
Label scoreLabelEntry = new Label(Integer.toString(highScores.get(i)));
nameLabelEntry.setTextFill(Color.WHITESMOKE);
nameLabelEntry.setFont(font);
scoreLabelEntry.setTextFill(Color.WHITESMOKE);
scoreLabelEntry.setFont(font);
topScores.add(nameLabelEntry, 0, i + 1);
topScores.add(scoreLabelEntry, 1, i + 1);
}
}
private void updateScores() throws IOException{
saveScores();
File file = new File("High_Scores.txt");
Scanner input = new Scanner(file);
ArrayList<Integer> highScores = new ArrayList<>();
while(input.hasNext()){
highScores.add(input.nextInt());
} input.close();
for (int i = 0; i < highScores.size(); i++) {
getLabelFromCell(topScores, 1, i + 1).setText(Integer.toString(highScores.get(i)));
}
}
private Label getLabelFromCell(GridPane gridPane, int columnIndex, int rowIndex) {
for (javafx.scene.Node node : gridPane.getChildren()) {
if (GridPane.getColumnIndex(node) == columnIndex && GridPane.getRowIndex(node) == rowIndex) {
if (node instanceof Label) {
return (Label) node;
}
}
} return null;
}
public void selector(double x,double y) {
ArrayList<Integer> indexes = new ArrayList<>();
for (Node node: this.getChildren()) {
if (node instanceof myCircle){
myCircle circle = (myCircle) node;
if (circle.contains(x,y)){
this.score += circle.getValue();
indexes.add(circle.getIndex());
}
}}
indexes.forEach(e-> {this.getChildren().remove(myCirclesArr[e]); add(true);});
}
private void increaseSpeed(){
this.animation.setRate(this.animation.getRate() + 0.5);
}
private DoubleProperty rateProperty(){
return animation.rateProperty();
}
protected void moveBallDownAndRemove() throws IOException {
ArrayList<Integer> indexes = new ArrayList<>();
ArrayList<Boolean> shouldAdd = new ArrayList<>();
if (this.getChildren().isEmpty()) roundOver();
else {
for (Node node : this.getChildren()) {
if (node instanceof myCircle){
myCircle circle = (myCircle) node;
if (circle.getCenterY() > getHeight()) {
indexes.add(circle.getIndex());
} else {
circle.adjustPosition();
shouldAdd.add((int)myCirclesArr[count].getCenterY() == (int)getHeight()/4);
}
}}
shouldAdd.forEach(this::add);
indexes.forEach(e-> this.getChildren().remove(myCirclesArr[e]));
}
}
private void endRoundDisplay() throws IOException {
label.setText("Nice !\nYour score for this play was :" + getScore() +
"\nIf you wish to play again, click the play button.");
updateScores();
getChildren().add(vBox);
}
}