-
Notifications
You must be signed in to change notification settings - Fork 0
/
FroggerApp
245 lines (193 loc) · 7.22 KB
/
FroggerApp
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
import javafx.animation.AnimationTimer;
import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
import java.util.Timer;
import java.util.TimerTask;
import javafx.geometry.Pos;
public class FroggerApp extends Application
{
int timepassed = 0;
Timer time = new Timer();
Text text = new Text("00:00:00");
Label lbl = new Label("00:00:00");
/*************************************
* This section initiates variables
************************************/
private AnimationTimer timer;
private Pane root;
private List<Node> wrench = new ArrayList<>();
private Node dodger;
/*************************************
* This section creates the content
************************************/
private Parent createContent()
{
root = new Pane ();
root.setStyle("-fx-background-color: blue");
root.setPrefSize(620,600);
dodger = initDodger();
root.getChildren().addAll(dodger,lbl);
timer = new AnimationTimer()
{
@Override
public void handle(long now)
{
onUpdate();
}
};
timer.start();
return root;
}//Ends Conten0t
/*************************************
* creates rectangle for player
************************************/
private Node initDodger()
{
Rectangle rect = new Rectangle(38, 38, Color.GREEN);
rect.setTranslateY(350 - 39);
return rect;
}// Ends Player creation
/*************************************
* creates rectangle for wrenches
************************************/
private Node spawnWrench()
{
Circle circ = new Circle(20, Color.PURPLE);
circ.setTranslateX((int) (Math.random() * 14) * 60 );
root.getChildren().addAll(circ);
return circ;
}// Ends wrench creation
/*************************************
* This section updates the frames
************************************/
private void onUpdate()
{
for (Node wrench : wrench)
wrench.setTranslateY(wrench.getTranslateY() + Math.random() * 20);
if (Math.random() < 0.075)
{
wrench.add(spawnWrench());
}
checkState();
}//Ends frame Update
/******************************************************
* This section stops the game if player collides with another
*******************************************************/
private void checkState()
{
for (Node wrench : wrench)
{
if (wrench.getBoundsInParent().intersects(dodger.getBoundsInParent()))
{
timer.stop();
String lose = "You Lose";
HBox hbox2 = new HBox();
hbox2.setTranslateX(210);
hbox2.setTranslateY(250);
root.getChildren().add(hbox2);
for (int i = 0; i < lose.toCharArray().length; i++)
{
char letter = lose.charAt(i);
Text text = new Text (String.valueOf(letter));
text.setFont(Font.font(48));
text.setOpacity(0);
hbox2.getChildren().add(text);
FadeTransition ft = new FadeTransition(Duration.seconds(0.66), text);
ft.setToValue(1);
ft.setDelay(Duration.seconds(i * 0.15));
ft.play();
}
root.getScene().setOnKeyPressed(event ->
{
switch (event.getCode())
{
case G:
dodger.setTranslateX(0);
dodger.setTranslateY(350 - 39);
break;
case B:
createContent();
timer.start();
break;
default:
}});
}
}
/***************************************************************************
* This section determines what happens when you reach the other side of the screen
***************************************************************************/
if (dodger.getTranslateX() >= 600)
{
timer.stop();
String win = "You Win";
HBox hbox = new HBox ();
hbox.setTranslateX(210);
hbox.setTranslateY(250);
root.getChildren().addAll(hbox);
for (int i = 0; i < win.toCharArray().length; i++)
{
char letter = win.charAt(i);
Text text = new Text (String.valueOf(letter));
text.setFont(Font.font(48));
text.setOpacity(0);
hbox.getChildren().add(text);
FadeTransition ft = new FadeTransition(Duration.seconds(0.66), text);
ft.setToValue(1);
ft.setDelay(Duration.seconds(i * 0.15));
ft.play();
}
}
}
/**********************************
* This section starts the game
******************************** */
@Override
public void start(Stage stage) throws Exception
{
stage.setScene (new Scene(createContent()));
stage.getScene().setOnKeyPressed(event ->
{
switch (event.getCode())
{
case W:
dodger.setTranslateY(dodger.getTranslateY() - 40);
break;
case S:
dodger.setTranslateY(dodger.getTranslateY() + 40);
break;
case A:
dodger.setTranslateX(dodger.getTranslateX() - 40);
break;
case D:
dodger.setTranslateX(dodger.getTranslateX() + 40);
break;
default:
break;
}
});
stage.setTitle("Dodger Pro");
stage.show();
}//End Start Exception
public static void main (String[] args)
{
launch(args);
}
}//End Main