-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
221 lines (182 loc) · 5.15 KB
/
Game.java
File metadata and controls
221 lines (182 loc) · 5.15 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
/**
* Collaboration: Looked up how to play sound & Looked up how to use a timer
*/
public class Game {
private Grid grid;
private int userRow;
private int msElapsed;
private int timesGet;
private int timesAvoid;
private float speed = 1.0f;
private boolean invul;
public Game() {
grid = new Grid(5, 10);
userRow = 0;
msElapsed = 0;
timesGet = 0;
timesAvoid = 0;
updateTitle();
grid.setImage(new Location(userRow, 0), "user.png");
}
private void play() {
while (!isGameOver()) {
Grid.pause((int) (100 * speed));
handleKeyPress();
if (msElapsed % 300 == 0) {
scrollLeft();
populateRightEdge();
}
updateTitle();
msElapsed += 100;
if (invul && InvulTimer.getTimerTick() == 10) {
grid.setImage(new Location(userRowLocation(), 0), "user.png");
invul = false;
InvulTimer.resetTimer();
}
}
setGameOverScreen();
Sound.playSound("gameover");
}
private void setGameOverScreen() {
if (timesAvoid == 3) {
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 5; y++) {
grid.setImage(new Location(y, x), "avoid.png");
}
}
grid.setImage(new Location(2, 4), "gameover.png");
} else {
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 5; y++) {
grid.setImage(new Location(y, x), "get.png");
}
}
grid.setImage(new Location(2, 4), "useri.png");
}
}
private int userRowLocation() {
for (int i = 0; i < 5; i++) {
if (grid.getImage(new Location(i, 0)) != null && grid.getImage(new Location(i, 0)).equals(userImage()))
return i;
}
return 0;
}
private void handleKeyPress() {
int key = grid.checkLastKeyPressed();
Location originalLoc = new Location(userRowLocation(), 0);
String imgMovingInto;
if (key == 38 && originalLoc.getRow() != 0) {
Location newLoc = new Location(originalLoc.getRow() - 1, 0);
imgMovingInto = grid.getImage(newLoc);
if (grid.getImage(newLoc) != null)
handleCollision(imgMovingInto, 1);
grid.setImage(newLoc, userImage());
grid.setImage(originalLoc, "");
} else if (key == 40 && originalLoc.getRow() != 4) {
Location newLoc = new Location(originalLoc.getRow() + 1, 0);
imgMovingInto = grid.getImage(newLoc);
if (grid.getImage(newLoc) != null)
handleCollision(imgMovingInto, 1);
grid.setImage(newLoc, userImage());
grid.setImage(originalLoc, "");
}
}
private void populateRightEdge() {
Location loc = new Location((int) (Math.random() * 5), 9);
double random = Math.random();
if (random < 0.4) {
grid.setImage(loc, "avoid.png");
} else if (random < 0.96) {
grid.setImage(loc, "get.png");
} else if (random < 0.97) {
grid.setImage(loc, "life.png");
} else if (!invul) {
grid.setImage(loc, "invul.png");
} else {
grid.setImage(loc, "get.png");
}
}
private void scrollLeft() {
String imgComingAtUser = grid.getImage(new Location(userRowLocation(), 1));
// Columns
for (int x = 1; x < 10; x++) {
// Rows
for (int y = 0; y < 5; y++) {
Location currentLoc = new Location(y, x);
Location nextLoc = new Location(y, x - 1);
if (grid.getImage(currentLoc) != null
&& (grid.getImage(nextLoc) == null || !grid.getImage(nextLoc).equals(userImage()))) {
grid.setImage(nextLoc, grid.getImage(currentLoc));
grid.setImage(currentLoc, "");
}
}
}
if (imgComingAtUser != null)
handleCollision(imgComingAtUser, 0);
}
private void handleCollision(String img, int handle) {
if (img.equals("get.png") || (img.equals("avoid.png") && invul)) {
Sound.playSound("get");
if (++timesGet == 100) {
updateTitle();
Sound.playSound("win");
setGameOverScreen();
return;
}
increaseSpeed();
} else if (img.equals("avoid.png")) {
if (handle == 0) {
grid.setImage(new Location(this.userRowLocation(), 0), "avoid.png");
} else {
grid.setImage(new Location(this.userRowLocation(), 0), "");
}
Sound.playSound("avoid");
timesAvoid++;
Grid.pause(3000);
resetScreen();
} else if (img.equals("life.png")) {
Sound.playSound("life");
timesAvoid--;
} else if (img.equals("invul.png") && !invul) {
Sound.playSound("invul");
grid.setImage(new Location(this.userRowLocation(), 0), "useri.png");
invul = true;
new InvulTimer();
}
}
private int getScore() {
return timesGet;
}
private void increaseSpeed() {
speed -= 0.01f;
}
private void updateTitle() {
if (timesAvoid == 3)
grid.setTitle("Game Over! | Your Score was " + timesGet + " | Want To Try Again?");
else if (timesGet == 100)
grid.setTitle("Game Won! | You've Reached to 100 Points! | Want To Play Again?");
else
grid.setTitle("Can You Get to 100? | Current Score: " + getScore() + " | Lives: " + (3 - timesAvoid));
}
private boolean isGameOver() {
return timesAvoid == 3 || timesGet >= 100;
}
private void resetScreen() {
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 5; y++) {
if (grid.getImage(new Location(y, x)) != null) {
grid.setImage(new Location(y, x), "");
}
}
}
grid.setImage(new Location(0, 0), "user.png");
}
private String userImage() {
if (invul)
return "useri.png";
return "user.png";
}
public static void main(String[] args) {
new Game().play();
}
}