?? launcher.java
字號:
import java.awt.Graphics;
/**
* <p>Title: UFO_Play</p>
*
* <p>Description: 基于applet的攻擊UFO的小游戲中的導彈發射架類</p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: 鹿児島大學</p>
* @author 柴 智
* @version 1.0
*/
public class Launcher extends Piece {
//Launcher類的構造函數
public Launcher(UFO_Canvas ufo_canvas) {
//導彈發射架屬性值的初始化
super(ufo_canvas);
w = 12;
h = 22;
px = opx = ufo_canvas.getWindow_size().width / 2;
py = opy = w / 2 + 1;
active = true;
img = ufo_canvas.getMissile();
}
//導彈發射架的移動函數
public void move() {
opx = px;
opy = py;
int dx = ufo_canvas.getMouse_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 > (ufo_canvas.getWindow_size().width - w / 2)) {
px = ufo_canvas.getWindow_size().width - w / 2;
}
}
}
//判斷導彈發射架是否移動
public boolean has_moved() {
if ((px - opx) != 0) {
return true;
}
return false;
}
//導彈發射架的繪制
public void draw() {
set_draw_rectangles(ufo_canvas.getPaint_area(), ufo_canvas.getNew_area());
Graphics bg = ufo_canvas.getBuffer().getGraphics();
bg.clipRect(ufo_canvas.getPaint_area().x, ufo_canvas.getPaint_area().y, w, h);
bg.drawImage(ufo_canvas.getBackdrop(), 0, 0, ufo_canvas.getBaseApplet());
bg.dispose();
//根據導彈的active屬性值進行相應的繪制
if (ufo_canvas.getM().active()) {
//導彈飛行時,發射架外觀為一實心矩形
ufo_canvas.getBuf_g().setColor(c);
ufo_canvas.getBuf_g().fillRect(ufo_canvas.getNew_area().x, ufo_canvas.getNew_area().y, w, h);
} else {
//否則,發射架外觀為一豎立的導彈
bg = ufo_canvas.getBuffer().getGraphics();
bg.clipRect(ufo_canvas.getNew_area().x, ufo_canvas.getNew_area().y, w, h);
bg.drawImage(img, ufo_canvas.getNew_area().x, ufo_canvas.getNew_area().y, ufo_canvas.getBaseApplet());
bg.dispose();
// bg = null ;
}
//使用新的區域
ufo_canvas.getPaint_area().add(ufo_canvas.getNew_area());
//將緩沖繪制到屏幕上
Graphics g = ufo_canvas.getBaseApplet().getGraphics();
g.clipRect(ufo_canvas.getPaint_area().x, ufo_canvas.getPaint_area().y, ufo_canvas.getPaint_area().width,
ufo_canvas.getPaint_area().height);
g.drawImage(ufo_canvas.getBuffer(), 0, 0, ufo_canvas.getBaseApplet());
g.dispose();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -