?? launch_missiles.java
字號:
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 + -