JAVA坦克大作战
- 源码
- src/com/mr/
- frame
- ChoosePanel.java
- LoginPanel.java
- MainFrame.java
- Mb.java
- StartGamePanel.java
- main
- model
- Bullet.java
- Tank.java
- TankEnemy.java
- TankPlayer.java
- Visible.java
- Wall.java
- XY.java
- wall
- Air.java
- Base.java
- Brick.java
- Grass.java
- Iron.java
- River.java
- type
- Direction.java
- TypeTank.java
- WallType.java
- util
- ImageUtil.java
- MapIO.java
- image
源码
src/com/mr/
frame
ChoosePanel.java
package com.mr.frame;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import com.mr.util.ImageUtil;
import com.mr.util.MapIO;
public class ChoosePanel extends JPanel implements KeyListener{
/**
*
*/
private MainFrame frame;// 主窗体
private Image backgroud;// 背景图片
int nowChoose = 0;
String[] lstring = MapIO.readMapList();//地图列表
List nowString =new ArrayList<>();
int nowPage = 1;
private static final long serialVersionUID = 4866596234392832441L;
public ChoosePanel(MainFrame frame) {
this.frame = frame;
addListener();// 添加组件监听
try {
backgroud = ImageIO.read(new File(ImageUtil.LOGIN_LIST_URL));// 读取背景图片
} catch (IOException e) {
e.printStackTrace();
}
}
private void addListener() {
frame.addKeyListener(this);// 主窗体载入键盘监听
}
@Override
public void paint(Graphics g) {
//绘制文字
g.drawImage(backgroud, 0, 0, getWidth(), getHeight(), this);// 绘制背景图片,填满整个面板
Font font = new Font("微软雅黑", Font.BOLD, 30);// 创建体字
g.setFont(font);// 使用字体
int startX = 200;
int startY = 100;
int nowY = startY;
nowString.clear();
for (int i=0; i<10; i++) {
if ((nowPage-1)*10+i
LoginPanel.java
package com.mr.frame;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import com.mr.util.ImageUtil;
/**
* 登陆面板(选择游戏模式)
*
* @author www.mingrisoft.com
*
*/
public class LoginPanel extends JPanel implements KeyListener {
/**
*
*/
private static final long serialVersionUID = -2946508653724645682L;
private MainFrame frame;// 主窗体
private Image backgroud;// 背景图片
private Image tank;// 坦克图标
private int tankY =370;// 坦克图标Y坐标
/**
* 登陆面板构造方法
*
* @param frame
* - 主窗体
*/
public LoginPanel(MainFrame frame) {
this.frame = frame;
addListener();// 添加组件监听
try {
backgroud = ImageIO.read(new File(ImageUtil.LOGIN_BACKGROUD_IMAGE_URL));// 读取背景图片
tank = ImageIO.read(new File(ImageUtil.PLAYER1_RIGHT_IMAGE_URL));// 读取坦克图标
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 重写绘图方法
*/
@Override
public void paint(Graphics g) {
g.drawImage(backgroud, 0, 0, getWidth(), getHeight(), this);// 绘制背景图片,填满整个面板
Font font = new Font("黑体", Font.BOLD, 35);// 创建体字
g.setFont(font);// 使用字体
int xWord = 320;
g.setColor(Color.WHITE);// 使用白色
g.drawString("开始", xWord, 400);// 绘制第1行
g.drawString("地图管理", xWord, 460);// 绘制第2行
g.drawString("设置", xWord, 520);// 绘制第3行
g.drawImage(tank, xWord - 70, tankY, this);// 绘制坦克图标
}
/**
* 跳转关卡面板
*/
/**
* 添加组件监听
*/
private void addListener() {
frame.addKeyListener(this);// 主窗体载入键盘监听
}
/**
* 当按键按下时
*/
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();// 获取按下的按键值
switch (code) {// 判断按键值
case KeyEvent.VK_W:// 如果按下的是“W”,效果同下
case KeyEvent.VK_UP:// 如果按下的是“↑”,效果同下
if (tankY == 370) {// 如果坦克图标在第一个位置
tankY = 490;
} else {
tankY -= 60;
}
break;
case KeyEvent.VK_S:// 如果按下的是“S”,效果同下
case KeyEvent.VK_DOWN:
if (tankY == 490) {
tankY = 370;
}
else {
tankY += 60;
}
break;
case KeyEvent.VK_Y:// 如果按下的是“Y”,效果同下
case KeyEvent.VK_NUMPAD1:// 如果按下的是小键盘1,效果同下
case KeyEvent.VK_ENTER:// 如果按下的是“Enter”,效果同下
if (tankY==370) {
setPanel(new ChoosePanel(frame));
frame.removeKeyListener(this);
}
break;
}
repaint();
}
/**
* 按键抬起时
*/
@Override
public void keyReleased(KeyEvent e) {
// 不实现此方法,但不可删除
}
/**
* 键入某按键事件
*/
@Override
public void keyTyped(KeyEvent e) {
// 不实现此方法,但不可删除
}
public void setPanel(JPanel panel) {
Container c = frame.getContentPane();// 获取主容器对象
c.removeAll();// 删除容器中所有组件
c.add(panel);// 容器添加面板
c.validate();// 容器重新验证所有组件
}
}
MainFrame.java
package com.mr.frame;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* 主窗体
*
* @author
*
*/
public class MainFrame extends JFrame {
private static final long serialVersionUID = 4301569215191263999L;
/**
* 构造方法
*/
public MainFrame() {
setTitle("坦克大战");// 设置标题
setSize(800, 600);// 设置宽高
setResizable(false);// 不可调整大小
Toolkit tool = Toolkit.getDefaultToolkit(); // 创建系统该默认组件工具包
Dimension d = tool.getScreenSize(); // 获取屏幕尺寸,赋给一个二维坐标对象
// 让主窗体在屏幕中间显示
setLocation((d.width - getWidth()) / 2, (d.height - getHeight()) / 2);
setVisible(true);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);// 关闭窗体时无操作
addListener();// 添加事件监听
setPanel(new LoginPanel(this));
}
/**
* 添加组件监听
*/
private void addListener() {
addWindowListener(new WindowAdapter() {// 添加窗体事件监听
public void windowClosing(WindowEvent e) {// 窗体关闭时
int closeCode = JOptionPane.showConfirmDialog(MainFrame.this, "是否退出游戏?", "提示!",
JOptionPane.YES_NO_OPTION);// 弹出选择对话框,并记录用户选择
if (closeCode == JOptionPane.YES_OPTION) {// 如果用户选择确定
System.exit(0);// 关闭程序
}
}
});
}
public void setPanel(JPanel panel) {
Container c = getContentPane();// 获取主容器对象
c.removeAll();// 删除容器中所有组件
c.add(panel);// 容器添加面板
c.validate();// 容器重新验证所有组件
}
}
Mb.java
package com.mr.frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
/**
* 按键捕捉模板
* @author Administrator
*
*/
public class Mb implements KeyListener{
public Mb() {
JFrame fr = new JFrame();
startlistern(fr);
fr.setVisible(true);
fr.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void startlistern(JFrame fr) {
fr.addKeyListener(this);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void keyPressed(KeyEvent e) {
// TODO 按键按下时
switch (e.getKeyCode()) {
case KeyEvent.VK_P:
System.out.println("p");
break;
default:
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO 按键放开时
System.out.println("d");
}
}
StartGamePanel.java
package com.mr.frame;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JPanel;
import com.mr.model.TankEnemy;
import com.mr.model.TankPlayer;
import com.mr.model.Wall;
import com.mr.model.wall.Base;
import com.mr.type.Direction;
import com.mr.type.TypeTank;
import com.mr.type.WallType;
import com.mr.util.MapIO;
import com.mr.model.*;
public class StartGamePanel extends JPanel implements KeyListener{
private static final long serialVersionUID = 231115635568170143L;
private MainFrame frame;// 主窗体
Graphics2D g2;
public static final int FRESH = 20;
private Thread thread;
private boolean up_key, down_key, right_key, left_key, space_key;
private List walls;
private TankPlayer player;
private List enemies;
private List bullets;//子弹
private int botCount = 20;//剩余坦克数
private String mapName;
private boolean isLive;
private int createBotTimer=0;//计时器
private int moveSpeed=2;
private String mapContent;
public StartGamePanel(MainFrame frame, String mapName) {
this.frame = frame;
this.mapName = mapName;
this.isLive = true;
bullets = new ArrayList<>();
readWalls();
readTank();
BaseTest();
addListener();// 添加组件监听
}
public void readTank() {
player = MapIO.readTankPlayer(mapContent);
enemies = MapIO.readTankEnemy(mapContent);
}
public void readWalls() {
mapContent = MapIO.readMap(mapName);
walls = MapIO.readMapWall(mapContent);
}
public void BaseTest() {
Wall ls1=null,ls2;
for (int i = 0; i < walls.size(); i++) {
if (walls.get(i).type==WallType.base) {
ls1=walls.get(i);
}
}
if(ls1==null) {
ls1=new Base(720, 500, 40, 40);
walls.add(ls1);
}
for (int i = 0; i < walls.size(); i++) {
ls2=walls.get(i);
if (!ls1.equals(ls2) && ls1.hit(ls2)) {
walls.remove(i);
i--;
}
}
}
public boolean baseDie() {
for (int i = 0; i < walls.size(); i++) {
if (walls.get(i).type == WallType.base && walls.get(i).die == true) {
return true;
}
}
return false;
}
public void tankTest() {
Tank ls1;
Wall ls2;
for (int i = 0; i < enemies.size(); i++) {
ls1 = enemies.get(i);
for (int j = 0; j < walls.size(); j++) {
ls2=walls.get(j);
if (ls1.hit(ls2)) {
walls.remove(j);
j--;
}
}
}
ls1=player;
for (int j = 0; j < walls.size(); j++) {
ls2=walls.get(j);
if (ls1.hit(ls2)) {
walls.remove(j);
j--;
}
}
}
/**
* 重写绘图方法
*/
@Override
public void paint(Graphics g) {
g2 = (Graphics2D)g;
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, 800, 600);
paintWalls();
paintTankPlayer();
paintBullets();
paintEnemies();
paintWord();
if (enemies.size()==0) {
paintWin();
isLive=false;
}
if (!player.live || baseDie()) {
paintDie();
isLive=false;
}
}
public void paintWord() {
Font font = new Font("楷体", Font.BOLD, 20);// 创建体字
g2.setFont(font);// 使用字体
g2.setColor(Color.black);
g2.drawString("剩余坦克数:" + String.valueOf(botCount), 300, 20);
}
/**
* 打印墙
*/
public void paintBullets() {
for (int i = 0; i < bullets.size(); i++) {
Bullet ls=bullets.get(i);
g2.drawImage(ls.image, ls.x, ls.y, ls.width, ls.height, this);
}
}
public void paintEnemies() {
TankEnemy ls1;
for (int i = 0; i < enemies.size(); i++) {
ls1 = enemies.get(i);
if (ls1.live) {
g2.drawImage(ls1.image, ls1.x, ls1.y, ls1.width, ls1.height, this);
}else {
enemies.remove(i);
i--;
}
}
}
public void paintWalls() {
for (int i = 0; i < walls.size(); i++) {
Wall ls= walls.get(i);
if (!ls.die) {
g2.drawImage(ls.image, ls.x, ls.y, ls.width, ls.height, this);
} else {
if (ls.type != WallType.base) {
walls.remove(i);
i--;
}
}
}
}
public void paintTankPlayer() {
if (player.live) {
g2.drawImage(player.image, player.x, player.y, player.width, player.height, this);
}
}
public void paintWin() {
Font font = new Font("微软雅黑", Font.BOLD, 60);// 创建体字
g2.setFont(font);// 使用字体
g2.setColor(Color.BLACK);
g2.drawString("你赢了", 300, 200);
}
public void paintDie() {
Font font = new Font("微软雅黑", Font.BOLD, 60);// 创建体字
g2.setFont(font);// 使用字体
g2.setColor(Color.BLACK);
g2.drawString("你输了", 300, 200);
}
/**
* 载入其他窗口
* @param panel
*/
public void setPanel(JPanel panel) {
Container c = frame.getContentPane();// 获取主容器对象
c.removeAll();// 删除容器中所有组件
c.add(panel);// 容器添加面板
c.validate();// 容器重新验证所有组件
}
//机器攻击及移动事件
public void create() {
for (int i = 0; i < enemies.size(); i++) {
TankEnemy ls = enemies.get(i);
if (ls.attackDooldown == 0) {
bullets.add(ls.getHead());
ls.attackDooldown = 1000;
}
if (createBotTimer%20 == 0 && createBotTimer!=0) {
int lx=ls.x,ly=ls.y;
if (ls.where == Direction.UP) {
ls.y -=moveSpeed;
}else if (ls.where == Direction.DOWN) {
ls.y +=moveSpeed;
}else if (ls.where == Direction.LEFT) {
ls.x -=moveSpeed;
}else {
ls.x +=moveSpeed;
}
ls.setHit();
if (hitTank(ls) || hitWall(ls)) {
ls.x=lx;
ls.y=ly;
ls.randomMove();
break;
}
}
if (createBotTimer%2000==0 && createBotTimer!=0) {
ls.randomMove();
}
}
}
/**
* 添加组件监听
*/
private void addListener() {
frame.addKeyListener(this);// 主窗体载入键盘监听
thread = new Run();
thread.start();
}
/**
* 线程
* @author Administrator
*
*/
class Run extends Thread{
@Override
public void run() {
while (isLive) {
try {
Thread.sleep(FRESH);
} catch (InterruptedException e) {
e.printStackTrace();
}
createBotTimer += FRESH;
Dooldown();
create();
playerMove();
bulletMove();
repaint();
}
}
}
public void Dooldown() {//攻击冷却
if (player.attackDooldown > 0) {
player.attackDooldown -= FRESH;
} else if (player.attackDooldown < 0) {
player.attackDooldown = 0;
}
for (int i = 0; i < enemies.size(); i++) {
TankEnemy ls = enemies.get(i);
if (ls.attackDooldown > 0) {
ls.attackDooldown -= FRESH;
}else if (ls.attackDooldown < 0) {
ls.attackDooldown = 0;
}
}
}
public boolean hitWall(Visible obj) {
for (int i = 0; i < walls.size(); i++) {
Wall ls=walls.get(i);
if (obj.hit(ls) && ls.type != WallType.air && ls.type != WallType.grass) {
return true;
}
}
if (obj.x<0 || obj.x>760 || obj.y<0 || obj.y>560) {
return true;
}
return false;
}
public boolean hitTank(Visible obj) {
for (int i = 0; i < enemies.size(); i++) {
TankEnemy ls=enemies.get(i);
if (!obj.equals(ls) && obj.hit(ls)) {
return true;
}
}
if (!obj.equals(player) && ( obj.hit(player))) {
return true;
}
return false;
}
public void playerMove() {
int lx=player.x,ly=player.y;
if (down_key) {
player.y += moveSpeed;
} else if (left_key) {
player.x -= moveSpeed;
} else if (right_key) {
player.x += moveSpeed;
}else if (up_key) {
player.y -= moveSpeed;
}
player.setHit();//刷新碰撞箱
if (down_key) {
player.setImage(Direction.DOWN);
} else if (left_key) {
player.setImage(Direction.LEFT);
} else if (right_key) {
player.setImage(Direction.RIGHT);
}else if (up_key) {
player.setImage(Direction.UP);
}
if (hitWall(player) || hitTank(player)) {
player.x=lx;
player.y=ly;
}
}
public void bulletMove() {
for(int i=0;i800 || ls.x<0 || ls.y>600 || ls.x<0) {
bullets.remove(i);
}
for (int j = 0; j < walls.size(); j++) {
Wall ls2=walls.get(j);
if (ls.hit(ls2) && ls2.type != WallType.air && ls2.type != WallType.grass) {
if (ls2.type != WallType.iron && ls2.type != WallType.river) {
if (!(ls2.type == WallType.base && ls.typeTank ==TypeTank.PLAYER)) {
ls2.die=true;
}
}
if (ls2.type != WallType.river) {
bullets.remove(i);
i--;
break;
}
}
}
if (ls.typeTank == TypeTank.PLAYER) {
for1: for (int j = 0; j < enemies.size(); j++) {
if (ls.hit(enemies.get(j))) {
enemies.get(j).live=false;
bullets.remove(i);
i--;
break for1;
}
}
}else if (ls.typeTank == TypeTank.ENEMY) {
if (ls.hit(player)) {
player.live=false;
}
}
}
}
/**
* 当按键按下时
*/
@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();// 获取按下的按键值
switch (code) {
case KeyEvent.VK_SPACE:
space_key = true;
if (player.attackDooldown <= 0) {
bullets.add(player.getHead());
player.attackDooldown = 500;
}
break;
case KeyEvent.VK_RIGHT:
space_key = false;
up_key = false;
down_key = false;
right_key = true;
left_key = false;
break;
case KeyEvent.VK_LEFT:
space_key = false;
up_key = false;
down_key = false;
right_key = false;
left_key = true;
break;
case KeyEvent.VK_UP:
space_key = false;
up_key = true;
down_key = false;
right_key = false;
left_key = false;
break;
case KeyEvent.VK_DOWN:
space_key = false;
up_key = false;
down_key = true;
right_key = false;
left_key = false;
break;
default:
break;
}
}
/**
* 按键抬起时
*/
@Override
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();// 获取按下的按键值
switch (code) {
case KeyEvent.VK_SPACE:
space_key = false;
break;
case KeyEvent.VK_RIGHT:
right_key = false;
break;
case KeyEvent.VK_LEFT:
left_key = false;
break;
case KeyEvent.VK_UP:
up_key = false;
break;
case KeyEvent.VK_DOWN:
down_key = false;
break;
default:
break;
}
// 不实现此方法,但不可删除
}
/**
* 键入某按键事件
*/
@Override
public void keyTyped(KeyEvent e) {
// 不实现此方法,但不可删除
}
}
main
Start.java
package com.mr.main;
import com.mr.frame.MainFrame;
public class Start {
public static void main(String[] args) {
new MainFrame();
}
}
model
Bullet.java
package com.mr.model;
import com.mr.type.Direction;
import com.mr.type.TypeTank;
import com.mr.util.ImageUtil;
public class Bullet extends Visible{
public TypeTank typeTank;
public Direction direction;
/**
*
* @param x
* @param y
* @param width
* @param height
* @param typeTank
* @param direction
*/
Bullet(int x, int y, int width, int height,TypeTank typeTank, Direction direction) {
super(ImageUtil.BULLET_URL, x, y, width, height);
this.typeTank = typeTank;
this.direction = direction;
}
}
Tank.java
package com.mr.model;
import com.mr.type.Direction;
import com.mr.type.TypeTank;
import com.mr.util.ImageUtil;
public class Tank extends Visible{
public Direction where;
public int attackDooldown=0;//攻击冷却
TypeTank typeTank;
public boolean live=true;
Tank(String url, int x, int y, int width, int height, TypeTank typeTank) {
super(url, x, y, width, height);
where = Direction.UP;
this.typeTank = typeTank;
}
public void setImage(Direction ls) {
switch (ls) {
case UP:
if(where != Direction.UP) {
where = Direction.UP;
if (typeTank == TypeTank.PLAYER) {
setImage(ImageUtil.PLAYER1_UP_IMAGE_URL);
} else {
setImage(ImageUtil.BOT_UP_IMAGE_URL);
}
}
break;
case DOWN:
if(where != Direction.DOWN) {
where = Direction.DOWN;
if (typeTank == TypeTank.PLAYER) {
setImage(ImageUtil.PLAYER1_DOWN_IMAGE_URL);
} else {
setImage(ImageUtil.BOT_DOWN_IMAGE_URL);
}
}
break;
case RIGHT:
if(where != Direction.RIGHT) {
where = Direction.RIGHT;
if (typeTank == TypeTank.PLAYER) {
setImage(ImageUtil.PLAYER1_RIGHT_IMAGE_URL);
} else {
setImage(ImageUtil.BOT_RIGHT_IMAGE_URL);
}
}
break;
case LEFT:
if(where != Direction.LEFT) {
where = Direction.LEFT;
if (typeTank == TypeTank.PLAYER) {
setImage(ImageUtil.PLAYER1_LEFT_IMAGE_URL);
} else {
setImage(ImageUtil.BOT_LEFT_IMAGE_URL);
}
}
break;
default:
break;
}
where=ls;
}
public Bullet getHead() {
Bullet reBullet;
int jx=x,jy=y;
switch (where) {
case UP:
reBullet = new Bullet(jx+15, jy, 10, 10, typeTank,where);
break;
case DOWN:
reBullet = new Bullet(jx+15, jy+40, 10, 10, typeTank,where);
break;
case RIGHT:
reBullet = new Bullet(jx+40, jy+15, 10, 10, typeTank,where);
break;
case LEFT:
reBullet = new Bullet(jx, jy+15, 10, 10, typeTank,where);
break;
default:
reBullet = new Bullet(jx+15, jy, 10, 10, typeTank,where);
break;
}
return reBullet;
}
}
TankEnemy.java
package com.mr.model;
import java.util.Random;
import com.mr.type.Direction;
import com.mr.type.TypeTank;
import com.mr.util.ImageUtil;
public class TankEnemy extends Tank{
public TankEnemy( int x, int y, int width, int height) {
super(ImageUtil.BOT_UP_IMAGE_URL, x, y, width, height, TypeTank.ENEMY);
}
public void randomMove() {
Random random = new Random();
Direction direction[]= new Direction[] {Direction.DOWN,Direction.LEFT,Direction.RIGHT,Direction.UP};
Direction ls=direction[random.nextInt(3)];
setImage(ls);
}
}
TankPlayer.java
package com.mr.model;
import com.mr.type.TypeTank;
import com.mr.util.ImageUtil;
public class TankPlayer extends Tank{
public TankPlayer( int x, int y, int width, int height) {
super(ImageUtil.PLAYER1_UP_IMAGE_URL, x, y, width, height, TypeTank.PLAYER);
}
}
Visible.java
package com.mr.model;
import java.awt.Image;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Visible {
public int x;
public int y;
public int width;
public int height;
public Image image;
public Rectangle kj = null;//碰撞检测
Visible(String url,int x,int y,int width, int height) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
try {
image = ImageIO.read(new File(url));
} catch (IOException e) {
e.printStackTrace();
}
setHit();
}
public void setImage(String url) {
try {
image = ImageIO.read(new File(url));
} catch (IOException e) {
e.printStackTrace();
}
}
public void setHit() {
kj =new Rectangle(this.x, this.y, this.width, this.height);
}
public boolean hit(Visible obj) {
if (obj.kj == null) {
return false;
}
return kj.intersects(obj.kj);
}
}
Wall.java
package com.mr.model;
import com.mr.type.WallType;
public class Wall extends Visible{
public WallType type;
public boolean die=false;
public Wall(String url, int x, int y, int width, int height, WallType type) {
super(url, x, y, width, height);
this.type = type;
}
}
XY.java
package com.mr.model;
public class XY {
public int x;
public int y;
public XY(int X,int Y) {
this.x=X;
this.y=Y;
}
}
wall
Air.java
package com.mr.model.wall;
import com.mr.model.Wall;
import com.mr.type.WallType;
import com.mr.util.ImageUtil;
public class Air extends Wall{
/**
* A
* @param x
* @param y
* @param width
* @param height
*/
public Air(int x, int y, int width, int height) {
super(ImageUtil.AIRWALL_IMAGE_URL, x, y, width, height, WallType.air);
}
}
Base.java
package com.mr.model.wall;
import com.mr.model.Wall;
import com.mr.type.WallType;
import com.mr.util.ImageUtil;
public class Base extends Wall{
/**
* B
* @param x
* @param y
* @param width
* @param height
*/
public Base(int x, int y, int width, int height) {
super(ImageUtil.BASE_IMAGE_URL, x, y, width, height, WallType.base);
}
}
Brick.java
package com.mr.model.wall;
import com.mr.model.Wall;
import com.mr.type.WallType;
import com.mr.util.ImageUtil;
public class Brick extends Wall{
/**
* K
* @param x
* @param y
* @param width
* @param height
*/
public Brick(int x, int y, int width, int height) {
super(ImageUtil.BRICKWALL_IMAGE_URL, x, y, width, height, WallType.brick);
}
}
Grass.java
package com.mr.model.wall;
import com.mr.model.Wall;
import com.mr.type.WallType;
import com.mr.util.ImageUtil;
public class Grass extends Wall{
/**
* G
* @param x
* @param y
* @param width
* @param height
*/
public Grass(int x, int y, int width, int height) {
super(ImageUtil.GRASSWALL_IMAGE_URL, x, y, width, height, WallType.grass);
}
}
Iron.java
package com.mr.model.wall;
import com.mr.model.Wall;
import com.mr.type.WallType;
import com.mr.util.ImageUtil;
public class Iron extends Wall{
/**
* I
* @param x
* @param y
* @param width
* @param height
*/
public Iron( int x, int y, int width, int height) {
super(ImageUtil.IRONWALL_IMAGE_URL, x, y, width, height, WallType.iron);
}
}
River.java
package com.mr.model.wall;
import com.mr.model.Wall;
import com.mr.type.WallType;
import com.mr.util.ImageUtil;
public class River extends Wall{
/**
* R
* @param x
* @param y
* @param width
* @param height
*/
public River( int x, int y, int width, int height) {
super(ImageUtil.RIVERWALL_IMAGE_URL, x, y, width, height, WallType.river);
}
}
type
Direction.java
package com.mr.type;
/**
* 方向枚举类
* @author Administrator
*
*/
public enum Direction {
/**
* 上
*/
UP,
/**
* 下
*/
DOWN,
/**
* 右
*/
RIGHT,
/**
* 左
*/
LEFT,
}
TypeTank.java
package com.mr.type;
/**
* 坦克枚举类
* @author zhaojiying
*/
public enum TypeTank {
PLAYER,
ENEMY,
}
WallType.java
package com.mr.type;
/**
* 墙块类型
*
*
*/
public enum WallType {
brick, // 砖墙 K
grass, // 草地 G
river, // 河流 R
iron, // 钢铁 I
base,// 基地 B
air, //空气 A
}
util
ImageUtil.java
package com.mr.util;
/**
* 绘图工具类
*
* @author Admin
*/
public class ImageUtil {
/**
* 地图列表
*/
public static final String MAP_LIST = "map/list.list";
/**
* 玩家向上图片路径
*/
public static final String PLAYER1_UP_IMAGE_URL = "image/tank/player_up.png";
/**
* 玩家向下图片路径
*/
public static final String PLAYER1_DOWN_IMAGE_URL = "image/tank/player_down.png";
/**
* 玩家向左图片路径
*/
public static final String PLAYER1_LEFT_IMAGE_URL = "image/tank/player_left.png";
/**
* 玩家向右图片路径
*/
public static final String PLAYER1_RIGHT_IMAGE_URL = "image/tank/player_right.png";
/**
* 电脑向上图片路径
*/
public static final String BOT_UP_IMAGE_URL = "image/tank/bot_up.png";
/**
* 电脑向下图片路径
*/
public static final String BOT_DOWN_IMAGE_URL = "image/tank/bot_down.png";
/**
* 电脑向左图片路径
*/
public static final String BOT_LEFT_IMAGE_URL = "image/tank/bot_left.png";
/**
* 电脑向右图片路径
*/
public static final String BOT_RIGHT_IMAGE_URL = "image/tank/bot_right.png";
/**
* 基地图片路径
*/
public static final String BASE_IMAGE_URL = "image/wall/base.png";
/**
* 摧毁后的基地图片路径
*/
public static final String BREAK_BASE_IMAGE_URL = "image/wall/break_base.png";
/**
* 砖墙图片路径
*/
public static final String BRICKWALL_IMAGE_URL = "image/wall/brick.png";
/**
* 草地图片路径
*/
public static final String GRASSWALL_IMAGE_URL = "image/wall/grass.png";
/**
* 空气图片路径
*/
public static final String AIRWALL_IMAGE_URL = "image/wall/air.png";
/**
* 铁墙图片路径
*/
public static final String IRONWALL_IMAGE_URL = "image/wall/iron.png";
/**
* 河流图片路径
*/
public static final String RIVERWALL_IMAGE_URL = "image/wall/river.png";
/**
* 坦克爆炸图片路径
*/
public static final String BOOM_IMAGE_URL = "image/boom/boom.png";
/**
* 登陆面板背景图片路径
*/
public static final String LOGIN_BACKGROUD_IMAGE_URL = "image/login_background.png";
/**
* 选择界面
*/
public static final String LOGIN_LIST_URL = "image/login_list.png";
/**
* 子弹
*/
public static final String BULLET_URL = "image/bullet/bullet.png";
}
MapIO.java
package com.mr.util;
import java.io.File;
import java.io.FileInputStream;
import java.util.*;
import com.mr.model.TankEnemy;
import com.mr.model.TankPlayer;
import com.mr.model.wall.*;
/**
* 读取地图类
* @author Administrator
*以形读图
*/
public class MapIO {
public final static String DATA_PATH = "map/";
public static String readMap(String grade) {
File fileMap = new File(DATA_PATH + grade);
FileInputStream in;
byte byt[] = null;
int len = 0;
try {
in = new FileInputStream(fileMap);
byt = new byte[4096];
len = in.read(byt);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
String reString = new String(byt, 0 ,len);
return reString;
}
public static String[] readMapList() {
File fil = new File("map");
String[] restring = fil.list();
return restring;
}
public static List readMapWall(String fillContent){
List reList =new ArrayList<>();
String[] lineStrings = fillContent.split("\n");
int i=0;
int j=0;
for(String temi:lineStrings) {
j=0;
for(char temj:temi.toCharArray()) {
switch (temj) {
case 'K':
reList.add(new Brick(j*20, i*20, 20, 20));
break;
case 'A':
reList.add(new Air(j*20, i*20, 20, 20));
break;
case 'B':
reList.add(new Base(j*20, i*20, 40, 40));
break;
case 'I':
reList.add(new Iron(j*20, i*20, 20, 20));
break;
case 'R':
reList.add(new River(j*20, i*20, 20, 20));
break;
case 'G':
reList.add(new Grass(j*20, i*20, 20, 20));
break;
default:
break;
}
j++;
}
i++;
}
return reList;
}
public static TankPlayer readTankPlayer(String fillContent) {
String[] lineStrings = fillContent.split("\n");
int i=0;
int j=0;
for(String temi:lineStrings) {
j=0;
for(char temj:temi.toCharArray()) {
if (temj == 'P') {
return new TankPlayer(j*20, i*20, 40, 40);
}
j++;
}
i++;
}
return new TankPlayer(400, 600, 40, 40);
}
public static List readTankEnemy(String fillContent) {
String[] lineStrings = fillContent.split("\n");
List reList =new ArrayList<>();
int i=0;
int j=0;
for(String temi:lineStrings) {
j=0;
for(char temj:temi.toCharArray()) {
if (temj == 'E') {
reList.add(new TankEnemy(j*20, i*20, 40, 40));
}
j++;
}
i++;
}
return reList;
}
}
image
boom
bullet
tank
wall