-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
237 lines (211 loc) · 10.4 KB
/
Game.java
File metadata and controls
237 lines (211 loc) · 10.4 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//Game.java
//Spencer Trepanier & George Zhang
//The Black Labyrinth
//A scroller game with a player, some spikes, bats, spiders, golems and a boss with fireballs.
import java.awt.*;
import javax.imageio.*;
import java.io.IOException;
import javax.swing.*;
import java.util.ArrayList;
public class Game extends BaseFrame{
public static final int INTRO=0, GAME=1, DEATH=2;//
public static final int LEVELSELECT=3, CONTROLS=7, WIN=8;//
public int offset;
private static int screen = INTRO;//
private static ArrayList<Platform> platforms; //arraylists of entities
private static ArrayList<Spider> spiders; //
private static ArrayList<Spikes> spikes; //
private static ArrayList<Bat> bats = new ArrayList<Bat>(); //
private static Boss boss;// //the boss
private Image intro, gameOver;//bg images for intro and game over screens
private Font arial;//
Button playButton,controlsButton,mainMenuButton;//buttons
Button exitButton;
private static Player player; //the player
public Game(){
super("Game",WIDTH, HEIGHT);
arial = new Font("Arial", Font.PLAIN, 60); //all the buttons
playButton = new Button("Arial", Font.PLAIN, "Play", 40, 350, 40);// //
controlsButton = new Button("Arial",Font.PLAIN,"Controls",40,390,40);// //
mainMenuButton = new Button("Arial",Font.PLAIN,"Main Menu",310,460,40);// //
exitButton = new Button("Arial",Font.PLAIN,"Exit",690,540,60);// //
player = new Player(200,545,32,55,5,0,20,100); //
boss = new Boss(3900,HEIGHT-200,300,200,5,200);// //
platforms = new ArrayList<Platform>(); //array list of platforms
spiders = new ArrayList<Spider>(); //array list of spiders
spiders.add(new Spider(150,545,90,60,1,9,3)); //adds spiders
spiders.add(new Spider(900,545,90,60,1,9,3)); //adds spiders
spiders.add(new Spider(1650,545,90,60,1,9,3)); //adds spiders
spiders.add(new Spider(2400,545,90,60,1,9,3)); //adds spiders
spiders.add(new Spider(3150,545,90,60,1,9,3)); //adds spiders
bats.add(new Bat(500, 80)); //adds bats
bats.add(new Bat(1000, 80)); //adds bats
bats.add(new Bat(1500, 80)); //adds bats
bats.add(new Bat(2000, 80)); //adds bats
bats.add(new Bat(2500, 80)); //adds bats
platforms.add(new Platform(400,500,91,25)); //adds platforms
platforms.add(new Platform(850,450,91,25)); //
platforms.add(new Platform(900,400,91,25)); //
platforms.add(new Platform(1000,350,91,25)); //
platforms.add(new Platform(1500,300,91,25)); //
platforms.add(new Platform(1300,400,91,25)); //
platforms.add(new Platform(1500,450,91,25)); //
platforms.add(new Platform(1500,475,91,25)); //
platforms.add(new Platform(1600,450,91,25)); //
platforms.add(new Platform(1750,450,91,25)); //
platforms.add(new Platform(1200,520,91,25)); //
platforms.add(new Platform(2500,450,91,25)); //
platforms.add(new Platform(2800,450,91,25)); //
back = new ImageIcon("BackGround/BackGround.png").getImage(); //gets background image
intro = new ImageIcon("intro.jpg").getImage(); //gets intro image
gameOver = new ImageIcon("Game Over.jpg").getImage(); //gets game over image
}
@Override
public void move(){ //tackles all the movement in the game
if(screen == INTRO){
buttonPressed(playButton, GAME); //if play button is pressed set screen to game
buttonPressed(controlsButton, CONTROLS);//if controls button is pressed set screen to controls
}
else if(screen == GAME){
for(int i=0; i<spiders.size(); i++){ //moves spiders
Spider spidey = spiders.get(i); //gets a spider from arraylist
spidey.move(player); //moves spider
spidey.attack(player); //spider attacks player
spidey.takeDamage(player); //spider checks if player is attacking
if(spidey.isDead()){ //if spider is dead, remove it from arraylist
spiders.remove(i);
}
}
for(int i=0; i<bats.size(); i++){ //moves bats
Bat bat = bats.get(i); //gets a bat from arraylist
bat.move(player,2); //moves bat
bat.takeDamage(player, 1); //checks if player is attacking
if(bat.isDead()){ //if bat is dead, remove it from arraylist
bats.remove(i);
}
}
if(boss!=null){ //if boss is not null
boss.move(player); //moves boss
boss.attack(player, 2); //boss attacks player
boss.takeDamage(player, 1); //checks if player is attacking
if(boss.isDead()){ //if boss is dead, set boss to null
boss = null;
}
}
player.move(keys); //moves player
}
else if(screen == CONTROLS){
buttonPressed(exitButton, INTRO); //sets screen to intro if exit button is pressed while on controls screen
}
else if(screen == DEATH){
if(buttonPressed(mainMenuButton, INTRO)){
player = null; //clears everything
boss = null; //
platforms.clear(); //
spiders.clear(); //
bats.clear(); //
player = new Player(200,545,32,55,5,0,20,3); //resets player and boss
boss = new Boss(1200,HEIGHT-200,300,200,5,200); //
spiders.add(new Spider(150,545,90,60,1,9,3)); //adds spiders
spiders.add(new Spider(900,545,90,60,1,9,3)); //adds spiders
spiders.add(new Spider(1650,545,90,60,1,9,3)); //adds spiders
spiders.add(new Spider(2400,545,90,60,1,9,3)); //adds spiders
spiders.add(new Spider(3150,545,90,60,1,9,3)); //adds spiders
bats.add(new Bat(500, 80)); //adds bats
bats.add(new Bat(1000, 80)); //adds bats
bats.add(new Bat(1500, 80)); //adds bats
bats.add(new Bat(2000, 80)); //adds bats
bats.add(new Bat(2500, 80)); //adds bats
platforms.add(new Platform(400,500,91,25)); //adds platforms
platforms.add(new Platform(850,450,91,25)); //
platforms.add(new Platform(900,400,91,25)); //
platforms.add(new Platform(1000,350,91,25)); //
platforms.add(new Platform(1500,300,91,25)); //
platforms.add(new Platform(1300,400,91,25)); //
platforms.add(new Platform(1500,450,91,25)); //
platforms.add(new Platform(1500,475,91,25)); //
platforms.add(new Platform(1600,450,91,25)); //
platforms.add(new Platform(1750,450,91,25)); //
platforms.add(new Platform(1200,520,91,25)); //
platforms.add(new Platform(2500,450,91,25)); //
platforms.add(new Platform(2800,450,91,25)); //
}
}
}
public boolean buttonPressed(Button butt, int scr){ //checks if the button is pressed and sets the screen to whatever the button leads to.
if(mb == 1 && butt.getRect().contains(mx,my)){
screen = scr;
return true;
}//
else{return false;}
}//
@Override
public void draw(Graphics g){ //draws the game
if(screen == INTRO){
g.drawImage(intro,-100,0,null); //draws intro screen
g.setFont(arial);// //sets fonts
g.setColor(Color.WHITE);// //
FontMetrics fm = g.getFontMetrics(arial);// //
int wid = fm.stringWidth("The Black Labyrinth");//
g.drawString("The Black Labyrinth",400-wid/2,150);//
g.setColor(Color.WHITE);//sets color to white
if(playButton.getRect().contains(mx,my)){g.setColor(Color.GREEN);} //if hover, set color to green
playButton.draw(g);//draws play button
g.setColor(Color.WHITE);//sets color to white
if(controlsButton.getRect().contains(mx,my)){g.setColor(Color.GREEN);} //if hover, set color to green
controlsButton.draw(g);//draws control button
}
else if(screen == GAME){
offset = -player.getRelX(); //sets game offset to negative player x
g.drawImage(back,offset,0,null); //draws background
// g.setColor(Color.BLACK);
// g.fillRect(0,0,getWidth(),getHeight());
if(boss!=null){ //draws the boss if not null
boss.draw(g); //
} //
for(Platform plat:platforms){ //draws all the entities with loops through arraylists
plat.draw(g); //draws platforms
} //
for(Spider spidey:spiders){ //
spidey.draw(g); //draws spiders
} //
for(Bat bat:bats){ //
bat.draw(g); //draws bats
} //
player.draw(g); //draws player
}
else if(screen == DEATH){
g.drawImage(gameOver,-130,0,null);//draws game over screen
g.setColor(Color.WHITE);
if(mainMenuButton.getRect().contains(mx,my)){g.setColor(Color.RED);}//set color to red if hovered
mainMenuButton.draw(g);//draws main menu button
}
else if(screen == CONTROLS){//draws controls
g.drawImage(intro,-100,0,null); //draw background
g.setFont(arial); //set font
g.setColor(Color.WHITE); //
FontMetrics fm = g.getFontMetrics(arial); //
int wid = fm.stringWidth("Controls");// finds width of the string
g.drawString("Controls",420-wid/2,150);//draws the string
g.drawString(String.format("W - %20s","Jump"),100,250);// draws each control string
g.drawString(String.format("A - %20s","Move Left"),100,310);//
g.drawString(String.format("D - %19s","Move Right"),100,370);//
g.drawString(String.format("Space bar - %10s","Attack"),100,430);//
exitButton.draw(g);
}//
}
public static Player getPlayer(){ //various getters and setters for the entities and the screen
return player; //
} //
//
public static ArrayList<Platform> getPlats(){ //
return platforms; //
} //
//
public static void setScreen(int newScreen){// //
screen = newScreen; //
} //
public static void main(String[] args) {
Game game = new Game();
}
}