在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Java游戏开发 → Java飞机大战(射击类小游戏源码,带声音效果)

Java飞机大战(射击类小游戏源码,带声音效果)

Java游戏开发

下载此实例
  • 开发语言:Java
  • 实例大小:43.30M
  • 下载次数:56
  • 浏览次数:860
  • 发布时间:2019-10-21
  • 实例类别:Java游戏开发
  • 发 布 人:Q180104
  • 文件格式:.zip
  • 所需积分:2
 相关标签: java 小游戏 游戏

实例介绍

【实例简介】Java小游戏的代码,图片都有啦,shoot game

【实例截图】

from clipboard


from clipboard

【核心代码】

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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
package shoot;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
/*
 *
 * 游戏面板类
 */
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Random;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
import com.sun.media.jfxmedia.AudioClip;
import com.sun.org.apache.xerces.internal.impl.xpath.XPath.Step;
import com.sun.org.apache.xml.internal.security.utils.IgnoreAllErrorHandler;
 
 
import shoot.Bee;
import shoot.FlyingObject;
 
import javafx.beans.binding.When;
import javazoom.jl.player.Player;
 
 
public class Gamepanel extends JPanel implements  Runnable,MouseMotionListener,MouseListener{
     
public static final int PANEL_WIDTH=GameFrame.FRAME_WIDTH-7;
public static final int PANEL_HEIGHT=GameFrame.FRAME_HEIGHT-30;
public static final int MAX_EPLANE_NUMBER=6;
 
     
private Background background;//天空背景对象
 
private HeroPlane heroPlane;//英雄机对象
private ArrayList<EnemyPlane> ePlanes;//敌机对象
 
private ArrayList<Enemybullet>enemybullets;//敌机子弹类对象
private ArrayList<HeroBullet> heroBullets;//英雄机子弹对象
private ArrayList<Boom> booms;//爆破对象
private Bee bee;//小蜜蜂对象
 
 
private int total_score=0;//游戏总得分
//AudioClip对象提供  play loop(循环) stop
java.applet.AudioClip all_bomb,enemy_bomb,bg,hero_bomb,hero_bullet;
 
 
 
 
  public Gamepanel() {
 
 
     
       
     // Image img=Toolkit.getDefaultToolkit().getImage("fish08_01.png");
       
 
 
 
 
 
 
     //初始化音乐
 
     try {
        all_bomb=Applet.newAudioClip(new File("music/all_bomb.wav").toURI().toURL());
        enemy_bomb=Applet.newAudioClip(new File("music/enemy_bomb.wav").toURI().toURL());
        bg=Applet.newAudioClip(new File("music/bg.wav").toURI().toURL());
        hero_bomb=Applet.newAudioClip(new File("music/hero_bomb.wav").toURI().toURL());
        hero_bullet=Applet.newAudioClip(new File("music/hero_bullet.wav").toURI().toURL());
         
         
 
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
       
       
     //初始化背景
    background=new Background(); 
    //初始化敌机
     ePlanes=new ArrayList<EnemyPlane>();
     for(int i=0;i<MAX_EPLANE_NUMBER;i  ) {
         ePlanes.add(new EnemyPlane());
         
     }
     //初始化英雄机
     heroPlane=new HeroPlane();
      
     //初始化敌机 子弹
     enemybullets=new ArrayList<Enemybullet>();
     //初始化英雄机子弹
     heroBullets=new ArrayList<HeroBullet>();
     //初始化爆破效果
     booms=new ArrayList<Boom>();
      
     //初始化小蜜蜂
     bee=new Bee();
     //添加鼠标监听器
     this.addMouseMotionListener(this);
     this.addMouseListener(this);
  }
     
     
     
     
 
    @Override//重写有两种重写,一个是全部重写,一个是追加式的重写,就像super。
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        //画天空
        background.draw(g);
         
        //画敌机
        for (int i = 0; i <ePlanes.size(); i  ) {
             
            EnemyPlane enemyPlane=ePlanes.get(i);
            enemyPlane.draw(g);
        }
        //画英雄机
        heroPlane.draw(g);
        //画敌机子弹
        for (int i=0;i<enemybullets.size();i  ) {
             
        Enemybullet enemybullet=enemybullets.get(i);
        enemybullet.draw(g);
         
        }
        //画英雄机子弹
        for (int i = 0; i < heroBullets.size(); i  ) {
            HeroBullet heroBullet=heroBullets.get(i);
            heroBullet.draw(g);
        }
        //画爆破效果
        for(int i=0;i<booms.size();i  ) {
       Boom boom=booms.get(i);
       boom.draw(g);
        }
        //画小蜜蜂
        bee.draw(g);
         
         
        //画分和命
        g.setColor(Color.blue);
        g.setFont(new Font("微软雅黑",Font.BOLD,30));
        g.drawString("命数:" heroPlane.getLife(), 10, 30);
        g.drawString("得分:" total_score, 10, 80);
         
        if (heroPlane.getLife()==0) {
             
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑",Font.BOLD,50));
            g.drawString("Game over!", PANEL_WIDTH/3-40, PANEL_HEIGHT/2);
             
        }
         
    }
 
 
 
 
 
 
    @Override
    public void run() {
         
        bg.play();
//      while (true) {
//     
//      for(int i=1;i<=10;i  ) {
//          x =3;
//          img=Toolkit.getDefaultToolkit().getImage("fish08_0" i ".png");
//          repaint();//重画,自动调用paint
//          while(x==350) {
//             
//              x=-100;
//              y=(int)(Math.random()*250);
//          }
//          try {
//              Thread.sleep(50);
//          } catch (InterruptedException e) {
//              // TODO Auto-generated catch block
//              e.printStackTrace();
//          }
//      }}
        while(true) {
            //1背景图移动
            background.move();
             
            //2对敌机的处理
            for (int i = 0; i < ePlanes.size(); i  ) {
                 
                EnemyPlane enemyPlane=ePlanes.get(i);
                //2.1敌机移动
            enemyPlane.move(heroPlane);
                 
                 
            //2.2敌机发射子弹
            Enemybullet enemybullet=enemyPlane.shoot();
            if (enemybullet!=null) {
                 
                enemybullets.add(enemybullet);
            }
             
                 
                 
            }
            //3对敌机子弹的处理
             
            for (int i=0;i<enemybullets.size();i  ) {
                 
                Enemybullet enemybullet=enemybullets.get(i);
                //3.1移动
                enemybullet.move();
                //3.2是否出界
                if (enemybullet.outOfBounds()) {
                    enemybullets.remove(i);
                    i--;
                }
                }
            //对蜜蜂处理
            bee.move();
            if (bee.outofBounds()) {
                bee.y=-4000;
                Random rand = new Random();
                bee.x=rand.nextInt(PANEL_WIDTH - this.WIDTH);
            }
             
             
            //4对英雄机子弹的处理
            //4.1移动
            for (int i = 0; i < heroBullets.size(); i  ) {
                HeroBullet heroBullet=heroBullets.get(i);
                heroBullet.move();
                //4.2是否越界
                 
                if (heroBullet.outOfBounds()) {
                    heroBullets.remove(i);
                    i--;
                }
            }
             
             
             
            //5.碰撞检测
 
             
             
             
            //5.1 检测敌机与英雄机,英雄机子弹的碰撞情况
            boolean heroPlane_ishitted=false;//记录英雄机被撞
          for (int i = 0; i < ePlanes.size(); i  ) {
                 
                EnemyPlane enemyPlane=ePlanes.get(i);
                boolean ePlane_isHitted=false;//记录敌机被撞
            //5.1.1跟英雄机撞
                if (enemyPlane.isHitted(heroPlane)) {
                    ePlane_isHitted=true;
                    Boom boom1=new Boom(heroPlane.x,heroPlane.y,heroPlane.width,heroPlane.height,heroPlane.xStep,heroPlane.yStep);
                    booms.add(boom1);
                    heroPlane.setLife(heroPlane.getLife()-1);//生命数-1
                     
                    //加上heroplane的高,为了防止出界后的敌机与英雄机相撞
                    heroPlane.move(0,Gamepanel.PANEL_HEIGHT heroPlane.height);
                     
                    //移除鼠标监听器,暂时无法移动
                     this.removeMouseListener(this);
                     this.removeMouseMotionListener(this);
                     heroPlane.setRestatrt_delay_count(40);//设置英雄机重启,需要延迟40个50ms(参见thread.sleep)
                     heroPlane.setCurrent_live(false);
                     
                }
                 
                 
                 
                 
                 
                 
                 
                 
                 
                //5.1.2跟英雄机子弹是否相撞  
                for(int j=0;j<heroBullets.size();j  ) {
                HeroBullet heroBullet=heroBullets.get(j);
                if (enemyPlane.isHitted(heroBullet)) {
                    ePlane_isHitted=true;
                    heroBullets.remove(j);//移除英雄机子弹
                    j--;
                     
                }
                //蜜蜂撞上了
                if (bee.isHitted(heroBullet)) {
                    Random random=new Random();
                    bee.y=-2000;
                    bee.x=random.nextInt(PANEL_WIDTH)-80;
                    heroBullets.remove(j);
                    i--;
                     
                    heroPlane.setLife(heroPlane.getLife() 1);
                }
                 
                  
                     
                }
                 
                if (ePlane_isHitted) {//若敌机被撞
                     
                    total_score =enemyPlane.getScore();
                    hero_bullet.play();
                    ePlanes.remove(i);//移除该敌机
                    i--;
                    Boom boom=new Boom(enemyPlane.x,enemyPlane.y,enemyPlane.width,enemyPlane.height,enemyPlane.xStep,enemyPlane.yStep);
                    booms.add(boom);
                     
                }
                 
          }
           
          //5.2检测英雄机和敌机子弹的碰撞情况
          //关于英雄机和敌机碰撞,见5.1
           
          for( int i=0; i<enemybullets.size();i  ) {
              Enemybullet enemybullet=enemybullets.get(i);
              if (heroPlane.isHitted(enemybullet)) {
                heroPlane_ishitted=true;
                enemybullets.remove(i);//移除该子弹
                i--;
                 
                 
            }
          }
        
               
         
           
           
           
           
          if (heroPlane_ishitted) {//如果英雄机被撞了
              hero_bomb.play();
            Boom boom=new Boom(heroPlane.x,heroPlane.y, heroPlane.width,heroPlane.height,heroPlane.xStep,heroPlane.yStep);
            booms.add(boom);
            heroPlane.setLife(heroPlane.getLife()-1);//生命数-1
             
             
            //加上heroplane的高,为了防止出界后的敌机与英雄机相撞
            heroPlane.move(0,Gamepanel.PANEL_HEIGHT heroPlane.height);
             
            //移除鼠标监听器,暂时无法移动
             this.removeMouseListener(this);
             this.removeMouseMotionListener(this);
             heroPlane.setRestatrt_delay_count(40);//设置英雄机重启,需要延迟40个50ms(参见thread.sleep)
             heroPlane.setCurrent_live(false);
              
        }
           
           
           
           
           
          //6对爆破对象进行处理
          for (int i = 0; i < booms.size(); i  ) {
              Boom boom=booms.get(i);
              boom.move();
              if (boom.booms_end()) {//该爆破结束
                   
                booms.remove(i);
            }
          }
          //7对英雄机状态处理
          if (heroPlane.getLife()>0) {
             
         
          if (heroPlane.getRestatrt_delay_count()>0) {
            heroPlane.setRestatrt_delay_count(heroPlane.getRestatrt_delay_count()-1);
        }
           
          //如果英雄机当前死亡,且重启延时已经为0
          if ( heroPlane.isCurrent_live()==false && heroPlane.getRestatrt_delay_count()==0) {
              heroPlane.move(Gamepanel.PANEL_WIDTH/2-35, Gamepanel.PANEL_HEIGHT-70);
              heroPlane.setCurrent_live(true);
                addMouseListener(this);
                addMouseMotionListener(this);
         //添加一个全屏爆炸
                all_bomb.play();
                Boom boom=new Boom(0, 0, PANEL_WIDTH, PANEL_HEIGHT, 0, 0);
                booms.add(boom);
                 
                for(int i =0;i<ePlanes.size();i  ) {//清除屏幕得分
                     EnemyPlane ePlane=ePlanes.get(i);
                     total_score =ePlane.getScore();
                     
                     
                }
                 
                ePlanes.clear();//清空所有敌机
                enemybullets.clear();//清空所有子弹
               
        }
          }
           
             
            //逐渐补充敌机数到MAX_EPLAN_NUMBER
          if (ePlanes.size()<MAX_EPLANE_NUMBER) {
            ePlanes.add(new EnemyPlane());
        }
           
          if (heroPlane.getLife()==0) {
            bg.stop();
        }
          repaint();
         
    try {
        Thread.sleep(30);//30ms
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     
         
         
        }
         
    }
 
 
 
 
 
    @Override
    public void mouseDragged(MouseEvent e) {
         
        heroPlane.move(e.getX(),e.getY());
    }
 
 
 
 
 
    @Override
    public void mouseMoved(MouseEvent e) {
     
        //System.out.println(e.getX() "," e.getY());
        heroPlane.move(e.getX(),e.getY());
         
         
    }
 
 
 
 
 
    @Override
    public void mouseClicked(MouseEvent e) {
     
         
    }
 
 
 
 
 
    @Override
    public void mousePressed(MouseEvent e) {
        hero_bullet.play();
    HeroBullet heroBullet=heroPlane.shoot();
    heroBullets.add(heroBullet);
         
    }
 
 
 
 
 
    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
         
    }
 
 
 
 
 
    @Override
    public void mouseEntered(MouseEvent e) {
     
    }
 
 
 
 
 
    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
         
    }
 
 
 
         
    }  

标签: java 小游戏 游戏

实例下载地址

Java飞机大战(射击类小游戏源码,带声音效果)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警