-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.java
100 lines (68 loc) · 2.12 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
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.util.Scanner;
public class Game extends Canvas implements Runnable {
private Thread thread;
private boolean running = false;
public static final int WIDTH = 640;
public static final int HEIGHT = WIDTH;
public static final int CELLNUM = 10;
public static final int CELLSIZE = WIDTH/CELLNUM;
private static final int TARGET_FPS = 60;
private static final double OPTIMAL_TIME = 1000000000 / ((double)TARGET_FPS);
public GameHandler handler = new GameHandler();
public Game(){
new Window(WIDTH, HEIGHT, "MyGame1", this);
handler.initBoard(CELLNUM, CELLSIZE);
render();
}
public void run(){
int loopcount = 0;
render();
render(); //for some reason these are necessary here to make the first tick() display
while(running){
loopcount++;
System.out.println("Update number " + loopcount);
render();
tick();
//try{Thread.sleep(5000);}catch(Exception e){}
}
stop();
}
private void tick(){
handler.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);
g.dispose();
bs.show();
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
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 static void main(String[] args){
new Game();
}
}