坦克大战(一期)-让敌方也打出子弹

Network
bingor_yhj 2 years ago
parent dbee942b01
commit 4a687543fa

@ -14,14 +14,16 @@ public class Bullet {
private DirEnum dir; private DirEnum dir;
public static final int WIDTH = ResourcesMgr.bulletD.getWidth(); public static final int WIDTH = ResourcesMgr.bulletD.getWidth();
public static final int HEIGHT = ResourcesMgr.bulletD.getHeight(); public static final int HEIGHT = ResourcesMgr.bulletD.getHeight();
public static final int SPEED = 5; public static final int SPEED = 8;
private boolean live = true; private boolean live = true;
private GroupEnum group = GroupEnum.BAD;
private TankFrame tankFrame; private TankFrame tankFrame;
public Bullet(int x, int y, DirEnum dir, TankFrame tankFrame) { public Bullet(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.dir = dir; this.dir = dir;
this.group = group;
this.tankFrame = tankFrame; this.tankFrame = tankFrame;
} }
@ -71,6 +73,10 @@ public class Bullet {
//碰撞 //碰撞
public void collide(Tank tank) { public void collide(Tank tank) {
if(this.group == tank.getGroup()) return;
//TODO 每次碰撞的时候都小new 一个 Rectangle,应该用一个Rectangle来记录子弹的位置产生内存占用垃圾回收器会时不时运行一下
Rectangle bullteRec = new Rectangle(this.x, this.y, WIDTH, HEIGHT); Rectangle bullteRec = new Rectangle(this.x, this.y, WIDTH, HEIGHT);
Rectangle tankRec = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT); Rectangle tankRec = new Rectangle(tank.getX(), tank.getY(), Tank.WIDTH, Tank.HEIGHT);
if(bullteRec.intersects(tankRec)) { if(bullteRec.intersects(tankRec)) {

@ -0,0 +1,11 @@
package com.msb;
/**
* @Author bingor
* @Date 2022-10-04 17:12
* @Description: com.msb
* @Version: 1.0
*/
public enum GroupEnum {
GOOD, BAD
}

@ -1,6 +1,7 @@
package com.msb; package com.msb;
import java.awt.*; import java.awt.*;
import java.util.Random;
/** /**
* @Author bingor * @Author bingor
@ -12,18 +13,21 @@ public class Tank {
private int x,y; private int x,y;
private DirEnum dir; private DirEnum dir;
private static final int SPEED = 10; private static final int SPEED = 5;
private boolean move = false; private boolean move = true;
//为了解决能够在坦克中发射子弹,将创建的子弹通过坦克发射出来,那么需要在坦克类中持有游戏窗口的引用 //为了解决能够在坦克中发射子弹,将创建的子弹通过坦克发射出来,那么需要在坦克类中持有游戏窗口的引用
private TankFrame tankFrame; private TankFrame tankFrame;
public static final int WIDTH = ResourcesMgr.tankD.getWidth(); public static final int WIDTH = ResourcesMgr.tankD.getWidth();
public static final int HEIGHT = ResourcesMgr.tankD.getHeight(); public static final int HEIGHT = ResourcesMgr.tankD.getHeight();
private boolean live = true; private boolean live = true;
private Random random = new Random();
private GroupEnum group = GroupEnum.BAD;
public Tank(int x, int y, DirEnum dir, TankFrame tankFrame) { public Tank(int x, int y, DirEnum dir, GroupEnum group, TankFrame tankFrame) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.dir = dir; this.dir = dir;
this.group = group;
this.tankFrame = tankFrame; this.tankFrame = tankFrame;
} }
@ -64,6 +68,8 @@ public class Tank {
default: break; default: break;
} }
if(random.nextInt(10) > 8) this.fire();
} }
public void setMove(boolean move) { public void setMove(boolean move) {
@ -74,7 +80,7 @@ public class Tank {
//子弹在坦克每个方向上的中心点应该是不一样的. TODO //子弹在坦克每个方向上的中心点应该是不一样的. TODO
int bX = this.x + WIDTH/2 - Bullet.WIDTH/2; int bX = this.x + WIDTH/2 - Bullet.WIDTH/2;
int bY = this.y+HEIGHT/2-Bullet.HEIGHT/2; int bY = this.y+HEIGHT/2-Bullet.HEIGHT/2;
tankFrame.bullets.add(new Bullet(bX, bY, this.dir, tankFrame)); tankFrame.bullets.add(new Bullet(bX, bY, this.dir, this.group, tankFrame));
} }
public int getX() { public int getX() {
@ -88,4 +94,8 @@ public class Tank {
public void die() { public void die() {
this.live = false; this.live = false;
} }
public GroupEnum getGroup() {
return group;
}
} }

@ -13,7 +13,7 @@ public class TankDemo {
//创建5个敌方坦克 //创建5个敌方坦克
for (int i=0; i<5; i++) { for (int i=0; i<5; i++) {
tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.DOWN, tankFrame)); tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.DOWN, GroupEnum.BAD, tankFrame));
} }
//现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑 //现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑

@ -17,7 +17,7 @@ import java.util.Objects;
*/ */
public class TankFrame extends Frame { public class TankFrame extends Frame {
Tank tank = new Tank(100, 500, DirEnum.RIGHT, this); Tank myTank = new Tank(100, 500, DirEnum.RIGHT, GroupEnum.GOOD, this);
// Bullet bullet = new Bullet(200, 200, DirEnum.DOWN); // Bullet bullet = new Bullet(200, 200, DirEnum.DOWN);
List<Bullet> bullets = new ArrayList<>(); List<Bullet> bullets = new ArrayList<>();
List<Tank> tanks = new ArrayList<>(); //敌方坦克 List<Tank> tanks = new ArrayList<>(); //敌方坦克
@ -52,7 +52,7 @@ public class TankFrame extends Frame {
g.drawString("子弹的数量:" + bullets.size(), 10, 60); g.drawString("子弹的数量:" + bullets.size(), 10, 60);
g.drawString("敌方坦克数量:" + tanks.size(), 10, 90); g.drawString("敌方坦克数量:" + tanks.size(), 10, 90);
g.setColor(color); g.setColor(color);
tank.paint(g); myTank.paint(g);
//画出敌方坦克 //画出敌方坦克
for (int i=0; i<tanks.size(); i++) { for (int i=0; i<tanks.size(); i++) {
@ -117,7 +117,7 @@ public class TankFrame extends Frame {
case KeyEvent.VK_RIGHT : bR = false; break; case KeyEvent.VK_RIGHT : bR = false; break;
case KeyEvent.VK_UP : bU = false; break; case KeyEvent.VK_UP : bU = false; break;
case KeyEvent.VK_DOWN : bD = false; break; case KeyEvent.VK_DOWN : bD = false; break;
case KeyEvent.VK_SPACE: tank.fire(); break; case KeyEvent.VK_SPACE: myTank.fire(); break;
default: break; default: break;
} }
setDir(); setDir();
@ -126,16 +126,16 @@ public class TankFrame extends Frame {
private void setDir() { private void setDir() {
//当没有任何方向键按下的时候,那么让坦克停止 //当没有任何方向键按下的时候,那么让坦克停止
if( !bL && !bR && !bU && !bD ) { if( !bL && !bR && !bU && !bD ) {
tank.setMove(false); myTank.setMove(false);
} else { } else {
tank.setMove(true); myTank.setMove(true);
} }
//为什么不用if-else 因为有可能是一起按两个方向键,那最后拿到哪个算哪个 //为什么不用if-else 因为有可能是一起按两个方向键,那最后拿到哪个算哪个
if(bL) tank.setDir(DirEnum.LEFT); if(bL) myTank.setDir(DirEnum.LEFT);
if(bR) tank.setDir(DirEnum.RIGHT); if(bR) myTank.setDir(DirEnum.RIGHT);
if(bU) tank.setDir(DirEnum.UP); if(bU) myTank.setDir(DirEnum.UP);
if(bD) tank.setDir(DirEnum.DOWN); if(bD) myTank.setDir(DirEnum.DOWN);
} }
} }

Loading…
Cancel
Save