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

📄 ufo_attack.java

📁 一个打飞碟小游戏,界面漂亮
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//这是一个用导弹攻击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 + -