-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyWorld.java
executable file
·459 lines (380 loc) · 21.6 KB
/
MyWorld.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
import java.awt.GraphicsEnvironment;
/**
* This is my World class that extends World.
*
* I put a few comments in all the classes so that the reader will have an
* easier time understanding my code.
*
* @author Bill Xiang
* @version 2.0
*/
public class MyWorld extends World
{
/** The first set of private final variables represent the game states. */
private static final int MENU = 0;
private static final int PLAYING = 1;
private static final int SUSPENDED = 2;
private static final int NEXTLEVEL = 3;
private static final int GAMEOVER = 4;
private static final int WIN = 5;
/** The second set of private variables represent the the direction the text class should lock
* meaning which way the text class should grow if it gets bigger (so that it doesn't go off-screen,
* as it would extend in both directions by default). [EXTRA]
*/
private int NO_LOCK = 0;
private int LOCK_LEFT = 1;
private int LOCK_RIGHT = 2;
/** The private static level variable helps me keep track of what level I'm on. */
private static int level = 0;
/** The private variable state represents the current game state. */
private int state = MENU;
/** Adjustable game settings [EXTRA] */
public static int NUM_CENTIPEDES = 11;
public static final int NUM_MUSHROOMS = 40;
public static final int NUM_CENTIPEDEMUSHROOMS = 5;
public static final int NUM_MILLIPEDEMUSHROOMS = 3;
public static final int GRID_SIZE = 20;
//public static final int WORLD_W = 480;
//public static final int WORLD_H = 580;
public static final int WORLD_W = 400;
public static final int WORLD_H = 600;
/** Allows you to adjust how far the mushrooms can spawn away the bottom
* Note that the offset from the top is always one row [EXTRA] */
public static final int NUM_ROWS_SPAWN_ABOVE_BOTTOM = 2;
/** Variables that will hold the class containing the Title and Enter images */
private Title title;
private Enter enter;
/** Pressing enter after game is over to play again */
private boolean enterPressed = false;
//Centipede[] centipedes = new Centipede[NUM_CENTIPEDES + NUM_CENTIPEDEMUSHROOMS];
/** All centipedes stored in an ArrayList */
ArrayList<Centipede> centipedes = new ArrayList<Centipede>();
ArrayList<Mushroom> mushrooms = new ArrayList<Mushroom>();
/** Score global variable */
Score score;
Player player;
Timer timer;
int levelDelay = 0;
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
/** Title and enter initalized and added */
super(WORLD_W, WORLD_H, 1, false);
title = new Title();
title.getImage().scale(WORLD_W, WORLD_H);
addObject(title, WORLD_W/2, WORLD_H/2);
enter = new Enter();
enter.getImage().scale(WORLD_W/2, WORLD_H/12);
addObject(enter, WORLD_W*9/13, WORLD_H*13/18);
level = 0;
NUM_CENTIPEDES = 10;
timer = new Timer(1000, true);
/*
String fonts[] =
GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for ( int i = 0; i < fonts.length; i++ )
{
System.out.println(fonts[i]);
}
*/
}
public void act() {
if (state == MENU) {
if (Greenfoot.isKeyDown("enter")) {
state = PLAYING;
removeObject(title);
removeObject(enter);
// Change state to PLAYING and set up the World by
// removing the Title and adding a Player,
//centipede, and mushrooms
score = new Score("0", 30, Color.RED, Color.BLACK, LOCK_LEFT);
addObject(score, 10, 10);
player = new Player();
GreenfootImage playerImage = player.getImage();
playerImage.scale(20, 20);
addObject(player, getWidth()/2, getHeight() - playerImage.getHeight()/2);
/** centipedes adding */
for (int i = 0; i < NUM_CENTIPEDES; i++) {
Centipede cent = new Centipede();
centipedes.add(cent);
GreenfootImage img = cent.getImage();
img.scale(GRID_SIZE, GRID_SIZE);
addObject(cent, img.getWidth()/2 + i * img.getWidth(), img.getHeight()/2);
//addObject(cent, img.getWidth() * 17 + i * img.getWidth(), img.getHeight()/2);
}
/** Normal mushroom adding */
for (int i = 0; i < NUM_MUSHROOMS; i++) {
Mushroom mush = new Mushroom();
GreenfootImage mushImage = mush.getImage();
mushImage.scale(GRID_SIZE, GRID_SIZE);
/** Random number from x from 0 inclusive to world width / the grid width EXclusive, multiplied by the grid width to fit the world,
* and half width of mushroom added to get to center */
int x = Greenfoot.getRandomNumber((int)getWidth()/GRID_SIZE) * GRID_SIZE + mushImage.getWidth()/2;
/** Random number from 0 inclusive to the world width / the grid width EXclusive (modified based on the NUM_ROWS_SPAWN_ABOVE_BOTTOM
* variable) plus one to include the excluded value and additionally make sure that no mushroom spawns on first row,
* multipled by the grid width to fit the world, and half width of mushroom added to get to center
*/
int y = (Greenfoot.getRandomNumber((int)(getHeight()/GRID_SIZE-(1+NUM_ROWS_SPAWN_ABOVE_BOTTOM)))+1) * GRID_SIZE + mushImage.getHeight()/2;//- mushImage.getHeight()*(NUM_ROWS_SPAWN_ABOVE_BOTTOM);
/** loops until it spawns at an area not already taken by another mushroom */
while (getObjectsAt(x, y, Mushroom.class).size() != 0) {
x = Greenfoot.getRandomNumber((int)getWidth()/GRID_SIZE) * GRID_SIZE + mushImage.getWidth()/2;
y = (Greenfoot.getRandomNumber((int)(getHeight()/GRID_SIZE-(1+NUM_ROWS_SPAWN_ABOVE_BOTTOM)))+1) * GRID_SIZE + mushImage.getHeight()/2;//- mushImage.getHeight()*(NUM_ROWS_SPAWN_ABOVE_BOTTOM);
}
mushrooms.add(mush);
addObject(mush, x, y);
//System.out.println(x + " " + y);
}
/** Same logic as normal mushroom */
for (int i = 0; i < NUM_CENTIPEDEMUSHROOMS; i++) {
CentipedeMushroom cmush = new CentipedeMushroom();
GreenfootImage mushImage = cmush.getImage();
mushImage.scale(GRID_SIZE, GRID_SIZE);
int x = Greenfoot.getRandomNumber((int)getWidth()/GRID_SIZE) * GRID_SIZE + mushImage.getWidth()/2;
int y = (Greenfoot.getRandomNumber((int)(getHeight()/GRID_SIZE-(1+NUM_ROWS_SPAWN_ABOVE_BOTTOM)))+1) * GRID_SIZE + mushImage.getHeight()/2;
while (getObjectsAt(x, y, Mushroom.class).size() != 0) {
x = Greenfoot.getRandomNumber((int)getWidth()/GRID_SIZE) * GRID_SIZE + mushImage.getWidth()/2;
y = (Greenfoot.getRandomNumber((int)(getHeight()/GRID_SIZE-(1+NUM_ROWS_SPAWN_ABOVE_BOTTOM)))+1) * GRID_SIZE + mushImage.getHeight()/2;
}
addObject(cmush, x, y);
}
for (int i = 0; i < NUM_MILLIPEDEMUSHROOMS; i++) {
MillipedeMushroom mmush = new MillipedeMushroom();
GreenfootImage mushImage = mmush.getImage();
mushImage.scale(GRID_SIZE, GRID_SIZE);
int x = Greenfoot.getRandomNumber((int)getWidth()/GRID_SIZE) * GRID_SIZE + mushImage.getWidth()/2;
int y = (Greenfoot.getRandomNumber((int)(getHeight()/GRID_SIZE-(1+NUM_ROWS_SPAWN_ABOVE_BOTTOM)))+1) * GRID_SIZE + mushImage.getHeight()/2;
while (getObjectsAt(x, y, Mushroom.class).size() != 0) {
x = Greenfoot.getRandomNumber((int)getWidth()/GRID_SIZE) * GRID_SIZE + mushImage.getWidth()/2;
y = (Greenfoot.getRandomNumber((int)(getHeight()/GRID_SIZE-(1+NUM_ROWS_SPAWN_ABOVE_BOTTOM)))+1) * GRID_SIZE + mushImage.getHeight()/2;
}
addObject(mmush, x, y);
}
}
}
else if (state == GAMEOVER) {
/** Displays game over, and user can press enter to restart */
GameOver gameOver = new GameOver();
gameOver.getImage().scale(WORLD_W/2, WORLD_H/8);
addObject(gameOver, WORLD_W/2, WORLD_H*7/8);
if (Greenfoot.isKeyDown("enter")) {
enterPressed = true;
} else if (enterPressed) {
enterPressed = false;
removeObjects(getObjects(Centipede.class));
removeObjects(getObjects(Mushroom.class));
removeObjects(getObjects(Player.class));
removeObjects(getObjects(GameOver.class));
removeObjects(getObjects(Score.class));
removeObjects(getObjects(Millipede.class));
level = 0;
NUM_CENTIPEDES = 10;
state = MENU;
}
} else if (state == WIN) {
/** Displays you win, and user can press enter to restart */
YouWin youWin = new YouWin();
youWin.getImage().scale(WORLD_W/2, WORLD_H/8);
addObject(youWin, WORLD_W/2, WORLD_H*7/8);
if (Greenfoot.isKeyDown("enter")) {
enterPressed = true;
// Reset the game variables
// Reset state to MENU
} else if (enterPressed) {
enterPressed = false;
removeObjects(getObjects(Centipede.class));
removeObjects(getObjects(Mushroom.class));
removeObjects(getObjects(Player.class));
removeObjects(getObjects(YouWin.class));
removeObjects(getObjects(Score.class));
removeObjects(getObjects(Millipede.class));
level = 0;
NUM_CENTIPEDES = 10;
state = MENU;
}
} else if (state == NEXTLEVEL) {
/**delay between levels*/
levelDelay++;
/** During part of that delay, extra mushrooms spawn */
if (levelDelay % 20 == 0 && levelDelay <= 120) {
Mushroom mush = new Mushroom();
/** creating image to get access to width method of image*/
GreenfootImage mushImage = new GreenfootImage((level+1) + "Mushroom_001.png");
mushImage.scale(GRID_SIZE, GRID_SIZE);
mush.level = level;
int x = Greenfoot.getRandomNumber((int)getWidth()/GRID_SIZE) * GRID_SIZE + mushImage.getWidth()/2;
int y = (Greenfoot.getRandomNumber((int)(getHeight()/GRID_SIZE-(1+NUM_ROWS_SPAWN_ABOVE_BOTTOM)))+1) * GRID_SIZE + mushImage.getHeight()/2;//- mushImage.getHeight()*(NUM_ROWS_SPAWN_ABOVE_BOTTOM);
while (getObjectsAt(x, y, Mushroom.class).size() != 0) {
x = Greenfoot.getRandomNumber((int)getWidth()/GRID_SIZE) * GRID_SIZE + mushImage.getWidth()/2;
y = (Greenfoot.getRandomNumber((int)(getHeight()/GRID_SIZE-(1+NUM_ROWS_SPAWN_ABOVE_BOTTOM)))+1) * GRID_SIZE + mushImage.getHeight()/2;//- mushImage.getHeight()*(NUM_ROWS_SPAWN_ABOVE_BOTTOM);
}
mushrooms.add(mush);
addObject(mush, x, y);
}
if (levelDelay == 130 || levelDelay == 150) {
CentipedeMushroom cmush = new CentipedeMushroom();
/** creating image to get access to width method of image*/
GreenfootImage mushImage = new GreenfootImage("centMushroom_001.png");
mushImage.scale(GRID_SIZE, GRID_SIZE);
cmush.level = level;
cmush.setImage(mushImage);
int x = Greenfoot.getRandomNumber((int)getWidth()/GRID_SIZE) * GRID_SIZE + mushImage.getWidth()/2;
int y = (Greenfoot.getRandomNumber((int)(getHeight()/GRID_SIZE-(1+NUM_ROWS_SPAWN_ABOVE_BOTTOM)))+1) * GRID_SIZE + mushImage.getHeight()/2;
while (getObjectsAt(x, y, Mushroom.class).size() != 0) {
x = Greenfoot.getRandomNumber((int)getWidth()/GRID_SIZE) * GRID_SIZE + mushImage.getWidth()/2;
y = (Greenfoot.getRandomNumber((int)(getHeight()/GRID_SIZE-(1+NUM_ROWS_SPAWN_ABOVE_BOTTOM)))+1) * GRID_SIZE + mushImage.getHeight()/2;
}
mushrooms.add(cmush);
addObject(cmush, x, y);
}
if (levelDelay == 170) {
MillipedeMushroom mmush = new MillipedeMushroom();
GreenfootImage mushImage = new GreenfootImage("millMushroom_001.png");
mushImage.scale(GRID_SIZE, GRID_SIZE);
mmush.level = level;
mmush.setImage(mushImage);
int x = Greenfoot.getRandomNumber((int)getWidth()/GRID_SIZE) * GRID_SIZE + mushImage.getWidth()/2;
int y = (Greenfoot.getRandomNumber((int)(getHeight()/GRID_SIZE-(1+NUM_ROWS_SPAWN_ABOVE_BOTTOM)))+1) * GRID_SIZE + mushImage.getHeight()/2;
while (getObjectsAt(x, y, Mushroom.class).size() != 0) {
x = Greenfoot.getRandomNumber((int)getWidth()/GRID_SIZE) * GRID_SIZE + mushImage.getWidth()/2;
y = (Greenfoot.getRandomNumber((int)(getHeight()/GRID_SIZE-(1+NUM_ROWS_SPAWN_ABOVE_BOTTOM)))+1) * GRID_SIZE + mushImage.getHeight()/2;
}
mushrooms.add(mmush);
addObject(mmush, x, y);
}
/** after delay is over, the level updates */
if (levelDelay > 200) {
level++;
NUM_CENTIPEDES++;
centipedes.clear();
if (level == 1) {
for (int i = 0; i < NUM_CENTIPEDES; i++) {
Centipede cent = new Centipede();
centipedes.add(cent);
GreenfootImage img = cent.getImage();
img.scale(GRID_SIZE, GRID_SIZE);
cent.level = 1;
addObject(cent, img.getWidth()/2 + i * img.getWidth(), img.getHeight()/2);
cent.dx = 2;
}
GreenfootImage image = new GreenfootImage("player2.png");
image.scale(20, 20);
player.setImage(image);
for (Mushroom mush : mushrooms) {
mush.level = 1;
mush.setScoreValue(mush.getScoreValue() + 4);
}
}
if (level == 2) {
for (int i = 0; i < NUM_CENTIPEDES; i++) {
Centipede cent = new Centipede();
centipedes.add(cent);
GreenfootImage img = cent.getImage();
img.scale(GRID_SIZE, GRID_SIZE);
cent.level = 2;
addObject(cent, img.getWidth()/2 + i * img.getWidth(), img.getHeight()/2);
cent.dx = 4;
}
GreenfootImage image = new GreenfootImage("player3.png");
image.scale(20, 20);
player.setImage(image);
for (Mushroom mush : mushrooms) {
mush.level = 2;
mush.setScoreValue(mush.getScoreValue() + 5);
}
}
if (level == 3) {
NUM_CENTIPEDES += 4;
for (int i = 0; i < NUM_CENTIPEDES; i++) {
Centipede cent = new Centipede();
centipedes.add(cent);
GreenfootImage img = cent.getImage();
img.scale(GRID_SIZE, GRID_SIZE);
cent.level = 3;
addObject(cent, img.getWidth()/2 + i * img.getWidth(), img.getHeight()/2);
cent.dx = 5;
}
GreenfootImage image = new GreenfootImage("player4.png");
image.scale(20, 20);
player.setImage(image);
for (Mushroom mush : mushrooms) {
mush.level = 3;
mush.setScoreValue(mush.getScoreValue() + 5);
}
}
if (level == 4) {
NUM_CENTIPEDES -= 3;
for (int i = 0; i < NUM_CENTIPEDES; i++) {
Centipede cent = new Centipede();
centipedes.add(cent);
GreenfootImage img = cent.getImage();
img.scale(GRID_SIZE, GRID_SIZE);
cent.level = 4;
addObject(cent, img.getWidth()/2 + i * img.getWidth(), img.getHeight()/2);
cent.dx = 6;
}
GreenfootImage image = new GreenfootImage("player5.png");
image.scale(20, 20);
player.setImage(image);
for (Mushroom mush : mushrooms) {
mush.level = 4;
mush.setScoreValue(mush.getScoreValue() + 5);
}
}
/** Reverts to PLAYING mode after level setup logic completed */
state = PLAYING;
if (level == 5) {
state = WIN;
}
}
} else if (state == PLAYING) {
/** After centipedes are gone, the state switches to NEXTLEVEL mode*/
if (getObjects(Centipede.class).isEmpty()) {
// Change state to NEXTLEVEL
state = NEXTLEVEL;
}
levelDelay = 0;
}
/** Pause and resume, using the p key */
if (Greenfoot.isKeyDown("p") && state == PLAYING) {
Paused pause = new Paused();
pause.getImage().scale(GRID_SIZE*6, GRID_SIZE*6);
addObject(pause, getWidth()/2, getHeight()/2);
state = SUSPENDED;
} else if (Greenfoot.isKeyDown("p") && state == SUSPENDED) {
removeObjects(getObjects(Paused.class));
state = PLAYING;
}
if (Greenfoot.isKeyDown("q") && state == PLAYING) {
for (int i = 0; i < NUM_CENTIPEDES; i++) {
Centipede cent = new Centipede();
centipedes.add(cent);
GreenfootImage img = cent.getImage();
img.scale(GRID_SIZE, GRID_SIZE);
cent.level = 4;
addObject(cent, img.getWidth()/2 + i * img.getWidth(), img.getHeight()/2);
cent.dx = 10;
}
}
}
/** Several getter and setter methods */
public int getState() {
return state;
}
public void setState(int stateNum) {
state = stateNum;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public int getTime() {
return timer.getTime();
}
public void setTime(int time) {
timer.setTime(time);
}
}