-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameLoop.java
More file actions
executable file
·67 lines (61 loc) · 2.04 KB
/
GameLoop.java
File metadata and controls
executable file
·67 lines (61 loc) · 2.04 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
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
public class GameLoop implements Runnable {
/**
* Frame Per Second.
*/
private static final int FPS = 30;
private GameFrame canvas;
private GameState state;
GameLoop(GameFrame frame) {
canvas = frame;
}
/**
* This must be called before the game loop starts.
*/
public void init() {
state = new GameState();
canvas.addKeyListener(state.getKeyListener());
canvas.addMouseListener(state.getMouseListener());
canvas.addMouseMotionListener(state.getMouseMotionListener());
}
@Override
public void run() {
boolean gameOver = false;
boolean won = false;
//define server
while (!gameOver && !won) {
try {
long start = System.currentTimeMillis();
//
state.update();
canvas.render(state);
gameOver = state.gameOver;
won = state.won;
//FOR MULTIPLAYER
// if (state.nextLevel){//WASMULTIPLAYER
// try
// {
// BufferedImage image = new BufferedImage(GameFrame.GAME_WIDTH, GameFrame.GAME_HEIGHT, BufferedImage.TYPE_INT_RGB);
// Graphics2D graphics2D = image.createGraphics();
// canvas.paint(graphics2D);
// ImageIO.write(image,"png", new File("gameScreen.png"));
// }
// catch(Exception exception)
// {
// exception.printStackTrace();
// }
// }
//
long delay = (1000 / FPS) - (System.currentTimeMillis() - start);
if (delay > 0)
Thread.sleep(delay);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
canvas.render(state);
}
}