[Java] 飞机大战 →→→→→进入此内容的聊天室

来自 , 2020-08-06, 写在 Java, 查看 159 次.
URL http://www.code666.cn/view/c8cbd669
  1. public class World extends JPanel {
  2.         public static final int WIDTH = 400;//public static final int WIDTH=400;
  3.         public static final int HEIGHT = 700;//public static final int HEIGHT=700;
  4.         private Sky sky = new Sky();//private Sky sky=new Sky();
  5.         private Hero hero = new Hero();//private Hero hero=new Hero();
  6.         private FlyingObject[] enemies = {}; // 敌人数组 private FlyingObject []enemies={};
  7.         private Bullet[] bullets = {}; // 子弹数组 private Bullet[]bullets={};
  8.  
  9.         public static final int START = 0;//public static final int START=0;
  10.         public static final int RUNNING = 1;//public static final int RUNNING=1;
  11.         public static final int PAUSE = 2;//public static final int PAUSE=2;
  12.         public static final int GAME_OVER = 3;//public static final int GAME_OVER=3;
  13.         private int state = START;//private int state=START;
  14.  
  15.         public static BufferedImage start;//public static BufferedImage start;
  16.         public static BufferedImage pause;//public static BufferedImage pause;
  17.         public static BufferedImage gameover;//public static BufferedImage gameover;
  18.         static {//static{静态块
  19.                 start = Images.loadImage("start.png");//start=Images.loadImage("start.png);
  20.                 pause = Images.loadImage("pause.png");//pause=Images.loadImage("pause.png");
  21.                 gameover = Images.loadImage("gameover.png");//gameover=Images.loadImage("gameover.png");
  22.         }
  23.  
  24.         public FlyingObject nextOne() {//public FlyingObject nextOne(){
  25.                 Random rand = new Random();//Random rand=new Random();
  26.                 int type = rand.nextInt(20);//int type=rand .nextInt(20);
  27.                 if (type < 5) {//if(type<5){
  28.                         return new Bee();//return new Bee();
  29.                 } else if (type < 20) {//else if (type<20){
  30.                         return new Airplane();//return new Airplane();
  31.                 } else {//else
  32.                         return new BigAirplane();//return new BigAirplane();
  33.                 }
  34.         }
  35.  
  36.         int flyEnterIndex = 0;//int flyEnterIndex=0;
  37.  
  38.         public void enterAction() {//public void enterAction(){
  39.                 flyEnterIndex++;//flyEnterIndex++;
  40.                 if (flyEnterIndex % 40 == 0) {//if(flyEnterIndex%40==0){
  41.                         FlyingObject obj = nextOne();//FlyingObject obj=nextOne;
  42.                         enemies = Arrays.copyOf(enemies, enemies.length + 1);//enemies =Arrays.copyOf(enemies,enemies.length+1);
  43.                         enemies[enemies.length - 1] = obj;//enemies [enemies.length-1]=obj;
  44.                 }
  45.         }
  46.  
  47.         public void stepAction() {//public void stepAction(){
  48.                 sky.step();//sky.step();
  49.                 for (int i = 0; i < enemies.length; i++) {//for(int i=0;i<enemies.length;i++)
  50.                         enemies[i].step();//enemies[i].step();
  51.                 }
  52.                 for (int i = 0; i < bullets.length; i++) {//for(int i=0;i<bullets.length;i++)
  53.                         bullets[i].step();//bullets[i].step();
  54.                 }
  55.         }
  56.  
  57.         int shootIndex = 0;//int shootIndex=0;
  58.  
  59.         public void shootAction() {//public void shootAction(){
  60.                 shootIndex++;//shootIndex++;
  61.                 if (shootIndex % 30 == 0) {//if(shootIndex%30==0){
  62.                         Bullet[] bs = hero.shoot();//Bullets[]bs=hero.shoot();
  63.                         bullets = Arrays.copyOf(bullets, bullets.length + bs.length);
  64.                         System.arraycopy(bs, 0, bullets, bullets.length - bs.length, bs.length);
  65.                 }//bullets=Arrays.copyOf(bullets,bullets.length+bs.length);
  66.         }//System.arraycopy(bs,0,bullets,bullets.length-bs.length,bs.length);
  67.  
  68.         private int score = 0;//private int score=0;
  69.  
  70.         public void bangAction() {//public void bangAction()
  71.                 for (int i = 0; i < bullets.length; i++) {//for(int i=0;i<bullets.length;i++){
  72.                         Bullet b = bullets[i];//Bullets b=bullets[i]
  73.                         for (int j = 0; j < enemies.length; j++) {//for(int j=0;j<enemies.length;j++){
  74.                                 FlyingObject f = enemies[j];//FlyingObject f=enemies[j];
  75.                                 if (b.isLife() && f.isLife() && b.hit(f)) {//if(b.islife()&&f.isLife()&&b.hit(f)){
  76.                                         b.goDead();//b.goDead();
  77.                                         f.goDead();//f.goDead();
  78.                                         if (f instanceof Enemy) {//if(f instanceof Enemy){
  79.                                                 Enemy e = (Enemy) f;//Enemy e=(Enemy) f;
  80.                                                 score += e.getScore();//score+=e.getScore();
  81.                                         }
  82.                                         if (f instanceof Award) {//if(f instanceof Award){
  83.                                                 Award a = (Award) f;//Award a=(Award)f;
  84.                                                 int type = a.getType();//int type=a.getType();
  85.                                                 switch (type) {//switch(type){
  86.                                                 case Award.DOUBLE_FIRE://case Award.DOUBLE_FIRE:
  87.                                                         hero.addDoubleFire();//hero.addDoubleFire();
  88.                                                         break;//break;
  89.                                                 case Award.LIFE://case Award.LIFE:
  90.                                                         hero.addLife();//hero.addLife
  91.                                                         break;//break;
  92.                                                 }
  93.                                         }
  94.  
  95.                                 }
  96.                         }
  97.                 }
  98.         }
  99.  
  100.  

回复 "飞机大战"

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

captcha