-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel2.java
More file actions
113 lines (100 loc) · 3.13 KB
/
Level2.java
File metadata and controls
113 lines (100 loc) · 3.13 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Short maze. Introduces backflip usage in game.
*/
public class Level2 extends Scrolling {
//Creates String representing the map layout.
String[] textMap = {
"*******************",
"*eeeeeeeeeeeeeee*.*",
"*e*************e*e*",
"*e*eeeeeeeeeee*e*e*",
"*e*eeeeeeeeeee*e*e*",
"*e*eeeeeeeeeee*b*e*",
"*e*eeeeeeeee***r*e*",
"*e*eeeeeeeee*m*o*e*",
"*e*************p*e*",
"*eeeeeeeeeeeee*eee*",
"*******************"
};
public Level2() {
super(550, 600, 1, 1100, 1200);
setScrollingBackground(new GreenfootImage("bricks2.jpg"));
prepare();//this method just adds some objects to the world.
drawMap();
}
/**
* Prepare method.
*/
public void prepare() {
Timer.timer = 0;
stopSounds();
addObject(new Score(), 75, 575);
addObject(new PackLives(), 480, 575);
addObject(new Pack(), getWidth()/2, getHeight()/2);
}
/**
* Draws map using string with case switch.
*/
private void drawMap() {
for (int i = 0; i < textMap.length; i++) {
String mapLine = textMap[i];
for(int j = 0; j < mapLine.length(); j++) {
char mapChar = mapLine.charAt(j);
int y = -150 + i * 50;
int x = -275 + j * 50;
switch(mapChar) {
case '*':
addObject(new Wall(), x, y);
break;
case '.':
addObject(new SmallYummy(), x, y);
break;
case 'b':
addObject(new BlueGhost(), x, y);
break;
case 'r':
addObject(new RedGhost(), x, y);
break;
case 'o':
addObject(new OrangeGhost(), x, y);
break;
case 'p':
addObject(new PinkGhost(), x, y);
break;
case 'm':
addObject(new BigYummy(), x, y);
break;
default:
break;
}
}
}
}
public void act() {
checkLives();
checkSounds();
nextLevel();
checkNoPack();
}
/**
* Checks if Pack is alive or level reset button pressed.
*/
private void checkNoPack() {
if (getObjects(Pack.class).isEmpty() || Greenfoot.isKeyDown("r")) {
stopSounds();
PackLives.lives--;
Greenfoot.setWorld(new Level2());
Greenfoot.delay(5);
}
}
/**
* If SmallYummy empty goes to next world.
*/
public void nextLevel() {
if (getObjects(SmallYummy.class).isEmpty()) {
stopSounds();
Greenfoot.setWorld(new Level3());
}
}
}