修改提交

master
msb_64243 3 years ago
parent 99a61ede8e
commit dad94bb810

@ -1,147 +1,150 @@
package com.tank; package com.tank;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Rectangle; import java.awt.Rectangle;
import com.tank.abstractfactory.BaseBullet; import com.tank.abstractfactory.BaseBullet;
/** /**
* *
* @author leiwei * @author leiwei
* *
*/ */
public class Bullet extends BaseBullet{ public class Bullet extends BaseBullet{
//子弹位置 //子弹位置
private int x; private int x;
private int y; private int y;
//子弹方向 //子弹方向
private Dir dir; private Dir dir;
//子弹速度 //子弹速度
private static final int SPEED = PropertyMrg.getInt("bulletSpeed"); private static final int SPEED = PropertyMrg.getInt("bulletSpeed");
//设置子弹的大小width,height //设置子弹的大小width,height
public static int width = ResouceMgr.bulletD.getWidth(); public static int width = ResouceMgr.bulletD.getWidth();
public static int height = ResouceMgr.bulletD.getHeight(); public static int height = ResouceMgr.bulletD.getHeight();
//定义子弹是否存活 //定义子弹是否存活
private boolean living = true; private boolean living = true;
TankFrame tf=null; TankFrame tf=null;
private TankGroup group = TankGroup.BAD; private TankGroup group = TankGroup.BAD;
Rectangle rect = new Rectangle(); Rectangle rect = new Rectangle();
public Dir getDir() { public Dir getDir() {
return dir; return dir;
} }
public void setDir(Dir dir) { public void setDir(Dir dir) {
this.dir = dir; this.dir = dir;
} }
public Bullet(int x, int y, Dir dir,TankGroup group,TankFrame tf) { public Bullet(int x, int y, Dir dir,TankGroup group,TankFrame tf) {
super(); super();
this.x = x; this.x = x;
this.y = y; this.y = y;
this.dir = dir; this.dir = dir;
this.tf = tf; this.tf = tf;
this.group = group; this.group = group;
rect.x = this.x; rect.x = this.x;
rect.y = this.y; rect.y = this.y;
rect.width = width; rect.width = width;
rect.height = height; rect.height = height;
//将子弹添加子弹队列中 //将子弹添加子弹队列中
tf.bullets.add(this); tf.bullets.add(this);
} }
//子弹显示出来 //子弹显示出来
@Override @Override
public void paint(Graphics p){ public void paint(Graphics p){
if(!this.living){ if(!this.living){
tf.bullets.remove(this); tf.bullets.remove(this);
} }
/*//获取画笔原来颜色 /*//获取画笔原来颜色
Color color = p.getColor(); Color color = p.getColor();
//设置将要画出的子弹颜色 //设置将要画出的子弹颜色
p.setColor(Color.red); p.setColor(Color.red);
p.fillOval(x,y,width,height); p.fillOval(x,y,width,height);
//画完子弹后将画笔颜色还原 //画完子弹后将画笔颜色还原
p.setColor(color);*/ p.setColor(color);*/
//将子弹根据方向画出来 //将子弹根据方向画出来
switch (dir) { switch (dir) {
case LEFT: case LEFT:
p.drawImage(ResouceMgr.bulletL, x, y, null); p.drawImage(ResouceMgr.bulletL, x, y, null);
break; break;
case UP: case UP:
p.drawImage(ResouceMgr.bulletU, x, y, null); p.drawImage(ResouceMgr.bulletU, x, y, null);
break; break;
case RIGHT: case RIGHT:
p.drawImage(ResouceMgr.bulletR, x, y, null); p.drawImage(ResouceMgr.bulletR, x, y, null);
break; break;
case DOWN: case DOWN:
p.drawImage(ResouceMgr.bulletD, x, y, null); p.drawImage(ResouceMgr.bulletD, x, y, null);
break; break;
} }
moving(); moving();
} }
public void moving(){ public void moving(){
//通过判断方向进行子弹的移动。 //通过判断方向进行子弹的移动。
switch(dir){ switch(dir){
case LEFT: case LEFT:
x-=SPEED; x-=SPEED;
break; break;
case UP: case UP:
y-=SPEED; y-=SPEED;
break; break;
case RIGHT: case RIGHT:
x+=SPEED; x+=SPEED;
break; break;
case DOWN: case DOWN:
y+=SPEED; y+=SPEED;
break; break;
default: default:
break; break;
} }
if(x<0 || y<0 || x > TankFrame.GAME_WIDTH || y>TankFrame.GAME_HEIGHT){ if(x<0 || y<0 || x > TankFrame.GAME_WIDTH || y>TankFrame.GAME_HEIGHT){
this.living =false; this.living =false;
} }
rect.x = this.x; rect.x = this.x;
rect.y = this.y; rect.y = this.y;
} }
@Override /**
public void collideWith(Tank t) { *
if(this.group.equals(t.getGroup())){ */
return; @Override
} public void collideWith(Tank t) {
if(this.group.equals(t.getGroup())){
if(this.rect.intersects(t.rect)){ return;
t.die(); }
this.die();
int eX = t.getX() + Tank.width/2 - Explode.width/2; if(this.rect.intersects(t.rect)){
int eY = t.getY() + Tank.height/2 - Explode.height/2; t.die();
tf.explodes.add(tf.gf.createExplode(eX, eY, tf)); this.die();
} int eX = t.getX() + Tank.width/2 - Explode.width/2;
} int eY = t.getY() + Tank.height/2 - Explode.height/2;
tf.explodes.add(tf.gf.createExplode(eX, eY, tf));
public TankGroup getGroup() { }
return group; }
}
public TankGroup getGroup() {
public void setGroup(TankGroup group) { return group;
this.group = group; }
}
public void setGroup(TankGroup group) {
private void die() { this.group = group;
this.living =false; }
}
} private void die() {
this.living =false;
}
}

Loading…
Cancel
Save