forked from duke-compsci308-spring2016/lab_bins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodemast.java
More file actions
269 lines (246 loc) · 9.59 KB
/
codemast.java
File metadata and controls
269 lines (246 loc) · 9.59 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
// This entire file is part of my masterpiece.
// Michelle Chen
package game_mmc56;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.stage.Stage;
import javafx.scene.text.FontPosture;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
class Game {
public static final String gameName = "B T B";
private static final int numberOf = 30;
public static final int KEY_INPUT_SPEED = 13;
private Scene theScene;
private Canvas canvas;
private GraphicsContext gc2;
private Group root;
private Rectangle catcher;
private Rectangle object;
private ArrayList<Rectangle> rectz;
private List<Shape> targets;
private boolean start = false;
//name of the game
public String getName() {
return gameName;
}
//setting up scene
public Scene myScene(int width, int height) {
//scene
root = new Group();
theScene = new Scene(root, width, height, Color.LAVENDERBLUSH);
//splash screen
canvas = new Canvas(width, height);
gc2 = splashScreen(width, height, canvas);
root.getChildren().add(canvas);
//starting game
startGame(width, height);
//level two.. not working
levelTwo(width, height, gc2);
//cheat code
cheat();
return theScene;
}
//starting game
private void startGame(int width, int height) {
theScene.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getClickCount() ==2) {
start = true;
gc2.clearRect(0, 0, 500, 500);
// square
catcher = new Rectangle(width /9, height /9, 30, 30);
catcher.setFill(Color.DARKBLUE);
root.getChildren().add(catcher);
// other squares
rectz= rectangles(numberOf);
for(Rectangle rect: rectz) {
root.getChildren().add(rect);
}
//respond to key presses
theScene.setOnKeyPressed(e -> keyInput(e.getCode()));
//winning game
canvas = new Canvas(width, height);
GraphicsContext gc = canvas.getGraphicsContext2D();
root.getChildren().add(canvas);
}
}
});
cheat();
levelTwo(width, height, gc2);
}
private void levelTwo(int width, int height, GraphicsContext gcc) {
theScene.addEventFilter(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
gcc.clearRect(0, 0, 500, 500);
gcc.setFill(Color.AQUA);
final Arc gun = new Arc(width/2, height, 50, 50, 0, 180);
gun.setFill(Color.DARKBLUE);
gun.setType(ArcType.ROUND);
root.getChildren().add(gun);
//square
root.getChildren().add(new Rectangle(215, 25, 75, 75));
fire(targets);
}
});
}
//this doesn't work
private void fire(List<Shape> targets) {
gc2.clearRect(0, 0, 500, 500);
final Shape bullet = new Circle(4, Color.BLACK);
root.getChildren().add(bullet);
final TranslateTransition bulletAnimation = new TranslateTransition(Duration.seconds(1), bullet);
final int bulletTargetX = new Random().nextInt(500);
bullet.boundsInParentProperty().addListener(new ChangeListener<Bounds>() {
@Override
public void changed(ObservableValue<? extends Bounds> observable,
Bounds oldValue, Bounds newValue) {
for (final Shape target : new ArrayList<Shape>(targets)) {
if (((Path)Shape.intersect(bullet, target)).getElements().size() > 0) {
targets.remove(target);
root.getChildren().remove(target);
}
}
}
});
bulletAnimation.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
root.getChildren().remove(bullet);
}
});
bulletAnimation.play();
}
//cheat key
private void cheat(){
theScene.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.ENTER){
gc2.clearRect(0, 0, 500, 500);
winningScreen(canvas);
}
}
});
}
//splash screen
public GraphicsContext splashScreen(int width, int height, Canvas canvas) {
GraphicsContext gc2 = canvas.getGraphicsContext2D();
Font myFont = Font.font("Times New Roman", FontWeight.BOLD, 30);
gc2.setFont(myFont);
gc2.setFill(Color.DARKBLUE);
gc2.fillText("BTB : BEAT THE BLOX", 80, 36);
Font myFont2 = Font.font("Times New Roman", FontWeight.BOLD, 15);
gc2.setFont(myFont2);
gc2.fillText("GTHC! You're a loyal Duke fan on a mission to shut down pesky UNC", 20, 80);
gc2.fillText("fans. Use your left and right keys to move your Duke Blue block around", 20, 98);
gc2.fillText("the screen, and touch the spinning UNC blox to make them dissapear.", 20, 116);
Font myFont3 = Font.font("Times New Roman", FontWeight.BOLD, 18);
gc2.setFont(myFont3);
gc2.fillText("double tap to start the game!", 125, 160);
gc2.drawImage(new Image("/images/ddmf.png"), 100, 200);
return gc2;
}
//arraylist of blox
private ArrayList<Rectangle> rectangles(int numberOf) {
ArrayList<Rectangle> theRectangles = new ArrayList<Rectangle>();
for (int a = 0; a<numberOf; a++) {
object = new Rectangle(300*Math.random()+100, 300*Math.random()+100, 15, 15);
object.setFill(Color.LIGHTSKYBLUE);
theRectangles.add(object);
}
return theRectangles;
}
//removing blox and checking if they're all gone
public void movement(double elapsedTime) {
if (start) {
for (Rectangle recta: rectz) {
recta.setRotate(recta.getRotate()-Math.random());
}
for (Rectangle rectan: rectz) {
Shape intersector = Shape.intersect(catcher, rectan);
if (intersector.getBoundsInLocal().getWidth()!=-1) {
rectan.setFill(Color.BLACK);
root.getChildren().remove(rectan);
rectz.remove(rectan);
break;
}
}
if (rectz.isEmpty()) {
gc2.clearRect(0, 0, 512, 512);
winningScreen(canvas);
}
}
}
//reading key input
private void keyInput (KeyCode code) {
switch (code) {
case RIGHT:
catcher.setX(catcher.getX() + KEY_INPUT_SPEED);
break;
case LEFT:
catcher.setX(catcher.getX() - KEY_INPUT_SPEED);
break;
case UP:
catcher.setY(catcher.getY() - KEY_INPUT_SPEED);
break;
case DOWN:
catcher.setY(catcher.getY() + KEY_INPUT_SPEED);
break;
default:
}
}
//screen after level one passed
public Canvas winningScreen(Canvas canvas){
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, 500, 500);
Font myFont = Font.font("Times New Roman", FontWeight.BOLD, 24);
gc.setFont(myFont);
gc.setFill(Color.DARKBLUE);
gc.fillText("CONGRATULATIONS!!!!!!", 100, 60);
Font myFont2 = Font.font("Times New Roman", FontWeight.BOLD, 17);
gc.setFont(myFont2);
gc.fillText("You've beaten all of the pesky UNC blox.", 100, 90);
Font myFont3 = Font.font("Times New Roman", FontWeight.NORMAL, 15);
gc.setFont(myFont3);
gc.fillText("But you're not done just yet... looks like UNC fans are back for more.", 40, 120);
gc.setFont(myFont2);
gc.fillText("Drag your mouse to start the next level!", 115, 160);
Font myFont4 = Font.font("Times New Roman", FontWeight.NORMAL, 13);
gc.setFont(myFont4);
gc.fillText("(ok to be honest this level pretty much doesn't work at all)", 100, 180);
gc.drawImage(new Image("/images/gthc.jpg"), 165, 200);
return canvas;
}
}