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

📄 ufo_attack.java

📁 这是个用Java语言编写的游戏小程序 标准目标
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	  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 ;	      break ;	    case LEFT:	    default :	      say_pos_x = say_margin ;	      break ;	  }          Graphics bg = buffer.getGraphics() ;          bg.setFont(say_font) ;          // Print style extras	  if (say_style == SHADOW) {            bg.setColor(new Color(150,150,150)) ;            bg.drawString(s, say_pos_x+2,say_pos_y+1) ;	  }          // Print string          bg.setColor(Color.white) ;          bg.drawString(s, say_pos_x,say_pos_y) ;          // Shift y position to next line	  say_pos_y += (int) (1.2 * fm.getHeight()) ;          // Free some resources          bg.dispose() ;	}	public void set_say_mode(int m) {	  say_mode = m ;	}	public void set_say_style(int s) {	  say_style = s ;	}	public void set_say_font(Font f) {	  say_font = f ;	}	public void set_say_margin(int margin) {	  say_margin = margin ;	}	public void set_say_pos(int x, int y) {	  say_pos_x = x ;	  say_pos_y = y ;	}}class Piece {  UFO_Attack a ;  int px,py ;  int opx,opy ;  int w,h ;  int vx,vy ;  Color c ;  boolean active = false ;  Image img = null ;  public void set_pos(int x, int y) {    px = opx = x ;    py = opy = y ;  }  public void set_vel(int x,int y) {    vx = x ;    vy = y ;  }  public void set_size(int x,int y) {    w = x ;    h = y ;  }  public void set_color(Color c) {    this.c = c ;  }  public void set_draw_rectangles(Rectangle o, Rectangle n) {    int sh = a.window_size.height ;     int x  = px - w/2 ;    int y  = (sh - py) - h/2 ;    int ox = opx - w/2 ;    int oy = (sh - opy) - h/2 ;        o.reshape(ox,oy,w,h) ;     n.reshape(x,y,w,h) ;   }  public boolean active() {    return active ;  }  public void active(boolean s) {    active = s ;  }  public boolean collision(Piece p) {    int dpx = Math.abs(px - p.px) ;    int dpy = Math.abs(py - p.py) ;    if ((dpx < (Math.max(w/2,p.w/2))+1) && (dpy < (Math.max(h/2,p.h/2)+1)))       return true ;    return false ;  }  public void draw() {    set_draw_rectangles(a.paint_area, a.new_area) ;    // a.buf_g.setColor(a.bgColor);    // a.buf_g.fillRect(a.paint_area.x, a.paint_area.y, w, h);    Graphics bg = a.buffer.getGraphics() ;    bg.clipRect(a.paint_area.x, a.paint_area.y, w, h);    bg.drawImage(a.backdrop,0,0,a) ;    bg.dispose() ;    // bg = null ;    a.buf_g.setColor(c);    a.buf_g.fillRect(a.new_area.x, a.new_area.y, w, h);    a.paint_area.add(a.new_area) ;    Graphics g = a.getGraphics() ;    g.clipRect(a.paint_area.x ,a.paint_area.y, a.paint_area.width, a.paint_area.height);    g.drawImage(a.buffer, 0, 0, a);    g.dispose() ;    // g = null ;  }  public void erase() {    set_draw_rectangles(a.paint_area, a.new_area) ;    a.paint_area.add(a.new_area) ;    // Copy the backdrop into the buffer    Graphics bg = a.buffer.getGraphics() ;    bg.clipRect(a.paint_area.x, a.paint_area.y, a.paint_area.width, a.paint_area.height);    bg.drawImage(a.backdrop,0,0,a) ;    bg.dispose() ;    // Paint the change into the screen    Graphics g = a.getGraphics() ;    g.clipRect(a.paint_area.x,a.paint_area.y,a.paint_area.width,a.paint_area.height);    g.drawImage(a.buffer,0,0, a);    g.dispose() ;  }}class Launcher extends Piece {  public Launcher (UFO_Attack a) {    this.a = a ;     w  = 12 ;    h  = 22 ;    px = opx = a.window_size.width/2 ;    py = opy = w/2+1 ;    active = true ;    img = a.missile ;  }  public void move() {    opx = px ;    opy = py ;    int dx     = a.mouse_x - px ;    int abs_dx = Math.abs(dx) ;    int step = 1 ;    if (abs_dx > 10)       step = 5 ;    else if (abs_dx > 1)      step = abs_dx/2 ;    if (dx != 0) {      px += step*(dx/abs_dx) ;      if (px < w/2)         px = w/2 ;      else if (px > (a.window_size.width - w/2))         px = a.window_size.width - w/2 ;    }  }  public boolean has_moved() {    if ((px - opx) != 0) return true ;    return false ;  }  public void draw() {    set_draw_rectangles(a.paint_area, a.new_area) ;    // a.buf_g.setColor(a.bgColor);    // a.buf_g.fillRect(a.paint_area.x, a.paint_area.y, w, h);    Graphics bg = a.buffer.getGraphics() ;    bg.clipRect(a.paint_area.x, a.paint_area.y, w, h);    bg.drawImage(a.backdrop,0,0,a) ;    bg.dispose() ;    // bg = null ;    if (a.M.active()) {      a.buf_g.setColor(c);      a.buf_g.fillRect(a.new_area.x, a.new_area.y, w, h);    }    else {      bg = a.buffer.getGraphics() ;      bg.clipRect(a.new_area.x, a.new_area.y, w, h);      bg.drawImage(img,a.new_area.x,a.new_area.y,a) ;      bg.dispose() ;      // bg = null ;    }    a.paint_area.add(a.new_area) ;    Graphics g = a.getGraphics() ;    g.clipRect(a.paint_area.x ,a.paint_area.y, a.paint_area.width, a.paint_area.height);    g.drawImage(a.buffer, 0, 0, a);    g.dispose() ;    // g = null ;  }}class Missile extends Piece {  public Missile (UFO_Attack a) {    this.a = a ;     px = opx = 0 ;    py = opy = 0 ;    vx = 0 ;    vy = 7 ;    w  = 12 ;    h  = 22 ;    active = false ;    img = a.missile ;  }  public void move() {    opx = px ;    opy = py ;    px = a.L.px ;    // Try to make the speed more realistic    int dx = px - opx ;    int nvy = vy*vy - dx*dx ;    if (nvy > 0) nvy = (int) Math.sqrt(nvy) ; // Should exceptions    if (nvy < 1) nvy = 1 ;    py += nvy ;    if (py > a.window_size.height + 2*h) active = false ;  }  int seq  = 0 ;  // int seq2 = 0 ;  public void draw() {    set_draw_rectangles(a.paint_area, a.new_area) ;    // a.buf_g.setColor(a.bgColor);    // a.buf_g.fillRect(a.paint_area.x, a.paint_area.y, w, h);    Graphics bg = a.buffer.getGraphics() ;    bg.clipRect(a.paint_area.x, a.paint_area.y, w, h);    bg.drawImage(a.backdrop,0,0,a) ;    bg.dispose() ;    // bg = null ;    // if ((++seq2 % 4) == 0) seq = ++seq % 1 ;    int dx = px - opx ;    seq = 0 ;    if (dx > 0)       seq = 1 ;    else if (dx < 0)      seq = 2 ;    bg = a.buffer.getGraphics() ;    bg.clipRect(a.new_area.x, a.new_area.y, w, h);    bg.drawImage(img,a.new_area.x-w*seq,a.new_area.y,a) ;    bg.dispose() ;    // bg = null ;    a.paint_area.add(a.new_area) ;    Graphics g = a.getGraphics() ;    g.clipRect(a.paint_area.x ,a.paint_area.y, a.paint_area.width, a.paint_area.height);    g.drawImage(a.buffer, 0, 0, a);    g.dispose() ;    // g = null ;  }}class UFO extends Piece {  public UFO (UFO_Attack a) {    this.a = a ;     vx = (Math.random() > 0.5 ? 1 : -1) ;    vy = -2 ;    w  = 20 ;    h  = 8 ;    int aw = a.window_size.width ;    px = opx =  (int) (w/2+1 + (aw-w-2)* Math.random()) ;    py = opy = a.window_size.height + h/2 + 1  ;    active = true ;    img = a.ufostrip ;  }  public void move() {    opx = px ;    opy = py ;    px += vx ;    py += vy ;    if (py < -h/2) active = false ;    if ((px <= w/2) ||         (px >= (a.window_size.width - w/2)) ||        (Math.random() > 0.96)) {      vx = -vx ;    }  }  int seq  = 0 ;  int seq2 = 0 ;  public void draw() {    set_draw_rectangles(a.paint_area, a.new_area) ;    // Clear the old image    Graphics bg = a.buffer.getGraphics() ;    bg.clipRect(a.paint_area.x, a.paint_area.y, w, h);    bg.drawImage(a.backdrop,0,0,a) ;    bg.dispose() ;    // bg = null ;    // Choose the image sequence (every 4 draws change frame)    if ((++seq2 % 4) == 0) seq = ++seq % 4 ;    // Paint new region into buffer    bg = a.buffer.getGraphics() ;    bg.clipRect(a.new_area.x, a.new_area.y, w, h);    bg.drawImage(img,a.new_area.x-w*seq,a.new_area.y,a) ;    bg.dispose() ;    // Add old and new areas for updating    a.paint_area.add(a.new_area) ;    // Paint changes into screen buffer    Graphics g = a.getGraphics() ;    g.clipRect(a.paint_area.x ,a.paint_area.y, a.paint_area.width, a.paint_area.height);    g.drawImage(a.buffer, 0, 0, a);    g.dispose() ;  }}	class Explosion extends Piece {  public Explosion (UFO_Attack a, int x, int y) {    this.a = a ;     w  = 30 ;    h  = 30 ;    px = opx = x ;    py = opy = y ;    active = true ;    img = a.missile_explosion ;  }  int seq  = 0 ;  int seq2 = 0 ;  public void draw() {    set_draw_rectangles(a.paint_area, a.new_area) ;    // Clear the old image    Graphics bkd_g = a.backdrop.getGraphics();    bkd_g.clipRect(a.paint_area.x, a.paint_area.y, w, h);    bkd_g.drawImage(a.bgimg,0,0,a.window_size.width,a.window_size.height,a) ;    // Choose the image sequence (every 4 draws change frame)    if ((++seq2 % 4) == 0) seq = ++seq % 5 ;    // Deactivate after drawing the last explosion frame    if (seq == 4) active = false ;    // Paint new region into buffer    bkd_g.clipRect(a.new_area.x, a.new_area.y, w, h);    bkd_g.drawImage(img,a.new_area.x-w*seq,a.new_area.y,a) ;    bkd_g.dispose() ;    // Paint changes into the buffer    Graphics bg = a.buffer.getGraphics() ;    bg.clipRect(a.new_area.x,a.new_area.y,w,h);    bg.drawImage(a.backdrop,0,0,a) ;    bg.dispose() ;    // Paint changes into screen buffer    Graphics g = a.getGraphics() ;    g.clipRect(a.paint_area.x ,a.paint_area.y, a.paint_area.width, a.paint_area.height);    g.drawImage(a.buffer, 0, 0, a);    g.dispose() ;  }  public void erase() {    set_draw_rectangles(a.paint_area, a.new_area) ;    // Clear the old image    Graphics bkd_g = a.backdrop.getGraphics();    bkd_g.clipRect(a.paint_area.x, a.paint_area.y, w, h);    bkd_g.drawImage(a.bgimg,0,0,a.window_size.width,a.window_size.height,a) ;    bkd_g.dispose() ;    // Do the same for the buffer and the screen    // It will flicker any UFO on top...    super.erase() ;  }}

⌨️ 快捷键说明

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