-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
198 lines (148 loc) · 3.94 KB
/
Game.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
package uet.oop.bomberman;
import uet.oop.bomberman.graphics.Screen;
import uet.oop.bomberman.gui.Frame;
import uet.oop.bomberman.input.Keyboard;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
/**
* Tạo vòng lặp cho game, lưu trữ một vài tham số cấu hình toàn cục,
* Gọi phương thức render(), update() cho tất cả các entity
*/
public class Game extends Canvas {
public static final int TILES_SIZE = 16,
WIDTH = TILES_SIZE * (31 / 2),
HEIGHT = 13 * TILES_SIZE;
public static int SCALE = 3;
public static final String TITLE = "BombermanGame";
private static final int BOMBRATE = 1;
private static final int BOMBRADIUS = 1;
private static final double BOMBERSPEED = 1.0;
public static final int TIME = 200;
public static final int POINTS = 0;
protected static int SCREENDELAY = 3;
protected static int bombRate = BOMBRATE;
protected static int bombRadius = BOMBRADIUS;
protected static double bomberSpeed = BOMBERSPEED;
protected int _screenDelay = SCREENDELAY;
private Keyboard _input;
private boolean _running = false;
private boolean _paused = true;
private Board _board;
private Screen screen;
private Frame _frame;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
public Game(Frame frame) {
_frame = frame;
_frame.setTitle(TITLE);
screen = new Screen(WIDTH, HEIGHT);
_input = new Keyboard();
_board = new Board(this, _input, screen);
addKeyListener(_input);
}
private void renderGame() {
BufferStrategy bs = getBufferStrategy();
if(bs == null) {
createBufferStrategy(3);
return;
}
screen.clear();
_board.render(screen);
for (int i = 0; i < pixels.length; i++) {
pixels[i] = screen._pixels[i];
}
Graphics g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
_board.renderMessages(g);
g.dispose();
bs.show();
}
private void renderScreen() {
BufferStrategy bs = getBufferStrategy();
if(bs == null) {
createBufferStrategy(3);
return;
}
screen.clear();
Graphics g = bs.getDrawGraphics();
_board.drawScreen(g);
g.dispose();
bs.show();
}
private void update() {
_input.update();
_board.update();
}
public void start() {
_running = true;
long lastTime = System.nanoTime();
long timer = System.currentTimeMillis();
final double ns = 1000000000.0 / 60.0; //nanosecond, 60 frames per second
double delta = 0;
int frames = 0;
int updates = 0;
requestFocus();
while(_running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1) {
update();
updates++;
delta--;
}
if(_paused) {
if(_screenDelay <= 0) {
_board.setShow(-1);
_paused = false;
}
renderScreen();
} else {
renderGame();
}
frames++;
if(System.currentTimeMillis() - timer > 1000) {
_frame.setTime(_board.subtractTime());
_frame.setPoints(_board.getPoints());
timer += 1000;
_frame.setTitle(TITLE + " | " + updates + " rate, " + frames + " fps");
updates = 0;
frames = 0;
if(_board.getShow() == 2)
--_screenDelay;
}
}
}
public static double getBomberSpeed() {
return bomberSpeed;
}
public static int getBombRate() {
return bombRate;
}
public static int getBombRadius() {
return bombRadius;
}
public static void addBomberSpeed(double i) {
bomberSpeed += i;
}
public static void addBombRadius(int i) {
bombRadius += i;
}
public static void addBombRate(int i) {
bombRate += i;
}
public void resetScreenDelay() {
_screenDelay = SCREENDELAY;
}
public Board getBoard() {
return _board;
}
public boolean isPaused() {
return _paused;
}
public void pause() {
_paused = true;
}
}