?? ufo_attack.java
字號:
//這是一個用導彈攻擊UFO的小游戲//游戲開始有聲音提示,并且顯示提示信息//用戶通過點擊鼠標開始新游戲//UFO從游戲界面的頂部不斷地產生,并不規則的動作向下方移動//一旦移動到最下方,游戲即失敗//游戲界面的下部有導彈發射管,游戲者通過點擊鼠標發射導彈//要盡量做到百發百中,因為新導彈的裝配需要時間//如果一枚導彈沒有擊中,UFO就可能在這段裝配時間中移動到底部//導致游戲失敗。import java.awt.*;import java.applet.*;import java.util.*;public class UFO_Attack extends Applet implements Runnable { Image buffer = null ; // 臨時圖象緩存 Image backdrop = null ; Image bgimg = null ; Image ufostrip = null ; // UFO Sequence Image missile = null ; // Misile Sequence Image missile_explosion = null ; // 導彈爆炸 MediaTracker tracker = null ; Graphics buf_g = null ; // Buffer graphics object Graphics bkd_g = null ; // Backdrop graphics object Dimension window_size = null; Font font ; Font font_s ; AudioClip explosion = null ; AudioClip newufo = null ; AudioClip missile_launch = null ;//每當在新的UFO產生,導彈發射或者導彈命中時候都會有不同的聲音提示 Thread game = null ; boolean game_over = true ; int mouse_x = 100 ; 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() { System.out.println("UFO Attack: A game by Sergio Fanchiotti") ; tracker = new MediaTracker(this) ; bgimg = getImage(this.getCodeBase(),"bgimg.gif") ; tracker.addImage(bgimg,0) ; ufostrip = getImage(this.getCodeBase(),"ufostrip.gif") ; tracker.addImage(ufostrip,0) ; missile = getImage(this.getCodeBase(),"missile.gif") ; tracker.addImage(missile,0) ; missile_explosion = getImage(this.getCodeBase(),"explosionstrip.gif") ; tracker.addImage(missile_explosion,0) ; font = new Font("Helvetica", Font.BOLD, 24) ; font_s = new Font("Helvetica", Font.BOLD, 14) ; // Define the colors to use 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() { // Change the cursor改變光標 getFrame(this).setCursor(Frame.CROSSHAIR_CURSOR) ; window_size = size(); 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); // Display initial message set_say_font(font) ; set_say_mode(CENTER) ; set_say_style(SHADOW) ; say("UFO",10,80) ; say("ATTACK") ; set_say_font(font_s) ; set_say_style(NORMAL) ; say("") ; say("Click to start") ; say("a game") ; //repaint() ; Graphics g = getGraphics() ; g.drawImage(buffer,0,0,this) ; // Initialize the launcher mouse_x = window_size.width/2 ; L = new Launcher(this) ; L.set_color(gunColor) ; // Initialize the missile M = new Missile(this) ; M.set_color(mColor) ; // Load the sound files now if (explosion == null) explosion = getAudioClip(getCodeBase(),"explosion.au") ; if (newufo == null) newufo = getAudioClip(getCodeBase(),"sonar.au") ; if (missile_launch == null) missile_launch = getAudioClip(getCodeBase(),"rocket.au") ; game_over = true ;//演奏聲音 newufo.play() ; // Play sounds missile_launch.play() ; explosion.play() ; } public void stop() { // Stop the game if running if (game != null) { game.stop() ; game = null ; // and eliminate the thread } // Reset the cursor getFrame(this).setCursor(Frame.DEFAULT_CURSOR) ; } // The main game thread public void run() { // Define some local variables UFO U ; Explosion E ; long count = 0 ; long ti = 0 ; // Wait for the images to load Graphics g = getGraphics() ; g.setColor(Color.red) ; g.drawString("Starting Game...", 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("Error getting images") ; return ; } showStatus("Loading completed") ; g.dispose() ; // Paint the backdrop buffer buf_g = backdrop.getGraphics(); buf_g.drawImage(bgimg,0,0,window_size.width,window_size.height,this) ; // Paint the screen buf_g = getGraphics(); buf_g.drawImage(backdrop,0,0,this) ; // Paing the buffer buf_g = buffer.getGraphics(); buf_g.drawImage(bgimg,0,0,window_size.width,window_size.height,this) ; // Display the buffer repaint() ; // Display the initial setup display_score() ; L.draw() ; showStatus("UFO ATTACK") ; // Action loop for (;;) { ti = System.currentTimeMillis() ; //如果有足夠的空間容納更多的UFO,試著隨機性的增加一個 if ((UV.size() < NU) && (Math.random() > (UV.size() == 0 ? 0.90 : 0.98))) { newufo.play() ; // Play a bell U = new UFO(this) ; U.set_color(ufoColor) ; // Set the UFO to fast descent if: if (score > 10 && Math.random() > 0.7) U.vy -= 1 ; // Add UFO to the list UV.addElement(U) ; } //在背景上面繪制爆炸的圖象,并在結束的時候消除它 for (int j=EV.size()-1; j>=0 ; --j) { E = (Explosion) EV.elementAt(j) ; if (E.active) { E.draw() ; } else { // System.out.println("Ending Explosion " + j) ; E.erase() ; EV.removeElementAt(j) ; } } // Move the launcher and the missile L.move() ; if (M.active() == true) M.move() ; // Move each UFO for (int i=0; i < UV.size(); ++i) { U = (UFO) UV.elementAt(i) ; U.move() ; } // Check for collision between UFO and missile for (int i=(UV.size()-1); i >=0 ; --i) { U = (UFO) UV.elementAt(i) ; if (U.active() && M.active() && U.collision(M)) { ++score ; explosion.stop() ; display_score() ; explosion.play() ; // Increase the max # of UFOs every 10 destroyed // till we reach a max of 5 attacking if ((NU < 5) && (score % 10) == 1) ++NU ; // Erase the missile and deactivate it M.active(false) ; M.erase() ; // Erase and deactivate the UFO hit U.active(false) ; U.erase() ; // Display an explosion E = new Explosion(this,U.px,U.py) ; // System.out.println("Adding Explosion") ; EV.addElement(E) ; } // Draw the UFO or eliminate it from the list if (U.active()) U.draw() ; else UV.removeElementAt(i) ; // If a UFO reaches the floor then you've lost if ((U.py - U.h/2) <=0) { game_over = true ; display_game_over() ; return ; } } // If the launcher moves... redraw if (L.has_moved() || ((M.py-M.h) < (L.py+L.h)) || (! M.active()) ) L.draw() ; // If active redraw the missile if (M.active() == true) M.draw() ; // Make the loop last 20ms in case the CPU is too fast... ti = System.currentTimeMillis() - ti ; ti = 20 - ti ; ti = ti > 0 ? 10 + ti : 10 ; // At least sleep por 10ms Thread.yield() ; // Let the GC work? try {Thread.sleep(ti);} catch (InterruptedException e) { } ; // Repaint every 100 cycles... just in case if ((count = ++count % 500) == 0) { repaint() ; // g.drawImage(buffer,0,0,this) ; } } }//顯示戰績// 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() ; // bg = null ; Graphics g = getGraphics() ; g.clipRect(0, 0, window_size.width, 40); g.drawImage(buffer,0,0,this) ; g.dispose() ; // g = null ; } public void display_game_over() { set_say_font(font) ; set_say_mode(CENTER) ; set_say_style(SHADOW) ; set_say_pos(10,80) ; say("GAME OVER") ; set_say_font(font_s) ; set_say_style(NORMAL) ; say("(click to start)") ; repaint() ; try {Thread.sleep(500);} catch (InterruptedException e) { } ; } public boolean mouseMove(Event e, int x, int y) { // showStatus("mouse at: (" + x + ", " + y + ")"); mouse_x = x ; return true; } // Capture the mouse clicks public boolean mouseDown(Event e, int x, int y) { if (game_over) { game_over = false ; if (game != null) { game.stop() ; 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; } // The paint methos just draws the buffer public void paint(Graphics g) { if (buffer != null) g.drawImage(buffer, 0, 0, this); } // Makes an animation smoother public void update(Graphics g) { paint(g) ; } // Get the Frame of this applet public Frame getFrame(Component c) { while( c != null && !(c instanceof java.awt.Frame) ) c = c.getParent(); return (Frame) c; } // Text Display Constants public static final int CENTER = 1 ; // Modes: Centered public static final int LEFT = 2 ; // Left Justified public static final int RIGHT = 3 ; // Right Justified public static final int FREE = 0 ; // At x,y public static final int NORMAL = 0 ; // Styles: Normal public static final int SHADOW = 1 ; // Shadowed // Text Display Variables 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 ; // Text Print Methods 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) ; // Compute the x position switch(say_mode) { 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 ;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -