java课程设计源码(游戏:急速生存)

  1. package cn.edu.ahu.RapidSurvial;  
  2.   
  3. import java.awt.Graphics;  
  4. import java.awt.Image;  
  5. import java.awt.Rectangle;  
  6. import java.awt.Toolkit;  
  7. import java.util.List;  
  8.   
  9. /** 
  10.  * 炸弹类 
  11.  * @author Your风之恋(AHU - java - 课程设计) 
  12.  * 
  13.  */  
  14.   
  15. public class Bomb {  
  16.     public static final int BWIDTH = 20;    //炸弹宽度  
  17.     public static final int BHEIGHT = 5;    //炸弹高度  
  18.     public static final int BXSPEED = 10;   //炸弹x方向上的速度  
  19.     public static final int BYSPEED = 10;   //炸弹y方向上的速度  
  20.       
  21.     int x;                                  //炸弹的左上角 x点的位置  
  22.     int y;                                  //炸弹的左上角 y点的位置  
  23.     int w;                                  //炸弹的宽度  
  24.     int h;                                  //炸弹的高度  
  25.       
  26.     RapidSurvialManager rsm;                //持有RapidSurvialManager的引用  
  27.     Fighter.Direction dir;                  //炸弹的方向  
  28.     boolean isLive = true;                  //是否有效  
  29.     boolean isEnemy;                        //区分敌我的量  
  30.     public static int sid = 0;              //记录战果  
  31.       
  32.     private static Toolkit tk =   
  33.         Toolkit.getDefaultToolkit();  
  34.     private static Image[] bombImage = null;  
  35.     static {  
  36.         bombImage = new Image[] {  
  37.                 tk.getImage(Bomb.class.getClassLoader().getResource("images/Bomb_LTR.png")),  
  38.                 tk.getImage(Bomb.class.getClassLoader().getResource("images/Bomb_RTL.png"))   
  39.         };  
  40.     }  
  41.       
  42.     //构造方法  
  43.     public Bomb(int x, int y) {  
  44.         this.x = x;  
  45.         this.y = y;  
  46.         this.w = BWIDTH;  
  47.         this.h = BHEIGHT;  
  48.     }  
  49.       
  50.     //构造方法  
  51.     public Bomb(int x, int y, RapidSurvialManager rsm) {  
  52.         this(x, y);  
  53.         this.rsm = rsm;  
  54.     }  
  55.       
  56.     //构造方法  
  57.     public Bomb(int x, int y, RapidSurvialManager rsm, Fighter.Direction dir, boolean isEnemy) {  
  58.         this(x, y, rsm);  
  59.         this.dir = dir;  
  60.         this.isEnemy = isEnemy;  
  61.     }  
  62.       
  63.     //画出自己的方法  
  64.     public void draw(Graphics g) {  
  65.         if(!isLive) {  
  66.             rsm.bombs.remove(this);  
  67.             return;  
  68.         }  
  69.         if(!isEnemy) {  
  70.             g.drawImage(bombImage[0], x, y, null);  
  71.               
  72.         } else {  
  73.       
  74.             g.drawImage(bombImage[1], x, y, null);  
  75.         }  
  76.           
  77.         setPostion();  
  78.     }  
  79.       
  80.     //根据方向计算下一重画的位置  
  81.     private void setPostion() {  
  82.         switch(dir) {  
  83.         case LTR:  
  84.             x += BXSPEED;  
  85.             break;  
  86.         case RTL:  
  87.             x -= BXSPEED;  
  88.             break;  
  89.         }  
  90.           
  91.         //出界处理  
  92.         if(x < 0 || y < 0 ||  
  93.                 x > RapidSurvialManager.MAINWIDTH ||  
  94.                 y > RapidSurvialManager.MAINHEIGHT) {  
  95.               
  96.             isLive = false;  
  97.         }  
  98.           
  99.     }  
  100.       
  101.     //返回自己的大小  
  102.     public Rectangle getRect() {  
  103.         return new Rectangle(x, y, w, h);  
  104.     }  
  105.       
  106.     //此方法用于与敌机一个子弹的碰撞检测  
  107.     public boolean hitBomb(Bomb b) {  
  108.         if(this.isLive   
  109.                 && this.getRect().intersects(b.getRect())   
  110.                     && b.isLive   
  111.                         && b.isEnemy != this.isEnemy) {  
  112.               
  113.             Explode e = new Explode(x + BWIDTH, y + BHEIGHT, rsm);  
  114.             rsm.explodes.add(e);  
  115.               
  116.   
  117.   
  118.             this.isLive = false;  
  119.             b.isLive = false;  
  120.             return true;  
  121.               
  122.         }  
  123.           
  124.         return false;  
  125.     }  
  126.       
  127.     //此方法用于与敌机一群子弹的碰撞检测  
  128.     public boolean hitBombs(List<Bomb> bombs) {  
  129.         for(int i = 0; i < bombs.size(); i++) {  
  130.             if(hitBomb(bombs.get(i))){  
  131.                 return true;  
  132.             }  
  133.         }  
  134.         return false;  
  135.     }  
  136.       
  137.     public boolean hitFighter(Fighter f) {  
  138.         if(this.isLive   
  139.                 && this.getRect().intersects(f.getRect())   
  140.                     && f.isLive   
  141.                         && f.isEnemy != this.isEnemy) {  
  142.               
  143.             Explode e = new Explode(x + BWIDTH, y + BHEIGHT, rsm);  
  144.             rsm.explodes.add(e);  
  145.               
  146.             if(!f.isEnemy) {  
  147.                 f.setLifeValue(f.getLifeValue() - 1);  
  148.                 if(f.getLifeValue() <= 0) {  
  149.                     f.isLive = false;  
  150.                 }  
  151.             } else {  
  152.                 f.isLive = false;  
  153.                 sid ++;  
  154.             }  
  155.   
  156.             this.isLive = false;  
  157.             return true;  
  158.               
  159.         }  
  160.           
  161.         return false;  
  162.     }  
  163.       
  164.     //此方法用于与一群战机的碰撞检测  
  165.     public boolean hitFighters(List<Fighter> enemys) {  
  166.         for(int i = 0; i < enemys.size(); i++) {  
  167.             if(hitFighter(enemys.get(i))){  
  168.                 return true;  
  169.             }  
  170.         }  
  171.         return false;  
  172.     }  
  173.       
  174.       
  175. }  
  1. package cn.edu.ahu.RapidSurvial;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Graphics;  
  5.   
  6. /** 
  7.  * 爆炸类 
  8.  * @author Your风之恋(AHU - java - 课程设计) 
  9.  * 
  10.  */  
  11. public class Explode {  
  12.       
  13.     int x;                                          //产生爆炸的x点坐标  
  14.     int y;                                          //产生爆炸的y点坐标  
  15.     RapidSurvialManager rsm;                        //持有RapidSurvialManager的引用  
  16.     boolean isLive = true;                          //是否有效,是:true,否:false  
  17.     int [] diameter = {47122334126};    //模拟爆炸产生的半径  
  18.     int step = 0;                                   //爆炸的步数  
  19.       
  20.     //构造函数  
  21.     public Explode(int x, int y, RapidSurvialManager rsm) {  
  22.         this.x = x;  
  23.         this.y = y;  
  24.         this.rsm = rsm;  
  25.     }  
  26.       
  27.     //画出自己的方法  
  28.     public void draw(Graphics g) {  
  29.         if(!isLive) {  
  30.             rsm.explodes.remove(this);  
  31.             return;  
  32.         }  
  33.         Color c = g.getColor();  
  34.         g.setColor(Color.ORANGE);  
  35.           
  36.         if(step == diameter.length) {  
  37.             isLive = false;  
  38.             step = 0;  
  39.             return;  
  40.         }  
  41.         g.fillOval(x, y, diameter[step], diameter[step]);  
  42.         g.setColor(c);  
  43.           
  44.         step ++;  
  45.     }  
  46.       
  47. }  
  48.    

  1. package cn.edu.ahu.RapidSurvial;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Graphics;  
  5. import java.awt.Image;  
  6. import java.awt.Rectangle;  
  7. import java.awt.Toolkit;  
  8. import java.awt.event.KeyEvent;  
  9.   
  10. public class Fighter {  
  11.   
  12.     public static final int FWIDTH = 70;    //战机宽度  
  13.     public static final int FHEIGHT = 10;   //战机高度  
  14.     public static final int FXSPEED = 4;    //战机在x方向上的速度  
  15.     public static final int FYSPEED = 4;    //战机在y方向上的速度  
  16.       
  17.     int x;                                  //战机在x方向上的位置  
  18.     int y;                                  //战机在y方向上的位置  
  19.     int w;                                  //战机的宽度  
  20.     int h;                                  //战机的高度  
  21.       
  22.     Direction dir;                          //方向  
  23.     RapidSurvialManager rsm;                //持有RapidSurvialManager的引用  
  24.     private boolean isUp = false;           //键盘↑ 是否被按下,初始化为false  
  25.     private boolean isDown = false;         //键盘↓ 是否被按下,初始化为false  
  26.     private boolean isRight = false;        //键盘→ 是否被按下,初始化为false  
  27.     private boolean isLeft = false;         //键盘← 是否被按下,初始化为false  
  28.     enum Direction {LTR, RTL};              //两个方向,LTR:从左向右,RTL:从右向左  
  29.     boolean isEnemy;                        //区分敌我的量,是敌人:true,否则为false  
  30.     boolean isLive = true;                  //判读是否存活,活着:true,否则为false  
  31.     private int lifeValue = 10;             //我方战机的生命值  
  32.     int speed;                              //产生一个速度值  
  33.     BloodBar bb = new BloodBar();           //可视的血量  
  34.     static int isRelive = 0;                //是否复活  
  35.     private int superStarCounts = 1;        //初始大决数  
  36.     private static Toolkit tk =   
  37.         Toolkit.getDefaultToolkit();  
  38.     private static Image[] fighterImage = null;  
  39.     static {  
  40.         fighterImage = new Image[] {  
  41.                 tk.getImage(Fighter.class.getClassLoader().getResource("images/EnemysFighter.png")),  
  42.                 tk.getImage(Fighter.class.getClassLoader().getResource("images/MyFighter_LTR.png"))   
  43.         };  
  44.     }  
  45.       
  46.     //构造函数  
  47.     Fighter(int x , int y,boolean isEnemy) {  
  48.         this.x = x;  
  49.         this.y = y;  
  50.         this.w = FWIDTH;  
  51.         this.h = FHEIGHT;  
  52.         this.isEnemy = isEnemy;  
  53.     }  
  54.       
  55.     //构造函数  
  56.     Fighter(int x, int y, boolean isEnemy, RapidSurvialManager rsm) {  
  57.         this(x, y, isEnemy);  
  58.         this.rsm = rsm;  
  59.         this.dir = Direction.LTR;  
  60.     }  
  61.       
  62.     //构造函数  
  63.     Fighter(int x, int y,boolean isEnemy, RapidSurvialManager rsm, Direction dir, int speed) {  
  64.         this(x, y, isEnemy, rsm);  
  65.         this.dir = dir;  
  66.         this.speed = speed;  
  67.     }  
  68.       
  69.     //设置lifeValue值  
  70.     public void setLifeValue(int lifeValue) {  
  71.         this.lifeValue = lifeValue;  
  72.     }  
  73.       
  74.     //得到lifeValue值  
  75.     public int getLifeValue() {  
  76.         return lifeValue;  
  77.     }  
  78.   
  79.     //设置superStarCounts值  
  80.     public void setSuperStarCounts(int superStarCounts) {  
  81.         this.superStarCounts = superStarCounts;  
  82.     }  
  83.   
  84.     //得到superStarCounts值  
  85.     public int getSuperStarCounts() {  
  86.         return superStarCounts;  
  87.     }  
  88.   
  89.     //用此方画出战机  
  90.     public void draw(Graphics g) {  
  91.           
  92.           
  93.         if(!isLive) {  
  94.             if(isEnemy) {  
  95.                 rsm.enemys.remove(this);  
  96.             }  
  97.             return;  
  98.         }  
  99.           
  100.         if(isEnemy) {     
  101.             g.drawImage(fighterImage[0], x, y, null);  
  102.               
  103.             go();  
  104.         } else {          
  105.             g.drawImage(fighterImage[1], x, y, null);  
  106.   
  107.             setPostion();  
  108.   
  109.             bb.draw(g);  
  110.         }  
  111.               
  112.     }  
  113.       
  114.     //让敌军动起来的方法  
  115.     public void go() {  
  116.         switch(dir) {  
  117.         case LTR:  
  118.             x += speed;  
  119.             break;  
  120.         case RTL:  
  121.             x -= speed;  
  122.             break;  
  123.         }  
  124.     }  
  125.       
  126.     //对按键被按下经行处理  
  127.     public void keyPressed(KeyEvent e) {  
  128.         int key = e.getKeyCode();  
  129.         switch(key) {  
  130.             case KeyEvent.VK_UP:  
  131.                 isUp = true;  
  132.                 break;  
  133.             case KeyEvent.VK_RIGHT:  
  134.                 isRight = true;  
  135.                 break;  
  136.             case KeyEvent.VK_DOWN:  
  137.                 isDown = true;  
  138.                 break;  
  139.             case KeyEvent.VK_LEFT:  
  140.                 isLeft = true;  
  141.                 break;  
  142.         }  
  143.           
  144.         setPostion();  
  145.     }  
  146.       
  147.     //对按键被释放经行处理  
  148.     public void keyReleased(KeyEvent e) {  
  149.         int key = e.getKeyCode();  
  150.         switch(key) {  
  151.             case KeyEvent.VK_UP:  
  152.                 isUp = false;  
  153.                 break;  
  154.             case KeyEvent.VK_RIGHT:  
  155.                 isRight = false;  
  156.                 break;  
  157.             case KeyEvent.VK_DOWN:  
  158.                 isDown = false;  
  159.                 break;  
  160.             case KeyEvent.VK_LEFT:  
  161.                 isLeft = false;  
  162.                 break;  
  163.             case KeyEvent.VK_Q:  
  164.                 fire();  
  165.                 break;  
  166.             case KeyEvent.VK_F2:  
  167.                 if(!isLive) {  
  168.                     isLive = true;  
  169.                     lifeValue = 10;  
  170.                     isRelive ++;  
  171.                 }  
  172.                       
  173.                 break;  
  174.             case KeyEvent.VK_W:  
  175.                 if(getSuperStarCounts() == 0) {  
  176.                     return;  
  177.                 }  
  178.                 if(!isLive) {  
  179.                     return;  
  180.                 }  
  181.                 setSuperStarCounts(getSuperStarCounts() - 1);  
  182.                 superFire();  
  183.                 break;  
  184.         }  
  185.     }  
  186.       
  187.   
  188.       
  189.     //根据按键的组合确定下一次的位置  
  190.     private void setPostion() {  
  191.   
  192.         if(isUp && !isRight && !isDown && !isLeft) {  
  193.             y -= FYSPEED;  
  194.         }  
  195.         if(!isUp && isRight && !isDown && !isLeft) {  
  196.             x += FXSPEED;  
  197.         }  
  198.         if(!isUp && !isRight && isDown && !isLeft) {  
  199.             y += FYSPEED;  
  200.         }  
  201.         if(!isUp && !isRight && !isDown && isLeft) {  
  202.             x -= FXSPEED;  
  203.         }  
  204.         if(isUp && isRight && !isDown && !isLeft) {  
  205.             x += FXSPEED;  
  206.             y -= FYSPEED;  
  207.         }  
  208.         if(isUp && !isRight && !isDown && isLeft) {  
  209.             y -= FYSPEED;  
  210.             x -= FXSPEED;  
  211.         }  
  212.         if(!isUp && isRight && isDown && !isLeft) {  
  213.             x += FXSPEED;  
  214.             y += FYSPEED;  
  215.         }  
  216.         if(!isUp && !isRight && isDown && isLeft) {  
  217.             x -= FXSPEED;  
  218.             y += FYSPEED;         
  219.         }  
  220.           
  221.         //对战机的出界处理  
  222.         if(x <= 0) {  
  223.             x = 0;  
  224.         }  
  225.         if(y < 45) {  
  226.             y = 45;  
  227.         }  
  228.         if(x + Fighter.FWIDTH > RapidSurvialManager.MAINWIDTH) {  
  229.             x = RapidSurvialManager.MAINWIDTH - Fighter.FWIDTH;  
  230.         }  
  231.         if(y + Fighter.FHEIGHT + 52> RapidSurvialManager.MAINHEIGHT) {  
  232.             y = RapidSurvialManager.MAINHEIGHT - Fighter.FHEIGHT - 52;  
  233.         }  
  234.               
  235.     }  
  236.       
  237.     //战机的开火处理  
  238.     public Bomb fire() {      
  239.         if(!isLive) {  
  240.             return null;  
  241.         }     
  242.         int x, y;  
  243.         if(!isEnemy) {  
  244.             x = this.x + Fighter.FWIDTH;  
  245.             y = this.y + Fighter.FHEIGHT / 2 - Bomb.BHEIGHT / 2 + 22;  
  246.         } else {  
  247.             x = this.x;  
  248.             y = this.y + Fighter.FHEIGHT / 2 - Bomb.BHEIGHT / 2 + 22;  
  249.         }     
  250.         Bomb b = new Bomb(x, y, rsm, dir, isEnemy);  
  251.         rsm.bombs.add(b);  
  252.         return b;  
  253.     }  
  254.       
  255.     //放大决的方法  
  256.     public SuperLine superFire() {  
  257.         if(!isLive) {  
  258.             return null;  
  259.         }  
  260.           
  261.         SuperLine s = new SuperLine(x, rsm, dir);  
  262.         rsm.superLines.add(s);  
  263.         return s;  
  264.     }  
  265.           
  266.     //得到自己的大小,用于碰撞检测  
  267.     public Rectangle getRect() {  
  268.         return new Rectangle(x, y, w, h*4);  
  269.     }  
  270.       
  271.     //血块类  
  272.     private class BloodBar {  
  273.         public void draw(Graphics g) {  
  274.             Color c = g.getColor();  
  275.             if(lifeValue <= 5) {  
  276.                 g.setColor(Color.RED);  
  277.                 g.drawRect(x+20, y-20, w * 2 / 34);  
  278.                   
  279.                 int w = FWIDTH * lifeValue / 10;  
  280.                 g.fillRect(x+20, y-20, w * 2 / 34);  
  281.                 g.drawString(""+ lifeValue, x, y-13);  
  282.             } else {  
  283.                 g.setColor(Color.GREEN);  
  284.                 g.drawRect(x+20, y-20, w * 2 / 34);  
  285.                   
  286.                 int w = FWIDTH * lifeValue / 10;  
  287.                 g.fillRect(x+20, y-20, w * 2 / 34);  
  288.                 g.drawString(""+ lifeValue, x, y-13);  
  289.                   
  290.                 g.setColor(c);  
  291.             }  
  292.         }  
  293.     }  
  294.       
  295.     //吃SuperStar的方法  
  296.     public boolean eat(SuperStar ss) {  
  297.         if(this.isLive   
  298.                 && ss.isLive  
  299.                     && this.getRect().intersects(ss.getRect())) {  
  300.             ss.isLive = false;  
  301.             setSuperStarCounts(getSuperStarCounts() + 1);  
  302.             return true;  
  303.         }  
  304.         return false;  
  305.     }  
  306.       
  307.     public int getScore() {  
  308.         return Bomb.sid - isRelive * 50;  
  309.     }  
  310.       
  311. }  

  1. package cn.edu.ahu.RapidSurvial;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Graphics;  
  5. import java.awt.Rectangle;  
  6. import java.util.List;  
  7. /** 
  8.  * 游戏中的大决类 
  9.  * @author Your风之恋(AHU - java - 课程设计) 
  10.  * 
  11.  */  
  12.   
  13. public class SuperLine {  
  14.     public static final int SXSPEED = 10;       //大决的移动速度  
  15.     public static final int LWIDTH = 10;        //大决的显示宽度  
  16.       
  17.     int x;                                      //大决开始x坐标  
  18.     int y;                                      //大决开始y坐标  
  19.     Fighter.Direction dir;                      //方向  
  20.     RapidSurvialManager rsm;                    //持有RapidSurvialManager的引用  
  21.     boolean isLive = true;                      //是否有效,是:true,否:false  
  22.       
  23.     //构造函数  
  24.     SuperLine(int x, RapidSurvialManager rsm) {  
  25.         this.x = x;  
  26.         this.y = LWIDTH;  
  27.         this.rsm = rsm;  
  28.     }  
  29.       
  30.     //构造函数  
  31.     SuperLine(int x, RapidSurvialManager rsm, Fighter.Direction dir) {  
  32.         this(x, rsm);  
  33.         this.dir = dir;  
  34.     }  
  35.       
  36.     //画方法  
  37.     public void draw(Graphics g) {  
  38.         if(!isLive) {  
  39.             rsm.superLines.remove(this);  
  40.             return;  
  41.         }  
  42.           
  43.         Color c = g.getColor();  
  44.         g.setColor(Color.PINK);  
  45.         g.fillRect(x, 0, LWIDTH, RapidSurvialManager.MAINHEIGHT);  
  46.         g.setColor(c);  
  47.           
  48.         go();  
  49.     }  
  50.       
  51.     //移动自己的方法  
  52.     public void go() {  
  53.         switch(dir) {  
  54.         case LTR:  
  55.             x += SXSPEED;  
  56.             break;  
  57.         case RTL:  
  58.             x -= SXSPEED;  
  59.             break;  
  60.         }  
  61.           
  62.         if(x >= RapidSurvialManager.MAINWIDTH) {  
  63.             isLive = false;  
  64.         }  
  65.     }  
  66.       
  67.     //拿到自己的大小,为碰撞检测服务  
  68.     public Rectangle getRect() {  
  69.         return new Rectangle(x, 0, y,RapidSurvialManager.MAINHEIGHT);  
  70.     }  
  71.       
  72.     //与一个战机经行碰撞检测  
  73.     public boolean hitFighter(Fighter f) {  
  74.         if(this.isLive   
  75.                 && this.getRect().intersects(f.getRect())  
  76.                     && f.isLive) {  
  77.             Explode e = new Explode(f.x, f.y,rsm);  
  78.             rsm.explodes.add(e);  
  79.               
  80.             f.isLive = false;  
  81.             Bomb.sid ++;  
  82.             return true;  
  83.         }  
  84.         return false;  
  85.     }  
  86.       
  87.     //与一群战机经行碰撞检测  
  88.     public boolean hitFighters(List<Fighter> enemys) {  
  89.         for(int i = 0; i < enemys.size(); i++) {  
  90.             if(this.hitFighter(enemys.get(i))) {  
  91.                 return true;  
  92.             }  
  93.         }  
  94.         return false;  
  95.     }  
  96. }  

  1. package cn.edu.ahu.RapidSurvial;  
  2.   
  3. import java.awt.Graphics;  
  4. import java.awt.Image;  
  5. import java.awt.Rectangle;  
  6. import java.awt.Toolkit;  
  7.   
  8. /** 
  9.  * 超星类 
  10.  * @author Your风之恋(AHU - java - 课程设计) 
  11.  * 
  12.  */  
  13. public class SuperStar {  
  14.     public static final int SSWIDTH = 20;       //超星的宽度  
  15.     public static final int SSHEIGHT = 20;      //超星的高度  
  16.       
  17.     int x;                                      //超星初始化x点坐标  
  18.     int y;                                      //超星初始化y点坐标  
  19.     int w;                                      //超星的宽度  
  20.     int h;                                      //超星的高度  
  21.     int speed;                                  //超星的移动速度  
  22.     boolean isLive = true;                      //是否有效,是:true,否:false  
  23.     RapidSurvialManager rsm;                    //持有RapidSurvialManager的引用  
  24.     Fighter.Direction dir;                      //方向  
  25.       
  26.     private static Toolkit tk =   
  27.         Toolkit.getDefaultToolkit();  
  28.     private static Image[] superStarImage = null;  
  29.     static {  
  30.         superStarImage = new Image[] {  
  31.                 tk.getImage(SuperStar.class.getClassLoader().getResource("images/SuperStar_RTL.png")),  
  32.         };  
  33.     }  
  34.       
  35.     //构造方法  
  36.     public SuperStar(int x, int y, RapidSurvialManager rsm, Fighter.Direction dir,int speed) {  
  37.         this.x = x;  
  38.         this.y = y;  
  39.         this.speed = speed;  
  40.         this.w = SSWIDTH;  
  41.         this.h = SSHEIGHT;  
  42.         this.rsm = rsm;  
  43.         this.dir = dir;  
  44.     }  
  45.       
  46.     //画方法  
  47.     public void draw(Graphics g) {  
  48.         if(!isLive) {  
  49.             rsm.superStars.remove(this);  
  50.             return;  
  51.         }  
  52.           
  53.         g.drawImage(superStarImage[0], x, y, null);  
  54.           
  55.         go();  
  56.     }  
  57.       
  58.     //移动自己的方法  
  59.     public void go() {  
  60.         switch(dir) {  
  61.         case LTR:  
  62.             x += speed;  
  63.             break;  
  64.         case RTL:  
  65.             x -= speed;  
  66.             break;  
  67.         }  
  68.           
  69.         if(x <= 0) {  
  70.             isLive = false;  
  71.         }  
  72.     }  
  73.   
  74.     //得到自己的大小  
  75.     public Rectangle getRect() {  
  76.         return new Rectangle(x, y, w, h);  
  77.     }  
  78.   
  79. }  

  1. package cn.edu.ahu.RapidSurvial;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Frame;  
  6. import java.awt.Graphics;  
  7. import java.awt.Image;  
  8. import java.awt.event.KeyAdapter;  
  9. import java.awt.event.KeyEvent;  
  10. import java.awt.event.WindowAdapter;  
  11. import java.awt.event.WindowEvent;  
  12. import java.util.ArrayList;  
  13. import java.util.List;  
  14. import java.util.Random;  
  15.   
  16. import cn.edu.ahu.RapidSurvial.Fighter.Direction;  
  17.   
  18. /** 
  19.  * 游戏的主类,用于管理其他图形元素 
  20.  * @author Your风之恋(AHU - java - 课程设计) 
  21.  * 
  22.  */  
  23.   
  24. public class RapidSurvialManager extends Frame{  
  25.     private static final long serialVersionUID = 1L;  
  26.       
  27.     public static final int MAINWIDTH = 1200;       //主窗口的宽度  
  28.     public static final int MAINHEIGHT = 600;       //主窗口的高度  
  29.     public static final int MAINXPOINT = 30;        //主窗口初始化x点的位置  
  30.     public static final int MAINYPOINT = 50;        //主窗口初始化y点的位置  
  31.     public static Random r= new Random();           //添加随机数产生器,用于控制敌方的AI  
  32.     Image bufferImage = null;                       //双缓冲的缓冲图像,用双缓冲取消屏幕闪烁  
  33.     public static int time = 0;  
  34.     Fighter myFighter = new Fighter(50300falsethis);      //创建主战机  
  35.     List<Bomb> bombs = new ArrayList<Bomb>();                   //炮弹集合  
  36.     List<Fighter> enemys = new ArrayList<Fighter>();            //敌机集合  
  37.     List<Explode> explodes = new ArrayList<Explode>();          //爆炸集合  
  38.     List<SuperLine> superLines = new ArrayList<SuperLine>();    //大决集合  
  39.     List<SuperStar> superStars = new ArrayList<SuperStar>();    //超星集合  
  40.       
  41.       
  42.     //主线程入口  
  43.     public  static void main(String[] args) {  
  44.         RapidSurvialManager rsm = new RapidSurvialManager();  
  45.         rsm.launchGameFrame();  
  46.     }  
  47.       
  48.     //重写paint()方法  
  49.     public void paint(Graphics g) {  
  50.         //显示一些基本信息,便于判断集合中的元素是否被删除  
  51.   
  52.         Color c = g.getColor();  
  53.         g.setColor(Color.ORANGE);  
  54.           
  55.         Font font = g.getFont();  
  56.         g.setFont(new Font("SansSerif", Font.ROMAN_BASELINE, 15));  
  57.         g.drawString("游戏时间: " + showTime(), 2550);  
  58.         g.drawString("消灭敌机: " + Bomb.sid, 2570);  
  59.         g.drawString("剩余绝招: " + myFighter.getSuperStarCounts(), 15050);  
  60.         g.drawString("游戏得分: " + myFighter.getScore(),15070);  
  61.           
  62.         g.setFont(font);  
  63.         g.setColor(Color.RED);  
  64.         g.drawLine(MAINXPOINT + MAINWIDTH * 4 / 5, MAINYPOINT - 25,  
  65.                    MAINXPOINT + MAINWIDTH * 4 / 5, MAINYPOINT + MAINHEIGHT);  
  66.         g.setColor(c);  
  67.         //画出主战机  
  68.         myFighter.draw(g);  
  69.   
  70.         for(int i = 0; i < superStars.size(); i++) {  
  71.             SuperStar ss = superStars.get(i);  
  72.             myFighter.eat(ss);  
  73.             ss.draw(g);  
  74.         }  
  75.           
  76.         //判断如果超星数少于0,则添加  
  77.         if (superStars.size() <= 0) {  
  78.             for(int i = 0; i < r.nextInt(2); i++) {  
  79.                 boolean flag1 = true;  
  80.                 boolean flag2 = true;  
  81.                 int x = 0, y = 0;  
  82.                 while(flag1 || flag2) {  
  83.                     int t1 = r.nextInt(3000);  
  84.                     int t2 = r.nextInt(1000);  
  85.                     if(t1 <= 1100 && t1 >= 1000) {  
  86.                         x = t1;  
  87.                         flag1 = false;  
  88.                     }   
  89.                     if(t2 <= 550 && t2 >= 30) {  
  90.                         y = t2;  
  91.                         flag2 = false;  
  92.                     }  
  93.                 }  
  94.                 if(r.nextInt(500) > 495) {  
  95.                     superStars.add(new SuperStar(x, y, this, Direction.RTL, r.nextInt(6) + 4));  
  96.                 }  
  97.             }  
  98.         }  
  99.           
  100.         //画出大决  
  101.         for(int i = 0; i < superLines.size(); i++) {  
  102.             SuperLine s = superLines.get(i);  
  103.             s.hitFighters(enemys);  
  104.             //s.hitBombs(bombs);  
  105.             s.draw(g);  
  106.         }  
  107.           
  108.         //如果敌机数量少于3,则继续添加  
  109.         if(enemys.size() <= 2) {  
  110.             for(int i = 0; i < r.nextInt(10); i++) {  
  111.                 boolean flag1 = true;  
  112.                 boolean flag2 = true;  
  113.                 int x = 0, y = 0;  
  114.                 while(flag1 || flag2) {  
  115.                     int t1 = r.nextInt(3000);  
  116.                     int t2 = r.nextInt(1000);  
  117.                     if(t1 <= 1100 && t1 >= 1000) {  
  118.                         x = t1;  
  119.                         flag1 = false;  
  120.                     }   
  121.                     if(t2 <= 550 && t2 >= 30) {  
  122.                         y = t2;  
  123.                         flag2 = false;  
  124.                     }  
  125.                 }         
  126.                   
  127.                 enemys.add(new Fighter(x, y, truethis, Direction.RTL,r.nextInt(6) + 4));  
  128.             }  
  129.         }  
  130.           
  131.         //画出炸弹  
  132.         for(int i = 0; i < bombs.size(); i++) {  
  133.             Bomb b = bombs.get(i);  
  134.               
  135.             b.hitFighters(enemys);  
  136.             b.hitFighter(myFighter);  
  137.             b.hitBombs(bombs);  
  138.             b.draw(g);  
  139.         }  
  140.           
  141.         //画出敌机  
  142.         for(int i = 0; i < enemys.size(); i++) {  
  143.             Fighter f = enemys.get(i);  
  144.               
  145.             if(f.x <= 0) {  
  146.                 f.isLive = false;  
  147.             }  
  148.               
  149.             if(f.isEnemy) {  
  150.                 if((r.nextInt(50)) > 48) {  
  151.                     f.fire();  
  152.                 }  
  153.             }  
  154.             f.draw(g);  
  155.         }  
  156.           
  157.         //画出爆炸  
  158.         for(int i = 0; i < explodes.size(); i++) {  
  159.             Explode e = explodes.get(i);  
  160.             e.draw(g);  
  161.         }  
  162.     }  
  163.       
  164.     //统一的主界面的调用函数  
  165.     public void launchGameFrame() {  
  166.         this.setBounds(MAINXPOINT, MAINYPOINT, MAINWIDTH, MAINHEIGHT);  
  167.         this.setBackground(Color.BLACK);  
  168.         this.setLayout(null);  
  169.         //关闭窗口的处理  
  170.         this.addWindowListener(new WindowAdapter() {  
  171.             public void windowClosing(WindowEvent e) {  
  172.                 setVisible(false);  
  173.                 System.exit(0);  
  174.             }  
  175.         });  
  176.           
  177.         //按键处理  
  178.         this.addKeyListener(new KeyAdapter() {  
  179.             public void keyPressed(KeyEvent e) {  
  180.                 myFighter.keyPressed(e);  
  181.             }  
  182.               
  183.             public void keyReleased(KeyEvent e) {  
  184.                 myFighter.keyReleased(e);  
  185.             }  
  186.               
  187.         });  
  188.           
  189.         //在窗口显示前开始线程重画  
  190.         new Thread(new PaintThread()).start();  
  191.               
  192.         this.setResizable(false);  
  193.         this.setVisible(true);  
  194.     }  
  195.       
  196.     //重写update()方法,实现双缓冲取消图像闪烁的情况  
  197.     public void update(Graphics g) {  
  198.         if(bufferImage == null) {  
  199.             bufferImage  =   
  200.                 this.createImage(MAINWIDTH, MAINHEIGHT);  
  201.         }  
  202.           
  203.         Graphics gBuffer = bufferImage.getGraphics();  
  204.         Color c = gBuffer.getColor();  
  205.         gBuffer.setColor(Color.BLACK);  
  206.         gBuffer.fillRect(00, MAINWIDTH, MAINHEIGHT);  
  207.         gBuffer.setColor(c);  
  208.           
  209.         paint(gBuffer);  
  210.         g.drawImage(bufferImage, 00,null);  
  211.     }  
  212.       
  213.     //绘图线程---用于不断的重绘  
  214.     class PaintThread implements Runnable {  
  215.         int step = 0;  
  216.         public void run() {  
  217.             while(true) {  
  218.                 repaint();  
  219.                 try {  
  220.                     Thread.sleep(20);  
  221.                 } catch (InterruptedException e) {  
  222.                     e.printStackTrace();  
  223.                 }  
  224.                   
  225.                 step ++;  
  226.                   
  227.                 if(step % 50 == 0) {  
  228.                     time ++;  
  229.                     step = 0;  
  230.                 }  
  231.                   
  232.             }  
  233.         }  
  234.     }  
  235.       
  236.     public String showTime() {  
  237.         int hour = time / 3600;  
  238.         int minite = (time - hour*3600) / 60;  
  239.         int second = (time - hour*3600) % 60;  
  240.         return hour + ":" + minite + ":" + second;  
  241.     }  
  242. }  


工程源码+ 文档+可执行文件:http://download.csdn.net/detail/haifengzhilian/4494594

  • 6
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值