-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.java
141 lines (104 loc) · 2.91 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
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.List;
public class Game implements Runnable{
private Display display;
private int width, height;
private String title;
private boolean running = false;
private Thread thread;
private BufferStrategy bs, bg;
private Graphics g,g1;
private List<Graphics> g2;
//State
private State gameState;
//Score
protected int Score;
public Game(String title, int width, int height){
g2 = new ArrayList<>();
this.title = title;
this.width = width;
this.height = height;
Score = 6;
}
private void init(){
display = new Display(title, width, height);
Assets.init();
gameState = new gameState(this);
gameState.setState(gameState);
}
private void tick(){
if(gameState.getState() != null)
gameState.getState().tick();
}
private void render(){
bs = display.getCanvas().getBufferStrategy();
if(bs == null){
display.getCanvas().createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
//CLear Screen
g.clearRect(0,0,width,height);
g1 = bs.getDrawGraphics();
for(int i = 0; i< State.numberEnermy; ++i){
g2.add(bs.getDrawGraphics());
}
//DRAW HERE
g.drawImage(Assets.bg,0,0,null);
for(int i=0; i<State.numberEnermy;++i){
if(gameState.getState() != null){
gameState.getState().render(g);
}
}
if(gameState.getState() != null)
gameState.getState().render(g);
//END DRAWING
bs.show();
g.dispose();
}
public void run(){
init();
int fps = 60;
double timePerTick = 100000000/fps;
double delta = 0;
long now;
long lastTime = System.nanoTime();
while(running){
now = System.nanoTime();
delta+= (now - lastTime)/timePerTick;
lastTime = now;
if(delta >=16) {
tick();
render();
delta -=16;
}
}
stop();
}
public synchronized void start(){
if(running)
return;
running = true;
thread = new Thread(this::run);
thread.start();
}
public synchronized void stop(){
if(!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//Score
public void setScore(int Score){
this.Score = Score;
}
public int getScore(){
return Score;
}
}