[Java] 游戏 →→→→→进入此内容的聊天室

来自 , 2020-02-20, 写在 Java, 查看 111 次.
URL http://www.code666.cn/view/712a3c98
  1. java打飞机完整代码
  2. 1
  3. 2
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10
  12. 11
  13. 12
  14. 13
  15. 14
  16. 15
  17. 16
  18. 17
  19. 18
  20. 19
  21. 20
  22. 21
  23. 22
  24. 23
  25. 24
  26. 25
  27. 26
  28. 27
  29. 28
  30. 29
  31. 30
  32. 31
  33. 32
  34. 33
  35. 34
  36. 35
  37. 36
  38. 37
  39. import java.util.Random;
  40.  
  41.  
  42.  敌飞机: 是飞行物,也是敌人
  43.  
  44. public class Airplane extends FlyingObject implements Enemy {
  45.     private int speed = 3;  //移动步骤
  46.  
  47.     /** 初始化数据 */
  48.     public Airplane(){
  49.         this.image = ShootGame.airplane;
  50.         width = image.getWidth();
  51.         height = image.getHeight();
  52.         y = -height;        
  53.         Random rand = new Random();
  54.         x = rand.nextInt(ShootGame.WIDTH - width);
  55.     }
  56.  
  57.     /** 获取分数 */
  58.     @Override
  59.     public int getScore() {
  60.         return 5;
  61.     }
  62.  
  63.     /** //越界处理 */
  64.     @Override
  65.     public     boolean outOfBounds() {  
  66.         return y>ShootGame.HEIGHT;
  67.     }
  68.  
  69.     /** 移动 */
  70.     @Override
  71.     public void step() {  
  72.         y += speed;
  73.     }
  74.  
  75. }
  76. 分数奖励
  77. 1
  78. 2
  79. 3
  80. 4
  81. 5
  82. 6
  83. 7
  84. 8
  85. 9
  86. /**
  87.  * 奖励
  88.  */
  89. public interface Award {
  90.     int DOUBLE_FIRE = 0;  //双倍火力
  91.     int LIFE = 1;   //1条命
  92.     /** 获得奖励类型(上面的0或1) */
  93.     int getType();
  94. }
  95. 蜜蜂
  96. 1
  97. 2
  98. 3
  99. 4
  100. 5
  101. 6
  102. 7
  103. 8
  104. 9
  105. 10
  106. 11
  107. 12
  108. 13
  109. 14
  110. 15
  111. 16
  112. 17
  113. 18
  114. 19
  115. 20
  116. 21
  117. 22
  118. 23
  119. 24
  120. 25
  121. 26
  122. 27
  123. 28
  124. 29
  125. 30
  126. 31
  127. 32
  128. 33
  129. 34
  130. 35
  131. 36
  132. 37
  133. 38
  134. 39
  135. 40
  136. 41
  137. 42
  138. 43
  139. import java.util.Random;
  140.  
  141. /** 蜜蜂 */
  142. public class Bee extends FlyingObject implements Award{
  143.     private int xSpeed = 1;   //x坐标移动速度
  144.     private int ySpeed = 2;   //y坐标移动速度
  145.     private int awardType;    //奖励类型
  146.  
  147.     /** 初始化数据 */
  148.     public Bee(){
  149.         this.image = ShootGame.bee;
  150.         width = image.getWidth();
  151.         height = image.getHeight();
  152.         y = -height;
  153.         Random rand = new Random();
  154.         x = rand.nextInt(ShootGame.WIDTH - width);
  155.         awardType = rand.nextInt(2);   //初始化时给奖励
  156.     }
  157.  
  158.     /** 获得奖励类型 */
  159.     public int getType(){
  160.         return awardType;
  161.     }
  162.  
  163.     /** 越界处理 */
  164.     @Override
  165.     public boolean outOfBounds() {
  166.         return y>ShootGame.HEIGHT;
  167.     }
  168.  
  169.     /** 移动,可斜着飞 */
  170.     @Override
  171.     public void step() {      
  172.         x += xSpeed;
  173.         y += ySpeed;
  174.         if(x > ShootGame.WIDTH-width){  
  175.             xSpeed = -1;
  176.         }
  177.         if(x < 0){
  178.             xSpeed = 1;
  179.         }
  180.     }
  181. }
  182. 子弹类:是飞行物体
  183. 1
  184. 2
  185. 3
  186. 4
  187. 5
  188. 6
  189. 7
  190. 8
  191. 9
  192. 10
  193. 11
  194. 12
  195. 13
  196. 14
  197. 15
  198. 16
  199. 17
  200. 18
  201. 19
  202. 20
  203. 21
  204. 22
  205. 23
  206. 24
  207. 25
  208. 26
  209. /**
  210.  * 子弹类:是飞行物
  211.  */
  212. public class Bullet extends FlyingObject {
  213.     private int speed = 3;  //移动的速度
  214.  
  215.     /** 初始化数据 */
  216.     public Bullet(int x,int y){
  217.         this.x = x;
  218.         this.y = y;
  219.         this.image = ShootGame.bullet;
  220.     }
  221.  
  222.     /** 移动 */
  223.     @Override
  224.     public void step(){    
  225.         y-=speed;
  226.     }
  227.  
  228.     /** 越界处理 */
  229.     @Override
  230.     public boolean outOfBounds() {
  231.         return y<-height;
  232.     }
  233.  
  234. }
  235. 敌人的分数
  236. 1
  237. 2
  238. 3
  239. 4
  240. 5
  241. 6
  242. 7
  243. /**
  244.  * 敌人,可以有分数
  245.  */
  246. public interface Enemy {
  247.     /** 敌人的分数  */
  248.     int getScore();
  249. }
  250. 飞行物(敌机,蜜蜂,子弹,英雄机)
  251. 1
  252. 2
  253. 3
  254. 4
  255. 5
  256. 6
  257. 7
  258. 8
  259. 9
  260. 10
  261. 11
  262. 12
  263. 13
  264. 14
  265. 15
  266. 16
  267. 17
  268. 18
  269. 19
  270. 20
  271. 21
  272. 22
  273. 23
  274. 24
  275. 25
  276. 26
  277. 27
  278. 28
  279. 29
  280. 30
  281. 31
  282. 32
  283. 33
  284. 34
  285. 35
  286. 36
  287. 37
  288. 38
  289. 39
  290. 40
  291. 41
  292. 42
  293. 43
  294. 44
  295. 45
  296. 46
  297. 47
  298. 48
  299. 49
  300. 50
  301. 51
  302. 52
  303. 53
  304. 54
  305. 55
  306. 56
  307. 57
  308. 58
  309. 59
  310. 60
  311. 61
  312. 62
  313. 63
  314. 64
  315. 65
  316. 66
  317. 67
  318. 68
  319. 69
  320. 70
  321. 71
  322. 72
  323. 73
  324. 74
  325. 75
  326. import java.awt.image.BufferedImage;
  327.  
  328. /**
  329.  * 飞行物(敌机,蜜蜂,子弹,英雄机)
  330.  */
  331. public abstract class FlyingObject {
  332.     protected int x;    //x坐标
  333.     protected int y;    //y坐标
  334.     protected int width;    //宽
  335.     protected int height;   //高
  336.     protected BufferedImage image;   //图片
  337.  
  338.     public int getX() {
  339.         return x;
  340.     }
  341.  
  342.     public void setX(int x) {
  343.         this.x = x;
  344.     }
  345.  
  346.     public int getY() {
  347.         return y;
  348.     }
  349.  
  350.     public void setY(int y) {
  351.         this.y = y;
  352.     }
  353.  
  354.     public int getWidth() {
  355.         return width;
  356.     }
  357.  
  358.     public void setWidth(int width) {
  359.         this.width = width;
  360.     }
  361.  
  362.     public int getHeight() {
  363.         return height;
  364.     }
  365.  
  366.     public void setHeight(int height) {
  367.         this.height = height;
  368.     }
  369.  
  370.     public BufferedImage getImage() {
  371.         return image;
  372.     }
  373.  
  374.     public void setImage(BufferedImage image) {
  375.         this.image = image;
  376.     }
  377.  
  378.     /**
  379.      * 检查是否出界
  380.      * @return true 出界与否
  381.      */
  382.     public abstract boolean outOfBounds();
  383.  
  384.     /**
  385.      * 飞行物移动一步
  386.      */
  387.     public abstract void step();
  388.  
  389.     /**
  390.      * 检查当前飞行物体是否被子弹(x,y)击(shoot)中
  391.      * @param Bullet 子弹对象
  392.      * @return true表示被击中了
  393.      */
  394.     public boolean shootBy(Bullet bullet){
  395.         int x = bullet.x;  //子弹横坐标
  396.         int y = bullet.y;  //子弹纵坐标
  397.         return this.x<x && x<this.x+width && this.y<y && y<this.y+height;
  398.     }
  399.  
  400. }
  401. 英雄机
  402. 1
  403. 2
  404. 3
  405. 4
  406. 5
  407. 6
  408. 7
  409. 8
  410. 9
  411. 10
  412. 11
  413. 12
  414. 13
  415. 14
  416. 15
  417. 16
  418. 17
  419. 18
  420. 19
  421. 20
  422. 21
  423. 22
  424. 23
  425. 24
  426. 25
  427. 26
  428. 27
  429. 28
  430. 29
  431. 30
  432. 31
  433. 32
  434. 33
  435. 34
  436. 35
  437. 36
  438. 37
  439. 38
  440. 39
  441. 40
  442. 41
  443. 42
  444. 43
  445. 44
  446. 45
  447. 46
  448. 47
  449. 48
  450. 49
  451. 50
  452. 51
  453. 52
  454. 53
  455. 54
  456. 55
  457. 56
  458. 57
  459. 58
  460. 59
  461. 60
  462. 61
  463. 62
  464. 63
  465. 64
  466. 65
  467. 66
  468. 67
  469. 68
  470. 69
  471. 70
  472. 71
  473. 72
  474. 73
  475. 74
  476. 75
  477. 76
  478. 77
  479. 78
  480. 79
  481. 80
  482. 81
  483. 82
  484. 83
  485. 84
  486. 85
  487. 86
  488. 87
  489. 88
  490. 89
  491. 90
  492. 91
  493. 92
  494. 93
  495. 94
  496. 95
  497. 96
  498. 97
  499. 98
  500. 99
  501. 100
  502. 101
  503. 102
  504. 103
  505. 104
  506. 105
  507. 106
  508. import java.awt.image.BufferedImage;
  509.  
  510. /**
  511.  * 英雄机:是飞行物
  512.  */
  513. public class Hero extends FlyingObject{
  514.  
  515.     private BufferedImage[] images = {};  //英雄机图片
  516.     private int index = 0;                //英雄机图片切换索引
  517.  
  518.     private int doubleFire;   //双倍火力
  519.     private int life;   //命
  520.  
  521.     /** 初始化数据 */
  522.     public Hero(){
  523.         life = 3;   //初始3条命
  524.         doubleFire = 0;   //初始火力为0
  525.         images = new BufferedImage[]{ShootGame.hero0, ShootGame.hero1}; //英雄机图片数组
  526.         image = ShootGame.hero0;   //初始为hero0图片
  527.         width = image.getWidth();
  528.         height = image.getHeight();
  529.         x = 150;
  530.         y = 400;
  531.     }
  532.  
  533.     /** 获取双倍火力 */
  534.     public int isDoubleFire() {
  535.         return doubleFire;
  536.     }
  537.  
  538.     /** 设置双倍火力 */
  539.     public void setDoubleFire(int doubleFire) {
  540.         this.doubleFire = doubleFire;
  541.     }
  542.  
  543.     /** 增加火力 */
  544.     public void addDoubleFire(){
  545.         doubleFire = 40;
  546.     }
  547.  
  548.     /** 增命 */
  549.     public void addLife(){  //增命
  550.         life++;
  551.     }
  552.  
  553.     /** 减命 */
  554.     public void subtractLife(){   //减命
  555.         life--;
  556.     }
  557.  
  558.     /** 获取命 */
  559.     public int getLife(){
  560.         return life;
  561.     }
  562.  
  563.     /** 当前物体移动了一下,相对距离,x,y鼠标位置  */
  564.     public void moveTo(int x,int y){    
  565.         this.x = x - width/2;
  566.         this.y = y - height/2;
  567.     }
  568.  
  569.     /** 越界处理 */
  570.     @Override
  571.     public boolean outOfBounds() {
  572.         return false;  
  573.     }
  574.  
  575.     /** 发射子弹 */
  576.     public Bullet[] shoot(){    
  577.         int xStep = width/4;      //4半
  578.         int yStep = 20;  //步
  579.         if(doubleFire>0){  //双倍火力
  580.             Bullet[] bullets = new Bullet[2];
  581.             bullets[0] = new Bullet(x+xStep,y-yStep);  //y-yStep(子弹距飞机的位置)
  582.             bullets[1] = new Bullet(x+3*xStep,y-yStep);
  583.             return bullets;
  584.         }else{      //单倍火力
  585.             Bullet[] bullets = new Bullet[1];
  586.             bullets[0] = new Bullet(x+2*xStep,y-yStep);  
  587.             return bullets;
  588.         }
  589.     }
  590.  
  591.     /** 移动 */
  592.     @Override
  593.     public void step() {
  594.         if(images.length>0){
  595.             image = images[index++/10%images.length];  //切换图片hero0,hero1
  596.         }
  597.     }
  598.  
  599.     /** 碰撞算法 */
  600.     public boolean hit(FlyingObject other){
  601.  
  602.         int x1 = other.x - this.width/2;                 //x坐标最小距离
  603.         int x2 = other.x + this.width/2 + other.width;   //x坐标最大距离
  604.         int y1 = other.y - this.height/2;                //y坐标最小距离
  605.         int y2 = other.y + this.height/2 + other.height; //y坐标最大距离
  606.  
  607.         int herox = this.x + this.width/2;               //英雄机x坐标中心点距离
  608.         int heroy = this.y + this.height/2;              //英雄机y坐标中心点距离
  609.  
  610.         return herox>x1 && herox<x2 && heroy>y1 && heroy<y2;   //区间范围内为撞上了
  611.     }
  612.  
  613. }
  614. 游戏启动主类
  615. 1
  616. 2
  617. 3
  618. 4
  619. 5
  620. 6
  621. 7
  622. 8
  623. 9
  624. 10
  625. 11
  626. 12
  627. 13
  628. 14
  629. 15
  630. 16
  631. 17
  632. 18
  633. 19
  634. 20
  635. 21
  636. 22
  637. 23
  638. 24
  639. 25
  640. 26
  641. 27
  642. 28
  643. 29
  644. 30
  645. 31
  646. 32
  647. 33
  648. 34
  649. 35
  650. 36
  651. 37
  652. 38
  653. 39
  654. 40
  655. 41
  656. 42
  657. 43
  658. 44
  659. 45
  660. 46
  661. 47
  662. 48
  663. 49
  664. 50
  665. 51
  666. 52
  667. 53
  668. 54
  669. 55
  670. 56
  671. 57
  672. 58
  673. 59
  674. 60
  675. 61
  676. 62
  677. 63
  678. 64
  679. 65
  680. 66
  681. 67
  682. 68
  683. 69
  684. 70
  685. 71
  686. 72
  687. 73
  688. 74
  689. 75
  690. 76
  691. 77
  692. 78
  693. 79
  694. 80
  695. 81
  696. 82
  697. 83
  698. 84
  699. 85
  700. 86
  701. 87
  702. 88
  703. 89
  704. 90
  705. 91
  706. 92
  707. 93
  708. 94
  709. 95
  710. 96
  711. 97
  712. 98
  713. 99
  714. 100
  715. 101
  716. 102
  717. 103
  718. 104
  719. 105
  720. 106
  721. 107
  722. 108
  723. 109
  724. 110
  725. 111
  726. 112
  727. 113
  728. 114
  729. 115
  730. 116
  731. 117
  732. 118
  733. 119
  734. 120
  735. 121
  736. 122
  737. 123
  738. 124
  739. 125
  740. 126
  741. 127
  742. 128
  743. 129
  744. 130
  745. 131
  746. 132
  747. 133
  748. 134
  749. 135
  750. 136
  751. 137
  752. 138
  753. 139
  754. 140
  755. 141
  756. 142
  757. 143
  758. 144
  759. 145
  760. 146
  761. 147
  762. 148
  763. 149
  764. 150
  765. 151
  766. 152
  767. 153
  768. 154
  769. 155
  770. 156
  771. 157
  772. 158
  773. 159
  774. 160
  775. 161
  776. 162
  777. 163
  778. 164
  779. 165
  780. 166
  781. 167
  782. 168
  783. 169
  784. 170
  785. 171
  786. 172
  787. 173
  788. 174
  789. 175
  790. 176
  791. 177
  792. 178
  793. 179
  794. 180
  795. 181
  796. 182
  797. 183
  798. 184
  799. 185
  800. 186
  801. 187
  802. 188
  803. 189
  804. 190
  805. 191
  806. 192
  807. 193
  808. 194
  809. 195
  810. 196
  811. 197
  812. 198
  813. 199
  814. 200
  815. 201
  816. 202
  817. 203
  818. 204
  819. 205
  820. 206
  821. 207
  822. 208
  823. 209
  824. 210
  825. 211
  826. 212
  827. 213
  828. 214
  829. 215
  830. 216
  831. 217
  832. 218
  833. 219
  834. 220
  835. 221
  836. 222
  837. 223
  838. 224
  839. 225
  840. 226
  841. 227
  842. 228
  843. 229
  844. 230
  845. 231
  846. 232
  847. 233
  848. 234
  849. 235
  850. 236
  851. 237
  852. 238
  853. 239
  854. 240
  855. 241
  856. 242
  857. 243
  858. 244
  859. 245
  860. 246
  861. 247
  862. 248
  863. 249
  864. 250
  865. 251
  866. 252
  867. 253
  868. 254
  869. 255
  870. 256
  871. 257
  872. 258
  873. 259
  874. 260
  875. 261
  876. 262
  877. 263
  878. 264
  879. 265
  880. 266
  881. 267
  882. 268
  883. 269
  884. 270
  885. 271
  886. 272
  887. 273
  888. 274
  889. 275
  890. 276
  891. 277
  892. 278
  893. 279
  894. 280
  895. 281
  896. 282
  897. 283
  898. 284
  899. 285
  900. 286
  901. 287
  902. 288
  903. 289
  904. 290
  905. 291
  906. 292
  907. 293
  908. 294
  909. 295
  910. 296
  911. 297
  912. 298
  913. 299
  914. 300
  915. 301
  916. 302
  917. 303
  918. 304
  919. 305
  920. 306
  921. 307
  922. 308
  923. 309
  924. 310
  925. 311
  926. 312
  927. 313
  928. 314
  929. 315
  930. 316
  931. 317
  932. 318
  933. 319
  934. 320
  935. 321
  936. 322
  937. 323
  938. 324
  939. 325
  940. 326
  941. 327
  942. 328
  943. 329
  944. 330
  945. 331
  946. 332
  947. 333
  948. 334
  949. 335
  950. 336
  951. 337
  952. 338
  953. 339
  954. 340
  955. 341
  956. 342
  957. 343
  958. 344
  959. 345
  960. 346
  961. 347
  962. 348
  963. 349
  964. 350
  965. 351
  966. 352
  967. 353
  968. 354
  969. 355
  970. 356
  971. 357
  972. 358
  973. 359
  974. 360
  975. 361
  976. 362
  977. 363
  978. import java.awt.Font;
  979. import java.awt.Color;
  980. import java.awt.Graphics;
  981. import java.awt.event.MouseAdapter;
  982. import java.awt.event.MouseEvent;
  983. import java.util.Arrays;
  984. import java.util.Random;
  985. import java.util.Timer;
  986. import java.util.TimerTask;
  987. import java.awt.image.BufferedImage;
  988.  
  989. import javax.imageio.ImageIO;
  990. import javax.swing.ImageIcon;
  991. import javax.swing.JFrame;
  992. import javax.swing.JPanel;
  993.  
  994. public class ShootGame extends JPanel {
  995.     public static final int WIDTH = 400; // 面板宽
  996.     public static final int HEIGHT = 654; // 面板高
  997.     /** 游戏的当前状态: START RUNNING PAUSE GAME_OVER */
  998.     private int state;
  999.     private static final int START = 0;
  1000.     private static final int RUNNING = 1;
  1001.     private static final int PAUSE = 2;
  1002.     private static final int GAME_OVER = 3;
  1003.  
  1004.     private int score = 0; // 得分
  1005.     private Timer timer; // 定时器
  1006.     private int intervel = 1000 / 100; // 时间间隔(毫秒)
  1007.  
  1008.     public static BufferedImage background;
  1009.     public static BufferedImage start;
  1010.     public static BufferedImage airplane;
  1011.     public static BufferedImage bee;
  1012.     public static BufferedImage bullet;
  1013.     public static BufferedImage hero0;
  1014.     public static BufferedImage hero1;
  1015.     public static BufferedImage pause;
  1016.     public static BufferedImage gameover;
  1017.  
  1018.     private FlyingObject[] flyings = {}; // 敌机数组
  1019.     private Bullet[] bullets = {}; // 子弹数组
  1020.     private Hero hero = new Hero(); // 英雄机
  1021.  
  1022.     static { // 静态代码块,初始化图片资源
  1023.         try {
  1024.             background = ImageIO.read(ShootGame.class
  1025.                     .getResource("background.png"));
  1026.             start = ImageIO.read(ShootGame.class.getResource("start.png"));
  1027.             airplane = ImageIO
  1028.                     .read(ShootGame.class.getResource("airplane.png"));
  1029.             bee = ImageIO.read(ShootGame.class.getResource("bee.png"));
  1030.             bullet = ImageIO.read(ShootGame.class.getResource("bullet.png"));
  1031.             hero0 = ImageIO.read(ShootGame.class.getResource("hero0.png"));
  1032.             hero1 = ImageIO.read(ShootGame.class.getResource("hero1.png"));
  1033.             pause = ImageIO.read(ShootGame.class.getResource("pause.png"));
  1034.             gameover = ImageIO
  1035.                     .read(ShootGame.class.getResource("gameover.png"));
  1036.         } catch (Exception e) {
  1037.             e.printStackTrace();
  1038.         }
  1039.     }
  1040.  
  1041.     /** 画 */
  1042.     @Override
  1043.     public void paint(Graphics g) {
  1044.         g.drawImage(background, 0, 0, null); // 画背景图
  1045.         paintHero(g); // 画英雄机
  1046.         paintBullets(g); // 画子弹
  1047.         paintFlyingObjects(g); // 画飞行物
  1048.         paintScore(g); // 画分数
  1049.         paintState(g); // 画游戏状态
  1050.     }
  1051.  
  1052.     /** 画英雄机 */
  1053.     public void paintHero(Graphics g) {
  1054.         g.drawImage(hero.getImage(), hero.getX(), hero.getY(), null);
  1055.     }
  1056.  
  1057.     /** 画子弹 */
  1058.     public void paintBullets(Graphics g) {
  1059.         for (int i = 0; i < bullets.length; i++) {
  1060.             Bullet b = bullets[i];
  1061.             g.drawImage(b.getImage(), b.getX() - b.getWidth() / 2, b.getY(),
  1062.                     null);
  1063.         }
  1064.     }
  1065.  
  1066.     /** 画飞行物 */
  1067.     public void paintFlyingObjects(Graphics g) {
  1068.         for (int i = 0; i < flyings.length; i++) {
  1069.             FlyingObject f = flyings[i];
  1070.             g.drawImage(f.getImage(), f.getX(), f.getY(), null);
  1071.         }
  1072.     }
  1073.  
  1074.     /** 画分数 */
  1075.     public void paintScore(Graphics g) {
  1076.         int x = 10; // x坐标
  1077.         int y = 25; // y坐标
  1078.         Font font = new Font(Font.SANS_SERIF, Font.BOLD, 22); // 字体
  1079.         g.setColor(new Color(0xFF0000));
  1080.         g.setFont(font); // 设置字体
  1081.         g.drawString("SCORE:" + score, x, y); // 画分数
  1082.         y=y+20; // y坐标增20
  1083.         g.drawString("LIFE:" + hero.getLife(), x, y); // 画命
  1084.     }
  1085.  
  1086.     /** 画游戏状态 */
  1087.     public void paintState(Graphics g) {
  1088.         switch (state) {
  1089.         case START: // 启动状态
  1090.             g.drawImage(start, 0, 0, null);
  1091.             break;
  1092.         case PAUSE: // 暂停状态
  1093.             g.drawImage(pause, 0, 0, null);
  1094.             break;
  1095.         case GAME_OVER: // 游戏终止状态
  1096.             g.drawImage(gameover, 0, 0, null);
  1097.             break;
  1098.         }
  1099.     }
  1100.  
  1101.     public static void main(String[] args) {
  1102.         JFrame frame = new JFrame("Fly");
  1103.         ShootGame game = new ShootGame(); // 面板对象
  1104.         frame.add(game); // 将面板添加到JFrame中
  1105.         frame.setSize(WIDTH, HEIGHT); // 设置大小
  1106.         frame.setAlwaysOnTop(true); // 设置其总在最上
  1107.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 默认关闭操作
  1108.         frame.setIconImage(new ImageIcon("images/icon.jpg").getImage()); // 设置窗体的图标
  1109.         frame.setLocationRelativeTo(null); // 设置窗体初始位置
  1110.         frame.setVisible(true); // 尽快调用paint
  1111.  
  1112.         game.action(); // 启动执行
  1113.     }
  1114.  
  1115.     /** 启动执行代码 */
  1116.     public void action() {
  1117.         // 鼠标监听事件
  1118.         MouseAdapter l = new MouseAdapter() {
  1119.             @Override
  1120.             public void mouseMoved(MouseEvent e) { // 鼠标移动
  1121.                 if (state == RUNNING) { // 运行状态下移动英雄机--随鼠标位置
  1122.                     int x = e.getX();
  1123.                     int y = e.getY();
  1124.                     hero.moveTo(x, y);
  1125.                 }
  1126.             }
  1127.  
  1128.             @Override
  1129.             public void mouseEntered(MouseEvent e) { // 鼠标进入
  1130.                 if (state == PAUSE) { // 暂停状态下运行
  1131.                     state = RUNNING;
  1132.                 }
  1133.             }
  1134.  
  1135.             @Override
  1136.             public void mouseExited(MouseEvent e) { // 鼠标退出
  1137.                 if (state == RUNNING) { // 游戏未结束,则设置其为暂停
  1138.                     state = PAUSE;
  1139.                 }
  1140.             }
  1141.  
  1142.             @Override
  1143.             public void mouseClicked(MouseEvent e) { // 鼠标点击
  1144.                 switch (state) {
  1145.                 case START:
  1146.                     state = RUNNING; // 启动状态下运行
  1147.                     break;
  1148.                 case GAME_OVER: // 游戏结束,清理现场
  1149.                     flyings = new FlyingObject[0]; // 清空飞行物
  1150.                     bullets = new Bullet[0]; // 清空子弹
  1151.                     hero = new Hero(); // 重新创建英雄机
  1152.                     score = 0; // 清空成绩
  1153.                     state = START; // 状态设置为启动
  1154.                     break;
  1155.                 }
  1156.             }
  1157.         };
  1158.         this.addMouseListener(l); // 处理鼠标点击操作
  1159.         this.addMouseMotionListener(l); // 处理鼠标滑动操作
  1160.  
  1161.         timer = new Timer(); // 主流程控制
  1162.         timer.schedule(new TimerTask() {
  1163.             @Override
  1164.             public void run() {
  1165.                 if (state == RUNNING) { // 运行状态
  1166.                     enterAction(); // 飞行物入场
  1167.                     stepAction(); // 走一步
  1168.                     shootAction(); // 英雄机射击
  1169.                     bangAction(); // 子弹打飞行物
  1170.                     outOfBoundsAction(); // 删除越界飞行物及子弹
  1171.                     checkGameOverAction(); // 检查游戏结束
  1172.                 }
  1173.                 repaint(); // 重绘,调用paint()方法
  1174.             }
  1175.  
  1176.         }, intervel, intervel);
  1177.     }
  1178.  
  1179.     int flyEnteredIndex = 0; // 飞行物入场计数
  1180.  
  1181.     /** 飞行物入场 */
  1182.     public void enterAction() {
  1183.         flyEnteredIndex++;
  1184.         if (flyEnteredIndex % 40 == 0) { // 400毫秒生成一个飞行物--10*40
  1185.             FlyingObject obj = nextOne(); // 随机生成一个飞行物
  1186.             flyings = Arrays.copyOf(flyings, flyings.length + 1);
  1187.             flyings[flyings.length - 1] = obj;
  1188.         }
  1189.     }
  1190.  
  1191.     /** 走一步 */
  1192.     public void stepAction() {
  1193.         for (int i = 0; i < flyings.length; i++) { // 飞行物走一步
  1194.             FlyingObject f = flyings[i];
  1195.             f.step();
  1196.         }
  1197.  
  1198.         for (int i = 0; i < bullets.length; i++) { // 子弹走一步
  1199.             Bullet b = bullets[i];
  1200.             b.step();
  1201.         }
  1202.         hero.step(); // 英雄机走一步
  1203.     }
  1204.  
  1205.     /** 飞行物走一步 */
  1206.     public void flyingStepAction() {
  1207.         for (int i = 0; i < flyings.length; i++) {
  1208.             FlyingObject f = flyings[i];
  1209.             f.step();
  1210.         }
  1211.     }
  1212.  
  1213.     int shootIndex = 0; // 射击计数
  1214.  
  1215.     /** 射击 */
  1216.     public void shootAction() {
  1217.         shootIndex++;
  1218.         if (shootIndex % 30 == 0) { // 300毫秒发一颗
  1219.             Bullet[] bs = hero.shoot(); // 英雄打出子弹
  1220.             bullets = Arrays.copyOf(bullets, bullets.length + bs.length); // 扩容
  1221.             System.arraycopy(bs, 0, bullets, bullets.length - bs.length,
  1222.                     bs.length); // 追加数组
  1223.         }
  1224.     }
  1225.  
  1226.     /** 子弹与飞行物碰撞检测 */
  1227.     public void bangAction() {
  1228.         for (int i = 0; i < bullets.length; i++) { // 遍历所有子弹
  1229.             Bullet b = bullets[i];
  1230.             bang(b); // 子弹和飞行物之间的碰撞检查
  1231.         }
  1232.     }
  1233.  
  1234.     /** 删除越界飞行物及子弹 */
  1235.     public void outOfBoundsAction() {
  1236.         int index = 0; // 索引
  1237.         FlyingObject[] flyingLives = new FlyingObject[flyings.length]; // 活着的飞行物
  1238.         for (int i = 0; i < flyings.length; i++) {
  1239.             FlyingObject f = flyings[i];
  1240.             if (!f.outOfBounds()) {
  1241.                 flyingLives[index++] = f; // 不越界的留着
  1242.             }
  1243.         }
  1244.         flyings = Arrays.copyOf(flyingLives, index); // 将不越界的飞行物都留着
  1245.  
  1246.         index = 0; // 索引重置为0
  1247.         Bullet[] bulletLives = new Bullet[bullets.length];
  1248.         for (int i = 0; i < bullets.length; i++) {
  1249.             Bullet b = bullets[i];
  1250.             if (!b.outOfBounds()) {
  1251.                 bulletLives[index++] = b;
  1252.             }
  1253.         }
  1254.         bullets = Arrays.copyOf(bulletLives, index); // 将不越界的子弹留着
  1255.     }
  1256.  
  1257.     /** 检查游戏结束 */
  1258.     public void checkGameOverAction() {
  1259.         if (isGameOver()==true) {
  1260.             state = GAME_OVER; // 改变状态
  1261.         }
  1262.     }
  1263.  
  1264.     /** 检查游戏是否结束 */
  1265.     public boolean isGameOver() {
  1266.  
  1267.         for (int i = 0; i < flyings.length; i++) {
  1268.             int index = -1;
  1269.             FlyingObject obj = flyings[i];
  1270.             if (hero.hit(obj)) { // 检查英雄机与飞行物是否碰撞
  1271.                 hero.subtractLife(); // 减命
  1272.                 hero.setDoubleFire(0); // 双倍火力解除
  1273.                 index = i; // 记录碰上的飞行物索引
  1274.             }
  1275.             if (index != -1) {
  1276.                 FlyingObject t = flyings[index];
  1277.                 flyings[index] = flyings[flyings.length - 1];
  1278.                 flyings[flyings.length - 1] = t; // 碰上的与最后一个飞行物交换
  1279.  
  1280.                 flyings = Arrays.copyOf(flyings, flyings.length - 1); // 删除碰上的飞行物
  1281.             }
  1282.         }
  1283.  
  1284.         return hero.getLife() <= 0;
  1285.     }
  1286.  
  1287.     /** 子弹和飞行物之间的碰撞检查 */
  1288.     public void bang(Bullet bullet) {
  1289.         int index = -1; // 击中的飞行物索引
  1290.         for (int i = 0; i < flyings.length; i++) {
  1291.             FlyingObject obj = flyings[i];
  1292.             if (obj.shootBy(bullet)) { // 判断是否击中
  1293.                 index = i; // 记录被击中的飞行物的索引
  1294.                 break;
  1295.             }
  1296.         }
  1297.         if (index != -1) { // 有击中的飞行物
  1298.             FlyingObject one = flyings[index]; // 记录被击中的飞行物
  1299.  
  1300.             FlyingObject temp = flyings[index]; // 被击中的飞行物与最后一个飞行物交换
  1301.             flyings[index] = flyings[flyings.length - 1];
  1302.             flyings[flyings.length - 1] = temp;
  1303.  
  1304.             flyings = Arrays.copyOf(flyings, flyings.length - 1); // 删除最后一个飞行物(即被击中的)
  1305.  
  1306.             // 检查one的类型(敌人加分,奖励获取)
  1307.             if (one instanceof Enemy) { // 检查类型,是敌人,则加分
  1308.                 Enemy e = (Enemy) one; // 强制类型转换
  1309.                 score += e.getScore(); // 加分
  1310.             } else { // 若为奖励,设置奖励
  1311.                 Award a = (Award) one;
  1312.                 int type = a.getType(); // 获取奖励类型
  1313.                 switch (type) {
  1314.                 case Award.DOUBLE_FIRE:
  1315.                     hero.addDoubleFire(); // 设置双倍火力
  1316.                     break;
  1317.                 case Award.LIFE:
  1318.                     hero.addLife(); // 设置加命
  1319.                     break;
  1320.                 }
  1321.             }
  1322.         }
  1323.     }
  1324.  
  1325.     /**
  1326.      * 随机生成飞行物
  1327.      *
  1328.      * @return 飞行物对象
  1329.      */
  1330.     public static FlyingObject nextOne() {
  1331.         Random random = new Random();
  1332.         int type = random.nextInt(20); // [0,20)
  1333.         if (type < 4) {
  1334.             return new Bee();
  1335.         } else {
  1336.             return new Airplane();
  1337.         }
  1338.     }
  1339.  
  1340. }
  1341.  

回复 "游戏"

这儿你可以回复上面这条便签

captcha