-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGame.java
More file actions
327 lines (277 loc) · 7.25 KB
/
Game.java
File metadata and controls
327 lines (277 loc) · 7.25 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.IOException;
import java.net.URL;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable
{
public static long time,timer;
public static boolean gameover,start=true,escape=false;
public static double sec,min;
public static final long time_last=3000;
public static int width=500,z=0;
public static int height=width/16*9;
public static int scale =3; // SCALES THE IMAGE THREE TIMES ITS INITIAL
public static String title ="Game";
private static Thread thread;
protected static JFrame frame;
private Keyboard key;
private static Keyboard Key;
public M k;
public Menustate r;
public Options o;
public int levelno;
public static int menu=0;
public Entity Entity;
protected int coins_score;
protected static int run;
Mouse mouse;
public level level;
protected Score score;
protected int flag=0;
private Player player;
public Projectile pj;
protected static boolean running =false;
public boolean m=true;
Rectangle[] r1 = new Rectangle[20];
private Screen screen;
int x=0,y=0;
Graphics g2;
private BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
private int[] pixels=((DataBufferInt)image.getRaster().getDataBuffer()).getData();
//Converting image object into array of INTEGER
// RASTER RECTANGULAR ARRAY OF PIXELS WHICH IS WRITEABLE
// A Raster encapsulates a DataBuffer that stores the sample values and a SampleModel that describes how to locate a given sample value in a DataBuffer.
// Constructor called only once
public Game(int x)
{
}
public Game()
{
Dimension size=new Dimension(width*scale,height*scale);
setPreferredSize(size);
frame=new JFrame();
screen = new Screen(width,height);
key=new Keyboard();
addKeyListener(key);
score=new Score();
level=level.spawn;
k=new M(key);
r=new Menustate(key);
o=new Options(key);
player=new Player(30,200,key,screen);
player.init(level);
r.minit(frame);
o.minit(frame);
mouse=new Mouse();
addMouseListener(mouse);
addMouseMotionListener(mouse);
}
public static int getWindowWidth()
{
return width*scale;
}
public static int getWindowHeight()
{
return height*scale;
}
public synchronized void start()
{
running =true;
thread=new Thread(this,"Display");
thread.start();
sound();
}
public void sound()
{
try
{
// Open an audio input stream.
URL url = this.getClass().getClassLoader().getResource("m2.wav");
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
// Get a sound clip resource.
Clip clip = AudioSystem.getClip();
//ContinuousAudioDataStream loop = null;
// Open audio clip and load samples from the audio input stream.
clip.open(audioIn);
clip.start();
//clip.loop(4);
//while()
//clip.start();
//if(running)
// clip.loop(1);
// clip.l;
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
//Clip.LOOP_CONTINUOUSLY;
}
public synchronized static void stop()
{
running =false;
try
{
menu=4;
thread.join();
} catch(InterruptedException e)
{
e.printStackTrace();
}
}
public void run()
{
run++;
long lastTime=System.nanoTime();
timer=System.currentTimeMillis();
time=System.currentTimeMillis()/1000;
final double ns=1000000000.0/90.0; //running 60 times a second
double delta=0;//
int frames=0;
int updates=0;
requestFocus();
while(running)
{
long now=System.nanoTime();
delta+=(now-lastTime)/ns;
lastTime=now;
while(delta>=1)
{
update();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis()-timer>1000)
{
timer+=1000;
//System.out.println(updates+"ups,"+frames+"fps");
frame.setTitle(title+"|"+frames +"fps"+"|updates"+updates);
updates=0;
frames=0;
}
}
}
public void update()
{
if(menu==0)
{
k.update();
}
if(menu==2)
sec+=1.5;
if(sec/100==60)
{
sec=0.0;
min+=1;
if(min==5)
{
level.gameover=true;
}
}
if(sec/100>10)
{
}
if(time==time_last)
{
running =false;
}
key.update();
if(key.esc&&!start)
menu=3;
if(menu==2)
{
player.update();
level.update(key);
}
if(menu==3)
{
r.update();
}
if(menu==1)// start options exit
r.update();
if(menu==5) //options
o.update();
}
public void render()
{
BufferStrategy bs = getBufferStrategy();
if(bs==null)
{ /*SAME AS DOUBLE BUFFERING CREATES AN OFFSCREEN IMAGEH
SO AS TO HAVE THE NEXT SCREEN PRINTED ONTO DIRECTELY
RATHER THAN HAVING IT APPEAR PIXEL BY PIXEL
*/
createBufferStrategy(3); // TWO OFF SCREENS BEHIND THE CURRENT
return;
}
Graphics g=bs.getDrawGraphics();
screen.clear();
if(menu==0)
{
k.render(screen);
}
if(menu==5)
{
o.render(screen);
}
if(menu==2)
{
int xScroll=player.x-screen.width/4;
int yScroll=player.y-screen.height/2;
level.render(xScroll,yScroll,screen);
player.render(screen);
}
if(menu==2)
System.arraycopy(screen.r1,0, r1, 0, r1.length);
System.arraycopy(screen.pixels,0, pixels, 0, pixels.length);
//Creating a link b/w Graphics and bufferstrategy
// All Graphics to be displayed on screen
{
g.setColor(Color.CYAN);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(image,0,0,getWidth(),getHeight(),null); //???????????????????????????????????
score.draw_score(g,key);
player.draw(g);
if(menu==0)
k.drawscreen(g);
if(menu==5)
{
o.drawscreen(g);
}
if(menu==1||menu==3)
r.drawscreen(g);
g.setColor(Color.GREEN);
}
g.dispose(); // Releases all system resources/graphics
bs.show();
}
//Entry point of program
public static void main(String args[])
{
Game game=new Game();
game.frame.setResizable(true);
game.frame.setFocusable(true);
game.frame.setTitle(game.title);
game.frame.add(game);
game.frame.pack();// sets the size of frame as the component
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
}