-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.java
296 lines (219 loc) · 8.21 KB
/
App.java
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
import processing.core.*;
import java.util.ArrayList;
import edu.nyu.cs.xz1991.assignment6Practice.Basket;
/**
* Solution to Assignment #6
* Basic controller for game Catching Fruits.
* @author Xintong Zhu (xz1991)
* @version 0.1
*/
public class App extends PApplet {
// variables define
// window size of this app
private final int w = 600;
private final int h = 600;
// make constants for some common colors
public final int WHITE = this.color(255, 255, 255); // white color
public final int BLACK = this.color(0, 0, 0); // black color
// make constants for some common spacings
public final static int APP_MARGIN = 60; // a margin value
public final static int NUM_APPLES = 3; // number of apples
public final static int NUM_BOMBS_VERTICAL = 2; // number of vertical falling bombs
public final static int APPLE_SPACING = 120; // spacing between apples
// define variables
// a flag to control the game running
private boolean runOrNot = true;
// variable to hold the basket
private Basket basket;
// variable to hold the point
private Point points;
// an array to hold apples
private ArrayList<Apple> apples = new ArrayList<Apple>();
// an array to hold vertically falling bombs
private ArrayList<Bomb> bombs = new ArrayList<Bomb>();
// variable to hold suddenly-appear bomb
private Bomb bombSurprise;
// variable to hold flying-across bomb
private Bomb bombFly;
// Getter and setter
/**
* Getter of apple ArrayList.
* @return An ArrayList of apples.
*/
public ArrayList<Apple> getApples(){
return this.apples;
} // getApples
/**
* Getter of bomb ArrayList.
* @return An ArrayList of bombs.
*/
public ArrayList<Bomb> getBombs(){
return this.bombs;
} // getBombs
/**
* Setter of the apples.
* @param apples An array list of apples.
*/
public void setApples(ArrayList<Apple> apples) {
this.apples = apples;
} // setApples
/**
* Setter of the bombs.
* @param bombs An array list of bombs.
*/
public void setBombs(ArrayList<Bomb> bombs) {
this.bombs = bombs;
} // setBombs
/**
* Called once to set up windows.
*/
public void settings() {
this.size(this.w, this.h);
} // settings
/**
* Called once on load.
* Used to create the window and "global" settings.
*/
public void setup() {
this.background(this.WHITE); // set background color
// initialize basket
this.basket = new Basket(this); // pass reference to this App object
// initialize point
this.points = new Point(this); // pass reference to this App object
// initialize all apples
int xApple = 50; // x position of first apple
int yApple = 0; // y position of first apple
// initialize all bombs
int xBomb = 30; // x position of first bomb
int yBomb = -2000; // y position of first bomb
int xBombFly = -5000; // a constant y position for the fly-across bomb
// loop as many times as there are apples
for (int a = 0; a < App.NUM_APPLES; a++) {
// create a new apple for each element of the array
Apple apple = new Apple (xApple, yApple, this); // pass the x, y coordinate and a reference to this App class
this.apples.add(apple); // add this apple to the array list
// update x so the next apple we draw appears further to the right
xApple += apple.getWidth() + App.APPLE_SPACING;
} // a for loop
// loop as many times as there are vertically falling bombs
for (int b = 0; b < App.NUM_BOMBS_VERTICAL; b++) {
// create a new bomb for each element of the array
Bomb bomb = new Bomb (xBomb, yBomb, this); // pass the x, y coordinate and a reference to this App class
this.bombs.add(bomb); // add this bomb to the array list
// update x so the next bomb we draw appears further to the right
xBomb += bomb.getWidth() + App.APPLE_SPACING;
} // b for loop
// initialize surprising bomb (overloaded constructor)
this.bombSurprise = new Bomb(this); // pass reference to this app
// initialize flying-across bomb (overloaded constructor)
this.bombFly = new Bomb(xBombFly, this);
} // setup
/**
* Called repeatedly many times per second.
* Used to update the animation and enforce game play logic.
*/
public void draw() {
// run the game if the point is equal to or larger than 0
if (runOrNot) {
// run the game
// wipe the screen blank
this.background(this.WHITE);
// draw the basket
this.basket.draw();
// loop through array list of apples
for (int a = 0; a < this.apples.size(); a++) {
Apple apple = this.apples.get(a);
apple.move(); // move the apple
apple.draw(); // draw the apple
} // a for loop
// loop through array list of bombs
for (int b = 0; b < this.bombs.size(); b++) {
Bomb bomb = this.bombs.get(b);
bomb.moveVertical(); // move the bomb vertically falling
bomb.draw(); // draw the bomb
} // b for loop
// detect if the user catches the apple
ArrayList<Apple> applesToRemove = new ArrayList<Apple>(); // will hold the next generation of apples
// use a for loop to iterate and find caught apples
for (Apple apple: this.apples) {
if (Basket.appleIsCaught(basket, apple)) {
// add caught apples to the applesToRemove array list
applesToRemove.add(apple);
}
} // this.apples for loop
// use a for loop to 'execute' each caught apple
for (Apple apple: applesToRemove) {
apple.catchApple();
// add 5 points for each caught apple
this.points.appleEarn(apple);
} // applesToRemove for loop
// detect if the user mistakenly catches the bomb
ArrayList<Bomb> bombsToRemove = new ArrayList<Bomb>(); // will hold the next generation of bombs
// use a for loop to iterate and find caught bombs
for (Bomb bomb: this.bombs) {
if (Basket.bombIsCaught(basket, bomb)) {
// add caught bombs to the bombsToRemove array list
bombsToRemove.add(bomb);
}
} // this.bombs for loop
// use a for loop to 'execute' each caught bomb
for (Bomb bomb: bombsToRemove) {
bomb.catchVerticalBomb();
// minus 15 points for each caught bomb
this.points.bombLose(bomb);
} // bombsToRemove for loop
// get the current second
int currentSecond = second();
// use current second to control the surprising bomb to appear suddenly and disappear after a few seconds if it is not mistakenly caught by the user
if (currentSecond % 10 < 5) {
// appear once during a span of time
this.bombSurprise.draw();
// check if this bomb is mistakenly caught
if (Basket.bombIsCaught(this.basket, this.bombSurprise)) {
this.bombSurprise.catchSurpriseBomb(); // change the bomb's position (to let the user know they mistakenly caught this bomb)
// lose the points
this.points.bombLose(this.bombSurprise);
// reset this bomb's bombCaught
this.bombSurprise.setBombCaughtFalse();
} // if bombIsCaught
} // if currentSecond
// draw the flying-across bomb
this.bombFly.moveHorizontal(); // move the bomb horizontally
this.bombFly.draw(); // draw itself onto screen
// check if this flying-across bomb is mistakenly caught
if (Basket.bombIsCaught(this.basket, this.bombFly)) {
// this flying-across bomb is caught
this.bombFly.catchFlyBomb();
// lose the points
this.points.bombLose(this.bombFly);
} // if bombIsCaught
// draw the points
this.points.draw();
// quit the game if the point is less than 0
int currentPoints = this.points.getPoint();
if (currentPoints < 0) {
// reset the flag to false to shut down the game
runOrNot = false;
}
} // if run the game
else {
// wipe the screen blank
this.background(this.BLACK);
String loseGame = "GAME OVER!";
String explanation = "points less than 0 :(";
this.fill(WHITE);
this.textSize(50);
this.text(loseGame, 150, 280);
this.textSize(25);
this.text(explanation, 180, 330);
} // if game loses
} // draw
/**
* Automatically called to start my program.
* This method calls PApplet's main method and passes it the class name of this class
* @param args Any command-line argument provided while running the program.
*/
public static void main (String[] args) {
PApplet.main("edu.nyu.cs.xz1991.assignment6Practice.App");
} // main
} // class