-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStarterGame.java
262 lines (205 loc) · 8.33 KB
/
StarterGame.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
import java.awt.*;
import java.awt.event.*;
import java.util.*;
//Partner: Reed Gilbert
//A Starter version of the scrolling game, featuring Avoids, Collects, RareAvoids, and RareCollects
//Players must reach a score threshold to win.
//If player runs out of HP (via too many Avoid/RareAvoid collisions) they lose.
public class StarterGame extends GameEngine2D {
//Starting Player coordinates
protected static final int STARTING_PLAYER_X = 0;
protected static final int STARTING_PLAYER_Y = 100;
//Score needed to win the game
protected static final int SCORE_TO_WIN = 300;
//Maximum that the game speed can be increased to
//(a percentage, ex: a value of 300 = 300% speed, or 3x regular speed)
protected static final int MAX_GAME_SPEED = 300;
//Interval that the speed changes when pressing speed up/down keys
protected static final int SPEED_CHANGE_INTERVAL = 20;
public static final String INTRO_SPLASH_FILE = "assets/splash.gif";
//Key pressed to advance past the splash screen
public static final int ADVANCE_SPLASH_KEY = KeyEvent.VK_ENTER;
//Interval that Entities get spawned in the game window
//ie: once every how many ticks does the game attempt to spawn new Entities
protected static final int SPAWN_INTERVAL = 45;
//A Random object for all your random number generation needs!
public static final Random rand = new Random();
//player's current score
protected int score;
//Stores a reference to game's Player object for quick reference (Though this Player presumably
//is also in the DisplayList, but it will need to be referenced often)
protected Player player;
public StarterGame(){
this(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public StarterGame(int gameWidth, int gameHeight){
super(gameWidth, gameHeight);
}
//Performs all of the initialization operations that need to be done before the game starts
protected void pregame(){
this.setBgColor(Color.BLACK);
this.player = new Player(STARTING_PLAYER_X, STARTING_PLAYER_Y);
this.entities.add(player);
this.score = 0;
super.setSplashImage(INTRO_SPLASH_FILE);
}
//Called on each game tick
protected void updateGame(){
//scroll all scrollable Entities on the game board
scrollEntities();
//Spawn new entities only at a certain interval
if (super.getTicksElapsed() % SPAWN_INTERVAL == 0){
spawnEntities();
}
updateTitleText();
gcOffscreenEntities();
}
//Update the text at the top of the game window
protected void updateTitleText(){
setTitleText("HP: " + player.getHP() + ", Score: " + this.score);
if (isGameOver()){
setTitleText("GAME OVER - You Lose!");
}
}
//Scroll all scrollable entities per their respective scroll speeds
protected void scrollEntities(){
for (int i = 0; i < entities.size(); i++) {
Entity entity = entities.get(i);
if (entity instanceof Scrollable) {
Scrollable scrollableEntity = (Scrollable) entity;
scrollableEntity.scroll();
}
if (entity instanceof Consumable) {
Consumable consumableEntity = (Consumable) entity;
collidedWithPlayer(consumableEntity);
}
}
}
//Handles "garbage collection" of the entities
//Flags entities in the displaylist that are no longer relevant
//(i.e. will no longer need to be drawn in the game window).
protected void gcOffscreenEntities(){
for(int i = 0;i < entities.size(); i++){
if (entities.get(i).getX() < -100){
entities.get(i).setGCFlag(true);
}
}
}
//Called whenever it has been determined that the Player collided with a consumable
protected void collidedWithPlayer(Consumable collidedWith){
if (((Entity) collidedWith).isColliding(player)){
System.out.println("Collided with Player!");
player.modifyHP(collidedWith.getHPModifier());
this.score += collidedWith.getScoreModifier();
System.out.println("HP: " + player.getHP() + " Score: " + score);
((Entity)collidedWith).setGCFlag(true);
}
}
//Spawn new Entities on the right edge of the game board
private void spawnEntities(){
int x = super.DEFAULT_WIDTH;
int numEntities = rand.nextInt(4);
for(int i = 0; i < numEntities; i++){
int y = 0;
Entity temp;
int randNum = rand.nextInt(10);
if (randNum == 0){
temp = new RareAvoid();
}
else if (randNum == 1){
temp = new RareCollect();
}
else if (randNum <= 5){
temp = new Avoid();
}
else{
temp = new Collect();
}
y += rand.nextInt(super.getWindowHeight() - temp.getHeight());
if (y + temp.getHeight() > super.getWindowHeight()){
y = super.getWindowHeight() - temp.getHeight() + rand.nextInt(20);
}
temp.setY(y);
temp.setX(x);
if (super.getAllCollisions(temp).size() > 0){
numEntities++;
temp.setGCFlag(true);
}
else{
entities.add(temp);
}
}
}
protected void handlePlayerMovement(int key){
if (key == super.UP_KEY){
if (player.getY() > 0){
player.setY(Math.max(player.getY() - player.getMoveSpeed(), 0));
}
}
if (key == super.DOWN_KEY){
if (player.getY()+player.getHeight() < super.getWindowHeight()){
player.setY(Math.min(player.getY() + player.getMoveSpeed(), super.getWindowHeight()));
}
}
if (key == super.RIGHT_KEY){
if (player.getX()+player.getWidth() < super.getWindowWidth()){
player.setX(Math.min(player.getX() + player.getMoveSpeed(), super.getWindowWidth()));
}
}
if (key == super.LEFT_KEY){
if (player.getX() > 0){
player.setX(Math.max(player.getX() - player.getMoveSpeed(), 0));
}
}
}
//Called once the game is over, performs any end-of-game operations
protected void postgame(){
if (player.getHP() <= 0){
super.setTitleText("GAME OVER - You Lose!");
}
else{
super.setTitleText("GAME OVER - You Won!");
}
}
//Returns a boolean indicating if the game is over (true) or not (false)
//Game can be over due to either a win or lose state
protected boolean isGameOver(){
if (player.getHP() <= 0){
return true;
}
if (this.score >= SCORE_TO_WIN){
return true;
}
return false;
}
//Reacts to a single key press on the keyboard
protected void keyReact(int key){
//if a splash screen is up, only react to the advance splash key
if (getSplashImage() != null){
if (key == ADVANCE_SPLASH_KEY)
super.setSplashImage(null);
return;
}
if (key == super.KEY_PAUSE_GAME && super.isPaused){
super.isPaused = false;
}
else if (key == super.KEY_PAUSE_GAME){
super.isPaused = true;
}
if (key == super.SPEED_DOWN_KEY && super.getGameSpeed()-SPEED_CHANGE_INTERVAL >= 1){
super.setGameSpeed(super.getGameSpeed()-SPEED_CHANGE_INTERVAL);
}
if (key == super.SPEED_UP_KEY && super.getGameSpeed()+SPEED_CHANGE_INTERVAL <= MAX_GAME_SPEED){
super.setGameSpeed(super.getGameSpeed()+SPEED_CHANGE_INTERVAL);
}
if (super.isPaused == false){
handlePlayerMovement(key);
}
}
//Handles reacting to a single mouse click in the game window
protected MouseEvent reactToMouseClick(MouseEvent click){
//Mouse functionality is not used at all in the Starter game...
//you may want to override this function for a CreativeGame feature though!
return click;//returns the mouse event for any child classes overriding this method
}
}