?? launch_missiles.java
字號:
default : say_pos_x = say_margin ; break ; } Graphics bg = buffer.getGraphics() ; bg.setFont(say_font) ; if (say_style == SHADOW) { bg.setColor(new Color(150,150,150)) ; bg.drawString(s, say_pos_x+2,say_pos_y+1) ; } bg.setColor(Color.white) ; // 在緩沖區內寫字符串 bg.drawString(s, say_pos_x,say_pos_y) ; say_pos_y += (int) (1.2 * fm.getHeight()) ; // 提升相應的 y坐標值 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 ; }}/* -------------------- 定義Piece基類 -------------------------- */class Piece { Launch_Missiles a ; int px,py ; // 定義新位置的x、y坐標 int opx,opy ; // 定義舊位置的x、y坐標 int w,h ; // 定義寬度,高度 int vx,vy ; // 定義x和y方向的速度 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.setBounds(ox,oy,w,h); n.setBounds(x,y,w,h) ; } public boolean active() { // 獲取active屬性值 return active ; } public void active(boolean s) { // 設置active屬性值 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) ; // 設置繪制的區域 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() ; 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() ; } public void erase() { // 清除 set_draw_rectangles(a.paint_area, a.new_area) ; // 設置繪制的區域 a.paint_area.add(a.new_area) ; // 使用新的區域 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() ; 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 (Launch_Missiles 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) ; 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() ; if (a.M.active()) { // 根據導彈的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() ; } 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() ; }}/* -------------------- 定義導彈Missile類 -------------------------- */class Missile extends Piece { public Missile (Launch_Missiles 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 ; int dx = px - opx ; int nvy = vy*vy - dx*dx ; if (nvy > 0) nvy = (int) Math.sqrt(nvy) ; if (nvy < 1) nvy = 1 ; py += nvy ; if (py > a.window_size.height + 2*h) active = false ; } int seq = 0 ; public void draw() { // 繪制導彈 set_draw_rectangles(a.paint_area, a.new_area) ; // 設置繪制區域 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() ; 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() ; 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() ; }}/* -------------------- 定義飛行物Flyer類 -------------------------- */class Flyer extends Piece { public Flyer (Launch_Missiles 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) ; 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() ; if ((++seq2 % 4) == 0) seq = ++seq % 4 ; 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() ; 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() ; }}/* -------------------- 定義爆炸Explosion類 -------------------------- */class Explosion extends Piece { public Explosion (Launch_Missiles 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) ; 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) ; if ((++seq2 % 4) == 0) seq = ++seq % 5 ; if (seq == 4) active = false ; 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() ; 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() ; 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) ; 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() ; super.erase() ; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -