-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
165 lines (129 loc) · 3.8 KB
/
Game.java
File metadata and controls
165 lines (129 loc) · 3.8 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
//Kaushik Siruvuri, Ishaan Sharma, Supreet Mishra
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.util.Random;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 4088146271165387233L;
public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;
private Random r;
private Thread thread;
private boolean running = false;
private Handler handler;
private HUD hud;
private Spawn spawner;
private Menu menu;
public enum STATE {
MENU,
HELP,
GAME,
END,
WIN
}
public STATE gameState = STATE.MENU;
public Game() {
handler = new Handler();
hud = new HUD();
menu = new Menu(this, handler, hud);
this.addKeyListener(new KeyInput(handler));
this.addMouseListener(menu);
new Window(WIDTH, HEIGHT, "Game Running", this);
spawner = new Spawn(handler, hud);
r = new Random();
Random b = new Random();
if (gameState == STATE.GAME) {
hud.setLevel(1);
hud.setScore(0);
hud.HEALTH = 3;
}
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop() {
try {
thread.join();
running = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
tick();
delta--;
}
if (running)
render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("FPS: " + frames);
frames = 0;
}
}
stop();
}
private void tick() {
handler.tick();
if (gameState == STATE.GAME) {
hud.tick();
spawner.tick();
if (HUD.HEALTH <= 0 || handler.lowestY() > 400){
handler.clearAll();
gameState = STATE.END;
HUD.HEALTH = 3;
}
if (HUD.bossHEALTH < 0){
handler.clearAll();
gameState = STATE.WIN;
HUD.bossHEALTH = 3;
HUD.HEALTH = 3;
}
} else if (gameState == STATE.MENU || gameState == STATE.END || gameState == STATE.WIN) {
menu.tick();
}
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, WIDTH, HEIGHT);
handler.render(g);
if (gameState == STATE.GAME) {
hud.render(g);
} else if (gameState == STATE.MENU || gameState == STATE.HELP || gameState == STATE.END || gameState == STATE.WIN) {
menu.render(g);
}
g.dispose();
bs.show();
}
public static float clamp(float var, float min, float max) {
if (var >= max) {
return var = max;
} else if (var <= min) {
return var = min;
} else {
return var;
}
}
public static void main(String args[]) {
new Game();
}
}