到了这里开始处理墙的问题了,
一、画墙
墙由一块块的砖组成,因此先定义一个wall
public class Wall {
/*
* 墙由一块一块的砖组成,将整个面板分为26*26个格子,每个格子10*10大小,得出界面大小
*/
int x;
int y;
int width = 10;
int height = 10;
boolean is_Dead;
wallKind kind;
Image wallImage;
enum wallKind {
cob, iron// 土墙,铁墙
}
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;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public boolean isIs_Dead() {
return is_Dead;
}
public void setIs_Dead(boolean is_Dead) {
this.is_Dead = is_Dead;
}
public wallKind getKind() {
return kind;
}
public void setKind(wallKind kind) {
this.kind = kind;
}
public Image getWallImage() {
return wallImage;
}
public void setWallImage(Image wallImage) {
this.wallImage = wallImage;
}
public Wall(int x, int y, wallKind kind) {//砖块的构造函数
super();
this.x = x;
this.y = y;
this.kind = kind;
}
public void draw() {//绘制砖块
switch (kind) {
case cob:
setWallImage(Toolkit.getDefaultToolkit().getImage("wall.gif"));break;
case iron:
setWallImage(Toolkit.getDefaultToolkit().getImage("steel.gif"));break;
}
}
}
然后再定义一个walls,储存墙的形状
public class Walls {
ArrayList<Wall> walls = new ArrayList<>();
int[][] wallArr = { {}, // 全0
{}, // 全0
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 }, // 3
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,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0, 1, 1, 0, 0 }, // 18
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 }, // 19
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 }, // 22
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 }, // 24
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 } };
public Walls() {//根据数组情况构造墙
super();
for (int i = 0; i < wallArr.length; i++) {
for (int j = 0; j < wallArr[i].length; j++) {
if (wallArr[i][j] == 1) {
walls.add(new Wall(j * 10, i * 10, wallKind.cob));
} else if (wallArr[i][j] == 3) {
walls.add(new Wall(j * 10, i * 10, wallKind.iron));
}
}
}
}
public void draw(Graphics g) {//绘制墙
for(int i= 0;i<walls.size();i++){
walls.get(i).draw();
g.drawImage(walls.get(i).getWallImage(), walls.get(i).x, walls.get(i).y, 10, 10, null);
}
}
}
这里用一个二维数组将墙的情况记录下来,然后对号入ArrayList,再绘制出来
二、坦克和墙的相撞
坦克撞墙不同于撞子弹,坦克和墙是不允许重合的,因此要在撞到之前控制其不要撞到,这要用到getNextRec()来获取以后的位置
@Override
public Rectangle getNextRec() {
switch(dir){
case U:return new Rectangle(local_x, local_y-speed, width, height);
case D:return new Rectangle(local_x, local_y+speed, width, height);
case L:return new Rectangle(local_x-speed, local_y, width, height);
case R:return new Rectangle(local_x+speed, local_y, width, height);
default:return new Rectangle(local_x, local_y, width, height);
}
}
EnemyTank同理,只是把dir改成pdir
然后写撞墙判断函数
@Override
public boolean is_CollisionWall(Walls wall) {
for(int i=0;i<wall.getWalls().size();i++){
if(this.getNextRec().intersects(wall.getWalls().get(i).getRec())){
return true;
}
}
return false;
}
涉及多个类交互,放到TankPanel中,因此在TankPanel中判断是否撞墙
mtank的判断,走之前判断是否撞墙
if (!mtank.is_CollisionWall(pwall)) {// 不撞墙才能走
mtank.move();
}
etank的判断
if (!etanks.get(i).is_CollisionWall(pwall)) {//判断是否撞墙------有可能一直撞墙
etanks.get(i).move();
}
这里出现了一个bug,由于之前etank的处理,dir==pdir,因此当其碰墙时就不走会导致永远卡在碰墙的前一个位置
改进如下:
if (!etanks.get(i).is_CollisionWall(pwall)) {//判断是否撞墙
etanks.get(i).move();
}else{
//改变移动方向
etanks.get(i).moveDir();
}
在EnemyTank中增加moveDir(),只改变移动方向而不移动位置,步数和开火照样计算,这样就解决了卡住的问题
public void moveDir() {// 只改变方向
step--;
fireNum--;
if (step == 0) {
step = r.nextInt(15) + 10;
dirNum = r.nextInt(4);
switch (dirNum) {
case 0:
setPdir(Direction.U);
break;
case 1:
setPdir(Direction.D);
break;
case 2:
setPdir(Direction.L);
break;
case 3:
setPdir(Direction.R);
break;
}
}
if (fireNum == 0) {
fire();
fireNum = r.nextInt(15) + 10;
}
}
三、子弹和墙的碰撞
在Bullet中写判断函数
public boolean is_CollisionWall(Walls wall) {
for (int i = 0; i < wall.getWalls().size(); i++) {
Wall w = wall.getWalls().get(i);
if (this.getNextRec().intersects(w.getRec())) {
if (w.getKind() == wallKind.cob) {
this.setIs_Dead(true);
w.setIs_Dead(true);
return true;
} else if (w.getKind() == wallKind.iron) {
this.setIs_Dead(true);
return true;
}
}
}
return false;
}
在子弹移动时加判断
if (!b.is_CollisionWall(pwall)) {
b.move();
}
walls的draw()函数增加判断死亡的代码
public void draw(Graphics g) {
for(int i= 0;i<walls.size();i++){
if(!walls.get(i).isIs_Dead()){
walls.get(i).draw();
g.drawImage(walls.get(i).getWallImage(), walls.get(i).x, walls.get(i).y, 10, 10, null);
}else{
walls.remove(i);
}
}
}
效果如下:
源码如下:
TankFrame:
public class TankFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static int Frame_Width = 260+6;
public static int Frame_Height = 260+29;
TankPanel TP = new TankPanel();
public void launchFrame() {
Container c = getContentPane();
setTitle("坦克大战");
// 窗口大小及位置
setSize(Frame_Width, Frame_Height);
setLocation(1000, 100);
add(TP);
addKeyListener(TP.mtank.kc);// 添加键盘控制
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
System.out.println(c.getSize());//可视面积
new Thread(TP).start();// 启动线程
}
public static void main(String[] args) {
new TankFrame().launchFrame();
}
}
TankPanel:
public class TankPanel extends JPanel implements Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
MyTank mtank = new MyTank(90, 240, false);// 我方坦克
ArrayList<EnemyTank> etanks = new ArrayList<>();// 敌方坦克
ArrayList<Boom> booms = new ArrayList<>();// 爆炸
Walls pwall = new Walls();// 墙
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(0, 0, TankFrame.Frame_Width, TankFrame.Frame_Height);
// 画墙
pwall.draw(g);
// 绘制我方坦克
mtank.draw();
g.drawImage(mtank.getTankImage(), mtank.getLocal_x(), mtank.getLocal_y(), mtank.getWidth(), mtank.getHeight(),
this);
if (!mtank.is_CollisionWall(pwall)) {// 不撞墙才能走
mtank.move();
}
// 绘制我方子弹
for (int i = 0; i < mtank.getBullets().size(); i++) {
Bullet b = mtank.getBullets().get(i);
for (int j = 0; j < etanks.size(); j++) {
if (b.is_CollisonTank(etanks.get(j))) {
//do nothing
}
}
if (b.isIs_Dead() == true) {
mtank.getBullets().remove(i);// 死亡则移除
} else {
b.draw();
g.drawImage(b.getBulletIamge(), b.getLocal_x(), b.getLocal_y(), Bullet.width, Bullet.height, this);
if (!b.is_CollisionWall(pwall)) {
b.move();
}
}
}
// 绘制敌方坦克及子弹
for (int i = 0; i < etanks.size(); i++) {
if (etanks.get(i).isIs_Dead()) {
booms.add(new Boom(etanks.get(i).getLocal_x(), etanks.get(i).getLocal_y()));// 添加爆炸
etanks.remove(i);
} else {
etanks.get(i).draw();
g.drawImage(etanks.get(i).getTankImage(), etanks.get(i).getLocal_x(), etanks.get(i).getLocal_y(),
etanks.get(i).getWidth(), etanks.get(i).getHeight(), this);
if (!etanks.get(i).is_CollisionWall(pwall)) {// 判断是否撞墙------有可能一直撞墙
etanks.get(i).move();
} else {
// 改变移动方向
etanks.get(i).moveDir();
}
// 绘制子弹
for (int j = 0; j < etanks.get(i).getBullets().size(); j++) {
Bullet b = etanks.get(i).getBullets().get(j);
if (b.is_CollisonTank(mtank)) {
System.out.println("我方被击中");
}
if (b.isIs_Dead()) {
etanks.get(i).getBullets().remove(j);
} else {
b.draw();
g.drawImage(b.getBulletIamge(), b.getLocal_x(), b.getLocal_y(), Bullet.width, Bullet.height,
this);
if (!b.is_CollisionWall(pwall)) {
b.move();
}
}
}
}
}
// 绘制爆炸
for (int i = 0; i < booms.size(); i++) {
if (booms.get(i).isIs_Dead()) {
booms.remove(i);
} else {
Boom boom = booms.get(i);
boom.draw();
g.drawImage(boom.getBoomImage(), boom.getX(), boom.getY(), boom.getWidth(), boom.getHeight(), this);
}
}
}
@Override
public void run() {
while (true) {
// 控制生成六只坦克
if (etanks.size() < 6) {
etanks.add(new EnemyTank());
}
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Tank:
public abstract class Tank {
// 位置
int local_x;
int local_y;
// 大小
int width = 20;
int height = 20;
// 速度
int speed = 3;
// 运动方向
Direction dir;
// 炮筒方向
Direction pdir;
// 坦克图片
Image tankImage;
// 敌我
boolean is_enemy;
// 死亡
boolean is_Dead;
// 子弹集
ArrayList<Bullet> bullets = new ArrayList<>();
public int getLocal_x() {
return local_x;
}
public void setLocal_x(int local_x) {
this.local_x = local_x;
}
public int getLocal_y() {
return local_y;
}
public void setLocal_y(int local_y) {
this.local_y = local_y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public Direction getDir() {
return dir;
}
public void setDir(Direction dir) {
this.dir = dir;
}
public Direction getPdir() {
return pdir;
}
public void setPdir(Direction pdir) {
this.pdir = pdir;
}
public Image getTankImage() {
return tankImage;
}
public void setTankImage(Image tankImage) {
this.tankImage = tankImage;
}
public boolean isIs_enemy() {
return is_enemy;
}
public void setIs_enemy(boolean is_enemy) {
this.is_enemy = is_enemy;
}
public boolean isIs_Dead() {
return is_Dead;
}
public void setIs_Dead(boolean is_Dead) {
this.is_Dead = is_Dead;
}
public ArrayList<Bullet> getBullets() {
return bullets;
}
public void setBullets(ArrayList<Bullet> bullets) {
this.bullets = bullets;
}
public abstract void draw();// 绘制相关函数(获取图片)
public abstract void move();// 移动函数
public abstract void fire();// 开火
public abstract boolean is_CollisionWall(Walls wall);//是否撞墙
public abstract Rectangle getRec();// 获取当前位置,返回对应矩形
public abstract Rectangle getNextRec();// 获取移动后的位置,返回对应矩形
}
MyTank:
package Tank;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import Bullet.Bullet;
import Direction.Direction;
import TankFrame.TankFrame;
import Wall.Walls;
public class MyTank extends Tank {
// 坦克移动方向
Direction dir = Direction.Stop;
// 坦克炮筒方向
Direction pdir = Direction.U;
//速度
int speed = 5;
public KeyContral kc = new KeyContral();
public MyTank(int local_x, int local_y, boolean is_enemy) {
super();
this.local_x = local_x;
this.local_y = local_y;
this.is_enemy = is_enemy;
}
@Override
public void draw() {
switch (pdir) {
case U:// 注意不能用Direction.U
setTankImage(Toolkit.getDefaultToolkit().getImage("p2tankU.gif"));
break;
case D:
setTankImage(Toolkit.getDefaultToolkit().getImage("p2tankD.gif"));
break;
case L:
setTankImage(Toolkit.getDefaultToolkit().getImage("p2tankL.gif"));
break;
case R:
setTankImage(Toolkit.getDefaultToolkit().getImage("p2tankR.gif"));
break;
default:
setTankImage(Toolkit.getDefaultToolkit().getImage("p2tankU.gif"));
}
}
@Override
public void move() {// 实现移动,并控制不越界
if (dir == Direction.U) {
if (local_y - speed >= 0)
setLocal_y(local_y - speed);
} else if (dir == Direction.D) {
if (local_y + speed <= TankFrame.Frame_Height - (height * 2 + height / 3))
setLocal_y(local_y + speed);
} else if (dir == Direction.L) {
if (local_x - speed >= 0)
setLocal_x(local_x - speed);
} else if (dir == Direction.R) {
if (local_x + speed <= TankFrame.Frame_Width - (width + width / 4))
setLocal_x(local_x + speed);
} else {
}
}
public class KeyContral extends KeyAdapter {// 按下方向改变
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_SPACE:// 空格开火
fire();
break;
case KeyEvent.VK_UP:
pdir = dir = Direction.U;
break;
case KeyEvent.VK_DOWN:
pdir = dir = Direction.D;
break;
case KeyEvent.VK_LEFT:
pdir = dir = Direction.L;
break;
case KeyEvent.VK_RIGHT:
pdir = dir = Direction.R;
break;
default:
dir = Direction.Stop;
}
}
@Override
public void keyReleased(KeyEvent e) {// 释放移动方向改变恢复,指向不改变
super.keyReleased(e);
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_UP:
dir = Direction.Stop;
break;
case KeyEvent.VK_DOWN:
dir = Direction.Stop;
break;
case KeyEvent.VK_LEFT:
dir = Direction.Stop;
break;
case KeyEvent.VK_RIGHT:
dir = Direction.Stop;
break;
}
}
}
@Override
public void fire() {
this.bullets.add(new Bullet(local_x + (width - Bullet.width) / 2, local_y + (height - Bullet.height) / 2, pdir,
is_enemy));// 子弹居中,稍微画图计算一下即可
}
@Override
public boolean is_CollisionWall(Walls wall) {
for(int i=0;i<wall.getWalls().size();i++){
if(this.getNextRec().intersects(wall.getWalls().get(i).getRec())){
return true;
}
}
return false;
}
@Override
public Rectangle getRec() {
return new Rectangle(local_x, local_y, width, height);
}
@Override
public Rectangle getNextRec() {
switch(dir){
case U:return new Rectangle(local_x, local_y-speed, width, height);
case D:return new Rectangle(local_x, local_y+speed, width, height);
case L:return new Rectangle(local_x-speed, local_y, width, height);
case R:return new Rectangle(local_x+speed, local_y, width, height);
default:return new Rectangle(local_x, local_y, width, height);
}
}
}
EnemyTank:
package Tank;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.Random;
import Bullet.Bullet;
import Direction.Direction;
import TankFrame.*;
import Wall.Walls;
public class EnemyTank extends Tank {
// 随机对象
Random r = new Random();
// 随机坐标
int x = r.nextInt(TankFrame.Frame_Width - 50);
int y = 0;
// 随机方向
int dirNum = r.nextInt(4);
int step = r.nextInt(15) + 10;
int fireNum = r.nextInt(15) + 10;
public EnemyTank() {
super();
// TODO Auto-generated constructor stub
setLocal_x(x);
setLocal_y(y);
switch (dirNum) {
case 0:
setPdir(Direction.U);
break;
case 1:
setPdir(Direction.D);
break;
case 2:
setPdir(Direction.L);
break;
case 3:
setPdir(Direction.R);
break;
}
setIs_enemy(true);
}
@Override
public void draw() {
switch (this.pdir) {
case U:
setTankImage(Toolkit.getDefaultToolkit().getImage("enemy1U.gif"));
break;
case D:
setTankImage(Toolkit.getDefaultToolkit().getImage("enemy1D.gif"));
break;
case L:
setTankImage(Toolkit.getDefaultToolkit().getImage("enemy1L.gif"));
break;
case R:
setTankImage(Toolkit.getDefaultToolkit().getImage("enemy1R.gif"));
break;
default:
setTankImage(Toolkit.getDefaultToolkit().getImage("enemy1U.gif"));
}
}
@Override
public void move() {
switch (pdir) {
case U:
if (local_y - speed >= 0)
setLocal_y(local_y - speed);
break;
case D:
if (local_y + speed <= TankFrame.Frame_Height - (height * 2 + height / 3))
setLocal_y(local_y + speed);
break;
case L:
if (local_x - speed >= 0)
setLocal_x(local_x - speed);
break;
case R:
if (local_x + speed <= TankFrame.Frame_Width - (width + width / 4))
setLocal_x(local_x + speed);
break;
default:// do nothing
}
// 随机行走的关键
step--;
if (step == 0) {
step = r.nextInt(15) + 10;
dirNum = r.nextInt(4);
switch (dirNum) {
case 0:
setPdir(Direction.U);
break;
case 1:
setPdir(Direction.D);
break;
case 2:
setPdir(Direction.L);
break;
case 3:
setPdir(Direction.R);
break;
}
}
// 实现随机开火
fireNum--;
if (fireNum == 0) {
fire();
fireNum = r.nextInt(15) + 10;
}
}
public void moveDir() {// 只改变方向
step--;
fireNum--;
if (step == 0) {
step = r.nextInt(15) + 10;
dirNum = r.nextInt(4);
switch (dirNum) {
case 0:
setPdir(Direction.U);
break;
case 1:
setPdir(Direction.D);
break;
case 2:
setPdir(Direction.L);
break;
case 3:
setPdir(Direction.R);
break;
}
}
if (fireNum == 0) {
fire();
fireNum = r.nextInt(15) + 10;
}
}
@Override
public void fire() {
this.bullets.add(new Bullet(local_x + (width - Bullet.width) / 2, local_y + (height - Bullet.height) / 2, pdir,
is_enemy));
}
@Override
public boolean is_CollisionWall(Walls wall) {
for (int i = 0; i < wall.getWalls().size(); i++) {
if (this.getNextRec().intersects(wall.getWalls().get(i).getRec())) {
return true;
}
}
return false;
}
@Override
public Rectangle getRec() {
return new Rectangle(local_x, local_y, width, height);
}
@Override
public Rectangle getNextRec() {
switch (pdir) {
case U:
return new Rectangle(local_x, local_y - speed, width, height);
case D:
return new Rectangle(local_x, local_y + speed, width, height);
case L:
return new Rectangle(local_x - speed, local_y, width, height);
case R:
return new Rectangle(local_x + speed, local_y, width, height);
default:
return new Rectangle(local_x, local_y, width, height);
}
}
}
Bullet:
package Bullet;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import Direction.Direction;
import Tank.Tank;
import TankFrame.*;
import Wall.*;
import Wall.Wall.wallKind;
public class Bullet {
// 位置
int local_x;
int local_y;
// 大小
public static int width = 4;
public static int height = 4;
// 方向
Direction dir;
// 速度
int speed = 8;
// 存活
boolean is_Dead;
// 敌我
boolean is_enemy;
// 图片
Image BulletIamge;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getLocal_x() {
return local_x;
}
public void setLocal_x(int local_x) {
this.local_x = local_x;
}
public int getLocal_y() {
return local_y;
}
public void setLocal_y(int local_y) {
this.local_y = local_y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Direction getDir() {
return dir;
}
public void setDir(Direction dir) {
this.dir = dir;
}
public boolean isIs_Dead() {
return is_Dead;
}
public void setIs_Dead(boolean is_Dead) {
this.is_Dead = is_Dead;
}
public boolean isIs_enemy() {
return is_enemy;
}
public void setIs_enemy(boolean is_enemy) {
this.is_enemy = is_enemy;
}
public Image getBulletIamge() {
return BulletIamge;
}
public void setBulletIamge(Image bulletIamge) {
BulletIamge = bulletIamge;
}
public Bullet(int local_x, int local_y, Direction dir, boolean is_enemy) {
super();
this.local_x = local_x;
this.local_y = local_y;
this.dir = dir;
this.is_enemy = is_enemy;
}
public void draw() {// 获取子弹图片
if (!isIs_enemy()) {
setBulletIamge(Toolkit.getDefaultToolkit().getImage("tankmissile.gif"));
} else {
setBulletIamge(Toolkit.getDefaultToolkit().getImage("enemymissile.gif"));
}
}
public void move() {
if (this.local_x < 0 || this.local_x > TankFrame.Frame_Width || this.local_y < 0
|| this.local_y > TankFrame.Frame_Height) {
setIs_Dead(true);// 触边界死亡
} else {
switch (this.dir) {
case U:
setLocal_y(local_y - speed);
break;
case D:
setLocal_y(local_y + speed);
break;
case L:
setLocal_x(local_x - speed);
break;
case R:
setLocal_x(local_x + speed);
break;
default:
setLocal_y(local_y - speed);
}
}
}
public boolean is_CollisionWall(Walls wall) {
for (int i = 0; i < wall.getWalls().size(); i++) {
Wall w = wall.getWalls().get(i);
if (this.getNextRec().intersects(w.getRec())) {
if (w.getKind() == wallKind.cob) {
this.setIs_Dead(true);
w.setIs_Dead(true);
return true;
} else if (w.getKind() == wallKind.iron) {
this.setIs_Dead(true);
return true;
}
}
}
return false;
}
public Rectangle getRec() {
return new Rectangle(local_x, local_y, width, height);
}
public Rectangle getNextRec() {
switch(dir){
case U:return new Rectangle(local_x, local_y-speed, width, height);
case D:return new Rectangle(local_x, local_y+speed, width, height);
case L:return new Rectangle(local_x-speed, local_y, width, height);
case R:return new Rectangle(local_x+speed, local_y, width, height);
default:return new Rectangle(local_x, local_y, width, height);
}
}
public boolean is_CollisonTank(Tank tank) {
if (tank.getRec().intersects(getRec()) && (tank.isIs_enemy() != isIs_enemy())) {
tank.setIs_Dead(true);
setIs_Dead(true);
return true;
}
return false;
}
}
Wall:
package Wall;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
public class Wall {
/*
* 墙由一块一块的砖组成,将整个面板分为26*26个格子,每个格子10*10大小,得出界面大小
*/
int x;
int y;
int width = 10;
int height = 10;
boolean is_Dead;
wallKind kind;
Image wallImage;
public enum wallKind {
cob, iron// 土墙,铁墙
}
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;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public boolean isIs_Dead() {
return is_Dead;
}
public void setIs_Dead(boolean is_Dead) {
this.is_Dead = is_Dead;
}
public wallKind getKind() {
return kind;
}
public void setKind(wallKind kind) {
this.kind = kind;
}
public Image getWallImage() {
return wallImage;
}
public void setWallImage(Image wallImage) {
this.wallImage = wallImage;
}
public Wall(int x, int y, wallKind kind) {
super();
this.x = x;
this.y = y;
this.kind = kind;
}
public void draw() {
switch (kind) {
case cob:
setWallImage(Toolkit.getDefaultToolkit().getImage("wall.gif"));break;
case iron:
setWallImage(Toolkit.getDefaultToolkit().getImage("steel.gif"));break;
}
}
public Rectangle getRec(){
return new Rectangle(x, y, width, height);
}
}
Walls:
package Wall;
import java.awt.Graphics;
import java.util.ArrayList;
import Wall.Wall.wallKind;
public class Walls {
ArrayList<Wall> walls = new ArrayList<>();
public ArrayList<Wall> getWalls() {
return walls;
}
public void setWalls(ArrayList<Wall> walls) {
this.walls = walls;
}
int[][] wallArr = { {}, // 全0
{}, // 全0
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 }, // 3
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 3, 3, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 }, // 8
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 }, // 10
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,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 1, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 }, // 24
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0 } };
public Walls() {
super();
for (int i = 0; i < wallArr.length; i++) {
for (int j = 0; j < wallArr[i].length; j++) {
if (wallArr[i][j] == 1) {
walls.add(new Wall(j * 10, i * 10, wallKind.cob));
} else if (wallArr[i][j] == 3) {
walls.add(new Wall(j * 10, i * 10, wallKind.iron));
}
}
}
}
public void draw(Graphics g) {
for(int i= 0;i<walls.size();i++){
if(!walls.get(i).isIs_Dead()){
walls.get(i).draw();
g.drawImage(walls.get(i).getWallImage(), walls.get(i).x, walls.get(i).y, 10, 10, null);
}else{
walls.remove(i);
}
}
}
}
Direction:
package Direction;
public enum Direction {
U,D,L,R,Stop
}