-
Notifications
You must be signed in to change notification settings - Fork 1
/
ShootGame.java
295 lines (270 loc) · 7.44 KB
/
ShootGame.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
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
package com.tarena.shoot;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
import java.util.Arrays;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
public class ShootGame extends JPanel{
public static final int WIDTH = 400; //width of the window
public static final int HEIGHT = 654; //height of the window
public static BufferedImage background; //background image
public static BufferedImage start; //starting image
public static BufferedImage pause; //pausing image
public static BufferedImage gameover; //gameover image
public static BufferedImage airplane; //image of aircrafts
public static BufferedImage bee; //image of bees
public static BufferedImage bullet; //image of bullets
public static BufferedImage hero0; //image of hero0
public static BufferedImage hero1; //image of hero1
public static final int START = 0;
public static final int RUNNING = 1;
public static final int PAUSE = 2;
public static final int GAME_OVER = 3;
private int state = 0;
private Hero hero = new Hero(); //hero array
private FlyingObject[] flyings = {}; //enemys array(bees and enemy aircrafts)
private Bullet[] bullets = {}; //bullet array
static{ //initialize static resources
try{
background = ImageIO.read(ShootGame.class.getResource("images/background.png"));
start = ImageIO.read(ShootGame.class.getResource("images/start.png"));
pause = ImageIO.read(ShootGame.class.getResource("images/pause.png"));
gameover = ImageIO.read(ShootGame.class.getResource("images/gameover.png"));
airplane = ImageIO.read(ShootGame.class.getResource("images/airplane.png"));
bee = ImageIO.read(ShootGame.class.getResource("images/bee.png"));
bullet = ImageIO.read(ShootGame.class.getResource("images/bullet.png"));
hero0 = ImageIO.read(ShootGame.class.getResource("images/hero0.png"));
hero1 = ImageIO.read(ShootGame.class.getResource("images/hero1.png"));
}catch(Exception e){
e.printStackTrace();
}
}
int flyEnterIndex = 0;
public void enterAction(){
flyEnterIndex++;
if(flyEnterIndex % 40 ==0){
FlyingObject f = nextOne();
flyings = Arrays.copyOf(flyings, flyings.length+1);
flyings[flyings.length-1] = f;
}
}
public FlyingObject nextOne(){
Random rand = new Random();
int i = rand.nextInt(20);
if(i<1){
return new Bee();
}else{
return new Airplane();
}
}
public void stepAction(){
hero.step();
for(int i =0; i<flyings.length; i++){
flyings[i].step();
}
for(int i = 0; i<bullets.length; i++){
bullets[i].step();
}
}
int shootIndex = 0;
public void shootAction(){
shootIndex++;
if(shootIndex % 30 ==0){
Bullet [] bs = hero.shoot();
bullets = Arrays.copyOf(bullets, bullets.length+bs.length);
System.arraycopy(bs, 0, bullets, bullets.length-bs.length, bs.length);
}
}
public void bangAction(){
for(int i =0; i<bullets.length ; i++){
bang(bullets[i]);
}
}
int score =0;
public void bang(Bullet bullet){
int index = -1;
for(int i = 0; i<flyings.length ; i++){
FlyingObject o = flyings[i];
if(o.shootBy(bullet)){
index = i;
break;
}
}
if(index != -1){
FlyingObject one = flyings[index];
if(one instanceof Airplane){
Airplane a = (Airplane)one;
score += a.getScore();
}
if(one instanceof Bee){
Bee b = (Bee)one;
int type =b.getType();
switch(type){
case Award.DOUBLE_FIRE:
hero.addDoubleFire();
break;
case Award.LIFE:
hero.addlife();
break;
}
}
FlyingObject f = flyings[index];
flyings[index] = flyings[flyings.length-1];
flyings[flyings.length-1] = f;
flyings = Arrays.copyOf(flyings, flyings.length-1);
}
}
public void outOfBoundAction(){
//flyingobjects
FlyingObject [] fAlive = new FlyingObject[flyings.length];
int index=0;
for(int i =0 ; i< flyings.length ; i++){
FlyingObject f = flyings[i];
if(!f.outOfBound()){
fAlive[index] = f;
index++;
}
}
flyings = Arrays.copyOf(fAlive, index);
//bullets
Bullet [] bAlive = new Bullet[bullets.length];
index=0;
for(int i =0 ; i< bullets.length ; i++){
Bullet b = bullets[i];
if(!b.outOfBound()){
bAlive[index] = b;
index++;
}
}
bullets = Arrays.copyOf(bAlive, index);
}
public void checkGameOverAction(){
if(checkGameOver()){
state = GAME_OVER;
}
}
public boolean checkGameOver(){
for(int i = 0 ; i< flyings.length ; i++){
FlyingObject f = flyings[i];
if(hero.hit(f)){
hero.subtractLife();
hero.clearDoubleFire();
FlyingObject temp = f;
f=flyings[flyings.length-1];
flyings[flyings.length-1] = temp;
flyings = Arrays.copyOf(flyings, flyings.length-1);
}
}
return hero.getLife()<=0;
}
public void action(){
MouseAdapter l = new MouseAdapter(){
public void mouseMoved(MouseEvent e){
if(state == RUNNING){
int x= e.getX();
int y= e.getY();
hero.moveTo(x,y);
}
}
public void mouseClicked(MouseEvent e){
switch (state){
case START:
state = RUNNING;
break;
case GAME_OVER:
state = START;
break;
}
}
public void mouseEntered(MouseEvent e){
if(state == PAUSE){
state = RUNNING;
}
}
public void mouseExited(MouseEvent e){
state = PAUSE;
}
};
this.addMouseListener(l);
this.addMouseMotionListener(l);
Timer timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
if(state== RUNNING){
enterAction();
stepAction();
shootAction();
bangAction();
outOfBoundAction();
checkGameOverAction();
}
repaint();
}
},10, 10);
}
public void paint(Graphics g){
g.drawImage(background, 0, 0, null);
paintHero(g);
paintFlyingObject(g);
paintBullet(g);
paintScore(g);
paintState(g);
}
public void paintHero(Graphics g){
g.drawImage(hero.image, hero.x, hero.y, null);
}
public void paintFlyingObject(Graphics g){
for(int i=0; i<flyings.length; i++){
FlyingObject f = flyings[i];
g.drawImage(f.image, f.x, f.y, null);
}
}
public void paintBullet(Graphics g){
for(int i=0; i<bullets.length; i++){
FlyingObject b = bullets[i];
g.drawImage(b.image, b.x, b.y, null);
}
}
public void paintScore(Graphics g){
g.setColor(new Color(0xffffff));
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
g.drawString("Life: "+ hero.getLife(), 10, 25);
g.drawString("Score: "+score, 10, 45);
}
public void paintState(Graphics g){
switch(state){
case START:
g.drawImage(start, 0, 0, null);
break;
case PAUSE:
g.drawImage(pause, 0, 0, null);
break;
case GAME_OVER:
score = 0;
hero = new Hero();
bullets = new Bullet [] {};
flyings = new FlyingObject [] {};
g.drawImage(gameover, 0, 0, null);
break;
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Fly"); //window
ShootGame game = new ShootGame(); //panel
frame.add(game); //add panel to our frame
frame.setSize(WIDTH, HEIGHT); //adjust window size
frame.setAlwaysOnTop(true); //always on top
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set that when the window is closed, the program terminates
frame.setLocationRelativeTo(null); //set relative position to null, that is, always in the center
frame.setVisible(true); //1. set visibility of the window to true 2.call the paint() function
game.action();
}
}