forked from ManarNouh/Cookie-Monster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.java
More file actions
201 lines (169 loc) · 5.47 KB
/
Copy path.java
File metadata and controls
201 lines (169 loc) · 5.47 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
package javafxapplication6;
import javafx.animation.AnimationTimer;
import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.ArrayList;
import java.util.List;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import static javafx.application.Application.launch;
import javafx.scene.text.Text;
class Player extends Rectangle {
public Player( double Wlim, double Hlim) {
double x = Wlim/2;
double y = Hlim - Hlim/8;
setX(x);
setY(y);
setWidth(50);
setHeight(20);
}
public void getBigger() {
setWidth(getWidth() + 10);
}
}
class Bomb extends Circle {
public Bomb(int r){
super(r,0,10);
PathTransition p= new PathTransition(Duration.millis(1500),
new Line(r,0,r,550),this);
p.setCycleCount(1);
p.play();
}
}
class Food extends Rectangle {
public PathTransition p;
public Food(int r) {
super(r,0,10,10);
p = new PathTransition(Duration.millis(1500),
new Line(r,0,r,550),this);
p.setCycleCount(1);
p.play();
}
}
class superFood extends Food {
public superFood(int r) {
super(r);
p = new PathTransition(Duration.millis(1500),
new Line(r,0,r,550),this);
p.setCycleCount(1);
p.play();
setWidth(70);
}
}
public class JavaFXApplication6 extends Application {
private List<Food> f = new ArrayList<>();
private List<Bomb> b = new ArrayList<>();
private List<superFood> sp = new ArrayList<>();
Player r = new Player(500,550);
private final Text T = new Text();
private static int score = 0;
BorderPane root = new BorderPane();
int k = 10;
private void checkbomb(){
Text o = new Text(100,100,"Game Over");
for(Bomb bomb: b){
if(bomb.getBoundsInParent().intersects(r.getBoundsInParent())){
r.setFill(Color.RED);
timer.stop();
root.getChildren().removeAll(r,bomb);
root.setCenter(o);
}
}
}
private void checksuper() {
for(superFood i : sp) {
if(i.getBoundsInParent().intersects(r.getBoundsInParent())) {
f.remove(i);
root.getChildren().remove(i);
r.getBigger();
Timeline t = new Timeline(
new KeyFrame(Duration.seconds(10), e->{
r.setWidth(r.getWidth() - 10);}));
t.play();
}
}
}
private final AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long CurrentNanoTime) {
if(Math.random() < 0.001) {
Bomb b1 = new Bomb((int)(Math.random()*500));
b.add(b1);
root.getChildren().add(b1);
}
if(Math.random()<0.015){
Food f1 = new Food((int)(Math.random()*500));
f.add(f1);
root.getChildren().add(f1);}
if(Math.random() < 0.01) {
superFood s1 = new superFood((int)(Math.random()*500));
sp.add(s1);
root.getChildren().add(s1);
}
fed();
cleanup();
checkbomb();
T.setText(Score());
checksuper();
}};
private void cleanup() {
for(Food i : f) {
if(i.getBoundsInParent().getMaxY() >= 555){
f.remove(i);
root.getChildren().remove(i);
}}
for(Bomb bomb : b){
if(bomb.getBoundsInParent().getMaxY() >= 555){
b.remove(bomb);
root.getChildren().remove(bomb);}
}
for(superFood s : sp){
if(s.getBoundsInParent().getMaxY() >= 555){
sp.remove(s);
root.getChildren().remove(s);}
}
}
public String Score() {
return " " + score;
}
private void fed() {
for(Food i : f){
if(i.getBoundsInParent().intersects(r.getBoundsInParent())) {
f.remove(i);
root.getChildren().remove(i);
score++; }
}
}
@Override
public void start(Stage primaryStage) {
r.setOnKeyPressed(
e -> {
switch(e.getCode()) {
case RIGHT: r.setX(r.getX() + k); break;
case LEFT: r.setX(r.getX() - k); break;
}
});
timer.start();
root.setBottom(T);
T.setFill(Color.CORNFLOWERBLUE);
primaryStage.setResizable(false);
root.getChildren().addAll(r);
Scene scene = new Scene(root, 500, 550);
primaryStage.setTitle("Cookie Monster");
scene.setFill(Color.ANTIQUEWHITE);
primaryStage.setScene(scene);
primaryStage.show();
r.requestFocus();
}
public static void main(String[] args) {
launch(args);
}
}