?? ufo_attack.java
字號:
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) ; //判斷是否處于相撞擊狀態//即兩個物體的距離小于給定的距離,包括高度和寬度//w和h的值和影響到dpx和dpy的px及py值是由預先設定的。//對擴展的Launcher,UFO,Missile和Explosion類來說,預先設定的值并不相同 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) ; //清除舊的圖象 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) ; // 選擇圖象序列,每4次繪制改變一次幀。 if ((++seq2 % 4) == 0) seq = ++seq % 5 ; //在已經繪制完成最后一次爆炸幀后,將active屬性設置為false 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() ; // It will flicker any UFO on top... //同樣對緩存和屏幕進行操作 super.erase() ; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -