-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
100 lines (92 loc) · 3.16 KB
/
Copy pathGame.java
File metadata and controls
100 lines (92 loc) · 3.16 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
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class Game extends JFrame implements Runnable{
private static final long serialVersionUID = 1L;
public int windowWidth = 1408; //640 , 1100 , 1300
public int windowHeight = 992; //480 , 825 , 975
public static int gameAreaOffset_top = 64;
public static int gameAreaOffset_left = 32;
public static int gameAreaOffset_right = 32;
public static int gameAreaOffset_bottom = 32;
private Thread thread;
private boolean running;
private BufferedImage image;
public Camera camera;
public static Graphics g;
private Menu menu;
public Game() {
thread = new Thread(this);
image = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB);
clearScreen(image);
menu = new Menu(image);
camera = new Camera(image, menu);
addKeyListener(camera);
addMouseListener(camera);
setSize(windowWidth, windowHeight);
setResizable(false);
setTitle("Minesweeper");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
start();
}
private synchronized void start() {
running = true;
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch(InterruptedException e) {
e.printStackTrace();
}
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if(bs == null) {
createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
camera.setGraphics(g);
if(GameState.gameStatus == GameState.GameStatus.Menu) {
menu.display(g);
}
if(GameState.gameStatus == GameState.GameStatus.Ingame) {
camera.displayData(g);
}
bs.show();
}
public void run() {
long lastTime = System.nanoTime();
final double ns = 1000000000.0 / 60.0;//60 times per second
double delta = 0;
requestFocus();
while(running) {
long now = System.nanoTime();
delta = delta + ((now-lastTime) / ns);
lastTime = now;
while (delta >= 1)//Make sure update is only happening 60 times a second
{
//handles all of the logic restricted time
camera.updateMouseLocation(getLocation().x, getLocation().y);
delta--;
}
render();//displays to the screen unrestricted time
}
}
public static void clearScreen(BufferedImage image) {
for(int x = 0; x < image.getWidth(); x++) {
for(int y = 0; y < image.getHeight(); y++) {
image.setRGB(x, y, -4934476); //light gray
}
}
}
public static void main(String[] args) {
Game game = new Game();
}
}