⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 launch_missiles.java

📁 java程序中关于多媒体编程 既有文件说明 更有例子 希望大家可以一起学习交流
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package launch_missiles;import java.awt.*;                  // 引入所需的类包import java.awt.event.*;import java.applet.*;import java.util.*;/** * <p>Title: 发射导弹游戏</p> * <p>Description: 该游戏为发射导弹攻击飞行物</p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: 北京师范大学计算中心</p> * @author 张庆利 * @version 1.0 */public class Launch_Missiles extends Applet implements Runnable {  Image buffer   = null ;	// 定义临时图像缓冲  Image backdrop = null ; // 定义背景幕  Image bgimg    = null ; // 定义原背景幕  Image ufostrip = null ;	// 定义飞行物序列图  Image missile  = null ; // 定义导弹序列图  Image missile_explosion = null ; // 定义导弹爆炸序列图  MediaTracker tracker = null ;// 定义媒体跟踪器,用来监测图像的装载  Graphics  buf_g = null ; // 定义缓冲中的图像对象  Graphics  bkd_g = null ; // 定义背景幕的图像对象  Dimension window_size = null;// 定义窗口尺寸  Font font ;  Font font_s ;              // 定义显示字的字体  AudioClip explosion  = null ; // 定义爆炸声  AudioClip newufo   = null ; // 定义新的飞行物出现时发出的声音  AudioClip missile_launch = null ; // 定义导弹发射的声音  Thread  game  = null ; // 定义程序的主线程  boolean game_over = true ; // 定义用来判断游戏结束与否  int mouse_x = 100 ; // 定义鼠标的X坐标,用来控制导弹和发射架的移动  Rectangle paint_area = new Rectangle() ; // 创建对象出现的区域  Rectangle new_area   = new Rectangle() ; // 创建对象即将出现的区域  Launcher L  = null ; // 定义一个导弹发射架  Missile  M  = null ; // 定义一个导弹  Vector   UV = new Vector() ; // 定义飞行物向量,即一个飞行物集合  Vector   EV = new Vector() ; // 定义爆炸向量,即一个爆炸集合  int NU  = 1 ; // 定义飞行物的数目  int score = 0 ; // 定义所得分数  Color gunColor;   // 相应对象的颜色设置  Color mColor;  Color ufoColor;  Color scoreColor;  Color bgColor;  public void init() {    // Launch_Missiles类的初始化    showStatus("发射导弹游戏") ;    tracker = new MediaTracker(this) ; // 媒体跟踪器监测图像装载的情况    bgimg = getImage(this.getCodeBase(),"images/"+"bgimg.gif") ;  // 装载图片    tracker.addImage(bgimg,0) ;    ufostrip = getImage(this.getCodeBase(),"images/"+"ufostrip.gif") ;    tracker.addImage(ufostrip,0) ;    missile  = getImage(this.getCodeBase(),"images/"+"missile.gif") ;    tracker.addImage(missile,0) ;    missile_explosion = getImage(this.getCodeBase(),"images/"+"explosionstrip.gif") ;    tracker.addImage(missile_explosion,0) ;    font = new Font("Helvetica", Font.BOLD, 24) ;    font_s = new Font("Helvetica", Font.BOLD, 14) ; // 设置显示字的字体    bgColor    = new Color(0,0,128);          // 设置所需的颜色    gunColor   = new Color(0,88,0);    mColor     = new Color(255,255,255);    ufoColor   = new Color(255,0,0);    scoreColor = new Color(0,0,255);  }  public void start() {    window_size = getSize();   // 获取窗口的尺寸    buffer = null;          // 创建缓冲区    buffer = createImage(window_size.width, window_size.height);    backdrop = null;    backdrop = createImage(window_size.width, window_size.height);    buf_g = buffer.getGraphics();     // 用背景色来填充缓冲区    buf_g.setColor(bgColor);    buf_g.fillRect(0, 0, window_size.width, window_size.height);    set_say_font(font) ;              // 显示初始化信息    set_say_mode(CENTER) ;    set_say_style(SHADOW) ;    say("发射导弹",10,80) ;    say("打击飞行物") ;    set_say_font(font_s) ;    set_say_style(NORMAL) ;    say("") ;    say("单击鼠标——游戏开始") ;    say("发射导弹游戏") ;    Graphics g = getGraphics() ;    // 将缓冲绘制到屏幕上    g.drawImage(buffer,0,0,this) ;    mouse_x = window_size.width/2 ;   // 初始化导弹发射架    L = new Launcher(this) ;    L.set_color(gunColor) ;    M = new Missile(this) ;           // 初始化导弹    M.set_color(mColor) ;    if (explosion == null)            // 加载声音文件      explosion = getAudioClip(getCodeBase(),"sounds/"+"explosion.au") ;    if (newufo == null)      newufo = getAudioClip(getCodeBase(),"sounds/"+"sonar.au") ;    if (missile_launch == null)      missile_launch = getAudioClip(getCodeBase(),"sounds/"+"rocket.au") ;    game_over = true ;    newufo.play() ;                 // 声音播放    missile_launch.play() ;    explosion.play() ;  }  public void stop() {    if (game != null)      game = null ;  }  public void run() {                 // 游戏主线程    Flyer  U ;                        // 定义本地变量和对象    Explosion  E ;    long count = 0 ;    long ti    = 0 ;    Graphics g = getGraphics() ;    	  // 等待图片装载完毕    g.setColor(Color.red) ;    g.drawString("游戏开始...", 20,20) ;    while (tracker.checkAll(true) == false) {      if ((ti++ % 2) == 0)        g.setColor(Color.red) ;      else        g.setColor(Color.green) ;      g.drawString("*", 10,22) ;      try {Thread.sleep(50);} catch (InterruptedException e) { } ;      if (ti > 1000) break ;  // 装载超时时强行退出    }    if (tracker.isErrorAny()) {       	  // 捕捉获取图片时的错误信息      showStatus("获取图片出现错误") ;      return ;    }    showStatus("装载图片成功") ;    g.dispose() ;    buf_g = backdrop.getGraphics();         // 绘制背景幕的缓冲区    buf_g.drawImage(bgimg,0,0,window_size.width,window_size.height,this) ;    buf_g = getGraphics();          // 将背景幕的缓冲绘制到屏幕上    buf_g.drawImage(backdrop,0,0,this) ;    buf_g = buffer.getGraphics();           // 绘制缓冲区    buf_g.drawImage(bgimg,0,0,window_size.width,window_size.height,this) ;    repaint() ;        // 重新绘制    display_score() ;         // 显示得分数    L.draw() ;                    // 绘制导弹发射架    showStatus("发射导弹游戏") ;    for (;;) {      // 事件循环      ti = System.currentTimeMillis() ;      if ((UV.size() < NU) &&(Math.random() > (UV.size() == 0 ? 0.90 : 0.98))) {                // 如果有多余的飞行物飞行空间则可增加一架飞行物        newufo.play() ;  // 播放警报声	U = new Flyer(this) ;        U.set_color(ufoColor) ;        if (score > 10 && Math.random() > 0.7) U.vy -= 1 ; // 在相应条件下提高飞行物的下降速度          UV.addElement(U) ;            // 在飞行物向量中增加一名成员    }    for (int j=EV.size()-1; j>=0 ; --j) {         // 在背景幕上绘制爆炸画面,结束后将其清除      E = (Explosion) EV.elementAt(j) ;      if (E.active) {        E.draw() ;        // 如果爆炸出现就进行其画面的绘制      }      else {        E.erase() ;         // 结束后从背景幕上清除,并从爆炸向量中删除        EV.removeElementAt(j) ;      }    }    L.move() ;        	    // 移动导弹发射架    if (M.active() == true) M.move() ;            // 如果导弹存在,移动导弹    for (int i=0; i < UV.size(); ++i) {             // 移动每个飞行物      U = (Flyer) UV.elementAt(i) ;      U.move() ;    }    for (int i=(UV.size()-1); i >=0 ; --i) {          // 监视飞行物与导弹之间的碰撞      U = (Flyer) UV.elementAt(i) ;      if (U.active() && M.active() && U.collision(M)) {        ++score ;  // 增加得分        explosion.stop() ;        display_score() ;        explosion.play() ;        if ((NU < 5) && (score % 10) == 1)  ++NU ;                // 每击落10架飞行物后便增加飞行物的最大出现数目,直到数目为5          M.active(false) ;         // 碰撞发生后,将导弹从背景幕上清除,并使其active属性为false        M.erase() ;        U.active(false) ;       //将被击中的飞行物从背景幕上清除,并使其active属性为false        U.erase() ;        E = new Explosion(this,U.px,U.py) ; //显示爆炸的场面        EV.addElement(E) ;	    //在爆炸向量中添加一员      }      if (U.active())          // 如果飞行物没有被击中,则显示出来,否则,将其从飞行物向量中删除        U.draw() ;      else        UV.removeElementAt(i) ;      if ((U.py - U.h/2) <=0) {           // 如果有一个飞行物成功着陆则游戏结束        game_over = true ;        display_game_over() ;        return ;      }    }    if (L.has_moved() || ((M.py-M.h) < (L.py+L.h)) || (! M.active()) )          //如果导弹发射架移动了,重画发射架      L.draw() ;      if (M.active() == true) M.draw() ;        //如果导弹的active属性值为true,则重画导弹        ti = System.currentTimeMillis() - ti ;        // 当CPU速度太快,则要使循环维持在20ms以上      ti = 20 - ti ;      ti = ti > 0 ?  10 + ti : 10 ;      Thread.yield() ;      try {        Thread.sleep(ti);   //处理线程sleep函数的异常      }      catch (InterruptedException e) { } ;      if ((count = ++count % 500) == 0) {         // 每100次循环重新绘制一次        repaint() ;      }    }  }  public void display_score() {   // 显示得分    Graphics bkd_g = backdrop.getGraphics();    bkd_g.clipRect(window_size.width/2, 0, window_size.width/2, 40);    bkd_g.drawImage(bgimg,0,0,window_size.width,window_size.height,this) ;    bkd_g.setColor(Color.red) ;    bkd_g.setFont(font) ;    String aux = score > 9 ? "" : "0" ;    bkd_g.drawString(aux+score, window_size.width - 60,30) ;    bkd_g.dispose() ;    Graphics bg = buffer.getGraphics() ;    bg.clipRect(0, 0, window_size.width, 40);    bg.drawImage(backdrop,0,0,this) ;    bg.dispose() ;    Graphics g = getGraphics() ;    g.clipRect(0, 0, window_size.width, 40);    g.drawImage(buffer,0,0,this) ;    g.dispose() ;  }  public void display_game_over() {   // 游戏结束时的提示    set_say_font(font) ;    set_say_mode(CENTER) ;    set_say_style(SHADOW) ;    set_say_pos(10,80) ;    say("游戏结束") ;    set_say_font(font_s) ;    set_say_style(NORMAL) ;    say("单击鼠标——游戏开始") ;    repaint() ;    try {      Thread.sleep(500);    }    catch (InterruptedException e) { } ;  }  public boolean mouseMove(Event e, int x, int y) {       // 处理鼠标移动事件    mouse_x = x ;                     	  // 返回鼠标所在位置的X坐标    return true;  }  public boolean mouseDown(Event e, int x, int y) {     // 处理鼠标的按下事件    if (game_over) {                    	  // 游戏结束时所做的处理      game_over = false ;      if (game != null) {        game = null ;      }      NU  = 1 ;      score = 0 ;      M.active(false) ;      UV.removeAllElements() ;      EV.removeAllElements() ;      game  = new Thread(this) ;        // 创建一个新线程      game.setPriority(Thread.MIN_PRIORITY) ;      game.start() ;              //启动线程      buf_g.dispose() ;      return true ;    }    if (M != null && ! M.active()) {          // 如果游戏没有结束并且导弹没有被发射,则发射导弹      missile_launch.stop() ;      missile_launch.play() ;      M.set_pos(L.px,L.py) ;      M.active(true) ;    }    return true;  }  public void paint(Graphics g) { 	// 将缓冲绘制到屏幕上    if (buffer != null)      g.drawImage(buffer, 0, 0, this);  }  public void update(Graphics g) {   // 防止动画画面闪烁    paint(g) ;  }  public Frame getFrame(Component c) {       //获取Frame对象    while( c != null && !(c instanceof java.awt.Frame) )      c = c.getParent();    return (Frame) c;  }  public static final int CENTER = 1 ;  // 模式: 居中   (定义文本显示的常量)  public static final int LEFT   = 2 ;  //       居左  public static final int RIGHT  = 3 ;  //       居右  public static final int FREE   = 0 ;  //       居于所给的(x,y)位置上  public static final int NORMAL = 0 ;  // 类型:正常  public static final int SHADOW = 1 ;  //      带有阴影  private int  say_pos_y   =  0 ;           // 定义文本显示变量  private int  say_pos_x   =  0 ;  private int  say_mode    = -1 ;  private int  say_style   = -1 ;  private int  say_margin  = 10 ;  private Font say_font    = null ;  public void say(String s, int x, int y) {     // 文本显示方法    set_say_pos(x, y) ;    say(s) ;  }  public void say(String s) {       // 文本显示方法    FontMetrics fm = getFontMetrics(say_font) ;         // 获取字体信息    switch(say_mode) {                  // 计算x坐标      case CENTER:        say_pos_x = (window_size.width - fm.stringWidth(s))/2 ;        break ;      case RIGHT:        say_pos_x = window_size.width - fm.stringWidth(s) - say_margin ;        break ;      case LEFT:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -