?? ufo_attack.java
字號:
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) ;
//提升相應的 y坐標值
say_pos_y += (int) (1.2 * fm.getHeight()) ;
// 釋放一些資源
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 {
UFO_Attack 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.reshape(ox,oy,w,h) ;
n.reshape(x,y,w,h) ;
}
//獲取active屬性值
public boolean active() {
return active ;
}
//設置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) ;
//繪制緩沖區
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() ;
}
}
//定義導彈發射架類,繼承于Piece類
class Launcher extends Piece {
//Launcher類的構造函數
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) ;
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() ;
//根據導彈的active屬性值進行相應的繪制
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() ;
}
}
//定義Missile類,繼承于Piece類
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 ;
// 使移動的速度更加實際化
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 ;
//導彈對象的繪制
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 = ++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() ;
}
}
//定義UFO類,繼承于Piece類
class UFO extends Piece {
//UFO類的構造函數
public UFO (UFO_Attack a) {
//UFO屬性值的初始化
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 ;
}
//UFO對象的移動函數
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 ;
//UFO對象的繪制函數
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() ;
// 由于UFO用的圖片是一個序列圖,要進行剪切再使用
//根據seq2的值選擇相應的部分
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() ;
}
}
//定義爆炸類,繼承于Piece類
class Explosion extends Piece {
//Explosion類的構造函數
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) ;
// 由于爆炸用的圖片是一個序列圖,要進行剪切再使用
//根據seq2的值選擇相應的部分
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() ;
// 對緩沖和屏幕執行同樣的操作
super.erase() ;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -