-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameLoop.java
254 lines (193 loc) · 6.81 KB
/
GameLoop.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
/*
FlappyBox by B. Marchena
*/
import java.awt.*;
public class GameLoop extends GameBase {
//gravity and jump constants
private final static int GRAV_SPEED = 5;
private static final int JUMP_HEIGHT = 100;
//player character
private Rect player;
//initializing counters
private int spCount = 0;
private int tmCount = 0;
//initialize game conditions
private int lvl = 1;
private int recTm;
private int score = -2;
private int hiscore = 0;
//initialize streak
private String streak = "";
//initial speed of the obstacles
private int speed = 10;
//game over condition
private boolean gameOver = false;
//initializing obstacle array
private Obstacle[] obstacles = new Obstacle[2];
//fonts for drawing title and stats
private Font titleFont = new Font("Helvetica", 1, 18);
@Override
public void initialize() {
//initialize player
player = new Rect(100, 400, 40, 40);
//create obstacles
genObstacles();
}
@Override
public void inTheGameLoop() {
//while the game isn't over
if(!gameOver) {
//increment counters
spCount++;
tmCount++;
//pull player down
player.moveDn(GRAV_SPEED);
//if the speed counter = 600 (7 - 10 sec generally) increase speed and level
if(spCount == 600){speed++; lvl++; spCount = 0;}
//check if player jumped
if(playerJump()) player.moveUp(JUMP_HEIGHT);
//move obstacles across the screen
moveObstacles();
//check for collisions
detectCollisions();
//handle collisions
handleCollisions();
}
//if game over, press enter to restart
if(released[EN] && gameOver){restartGame();}
}
private void detectCollisions() {
//if player hits an obstacle, game over
if(player.overlaps(obstacles[0])) gameOver = true;
if(player.overlaps(obstacles[1])) gameOver = true;
//if player hits the floor or ceiling, game over
if(player.y < 50) gameOver = true;
if(player.y + player.h > 680) gameOver = true;
//if upper obstacle leaves the screen, generate a new one
if (obstacles[0].x <= 0 - obstacles[0].w) {
genUpperObstacle();
}
//if lower obstacle leaves the screen, generate a new one
if (obstacles[1].x < 0 - obstacles[1].w) {
genLowerObstacle();
}
}
private void handleCollisions(){
}
public void restartGame(){
//reinitialize game conditions
gameOver = false;
score = -2;
speed = 10;
player.x =100;
player.y= 400;
tmCount = 0;
lvl = 1;
released[EN] = false;
genObstacles();
}
@Override
public void paint(Graphics g) {
//paint all objects and ui
super.paint(g);
player.draw(g);
drawObstacles(g);
showStats(g);
}
private static int getRandomNumberInRange(int min, int max) {
//used to generate random parameters for obstacles
if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
return (int)(Math.random() * ((max - min) + 1)) + min;
}
private boolean playerJump(){
//checks if player jumped
if (released[SP]) {
released[SP] = false;
return true;
}
return false;
}
public void showStats(Graphics g){
//calculate time for display
int tm = tmCount / 60;
//display current level count
g.drawString("Level " + lvl, 50, 20);
//setting title in distinct font
Font current = g.getFont();
g.setFont(titleFont);
g.drawString("FlappyBox", 450, 30);
g.setFont(current);
//display scores
g.drawString("Score: " + score, 730, 20);
g.drawString("High Score: " + hiscore, 730, 40);
//display times
g.drawString("Current Time: " + tm, 850, 20);
g.drawString("Record Time: " + recTm, 850, 40);
//scorestreaks
if(lvl == 1 && !gameOver) streak = "Amateur hour.";
if(lvl == 2 && !gameOver) streak = "Your first steps.";
if(lvl == 3 && !gameOver) streak = "Not bad.";
if(lvl == 4 && !gameOver) streak = "Now we're getting somewhere.";
if(lvl == 5 && !gameOver) streak = "You're pretty good.";
if(lvl == 6 && !gameOver) streak = "Kickin' it up a notch.";
if(lvl == 7 && !gameOver) streak = "Eyes on the prize.";
if(lvl == 8 && !gameOver) streak = "Zoom, zoom, zoom.";
if(lvl == 9 && !gameOver) streak = "It's all uphill from here.";
if(lvl >= 10 && !gameOver) streak = "Now THIS is podracing.";
//display scorestreak
g.drawString(streak,50,40);
//game over 'streak'
if(gameOver) streak = "Womp, womp.";
//display game over status
if(gameOver) g.drawString("GAME OVER!", player.x, player.y - 10);
//record best time and score
if(tm > recTm) recTm = tm;
if(score > hiscore) hiscore = score;
}
private void genUpperObstacle(){
//generates an obstacle with an upper-bound of the ceiling
int w;
int h;
w = getRandomNumberInRange(100,300); // min width: 100; max width: 300
h = getRandomNumberInRange(200,400); // min height: 200; max height: 400
obstacles[0] = new UpperObstacle(w, h);
score++;
}
private void genLowerObstacle(){
//generates an obstacle with a lower-bound of the floor
int y;
int w;
int h;
y = getRandomNumberInRange(400,600); //min y: 400; max y: 600
w = getRandomNumberInRange(100,300); //min width: 100; max width: 300
h = 650 - y; //height is set to the remaining length between y and floor
obstacles[1] = new LowerObstacle(y, w, h);
score++;
}
private void genObstacles(){
genUpperObstacle();
genLowerObstacle();
}
private void moveObstacles(){
//if the upper obstacle isn't off the screen, move it
if (obstacles[0].x > 0 - obstacles[0].w) {
obstacles[0].moveLt(speed);
}
// if the upper obstacle is halfway across OR
// the lower obstacle is already on screen, move the lower obstacle.
if (obstacles[0].x < 550 || obstacles[1].x<1000) {
obstacles[1].moveLt(speed);
}
}
private void drawObstacles(Graphics g){
//draw each obstacle
for(int i=0;i<2;i++){
obstacles[i].draw(g);
}
//draw the floor and ceiling
g.drawLine(0,50,1000,50); //ceiling
g.drawLine(0,650,1000,650); //floor
}
}