📄 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 + -