course3 爆炸效果

master
Jian Hu 3 years ago
parent d77b7b1ed2
commit a9c714596a

Binary file not shown.

@ -9,12 +9,14 @@ public class Bullet {
public static final int WIDTH = ResourceManager.bulletD.getWidth();
public static final int HEIGHT = ResourceManager.bulletD.getHeight();
private boolean live = true;
private Group group = Group.BAD;
private TankFrameV3 tf;
public Bullet(int x, int y, Direction direction, TankFrameV3 tf) {
public Bullet(int x, int y, Direction direction, Group group, TankFrameV3 tf) {
this.x = x;
this.y = y;
this.direction = direction;
this.group = group;
this.tf = tf;
}
@ -59,6 +61,8 @@ public class Bullet {
//检测是否跟坦克碰撞
public void collideWith(Tank tank){
//关闭队友伤害
if(this.group == tank.getGroup()) return;
Rectangle bulletRect = new Rectangle(x, y, WIDTH, HEIGHT);
Rectangle tankRect = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT);
if(bulletRect.intersects(tankRect)){
@ -102,4 +106,12 @@ public class Bullet {
public void setLive(boolean live) {
this.live = live;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
}

@ -0,0 +1,43 @@
package com.demo.tank.course3;
import java.awt.*;
public class Explode {
private int x, y;
public static final int WIDTH = ResourceManager.explodes[0].getWidth();
public static final int HEIGHT = ResourceManager.explodes[0].getHeight();
private boolean living = true;
private TankFrameV3 tf;
private int step = 0;
public Explode(int x, int y, TankFrameV3 tf) {
this.x = x;
this.y = y;
this.tf = tf;
}
public void paint(Graphics g){
g.drawImage(ResourceManager.explodes[step++], x, y, null);
if(step >= ResourceManager.explodes.length){
step = 0;
}
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}

@ -0,0 +1,5 @@
package com.demo.tank.course3;
public enum Group {
GOOD, BAD
}

@ -6,7 +6,7 @@ public class MainV3 {
public static void main(String[] args) throws InterruptedException, IOException {
TankFrameV3 tf = new TankFrameV3();
for(int i = 0; i < 5; i++){
tf.enemyTanks.add(new Tank(50 + i*80, 200, Direction.DOWN, tf));
tf.enemyTanks.add(new Tank(50 + i*80, 200, Direction.DOWN, Group.BAD, tf));
}
while (true){

@ -7,6 +7,7 @@ import java.io.IOException;
public class ResourceManager {
public static BufferedImage tankL, tankR, tankU, tankD;
public static BufferedImage bulletL, bulletR, bulletU, bulletD;
public static BufferedImage[] explodes = new BufferedImage[16];
static {
try {
@ -19,6 +20,10 @@ public class ResourceManager {
bulletR = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/bulletR.gif"));
bulletU = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/bulletU.gif"));
bulletD = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/bulletD.gif"));
for (int i= 0; i<16; i++){
explodes[i] = ImageIO.read(ResourceManager.class.getClassLoader().getResourceAsStream("com/demo/tank/images/e"+ (i+1)+".gif"));
}
} catch (IOException e) {
e.printStackTrace();
}

@ -1,22 +1,25 @@
package com.demo.tank.course3;
import java.awt.*;
import java.util.Random;
public class Tank {
private int x,y;
private Direction dir;
private static final int SPEED = 10;
private boolean moving;
private static final int SPEED = 8;
private boolean moving = false;
private boolean living = true;
private Group group = Group.BAD;
private TankFrameV3 tankFrame;
public static final int WIDTH = ResourceManager.tankD.getWidth();
public static final int HEIGHT = ResourceManager.tankD.getHeight();
private Random random = new Random();
public Tank(int x, int y, Direction dir, TankFrameV3 tankFrame) {
public Tank(int x, int y, Direction dir, Group group, TankFrameV3 tankFrame) {
this.x = x;
this.y = y;
this.dir = dir;
this.group = group;
this.tankFrame = tankFrame;
}
@ -55,6 +58,13 @@ public class Tank {
default:
break;
}
// if(random.nextInt(10) > 8) this.fire();
}
public void fire() {
int bx = x + Tank.WIDTH/2 - Bullet.WIDTH/2;
int by = y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
tankFrame.bullets.add(new Bullet(bx, by, this.dir, this.group, tankFrame));
}
public void die(){
@ -101,9 +111,11 @@ public class Tank {
this.living = living;
}
public void fire() {
int bx = x + Tank.WIDTH/2 - Bullet.WIDTH/2;
int by = y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
tankFrame.bullets.add(new Bullet(bx, by, this.dir, tankFrame));
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
}

@ -13,10 +13,11 @@ public class TankFrameV3 extends Frame {
public static final int GAME_HEIGHT = 600;
Image image = null;
Tank tank = new Tank(500, 500, Direction.UP, this);
Tank tank = new Tank(500, 500, Direction.UP, Group.GOOD,this);
// Bullet bullet = new Bullet(520, 440, Direction.UP);
List<Bullet> bullets = new ArrayList();
List<Tank> enemyTanks = new ArrayList<>();
Explode explode = new Explode(200, 300, this);
public TankFrameV3(){
setVisible(true);
@ -57,6 +58,8 @@ public class TankFrameV3 extends Frame {
tank.paint(g);
explode.paint(g);
for(int i = 0; i< enemyTanks.size(); i++){
enemyTanks.get(i).paint(g);
}

Loading…
Cancel
Save