- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyGameFrame.java
More file actions
Latest commit
111 lines (82 loc) · 2.26 KB
/
Copy pathMyGameFrame.java
File metadata and controls
111 lines (82 loc) · 2.26 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
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Frame;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.event.KeyAdapter;
importjava.awt.event.KeyEvent;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjavax.swing.JFrame;
publicclassMyGameFrameextendsFrame {
ImageplaneImg = GameUtil.getImage("images/plane.png");
Imagebg = GameUtil.getImage("images/bg.jpg");
Planeplane = newPlane(planeImg,250,250);
Shellshell = newShell();
Shell[] shells = newShell[50];
@Override
publicvoidpaint(Graphicsg) {
g.drawImage(bg, 0, 0, null);
plane.drawSelf(g);
for(inti =0; i<shells.length;i++) {
shells[i].draw(g);
booleanpeng = shells[i].getRect().intersects(plane.getRect());
if(peng) {
plane.live=false;
}
}
shell.draw(g);
}
classPaintThreadextendsThread {
@Override
publicvoidrun() {
while(true){
repaint();
try {
Thread.sleep(40); //1s=1000ms
} catch (InterruptedExceptione) {
e.printStackTrace();
}
}
}
}
classKeyMonitorextendsKeyAdapter {
@Override
publicvoidkeyPressed(KeyEvente) {
plane.addDirection(e);
}
@Override
publicvoidkeyReleased(KeyEvente) {
plane.minusDirection(e);
}
}
publicvoidlaunchFrame(){
this.setTitle("lxy·");
this.setVisible(true);
this.setSize(Constant.GAME_WIDTH , Constant.GAME_HEIGHT);
this.setLocation(300, 300);
this.addWindowListener(newWindowAdapter() {
@Override
publicvoidwindowClosing(WindowEvente) {
System.exit(0);
}
});
newPaintThread().start();
addKeyListener(newKeyMonitor());
for(inti =0; i<shells.length;i++) {
shells[i]= newShell();
}
}
publicstaticvoidmain(String[] args) {
MyGameFramef = newMyGameFrame();
f.launchFrame();
}
privateImageoffScreenImage=null;
publicvoidupdate(Graphicsg) {
if(offScreenImage == null)
offScreenImage = this.createImage(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
GraphicsgOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
}