-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame2.java
More file actions
199 lines (114 loc) · 3.63 KB
/
Game2.java
File metadata and controls
199 lines (114 loc) · 3.63 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
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
199
package pong;
import pong.listeners.KeyManager;
import pong.states.GameState;
import java.awt.*;
import java.awt.image.BufferStrategy;
public class Game2 implements Runnable {
int width, height;
boolean running = false;
private Display display;
private BufferStrategy bs;
private Graphics g;
private Thread thread;
private GameState currentState;
private KeyManager keyManager;
public Game2(int width, int height) {
this.width = width;
this.height = height;
}
private void init() {
display = new Display("Pong", width, height);
keyManager = new KeyManager();
display.getFrame().addKeyListener(keyManager);
currentState = new GameState(width, height, keyManager);
}
public void render() {
if (display.getCanvas().getBufferStrategy() == null)
display.getCanvas().createBufferStrategy(3);
bs = display.getCanvas().getBufferStrategy();
g = bs.getDrawGraphics();
g.clearRect(0, 0, width, height);
currentState.render(g);
bs.show(); // Shows BufferStrategy
g.dispose(); // disposes of graphics to free up memory
}
public void update() {
keyManager.update();
currentState.update();
}
public void run() {
init();
/*
long lastTime = System.nanoTime();
long currentTime;
double delta;
int frames = 0;
long timeOne = System.nanoTime();
long timeTwo;
double change;
double interval = 0.01428;
while (running) {
currentTime = System.nanoTime();
delta = (currentTime-lastTime)/1000000000.0;
if (delta >= interval) {
// Code
update();
render();
//Log
lastTime = currentTime;
/*
frames++;
if (frames == 60) {
timeTwo = System.nanoTime();
change = (timeTwo-timeOne)/1000000000.0;
System.out.println("Running at: " + frames + " per " + change + " seconds");
frames = 0;
timeOne = timeTwo;
}
}
}
*/
int fps = 60;
double target = 1000000000/fps;
long lastTime = System.nanoTime();
long now;
long timer = 0;
int ticks = 0;
double delta = 0; // Delta referring to change in something
while (running) { // You have to constantly redraw
now = System.nanoTime();
delta += (now - lastTime)/target;
timer += now - lastTime;
lastTime = now;
if (delta >= 1) {
update();
render();
ticks++;
delta--;
}
/*
if (timer >= 1000000000) {
System.out.println("Running at: " + ticks + " per 1second");
ticks = 0;
timer = 0;
}
*/
}
stopGame();
}
public void startGame() {
if (running) return;
running = true;
thread = new Thread(this);
thread.start();
}
public void stopGame() {
if (!running) return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}