图片
备注:当年初学的时候写的,很多写得不好,见谅~haha
给初学者看看咯,要是觉得不好也不要喷。
Main.java
/**
* @author Notzuonotdied
* @version1.0 起始时间:2016年1月27日 15:40:51 功能: 1.打印出我的坦克和怪物坦克2.让怪物自动行走 3.保证坦克之间不能重叠
* 4.保证坦克不能越界 终止时间:2016年1月28日 00:59:06
* @version2.0 起始时间:2016年1月28日 18:34:57 功能:1.让我的坦克可以发射子弹2.让敌人坦克可以自由发射子弹
* 3.做出坦克被击毁的特效 终止时间:2016年1月29日00:01:47
* @version2.1 起始时间:2016年1月29日 23:33:04 功能:1.添加墙 2.添加树木 3.设置背景4.修bug
* 终止时间:2016年1月31日 23:47:46
* @version3.0 起始时间:2016年1月31日 23:48:11 功能:1.添加菜单栏2.为菜单栏的子菜单添加功能 终止时间:2016年2月1日
* 21:15:11
* @version3.1 起始时间:2016年2月6日 18:14:27 功能:1.保存功能 2.继续游戏功能 3.保存退出功能
* 终止时间:2016年2月8日 08:59:30
* @version3.2 起始时间:2016年2月8日 09:00:08 功能:1.声音的功能 终止时间:2016年2月8日 09:18:56
* @version3.3 起始时间:2016年2月8日 13:20:18 功能:1.修复bug 2.添加保存游戏 3.添加完善的注释
* 终止时间:2016年2月8日 13:28:04
* @version3.4 起始时间:2016年2月8日 23:15:50 功能:1.修复bug 2.添加关卡 3.调节我的坦克的速度 4.添加界面选择项
* 5.对赢和输添加选择方案 终止时间:2016年2月9日 13:18:34
* */
@SuppressWarnings("serial")
public class Main extends JFrame implements ActionListener, Runnable {
public boolean button = false;
public boolean gbutton = false;
// 定义显示屏幕的宽度和高度
public static final int screenwidth = 800;
public static final int screenheight = 700;
// 定义坦克运动范围
public static final int rangx = screenwidth - TankMember.size;
public static final int rangy = screenheight - TankMember.size;
// 定义组件
GamePanel gamepanel = null;
GameStartPanel gamestartpanel = null;
// 定义菜单栏
MenuBar mb = null;
// 定义菜单栏上的选项
Menu m1 = null, m2 = null, m3 = null, m4 = null;
// 定义菜单栏的子菜单
MenuItem mi1 = null, mi2 = null, mi3 = null, mi4 = null, mi5 = null,
mi6 = null, mi7 = null, mi8 = null, mi9 = null, mi10 = null,
mi11 = null, mi12 = null, mi13 = null, mi14 = null, mi15 = null,
mi16 = null;
// ----------------------------主函数在此!--------------------------------
public static void main(String[] args) {
// 实例化
Main m = new Main();
// 启动线程
Thread t = new Thread(m);
t.start();
}
// 构造函数
public Main() {
// 创建组件
// 菜单条
mb = new MenuBar();
// 菜单条上的菜单
m1 = new Menu("游戏开始");
m2 = new Menu("游戏说明");
m3 = new Menu("游戏帮助");
m4 = new Menu("游戏作者");
// 设置字体
m1.setFont(new Font("微软雅黑", Font.BOLD, 15));
m2.setFont(new Font("微软雅黑", Font.BOLD, 15));
m3.setFont(new Font("微软雅黑", Font.BOLD, 15));
m4.setFont(new Font("微软雅黑", Font.BOLD, 15));
// 游戏开始菜单下的子菜单
mi1 = new MenuItem("开始 游戏");
mi14 = new MenuItem("继续游戏");
mi15 = new MenuItem("保存游戏");
mi2 = new MenuItem("级别 1");
mi3 = new MenuItem("级别 2");
mi4 = new MenuItem("级别 3");
mi12 = new MenuItem("直接退出游戏");
mi13 = new MenuItem("存盘退出游戏");
// 游戏说明下的子菜单
mi5 = new MenuItem("游戏说明");
mi8 = new MenuItem("开始游戏");
mi16 = new MenuItem("继续游戏");
mi9 = new MenuItem("级别 1");
mi10 = new MenuItem("级别 2");
mi11 = new MenuItem("级别 3");
// 游戏帮助下的子菜单
mi6 = new MenuItem("游戏帮助");
// 游戏作者下的子菜单
mi7 = new MenuItem("游戏作者");
// 设置字体
mi1.setFont(new Font("微软雅黑", Font.BOLD, 15));
mi2.setFont(new Font("微软雅黑", Font.BOLD, 15));
mi3.setFont(new Font("微软雅黑", Font.BOLD, 15));
mi4.setFont(new Font("微软雅黑", Font.BOLD, 15));
mi5.setFont(new Font("微软雅黑", Font.BOLD, 15));
mi6.setFont(new Font("微软雅黑", Font.BOLD, 15));
mi7.setFont(new Font("微软雅黑", Font.BOLD, 15));
mi8.setFont(new Font("微软雅黑", Font.BOLD, 15));
mi9.setFont(new Font("微软雅黑", Font.BOLD, 15));
mi10.setFont(new Font("微软雅黑", Font.BOLD, 15));
mi11.setFont(new Font("微软雅黑", Font.BOLD, 15));
mi12.setFont(new Font("微软雅黑", Font.BOLD, 15));
mi13.setFont(new Font("微软雅黑", Font.BOLD, 15));
mi14.setFont(new Font("微软雅黑", Font.BOLD, 15));
mi15.setFont(new Font("微软雅黑", Font.BOLD, 15));
mi16.setFont(new Font("微软雅黑", Font.BOLD, 15));
// 游戏开始菜单栏
m1.add(mi1);
m1.add(mi14);
m1.add(mi15);
// 添加分割线
m1.addSeparator();
m1.add(mi2);
m1.add(mi3);
m1.add(mi4);
// 添加分割线
m1.addSeparator();
m1.add(mi12);
m1.add(mi13);
// 游戏说明菜单栏
m2.add(mi5);
// 添加分割线
m2.addSeparator();
m2.add(mi8);
m2.add(mi16);
m2.add(mi9);
m2.add(mi10);
m2.add(mi11);
// 游戏帮助菜单栏
m3.add(mi6);
// 游戏作者菜单栏
m4.add(mi7);
// 添加
mb.add(m1);
mb.add(m2);
mb.add(m3);
mb.add(m4);
// 监听
mi1.addActionListener(this);
mi1.setActionCommand("start");
mi2.addActionListener(this);
mi2.setActionCommand("level1");
mi3.addActionListener(this);
mi3.setActionCommand("level2");
mi4.addActionListener(this);
mi4.setActionCommand("level3");
mi5.addActionListener(this);
mi5.setActionCommand("instruction");
mi6.addActionListener(this);
mi6.setActionCommand("help");
mi7.addActionListener(this);
mi7.setActionCommand("author");
mi8.addActionListener(this);
mi8.setActionCommand("instruction1");
mi9.addActionListener(this);
mi9.setActionCommand("instruction2");
mi10.addActionListener(this);
mi10.setActionCommand("instruction3");
mi11.addActionListener(this);
mi11.setActionCommand("instruction4");
mi12.addActionListener(this);
mi12.setActionCommand("exit");
mi13.addActionListener(this);
mi13.setActionCommand("savebeforeexit");
mi14.addActionListener(this);
mi14.setActionCommand("refomergame");
mi15.addActionListener(this);
mi15.setActionCommand("save");
mi16.addActionListener(this);
mi16.setActionCommand("regameinstruction");
// 添加到菜单条上
this.setMenuBar(mb);
// 实例化开始面板
gamestartpanel = new GameStartPanel();
gamestartpanel.setBackground(Color.BLACK);
this.add(gamestartpanel);
this.addKeyListener(gamestartpanel);
// 界面的设置
this.setIconImage((new ImageIcon(getClass().getClassLoader()
.getResource("images/wangyu.jpg"))).getImage());
// 设置软件名字
this.setTitle("MyTankWar-无聊版");
// 禁止用户改变窗体大小
this.setResizable(false);
// 设置窗体的尺寸
this.setSize(screenwidth, screenheight + 30);
// 设置窗体出现的位置
this.setLocation(250, 6);
// 退出时清除占用的内存
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 可视化
this.setVisible(true);
}
// 响应菜单栏上的操作
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("instruction")) {
JOptionPane
.showMessageDialog(
null,
new JLabel(
"<html><h2><font color='red'>个人制作</font> 的仿制版坦克大战游戏!</h2></html>"),
"游戏说明", JOptionPane.PLAIN_MESSAGE);
} else if (e.getActionCommand().equals("author")) {
JOptionPane
.showMessageDialog(
null,
new JLabel(
"<html><h2><font color='red'>CSDN</font> --Notzuonotdied</h2></html>"),
"游戏作者", JOptionPane.PLAIN_MESSAGE);
} else if (e.getActionCommand().equals("help")) {
JOptionPane
.showMessageDialog(
null,
new JLabel(
"<html><h2><font color='red'>用→ ← ↑ ↓控制方向,空格键开火,回车键开始!</font></h2></html>"),
"游戏帮助", JOptionPane.PLAIN_MESSAGE);
} else if (e.getActionCommand().equals("start")) {
GamePanel.monster.clear();
GamePanel.monstersize = 6;
Monster.bulletsize = 2;
GamePanel.level = 0;
gamepanel = new GamePanel(0);
gamepanel.setBackground(Color.BLACK);
GamePanel.button = true;
Thread t = new Thread(gamepanel);
t.start();
// 启动声音
Audio audio = new Audio("bgm.wav");
audio.start();
// 删除旧的面板
this.remove(gamestartpanel);
this.add(gamepanel);
// 监听
this.addKeyListener(gamepanel);
this.setVisible(true);
} else if (e.getActionCommand().equals("level1")) {
Object[] options = { "确定", "取消" };
int response = JOptionPane.showOptionDialog(this, "是否使用级别1模式", "",
JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null,
options, options[0]);
if (response == 0) {
GamePanel.monster.clear();
GamePanel.CWalls.clear();
GamePanel.monstersize = 8;
GamePanel.level = 1;
Monster.bulletsize = 3;
gamepanel = new GamePanel(0);
}
} else if (e.getActionCommand().equals("level2")) {
Object[] options = { "确定", "取消" };
int response = JOptionPane.showOptionDialog(this, "是否使用级别2模式", "",
JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null,
options, options[0]);
if (response == 0) {
GamePanel.monster.clear();
GamePanel.CWalls.clear();
GamePanel.monstersize = 9;
GamePanel.level = 2;
Monster.bulletsize = 4;
gamepanel = new GamePanel(0);
}
} else if (e.getActionCommand().equals("level3")) {
Object[] options = { "确定", "取消" };
int response = JOptionPane.showOptionDialog(this, "是否使用级别3模式", "",
JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null,
options, options[0]);
if (response == 0) {
GamePanel.monster.clear();
GamePanel.CWalls.clear();
GamePanel.monstersize = 10;
Monster.bulletsize = 5;
GamePanel.level = 3;
gamepanel = new GamePanel(0);
}
} else if (e.getActionCommand().equals("instruction1")) {
JOptionPane
.showMessageDialog(
null,
new JLabel(
"<html><h2><font color='red'>开始游戏</font>改变怪物连发子弹数为:2,怪物数为:6</h2></html>"),
"游戏帮助", JOptionPane.PLAIN_MESSAGE);
} else if (e.getActionCommand().equals("instruction2")) {
JOptionPane
.showMessageDialog(
null,
new JLabel(
"<html><h2><font color='red'>级别1:</font>改变怪物连发子弹数为:3,怪物数为:8</h2></html>"),
"游戏帮助", JOptionPane.PLAIN_MESSAGE);
} else if (e.getActionCommand().equals("instruction3")) {
JOptionPane
.showMessageDialog(
null,
new JLabel(
"<html><h2><font color='red'>级别2:</font>改变怪物连发子弹数为:4,怪物数为:9</h2></html>"),
"游戏帮助", JOptionPane.PLAIN_MESSAGE);
} else if (e.getActionCommand().equals("instruction4")) {
JOptionPane
.showMessageDialog(
null,
new JLabel(
"<html><h2><font color='red'>级别3:</font>改变怪物连发子弹数为:5,怪物数为:10</h2></html>"),
"游戏帮助", JOptionPane.PLAIN_MESSAGE);
} else if (e.getActionCommand().equals("exit")) {
Object[] options = { "不玩了!", "继续玩!" };
int response = JOptionPane.showOptionDialog(this, new JLabel(
"<html><h2><font color='red'>是否</font>退出游戏?</h2></html>"),
"", JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options, options[0]);
if (response == 0) {
// 正常退出程序
System.exit(0);
}
} else if (e.getActionCommand().equals("savebeforeexit")) {
Object[] options = { "保存并退出", "继续玩游戏" };
int response = JOptionPane
.showOptionDialog(
this,
new JLabel(
"<html><h2><font color='red'>是否</font>存盘并退出游戏?</h2></html>"),
"", JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options,
options[0]);
if (response == 0) {
// 保存我的坦克的信息和怪物的信息
Recorder rc = new Recorder();
rc.SaveMonsterInfo();
rc.SaveMyTankInfo();
// 正常退出程序
System.exit(0);
}
} else if (e.getActionCommand().equals("save")) {
// 保存我的坦克的信息和怪物的信息
Recorder rc = new Recorder();
// 开始保存
rc.SaveMonsterInfo();
rc.SaveMyTankInfo();
rc.SaveCWallInfo();
} else if (e.getActionCommand().equals("refomergame")) {
// 删除旧的面板
this.remove(gamestartpanel);
if (GamePanel.button == true) {
Object[] options = { "恢复游戏", "继续游戏" };
int response = JOptionPane
.showOptionDialog(
this,
new JLabel(
"<html><h2><font color='red'>是否</font>恢复保存的游戏并放弃本次游戏?</h2></html>"),
"", JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options,
options[0]);
if (response == 0) {
this.remove(gamepanel);
// 清除原先储存在vector中的数据,防止多余的数据影响恢复体验
GamePanel.monster.clear();
GamePanel.CWalls.clear();
}
}
// 初始化界面
gamepanel = new GamePanel(1);
// 启动线程
Thread t = new Thread(gamepanel);
t.start();
this.add(gamepanel);
// 监听
this.addKeyListener(gamepanel);
this.setVisible(true);
} else if (e.getActionCommand().equals("regameinstruction")) {
JOptionPane
.showMessageDialog(
null,
new JLabel(
"<html><h2><font color='red'>继续游戏:</font>恢复游戏为保存的状态</h2></html>"),
"游戏帮助", JOptionPane.PLAIN_MESSAGE);
}
}
public void run() {
while (!this.gbutton) {
if (GamePanel.buttonwin || GamePanel.buttonfail) {
try {
if (GamePanel.buttonwin) {
Object[] options = { "确定", "取消" };
int response = JOptionPane.showOptionDialog(this,
"是否继续闯关", "", JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options,
options[0]);
if (response == 0) {
GamePanel.monster.clear();
GamePanel.CWalls.clear();
if (GamePanel.level == 0) {
GamePanel.monstersize = 8;
Monster.bulletsize = 3;
GamePanel.level = 1;
gamepanel = new GamePanel(0);
} else if (GamePanel.level == 1) {
GamePanel.monstersize = 9;
Monster.bulletsize = 4;
GamePanel.level = 2;
gamepanel = new GamePanel(0);
} else if (GamePanel.level == 2) {
GamePanel.monstersize = 10;
Monster.bulletsize = 10;
GamePanel.level = 3;
gamepanel = new GamePanel(0);
}
}
GamePanel.buttonwin = false;
}
if (GamePanel.buttonfail) {
Object[] options = { "确定", "取消" };
int response = JOptionPane.showOptionDialog(this,
"是否继续游戏", "", JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options,
options[0]);
if (response == 0) {
GamePanel.monster.clear();
GamePanel.CWalls.clear();
if (GamePanel.level == 0) {
GamePanel.monstersize = 6;
Monster.bulletsize = 2;
gamepanel = new GamePanel(0);
} else if (GamePanel.level == 1) {
GamePanel.monstersize = 8;
Monster.bulletsize = 3;
gamepanel = new GamePanel(0);
} else if (GamePanel.level == 2) {
GamePanel.monstersize = 9;
Monster.bulletsize = 4;
gamepanel = new GamePanel(0);
} else if (GamePanel.level == 3) {
GamePanel.monstersize = 10;
Monster.bulletsize = 10;
gamepanel = new GamePanel(0);
}
}
GamePanel.buttonfail = false;
}
Thread.sleep(1500);
} catch (Exception e) {
e.printStackTrace();
}
}
if (!this.button) {
try {
// 开始游戏
if (GameStartPanel.button1 == true) {
GamePanel.monster.clear();
GamePanel.monstersize = 6;
Monster.bulletsize = 2;
gamepanel = new GamePanel(0);
gamepanel.setBackground(Color.BLACK);
GamePanel.button = true;
Thread t1 = new Thread(gamepanel);
t1.start();
// 启动声音
Audio audio = new Audio("bgm.wav");
audio.start();
// 删除旧的面板
this.remove(gamestartpanel);
this.add(gamepanel);
// 监听
this.addKeyListener(gamepanel);
this.setVisible(true);
this.button = true;
}
// 继续游戏
if (GameStartPanel.button0 == true) {
// 删除旧的面板
this.remove(gamestartpanel);
if (GamePanel.button == true) {
Object[] options = { "恢复游戏", "继续游戏" };
int response = JOptionPane
.showOptionDialog(
this,
new JLabel(
"<html><h2><font color='red'>是否</font>恢复保存的游戏并放弃本次游戏?</h2></html>"),
"", JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE, null,
options, options[0]);
if (response == 0) {
this.remove(gamepanel);
// 清除原先储存在vector中的数据,防止多余的数据影响恢复体验
GamePanel.monster.clear();
GamePanel.CWalls.clear();
}
}
// 初始化界面
gamepanel = new GamePanel(1);
// 启动线程
Thread t = new Thread(gamepanel);
t.start();
this.add(gamepanel);
// 监听
this.addKeyListener(gamepanel);
this.setVisible(true);
this.button = true;
}
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
@SuppressWarnings("serial")
// 游戏面板
class GamePanel extends JPanel implements KeyListener, Runnable {
// 总成绩
public static int score = 0;
// 设置关卡
public static int level = 0;
// 定义我的坦克可以连发的子弹数
int buttlenumber = 2;
// 定义怪物的数量
public static int monstersize = 6;
// 定义随机变量
private Random r = new Random();
private int movesteps = 0;
// 控制界面是否移除的变量
public static boolean button = false;
// 控制是否在我的坦克死亡后继续游戏的变量
public static boolean choice = false;
public static boolean buttonwin = false;
public static boolean buttonfail = false;
// 定义我的坦克类
static MyTank myTank = null;
// 定义一个敌人的坦克集合
static Vector<Monster> monster = new Vector<Monster>();
// 定义一个爆炸集合
static Bomb bomb = null;
static Vector<Bomb> bombs = new Vector<Bomb>();
// 定义一个普通墙集合
CommentWall CWall = null;
static Vector<CommentWall> CWalls = new Vector<CommentWall>();
// 定义一个石墙集合----这个是边界的围墙
BlockWall BWall = null;
Vector<BlockWall> BWalls = new Vector<BlockWall>();
// 定义一个石墙集合----这是内部的石墙
static Vector<BlockWall> BWalls_1 = new Vector<BlockWall>();
// 定义一个树木集合
Tree tr = null;
Vector<Tree> tree = new Vector<Tree>();
// 用于判断记录文件是否为空
FileInputStream fin = null;
// GamePanel构造函数
public GamePanel(int flag) {
int size = 0;
// 判断记录的文本是否为空
try {
// 判读文本是否存在,不存在就新建一个空白文本
File f = new File("D:\\myrecoder.txt");
if (f.exists()) {
fin = new FileInputStream("D:\\myrecoder.txt");
// 获取
size = fin.available();
// 已经不需要判断了,关闭流
fin.close();
} else {
f.createNewFile();
}
if (size != 0 && flag == 1) {
// 恢复数据
new Recorder().ReInfo();
} else {
// 创建我的坦克,设置坦克的位置
myTank = new MyTank(600, 500);
myTank.setDirect(0);
// 创建怪物的坦克
for (int i = 0; i < GamePanel.monstersize; i++) {
// 定义坦克的位置
movesteps = r.nextInt(30) + 35;
Monster mon = new Monster((i + 1) * 80, movesteps);
Monster.setTankNumber(monstersize);
mon.setDirect(2);
// 启动敌人坦克
Thread mont = new Thread(mon);
mont.start();
// 将敌人坦克添加到坦克集合中
monster.add(mon);
// 为敌人添加子弹
Bullet bullet = new Bullet(
mon.getX() + TankMember.size / 2, mon.getY()
+ TankMember.size / 2, 2);
// 加到子弹集合中
mon.bullets.add(bullet);
// 启动子弹线程
Thread t2 = new Thread(bullet);
t2.start();
}
// 初始化普通墙
DrawCWall();
}
} catch (Exception e) {
e.printStackTrace();
}
// 初始化石墙和树木
Initialization();
}
// 画笔函数
public void paint(Graphics g) {
super.paint(g);
// 初始化背景图片
Image Background = Toolkit.getDefaultToolkit().getImage(
GamePanel.class.getResource("/images/background.gif"));
g.drawImage(Background, 35, -160, Main.screenwidth,
Main.screenwidth + 80, null);
// 画出我的坦克
DrawMyTank(g);
// 画出敌人坦克
DrawMonster(g);
// 画出墙,爆炸效果,树木,普通墙
DrawItem(g);
// 信息显示
DrawInfo(g);
}
// 初始化普通墙
public void DrawCWall() {
// 初始化普通墙
for (int i = 0; i < 15; i++) {
if (i < 12) {
CWalls.add(new CommentWall(CommentWall.commentwallwidth
* (i + 4), 158));
CWalls.add(new CommentWall(CommentWall.commentwallwidth
* (i + 20), 158));
}
CWalls.add(new CommentWall(154, CommentWall.commentwallheight
* (i + 8)));
CWalls.add(new CommentWall(632, CommentWall.commentwallheight
* (i + 8)));
}
for (int i = 0; i < 3; i++) {
CWalls.add(new CommentWall(CommentWall.commentwallwidth * (i + 13),
215));
CWalls.add(new CommentWall(CommentWall.commentwallwidth * (i + 13),
237));
CWalls.add(new CommentWall(CommentWall.commentwallwidth * (i + 21),
215));
CWalls.add(new CommentWall(CommentWall.commentwallwidth * (i + 21),
237));
}
}
// 画出我的坦克
public void DrawMyTank(Graphics g) {
// 画出我自己的坦克
if (myTank.isLive) {
myTank.drawTank(myTank.x, myTank.y, g, 0);
this.repaint();
}
// 画出子弹
for (int i = 0; i < myTank.bullets.size(); i++) {
Bullet bullet = myTank.bullets.get(i);
if (bullet != null && bullet.isLive == true
&& myTank.isLive == true
&& !bullet.BulletComeAcrossCWall(bullet)
&& !bullet.BulletComeAcrossMonster(bullet)
&& !bullet.BulletComeAcrossBWall(bullet)) {
// 画出子弹的轨迹
bullet.drawBullet(g);
// 判断是否集中了怪物
bullet.HitMonster();
}
if (bullet.isLive == false) {
// 如果子弹存在状态为假就移除子弹
myTank.bullets.remove(bullet);
}
}
}
// 画出怪物
public void DrawMonster(Graphics g) {
for (int i = 0; i < monster.size(); i++) {
Monster mon = monster.get(i);
if (mon.isLive) {
mon.drawTank(mon.getX(), mon.getY(), g, 1);
this.repaint();
for (int j = 0; j < mon.bullets.size(); j++) {
// 取出一个子弹
Bullet bullet = mon.bullets.get(j);
if (bullet.isLive && !bullet.BulletComeAcrossCWall(bullet)
&& !bullet.BulletComeAcrossBWall(bullet)) {
// 画出子弹的轨迹
bullet.drawBullet(g);
// 判断是否击中了我的坦克
bullet.HitMyTank();
}
if (bullet.isLive == false) {
mon.bullets.remove(bullet);
}
}
}
// 怪物死亡就移除
if (mon.isLive == false) {
monster.remove(mon);
GamePanel.score++;
}
}
}
// 画出墙,爆炸效果,树木,普通墙
public void DrawItem(Graphics g) {
// 画出爆炸效果
for (int i = 0; i < GamePanel.bombs.size(); i++) {
bomb = GamePanel.bombs.get(i);
if (bomb.isLive) {
bomb.drawBomb(g);
} else {
bombs.remove(bomb);
}
}
// 画出普通墙
for (int i = 0; i < GamePanel.CWalls.size(); i++) {
CWall = GamePanel.CWalls.get(i);
// 如果普通墙存在状态为真就画出来
if (CWall.isLive) {
CWall.drawCWall(g);
} else {
GamePanel.CWalls.remove(CWall);
}
}
// 画出石墙
// 外围石墙
for (int i = 0; i < this.BWalls.size(); i++) {
this.BWalls.get(i).drawBWall(g);
}
// 内部石墙
for (int i = 0; i < GamePanel.BWalls_1.size(); i++) {
GamePanel.BWalls_1.get(i).drawBWall(g);
}
// 画出树木
for (int i = 0; i < this.tree.size(); i++) {
this.tree.get(i).drawTree(g);
}
}
// 信息显示
public void DrawInfo(Graphics g) {
// 左上角文字
g.setColor(Color.black);
g.setFont(new Font("楷体", Font.BOLD, 18));
g.drawString("坦克大战-无聊版", 35, 33);
// 正上方中间文字
g.setFont(new Font("楷体", Font.BOLD, 23));
g.drawString("区域内还有敌方坦克: ", 280, 23);
g.drawString("总成绩: ", 550, 23);
// 提示信息文字属性设置
g.setColor(Color.RED);
g.setFont(new Font("TimesRoman", Font.ITALIC, 30));
g.drawString("" + GamePanel.monster.size(), 520, 23);
g.drawString("" + GamePanel.score, 640, 23);
// 提示信息
if (monster.size() == 0 && myTank.isLive) {
Font f = g.getFont();
g.setFont(new Font("TimesRoman", Font.BOLD, 60)); // 判断是否赢得比赛--赢了
g.drawString("You are Winner! ", 175, 310);
g.setFont(f);
// 选择方案
GamePanel.buttonwin = true;
if (level == 3) {
g.drawString("You are Crazy! ", 175, 370);
}
}
if (myTank.isLive == false) {
Font f = g.getFont();
g.setFont(new Font("TimesRoman", Font.BOLD, 60)); // 判断是否赢得比赛--输了
g.drawString("You are lost! ", 180, 310);
g.setFont(f);
// 选择方案
GamePanel.buttonfail = true;
}
}
// 初始化工作
public void Initialization() {
// 初始化除了普通墙外的所有墙体--注:其他的墙体都设置为无敌了,不可攻击
// 初始化石墙-外部围墙
for (int i = 0; i < 23; i++) {
BWalls.add(new BlockWall(BlockWall.BlockWallwidth * i, 0));
BWalls.add(new BlockWall(BlockWall.BlockWallwidth * i,
Main.screenheight - BlockWall.BlockWallheight));
BWalls.add(new BlockWall(0, BlockWall.BlockWallheight * i));
BWalls.add(new BlockWall(Main.screenwidth
- BlockWall.BlockWallwidth, BlockWall.BlockWallheight * i));
}
// 初始化石墙-内部围墙
for (int i = 0; i < 4; i++) {
BWalls_1.add(new BlockWall(BlockWall.BlockWallwidth * (i + 6),
BlockWall.BlockWallheight * 5));
BWalls_1.add(new BlockWall(BlockWall.BlockWallwidth * (i + 13),
BlockWall.BlockWallheight * 5));
}
// 初始化石墙-内部墙-上两个
for (int i = 0; i < 5; i++) {
if (i < 2) {
BWalls_1.add(new BlockWall(BlockWall.BlockWallwidth * (i + 5),
BlockWall.BlockWallheight * 10));
BWalls_1.add(new BlockWall(BlockWall.BlockWallwidth * (i + 16),
BlockWall.BlockWallheight * 10));
BWalls_1.add(new BlockWall(BlockWall.BlockWallwidth * (i + 5),
BlockWall.BlockWallheight * 11));
BWalls_1.add(new BlockWall(BlockWall.BlockWallwidth * (i + 16),
BlockWall.BlockWallheight * 11));
} else {
BWalls_1.add(new BlockWall(BlockWall.BlockWallwidth * (i + 8),
BlockWall.BlockWallheight * 10));
}
}
// 初始化石墙-内部墙-最下一个
for (int i = 0; i < 5; i++) {
if (i == 0 || i == 4) {
BWalls_1.add(new BlockWall(BlockWall.BlockWallwidth * (i + 9),
BlockWall.BlockWallheight * 13));
} else {
BWalls_1.add(new BlockWall(BlockWall.BlockWallwidth * (i + 9),
BlockWall.BlockWallheight * 14));
}
}
// 初始化石墙-内部墙-左右两个
for (int i = 0; i < 2; i++) {
BWalls_1.add(new BlockWall(BlockWall.BlockWallwidth * 3,
BlockWall.BlockWallheight * (i + 7)));
BWalls_1.add(new BlockWall(BlockWall.BlockWallwidth * 19,
BlockWall.BlockWallheight * (i + 7)));
BWalls_1.add(new BlockWall(BlockWall.BlockWallwidth * 6,
BlockWall.BlockWallheight * (i + 16)));
BWalls_1.add(new BlockWall(BlockWall.BlockWallwidth * 16,
BlockWall.BlockWallheight * (i + 16)));
}
// 初始化草丛
for (int i = 0; i < 5; i++) {
tree.add(new Tree(Tree.Treewidth * 1, Tree.Treeheight * (5 + i)));
tree.add(new Tree(Tree.Treewidth * 2, Tree.Treeheight * (5 + i)));
tree.add(new Tree(Tree.Treewidth * 19, Tree.Treeheight * (5 + i)));
tree.add(new Tree(Tree.Treewidth * 20, Tree.Treeheight * (5 + i)));
}
}
// 响应键盘
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
myTank.setDirect(2);
this.repaint();
myTank.move();
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
// 设置我的坦克的方向
myTank.setDirect(0);
this.repaint();
myTank.move();
} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
myTank.setDirect(3);
this.repaint();
myTank.move();
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
myTank.setDirect(1);
this.repaint();
myTank.move();
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
if (myTank.bullets.size() < buttlenumber) {
myTank.ShotEnemy();
}
}
// 必须调用this.repain()函数,来重新绘制界面
this.repaint();
}
public void keyReleased(KeyEvent e) {
}
public void run() {
// 刷新界面
while (!GamePanel.choice) {
try {
Thread.sleep(66);
} catch (Exception e) {
e.printStackTrace();
}
this.repaint();
}
}
}
@SuppressWarnings("serial")
// 开始面板
class GameStartPanel extends JPanel implements KeyListener {
// 设置开始游戏继续游戏的位置
private int Infox = 320;
private int Infoy = 400;
// 图片类
public int flag = 1;
private int x = Infox - TankMember.size / 2 * 3 - 8;;
private int y = Infoy - 30;
// 返回值
public static boolean button0 = false;
public static boolean button1 = false;
// 初始化图片坦克的图片
Image[] tankImages = new Image[] {
Toolkit.getDefaultToolkit().getImage(
TankMember.class.getResource("/images/1.gif")),
Toolkit.getDefaultToolkit().getImage(
TankMember.class.getResource("/images/m0.gif")), };
public void paint(Graphics g) {
super.paint(g);
// 画出信息代码部分
DrawInfo(g);
}
// 画出信息
public void DrawInfo(Graphics g) {
g.fillRect(0, 0, Main.screenwidth + 10, Main.screenheight);
// 提示信息
g.setColor(Color.YELLOW);
// 开关信息的字体
Font myfont1 = new Font("楷体", Font.BOLD, 60);
g.setFont(myfont1);
g.drawString("坦克大战-无聊版", 180, 310);
g.setColor(Color.RED);
g.drawString("坦克大战-无聊版", 176, 315);
g.setColor(Color.GRAY);
Font myfont = new Font("楷体", Font.BOLD, 30);
g.setFont(myfont);
g.drawString("开始游戏", Infox, Infoy);
g.drawString("继续游戏", Infox, Infoy + 40);
// 画出坦克
g.drawImage(tankImages[this.flag], this.x, this.y, null);
this.repaint();
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_DOWN
|| e.getKeyCode() == KeyEvent.VK_UP) {
if (flag == 1) {
this.x = Infox - TankMember.size / 2 * 3;
this.y = Infoy + 15;
this.flag = 0;
} else {
this.x = Infox - TankMember.size / 2 * 3 - 8;
this.y = Infoy - 30;
this.flag = 1;
}
this.repaint();
}
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (this.flag == 0) {
GameStartPanel.button0 = true;
} else {
GameStartPanel.button1 = true;
}
// 启动声音
Audio audio = new Audio("StartGamePanel.wav");
audio.start();
}
}
public void keyReleased(KeyEvent e) {
}
}
Direction.java
public enum Direction {
UP, RIGHT, DOWN, LEFT, STOP
}
CommentWall.java
package TankWar;
import java.awt.*;
public class CommentWall {
// 定义墙的大小
public final static int commentwallwidth = 22;
public final static int commentwallheight = 21;
// 定义坐标
int x;
int y;
// 定义普通墙的存在状态
boolean isLive = true;
// 初始化墙的图片
private static Image[] wallImags = null;
static {
wallImags = new Image[] {
Toolkit.getDefaultToolkit().getImage(
CommentWall.class.getResource("/images/commonWall.gif")), };
}
// 画出普通墙
public void drawCWall(Graphics g) {
g.drawImage(wallImags[0], x, y, null);
}
// 构造函数
public CommentWall(int x, int y) {
this.x = x;
this.y = y;
}
public Rectangle getRect() {
return new Rectangle(x, y, commentwallwidth, commentwallheight);
}
}
Bullet.java
package TankWar;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.Vector;
public class Bullet implements Runnable {
// 定义子弹的尺寸
static int bulletwidth = 12;
static int bulletheight = 12;
// 定义坐标
int x;
int y;
// 定义每一颗子弹的伤害值
public static int damage = 25;
// 定义爆炸集合
Bomb bomb = null;
Vector<Bomb> bombs = new Vector<Bomb>();
// 定义子弹的速度
int speed = 16;
// 定义子弹的存在状态
boolean isLive = true;
// 定义方向
int direct = 0;
// 定义一个子弹集合
Vector<Bullet> bullets = new Vector<Bullet>();
// 子弹图片初始化
// 定义为全局静态变量
private static Image[] BulletImages = null;
static {
BulletImages = new Image[] {
Toolkit.getDefaultToolkit().getImage(
Bullet.class.getResource("/images/bullet1.gif")),
Toolkit.getDefaultToolkit().getImage(
Bullet.class.getResource("/images/bullet2.gif")),
Toolkit.getDefaultToolkit().getImage(
Bullet.class.getResource("/images/bullet3.gif")),
Toolkit.getDefaultToolkit().getImage(
Bullet.class.getResource("/images/bullet4.gif")), };
}
// 画出子弹
public void drawBullet(Graphics g) {
switch (direct) {
case 0:
g.drawImage(BulletImages[0], x, y, null);
break;
case 1:
g.drawImage(BulletImages[1], x, y, null);
break;
case 2:
g.drawImage(BulletImages[2], x, y, null);
break;
case 3:
g.drawImage(BulletImages[3], x, y, null);
break;
}
}
// 判断怪物和我的子弹与普通墙相遇的时候--击中普通墙,则普通墙消失,子弹也消失
public boolean BulletComeAcrossCWall(Bullet bullet) {
for (int i = 0; i < GamePanel.CWalls.size(); i++) {
CommentWall CWall = GamePanel.CWalls.get(i);
if (CWall.getRect().intersects(bullet.bulletgetRect())) {
bullet.isLive = false;
CWall.isLive = false;
GamePanel.CWalls.remove(CWall);
return true;
}
}
return false;
}
// 判断怪物和我的子弹与石墙相遇的时候--子弹消失
public boolean BulletComeAcrossBWall(Bullet bullet) {
for (int i = 0; i < GamePanel.BWalls_1.size(); i++) {
BlockWall BWall = GamePanel.BWalls_1.get(i);
if (BWall.getRect().intersects(bullet.bulletgetRect())) {
bullet.isLive = false;
return true;
}
}
return false;
}
// 判断我的坦克的子弹和怪物的子弹相遇的情况,子弹相遇后抵消,都消失
public boolean BulletComeAcrossMonster(Bullet bullet) {
for (int i = 0; i < GamePanel.monster.size(); i++) {
Monster mon = GamePanel.monster.get(i);
for (int j = 0; j < mon.bullets.size(); j++) {
Bullet b = mon.bullets.get(j);
if (b.bulletgetRect().intersects(bullet.bulletgetRect())) {
// 两者的存在状态都为死亡
bullet.isLive = false;
mon.bullets.remove(b);
return true;
}
}
}
return false;
}
// 判断怪物是否击中了我
public void HitMyTank() {
// 取出每一只怪物
for (int i = 0; i < GamePanel.monster.size(); i++) {
// 取出怪物
Monster mon = GamePanel.monster.get(i);
bullets = mon.getBullets();
// 取出怪物的每一个子弹
for (int j = 0; j < bullets.size(); j++) {
Bullet bullet = bullets.get(j);
if (GamePanel.myTank.isLive) {
// 调用判断是否相交的函数
HitTank(bullet, GamePanel.myTank);
}
}
}
}
// 判断我的坦克的子弹是否击中了怪物
public void HitMonster() {
// 判断是否击中了怪物
for (int i = 0; i < GamePanel.myTank.bullets.size(); i++) {
// 取出子弹
Bullet bullet = GamePanel.myTank.bullets.get(i);
if (bullet.isLive) {
// 取出每只怪物,进行匹配
for (int j = 0; j < GamePanel.monster.size(); j++) {
// 取出怪物
Monster mon = GamePanel.monster.get(j);
if (mon.isLive) {
// 调用判断是否相交的函数
HitTank(bullet, mon);
}
}
}
}
}
Graphics g = null;
// 判断子弹是否击中了目标--我的坦克或者是怪物
public boolean HitTank(Bullet bullet, TankMember tank) {
if (bullet.bulletgetRect().intersects(tank.getRect())) {
// 两者的存在状态都为死亡
bullet.isLive = false;
tank.isLive = false;
// 把子弹从子弹集合中去除
tank.bullets.remove(bullet);
// 新建爆炸效果
GamePanel.bomb = new Bomb(tank.getX(), tank.getY());
GamePanel.bombs.add(GamePanel.bomb);
try {
Thread.sleep(50);
// 启动声音
Audio audio = new Audio("Explosion.wav");
audio.start();
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
// run函数
public void run() {
while (isLive) {
switch (direct) {
case 0:
// 上
y -= speed;
break;
case 1:
// 右
x += speed;
break;
case 2:
// 下
y += speed;
break;
case 3:
// 左
x -= speed;
break;
}
try {
Thread.sleep(66);
} catch (Exception e) {
e.printStackTrace();
}
// 子弹何时死亡
if (x < 0 || x > Main.rangx || y < 0 || y > Main.rangy) {
this.isLive = false;
break;
}
}
}
// 调用坦克类的Rectangle函数
public Rectangle getRect() {
return new Rectangle(x, y, TankMember.size, TankMember.size);
}
// 调用子弹类的Rectangle函数
public Rectangle bulletgetRect() {
return new Rectangle(x, y, bulletwidth, bulletheight);
}
// 构造函数
public Bullet(int x, int y, int direct) {
this.x = x;
this.y = y;
this.direct = direct;
}
}
Bomb.java
package TankWar;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
class Bomb {
// 定义爆炸效果的左上角坐标(x,y)
private int x;
private int y;
// 构造函数
public Bomb(int x, int y) {
this.x = x;
this.y = y;
}
// 爆炸效果是否还存在,true为存在,false为不存在
boolean isLive = true;
// 爆炸效果图片初始化
// 定义为全局静态变量
private static Image[] BombImages = null;
static {
try {
BombImages = new Image[] {
// ImageIO.read(new File("bomb1.gif")),
// ImageIO.read(new File("bomb2.gif")),
// ImageIO.read(new File("bomb3.gif")),
// ImageIO.read(new File("bomb4.gif")),
// ImageIO.read(new File("bomb5.gif")),
// ImageIO.read(new File("bomb6.gif")),
// ImageIO.read(new File("bomb7.gif")),
// ImageIO.read(new File("bomb8.gif")),
// ImageIO.read(new File("bomb.gif")),};
Toolkit.getDefaultToolkit().getImage(
Bomb.class.getResource("/images/1.png")),
Toolkit.getDefaultToolkit().getImage(
Bomb.class.getResource("/images/2.png")),
Toolkit.getDefaultToolkit().getImage(
Bomb.class.getResource("/images/3.png")),
Toolkit.getDefaultToolkit().getImage(
Bomb.class.getResource("/images/4.png")),
Toolkit.getDefaultToolkit().getImage(
Bomb.class.getResource("/images/5.png")),
Toolkit.getDefaultToolkit().getImage(
Bomb.class.getResource("/images/6.png")),
Toolkit.getDefaultToolkit().getImage(
Bomb.class.getResource("/images/7.png")),
Toolkit.getDefaultToolkit().getImage(
Bomb.class.getResource("/images/8.png")),
Toolkit.getDefaultToolkit().getImage(
Bomb.class.getResource("/images/8.png")), };
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Graphics g;
static int step = 0;
// 画出爆炸效果
public void drawBomb(Graphics g) {
try {
Thread.sleep(50);
if (step == BombImages.length) {
GamePanel.bombs.remove(GamePanel.bomb);
step = 0;
this.isLive = false;
}
// System.out.println("画出爆炸效果!");
g.drawImage(BombImages[step], x - TankMember.size / 2, y
- TankMember.size , 2 * TankMember.size,
2 * TankMember.size, null);
step++;
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("输出图片出现问题了!");
e.printStackTrace();
}
}
}
BlockWall
package TankWar;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
public class BlockWall {
// 定义墙的大小
public final static int BlockWallwidth = 35;
public final static int BlockWallheight = 36;
// 定义坐标
int x;
int y;
// 定义普通墙的存在状态--无敌的
boolean isLive = true;
// 初始化墙的图片
private static Image[] wallImags = null;
static {
wallImags = new Image[] { Toolkit.getDefaultToolkit().getImage(
BlockWall.class.getResource("/images/BlockWall.gif")), };
}
// 画出石墙
public void drawBWall(Graphics g) {
g.drawImage(wallImags[0], x, y, 35, 35, null);
}
// 构造函数
public BlockWall(int x, int y) {
this.x = x;
this.y = y;
}
public Rectangle getRect() {
return new Rectangle(x, y, BlockWallheight, BlockWallwidth);
}
}
Audio.java
package TankWar;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
//播放声音的类
class Audio extends Thread {
private String filename;
java.net.URL path = null;
public Audio(String wavfile) {
filename = wavfile;
path = getClass().getResource("/Sound/" + filename);
}
public void run() {
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(path);
} catch (Exception e1) {
e1.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (Exception e) {
e.printStackTrace();
return;
}
auline.start();
int nBytesRead = 0;
// 这是缓冲
byte[] abData = new byte[1024];
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
auline.write(abData, 0, nBytesRead);
}
} catch (Exception e) {
e.printStackTrace();
return;
} finally {
auline.drain();
auline.close();
}
}
}
Recorder.java
package TankWar;
import java.io.*;
public class Recorder {
private static FileWriter fw = null;
private static BufferedWriter bw = null;
private static FileReader fr = null;
private static BufferedReader br = null;
// 临时起保存作用的字符串
private static String bulletscoordinate = "";
// 临时变量
private static Monster mon = null;
private static CommentWall cwall = null;
public void SaveCWallInfo() {
try {
// 创建
fw = new FileWriter("D:\\myrecoder.txt", true);
bw = new BufferedWriter(fw);
// 保存所有活着的怪物的坐标和方向
for (int i = 0; i < GamePanel.CWalls.size(); i++) {
cwall = GamePanel.CWalls.get(i);
if (cwall.isLive == true) {
// 把活的怪物的数据读取出来
String recoder = cwall.x + " " + cwall.y;
// 把活的怪物的数据写入到文本中
// 2作为标识符,作为普通墙的标识符
bw.write("2" + " " + recoder + "\r\n");
}
}
} catch (Exception e) {
// 将错误输出
e.printStackTrace();
} finally {
// 关闭流,先开后关,后开先关
try {
bw.close();
fw.close();
} catch (IOException e) {
// 捕获错误并输出
e.printStackTrace();
}
}
}
public void SaveMyTankInfo() {
try {
// 创建
fw = new FileWriter("D:\\myrecoder.txt", true);
bw = new BufferedWriter(fw);
bulletscoordinate = "";
// 保存我的坦克的坐标和方向
if (GamePanel.myTank.isLive == true) {
for (int i = 0; i < GamePanel.myTank.bullets.size(); i++) {
Bullet bullet = GamePanel.myTank.bullets.get(i);
if (bullet.isLive == true) {
// 如果子弹的存在状态是真的话,就保存子弹的数据累加到字符串中
bulletscoordinate += bullet.x + " " + bullet.y + " "
+ bullet.direct + " ";
}
}
// 把我的坦克的数据读取出来
String recoder = GamePanel.myTank.x + " " + GamePanel.myTank.y
+ " " + GamePanel.myTank.direct + " "
+ bulletscoordinate;
// 把我的坦克的数据写入到文本中
// 0作为标识符,来区分我的坦克和怪物的数据
bw.write("0" + " " + recoder + "\r\n");
}
} catch (Exception e) {
// 将错误输出
e.printStackTrace();
} finally {
// 关闭流,先开后关,后开先关
try {
bw.close();
fw.close();
} catch (IOException e) {
// 捕获错误并输出
e.printStackTrace();
}
}
}
public void SaveMonsterInfo() {
try {
// 创建
fw = new FileWriter("D:\\myrecoder.txt");
bw = new BufferedWriter(fw);
// 保存所有活着的怪物的坐标和方向
for (int i = 0; i < GamePanel.monster.size(); i++) {
Monster mon = GamePanel.monster.get(i);
if (mon.isLive == true) {
for (int j = 0; j < mon.bullets.size(); j++) {
Bullet bullet = mon.bullets.get(j);
if (bullet.isLive == true) {
// 如果子弹的存在状态是真的话,就保存子弹的数据累加到字符串中
bulletscoordinate += bullet.x + " " + bullet.y
+ " " + bullet.direct + " ";
}
}
// 把活的怪物的数据读取出来
String recoder = mon.x + " " + mon.y + " " + mon.direct
+ " " + bulletscoordinate;
// 把活的怪物的数据写入到文本中
// 1作为标识符,来区分我的坦克和怪物的数据
bw.write("1" + " " + recoder + "\r\n");
// 清空字符串
bulletscoordinate = "";
}
}
} catch (Exception e) {
// 将错误输出
e.printStackTrace();
} finally {
// 关闭流,先开后关,后开先关
try {
bw.close();
fw.close();
} catch (IOException e) {
// 捕获错误并输出
e.printStackTrace();
}
}
}
// 从记录文本文件中恢复数据
// 原理:将数据从文本中一行行读取出来后对游戏面板中的怪物集合和我的坦克进行初始化
// 也就是说省略了实例化创建我的坦克和怪物坦克的过程。
public void ReInfo() {
try {
fr = new FileReader("d:\\myrecoder.txt");
br = new BufferedReader(fr);
String Info = "";
Bullet bullet = null;
// 临时变量
int tempx = 0;
int tempy = 0;
int tempd = 0;
while ((Info = br.readLine()) != null) {
String[] RecoveryInfo = Info.split(" ");
// 类型转换,作为判断标识符
int n = Integer.parseInt(RecoveryInfo[0]);
// 如果第一个标识符是1,数据就是怪物的
if (n == 1) {
// 类型转换
tempx = Integer.parseInt(RecoveryInfo[1]);
tempy = Integer.parseInt(RecoveryInfo[2]);
tempd = Integer.parseInt(RecoveryInfo[3]);
// 恢复并进行初始化
mon = new Monster(tempx, tempy);
mon.direct = tempd;
mon.isLive = true;
GamePanel.monster.add(mon);
// 启动敌人坦克
Thread mont = new Thread(mon);
mont.start();
// 子弹的数据
int num = (RecoveryInfo.length - 1) / 3 - 1;
if (RecoveryInfo.length > 4) {
for (int i = 0; i < num; i++) {
if (i < num) {
// 类型转换
tempx = Integer
.parseInt(RecoveryInfo[3 * i + 4]);
tempy = Integer
.parseInt(RecoveryInfo[3 * i + 5]);
tempd = Integer
.parseInt(RecoveryInfo[3 * i + 6]);
// 恢复并进行初始化
bullet = new Bullet(tempx, tempy, tempd);
mon.bullets.add(bullet);
// 启动子弹线程
Thread t = new Thread(bullet);
t.start();
}
}
}
}
// 如果第一个标识符是0,数据就是我的坦克的
if (n == 0) {
// 类型转换
tempx = Integer.parseInt(RecoveryInfo[1]);
tempy = Integer.parseInt(RecoveryInfo[2]);
tempd = Integer.parseInt(RecoveryInfo[3]);
// 恢复并进行初始化
GamePanel.myTank = new MyTank(tempx, tempy);
GamePanel.myTank.direct = tempd;
GamePanel.myTank.isLive = true;
// 子弹的数据
int num = (RecoveryInfo.length - 1) / 3 - 1;
if (RecoveryInfo.length > 4) {
for (int i = 0; i < num; i++) {
if (i < num) {
// 类型转换
tempx = Integer
.parseInt(RecoveryInfo[3 * i + 4]);
tempy = Integer
.parseInt(RecoveryInfo[3 * i + 5]);
tempd = Integer
.parseInt(RecoveryInfo[3 * i + 6]);
// 恢复并进行初始化
bullet = new Bullet(tempx, tempy, tempd);
GamePanel.myTank.bullets.add(bullet);
Thread t = new Thread(bullet);
t.start();
}
}
}
}
// 如果第一个标识符是2,数据就是普通墙的
if (n == 2) {
// 类型转换
tempx = Integer.parseInt(RecoveryInfo[1]);
tempy = Integer.parseInt(RecoveryInfo[2]);
// 恢复并进行初始化
cwall = new CommentWall(tempx, tempy);
cwall.isLive = true;
GamePanel.CWalls.add(cwall);
}
}
} catch (Exception e) {
// 捕获错误信息
e.printStackTrace();
} finally {
try {
br.close();
fr.close();
} catch (Exception e2) {
// 捕获错误
e2.printStackTrace();
}
}
}
}
TankInfomation.java
package TankWar;
public class TankInfomation {
int x = 0;
int y = 0;
int direct = 0;
public TankInfomation(int x,int y,int direct) {
this.x = x;
this.y = y;
this.direct = direct;
}
}
TankMember.java
package TankWar;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.Random;
import java.util.Vector;
// 主类-坦克类
abstract class TankMember {
// 定义一个类型:0.为我的坦克,1.为怪物
int type;
// 定义我的坦克的生命状态
public boolean isLive = true;
// 定义坐标(x,y)
public int x = 0;
public int y = 0;
private int oldX, oldY;
// 定义坦克的宽度和长度
public final static int size = 35;
// 定义坦克的速度
public int speed = 5;
// 定义坦克的方向
// 说明:0为方向上,1为方向右,2为方向下,3为方向左
public int direct = 0;
// 定义子弹集合
Bullet bullet = null;
Vector<Bullet> bullets = new Vector<Bullet>();
// 定义爆炸集合
Bomb bomb = null;
Vector<Bomb> bombs = new Vector<Bomb>();
// 开火的能力
public void ShotEnemy() {
if (GamePanel.myTank.isLive) {
// 启动声音
Audio audio = new Audio("Shot.wav");
audio.start();
}
switch (this.direct) {
case 0:// 上
bullet = new Bullet(x + (size - 8) / 2, y, 0);
bullets.add(bullet);
break;
case 1:// 右
bullet = new Bullet(x + size / 2, y + (size - 6) / 2, 1);
bullets.add(bullet);
break;
case 2:// 下
bullet = new Bullet(x + (size - 10) / 2, y + size / 2, 2);
bullets.add(bullet);
break;
case 3:// 左
bullet = new Bullet(x, y + (size - 6) / 2, 3);
bullets.add(bullet);
break;
}
// 启动子弹线程
Thread t = new Thread(bullet);
t.start();
}
// 初始化坦克图片
private static Image[] tankImages = null;// 定义为全局静态变量
static {
tankImages = new Image[] {
Toolkit.getDefaultToolkit().getImage(
TankMember.class.getResource("/images/1.gif")),
Toolkit.getDefaultToolkit().getImage(
TankMember.class.getResource("/images/2.gif")),
Toolkit.getDefaultToolkit().getImage(
TankMember.class.getResource("/images/3.gif")),
Toolkit.getDefaultToolkit().getImage(
TankMember.class.getResource("/images/4.gif")),
Toolkit.getDefaultToolkit().getImage(
TankMember.class.getResource("/images/m0.gif")),
Toolkit.getDefaultToolkit().getImage(
TankMember.class.getResource("/images/m1.gif")),
Toolkit.getDefaultToolkit().getImage(
TankMember.class.getResource("/images/m2.gif")),
Toolkit.getDefaultToolkit().getImage(
TankMember.class.getResource("/images/m3.gif")), };
}
// 画出坦克的函数
public void drawTank(int x, int y, Graphics g, int type) {
switch (direct) {
case 0:
if (type == 1) {
g.drawImage(tankImages[0], x, y, null);
} else {
g.drawImage(tankImages[4], x - 7, y - 7, null);
}
break;
case 1:
if (type == 1) {
g.drawImage(tankImages[1], x, y, null);
} else {
g.drawImage(tankImages[5], x - 7, y - 7, null);
}
break;
case 2:
if (type == 1) {
g.drawImage(tankImages[2], x, y, null);
} else {
g.drawImage(tankImages[6], x - 7, y - 7, null);
}
break;
case 3:
if (type == 1) {
g.drawImage(tankImages[3], x, y, null);
} else {
g.drawImage(tankImages[7], x - 7, y - 7, null);
}
break;
}
}
// 定义我的坦克的移动函数
public abstract void move();
// 一旦与其他坦克相遇就转向
public int DictionDirection(int direct) {
switch (type) {
case 0:
while (direct == 0) {
this.direct = (int) (Math.random() * 4);
}
break;
case 1:
while (direct == 1) {
this.direct = (int) (Math.random() * 4);
}
break;
case 2:
while (direct == 2) {
this.direct = (int) (Math.random() * 4);
}
break;
case 3:
while (direct == 3) {
this.direct = (int) (Math.random() * 4);
}
break;
}
return direct;
}
// 判断是否越界
public boolean isTouchBorder() {
if (x < BlockWall.BlockWallwidth) {
x = BlockWall.BlockWallwidth;
return true;
}
if (y < BlockWall.BlockWallheight) {
y = BlockWall.BlockWallheight;
return true;
}
if (x + size > Main.rangx) {
x = Main.rangx - size;
return true;
}
if (y + size > Main.rangy) {
y = Main.rangy - size;
return true;
}
return false;
}
// 判断是否与普通墙相撞
public abstract boolean isTouchCWall();
// 判断是否与石墙相撞
public abstract boolean isTouchBWall();
// 判断是否接触
public abstract boolean isTouchOtherTank(int direct);
// 调用Rectangle函数
public Rectangle getRect() {
return new Rectangle(this.x, this.y, size, size);
}
// 保存旧的位置,保证出现错误的移动可以恢复到原先的位置
public void settoOldDirect() {
this.oldX = x;
this.oldY = y;
}
// 在出现错误的移动后,改变现有位置为保存的旧的位置
public void changtoOldDirect() {
x = oldX;
y = oldY;
}
Graphics g = null;
// 以下为成员变量的构造函数
public TankMember(int x, int y) {
this.x = x;
this.y = y;
this.oldX = x;
this.oldY = y;
}
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 getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
public Vector<Bullet> getBullets() {
return bullets;
}
public void setBullets(Vector<Bullet> bullets) {
this.bullets = bullets;
}
}
// 我的坦克类
class MyTank extends TankMember {
// 定义坦克的速度
public int speed = 7;
// 定义坦克的生命值
public static int blood = 200;
// 防止重叠
public boolean isTouchOtherTank(int direct) {
switch (this.direct) {
case 0:
for (int i = 0; i < GamePanel.monster.size(); i++) {
Monster mon = GamePanel.monster.get(i);
if (mon.direct == 0 || mon.direct == 2) {
// 左点
if (this.x >= mon.x && this.x <= mon.x + size + 1
&& this.y >= mon.y && this.y <= mon.y + size + 1) {
return true;
}
if (this.x + size + 1 >= mon.x
&& this.x + size + 1 <= mon.x + size + 1
&& this.y >= mon.y && this.y <= mon.y + size + 1) {
return true;
}
}
if (mon.direct == 3 || mon.direct == 1) {
if (this.x >= mon.x && this.x <= mon.x + size + 1
&& this.y >= mon.y && this.y <= mon.y + size + 1) {
return true;
}
if (this.x + size + 1 >= mon.x
&& this.x + size + 1 <= mon.x + size + 1
&& this.y >= mon.y && this.y <= mon.y + size + 1) {
return true;
}
}
}
break;
case 1:
// 坦克向右
// 取出所有的敌人坦克
for (int i = 0; i < GamePanel.monster.size(); i++) {
// 取出第一个坦克
Monster mon = GamePanel.monster.get(i);
// 如果敌人的方向是向下或者向上
if (mon.direct == 0 || mon.direct == 2) {
// 上点
if (this.x + size >= mon.x && this.x + size <= mon.x + size
&& this.y >= mon.y && this.y <= mon.y + size) {
return true;
}
// 下点
if (this.x + size >= mon.x && this.x + size <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
}
if (mon.direct == 3 || mon.direct == 1) {
if (this.x + size >= mon.x && this.x + size <= mon.x + size
&& this.y >= mon.y && this.y <= mon.y + size) {
return true;
}
if (this.x + size >= mon.x && this.x + size <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
}
}
break;
case 2:
// 坦克向下
// 取出所有的敌人坦克
for (int i = 0; i < GamePanel.monster.size(); i++) {
// 取出第一个坦克
Monster mon = GamePanel.monster.get(i);
// 如果敌人的方向是向下或者向上
if (mon.direct == 0 || mon.direct == 2) {
// 我的左点
if (this.x >= mon.x && this.x <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
// 我的右点
if (this.x + size >= mon.x && this.x + size <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
}
if (mon.direct == 3 || mon.direct == 1) {
if (this.x >= mon.x && this.x <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
if (this.x + size >= mon.x && this.x + size <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
}
}
break;
case 3:
// 向左
// 取出所有的敌人坦克
for (int i = 0; i < GamePanel.monster.size(); i++) {
// 取出第一个坦克
Monster mon = GamePanel.monster.get(i);
// 如果敌人的方向是向下或者向上
if (mon.direct == 0 || mon.direct == 2) {
// 我的上一点
if (this.x >= mon.x && this.x <= mon.x + size
&& this.y >= mon.y && this.y <= mon.y + size) {
return true;
}
// 下一点
if (this.x >= mon.x && this.x <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
}
if (mon.direct == 3 || mon.direct == 1) {
// 上一点
if (this.x >= mon.x && this.x <= mon.x + size
&& this.y >= mon.y && this.y <= mon.y + size) {
return true;
}
if (this.x >= mon.x && this.x <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
}
}
break;
}
return false;
}
// 定义我的坦克的移动函数
public void move() {
// 保存旧的位置
settoOldDirect();
switch (direct) { // 选择移动方向
case 0:
if (!isTouchOtherTank(direct) && !isTouchBorder()
&& !isTouchCWall() && !isTouchBWall()) {
this.y -= speed;
if (isTouchOtherTank(direct) || isTouchBorder()
|| isTouchCWall() || isTouchBWall()) {
this.changtoOldDirect();
}
}
break;
case 1:
this.settoOldDirect();
if (!isTouchOtherTank(direct) && !isTouchBorder()
&& !isTouchCWall() && !isTouchBWall()) {
this.x += speed;
if (isTouchOtherTank(direct) || isTouchBorder()
|| isTouchCWall() || isTouchBWall()) {
this.changtoOldDirect();
}
}
break;
case 2:
this.settoOldDirect();
if (!isTouchOtherTank(direct) && !isTouchBorder()
&& !isTouchCWall() && !isTouchBWall()) {
this.y += speed;
if (isTouchOtherTank(direct) || isTouchBorder()
|| isTouchCWall() || isTouchBWall()) {
this.changtoOldDirect();
}
}
break;
case 3:
this.settoOldDirect();
if (!isTouchOtherTank(direct) && !isTouchBorder()
&& !isTouchCWall() && !isTouchBWall()) {
x -= speed;
if (isTouchOtherTank(direct) || isTouchBorder()
|| isTouchCWall() || isTouchBWall()) {
this.changtoOldDirect();
}
}
break;
}
}
public boolean isTouchCWall() {
for (int i = 0; i < GamePanel.CWalls.size(); i++) {
CommentWall CWall = GamePanel.CWalls.get(i);
if (GamePanel.myTank.getRect().intersects(CWall.getRect())) {
return true;
}
}
return false;
}
public boolean isTouchBWall() {
for (int i = 0; i < GamePanel.BWalls_1.size(); i++) {
BlockWall BWall = GamePanel.BWalls_1.get(i);
if (GamePanel.myTank.getRect().intersects(BWall.getRect())) {
return true;
}
}
return false;
}
public MyTank(int x, int y) {
super(x, y);
}
}
// 怪物类
// 由于怪物是多个的,且要求互不影响,所以要做成多线程的
class Monster extends TankMember implements Runnable {
// 定义坦克类型
int type = 1;
// 定义怪物连发子弹的数量
static int bulletsize = 2;
// 定义怪物的数量
private static int TankNumber = 0;
// 定义坦克的生命值
public static int blood = 50;
// 定义随机变量
private static Random r = new Random();
private int movesteps = r.nextInt(30) + 15;
// 定义怪物的停顿时间
private final int WaitingTime = 25;
// 定义让坦克随机产生一个随机的新的方向的等待时间
private final int WaitingforRandom = 30;
// 定义我的坦克的移动函数
public void move() {
// 保存旧的位置,保证出现错误的移动可以恢复到原先的位置
settoOldDirect();
switch (direct) { // 选择移动方向
case 0:
y -= speed;
break;
case 1:
x += speed;
break;
case 2:
y += speed;
break;
case 3:
x -= speed;
break;
}
}
// 防止重叠
// public boolean isTouchOtherTank(int direct) {
// for (int i = 0; i < GamePanel.monster.size(); i++) {
// Monster mon = GamePanel.monster.get(i);
// if (this != mon) {
// if (mon.getRect().intersects(this.getRect())) {
// return true;
// }
// }
// }
// return false;
// }
//
// public boolean isTouchMyTank(Monster mon) {
// if (mon.getRect().intersects(GamePanel.myTank.getRect())) {
// return true;
// }
// return false;
// }
public boolean isTouchOtherTank(int direct) {
switch (this.direct) {
case 0:
for (int i = 0; i < GamePanel.monster.size(); i++) {
Monster mon = GamePanel.monster.get(i);
if (this != mon) {
if (mon.direct == 0 || mon.direct == 2) {
// 左点
if (this.x >= mon.x && this.x <= mon.x + size + 1
&& this.y >= mon.y
&& this.y <= mon.y + size + 1) {
return true;
}
if (this.x + size + 1 >= mon.x
&& this.x + size + 1 <= mon.x + size + 1
&& this.y >= mon.y
&& this.y <= mon.y + size + 1) {
return true;
}
}
if (mon.direct == 3 || mon.direct == 1) {
if (this.x >= mon.x && this.x <= mon.x + size + 1
&& this.y >= mon.y
&& this.y <= mon.y + size + 1) {
return true;
}
if (this.x + size + 1 >= mon.x
&& this.x + size + 1 <= mon.x + size + 1
&& this.y >= mon.y
&& this.y <= mon.y + size + 1) {
return true;
}
}
}
if (this.type == 1) {
if (GamePanel.myTank.direct == 0
|| GamePanel.myTank.direct == 2) {
// 左点
if (this.x >= GamePanel.myTank.x
&& this.x <= GamePanel.myTank.x + size + 1
&& this.y >= GamePanel.myTank.y
&& this.y <= GamePanel.myTank.y + size + 1) {
return true;
}
if (this.x + size + 1 >= GamePanel.myTank.x
&& this.x + size + 1 <= GamePanel.myTank.x
+ size + 1
&& this.y >= GamePanel.myTank.y
&& this.y <= GamePanel.myTank.y + size + 1) {
return true;
}
}
if (GamePanel.myTank.direct == 3
|| GamePanel.myTank.direct == 1) {
if (this.x >= GamePanel.myTank.x
&& this.x <= GamePanel.myTank.x + size + 1
&& this.y >= GamePanel.myTank.y
&& this.y <= GamePanel.myTank.y + size + 1) {
return true;
}
if (this.x + size + 1 >= GamePanel.myTank.x
&& this.x + size + 1 <= GamePanel.myTank.x
+ size + 1
&& this.y >= GamePanel.myTank.y
&& this.y <= GamePanel.myTank.y + size + 1) {
return true;
}
}
}
}
case 1:
// 坦克向右
// 取出所有的敌人坦克
for (int i = 0; i < GamePanel.monster.size(); i++) {
// 取出第一个坦克
Monster mon = GamePanel.monster.get(i);
// 如果不是自己
if (mon != this) {
// 如果敌人的方向是向下或者向上
if (mon.direct == 0 || mon.direct == 2) {
// 上点
if (this.x + size >= mon.x
&& this.x + size <= mon.x + size
&& this.y >= mon.y && this.y <= mon.y + size) {
return true;
}
// 下点
if (this.x + size >= mon.x
&& this.x + size <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
}
if (mon.direct == 3 || mon.direct == 1) {
if (this.x + size >= mon.x
&& this.x + size <= mon.x + size
&& this.y >= mon.y && this.y <= mon.y + size) {
return true;
}
if (this.x + size >= mon.x
&& this.x + size <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
}
}
if (this.type == 1) {
// 如果敌人的方向是向下或者向上
if (GamePanel.myTank.direct == 0
|| GamePanel.myTank.direct == 2) {
// 上点
if (this.x + size >= GamePanel.myTank.x
&& this.x + size <= GamePanel.myTank.x + size
&& this.y >= GamePanel.myTank.y
&& this.y <= GamePanel.myTank.y + size) {
return true;
}
// 下点
if (this.x + size >= GamePanel.myTank.x
&& this.x + size <= GamePanel.myTank.x + size
&& this.y + size >= GamePanel.myTank.y
&& this.y + size <= GamePanel.myTank.y + size) {
return true;
}
}
if (GamePanel.myTank.direct == 3
|| GamePanel.myTank.direct == 1) {
if (this.x + size >= GamePanel.myTank.x
&& this.x + size <= GamePanel.myTank.x + size
&& this.y >= GamePanel.myTank.y
&& this.y <= GamePanel.myTank.y + size) {
return true;
}
if (this.x + size >= GamePanel.myTank.x
&& this.x + size <= GamePanel.myTank.x + size
&& this.y + size >= GamePanel.myTank.y
&& this.y + size <= GamePanel.myTank.y + size) {
return true;
}
}
}
}
break;
case 2:
// 坦克向下
// 取出所有的敌人坦克
for (int i = 0; i < GamePanel.monster.size(); i++) {
// 取出第一个坦克
Monster mon = GamePanel.monster.get(i);
// 如果不是自己
if (mon != this) {
// 如果敌人的方向是向下或者向上
if (mon.direct == 0 || mon.direct == 2) {
// 我的左点
if (this.x >= mon.x && this.x <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
// 我的右点
if (this.x + size >= mon.x
&& this.x + size <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
}
if (mon.direct == 3 || mon.direct == 1) {
if (this.x >= mon.x && this.x <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
if (this.x + size >= mon.x
&& this.x + size <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
}
}
if (this.type == 1) {
// 如果敌人的方向是向下或者向上
if (GamePanel.myTank.direct == 0
|| GamePanel.myTank.direct == 2) {
// 我的左点
if (this.x >= GamePanel.myTank.x
&& this.x <= GamePanel.myTank.x + size
&& this.y + size >= GamePanel.myTank.y
&& this.y + size <= GamePanel.myTank.y + size) {
return true;
}
// 我的右点
if (this.x + size >= GamePanel.myTank.x
&& this.x + size <= GamePanel.myTank.x + size
&& this.y + size >= GamePanel.myTank.y
&& this.y + size <= GamePanel.myTank.y + size) {
return true;
}
}
if (GamePanel.myTank.direct == 3
|| GamePanel.myTank.direct == 1) {
if (this.x >= GamePanel.myTank.x
&& this.x <= GamePanel.myTank.x + size
&& this.y + size >= GamePanel.myTank.y
&& this.y + size <= GamePanel.myTank.y + size) {
return true;
}
if (this.x + size >= GamePanel.myTank.x
&& this.x + size <= GamePanel.myTank.x + size
&& this.y + size >= GamePanel.myTank.y
&& this.y + size <= GamePanel.myTank.y + size) {
return true;
}
}
}
}
break;
case 3:
// 向左
// 取出所有的敌人坦克
for (int i = 0; i < GamePanel.monster.size(); i++) {
// 取出第一个坦克
Monster mon = GamePanel.monster.get(i);
// 如果不是自己
if (mon != this) {
// 如果敌人的方向是向下或者向上
if (mon.direct == 0 || mon.direct == 2) {
// 我的上一点
if (this.x >= mon.x && this.x <= mon.x + size
&& this.y >= mon.y && this.y <= mon.y + size) {
return true;
}
// 下一点
if (this.x >= mon.x && this.x <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
}
if (mon.direct == 3 || mon.direct == 1) {
// 上一点
if (this.x >= mon.x && this.x <= mon.x + size
&& this.y >= mon.y && this.y <= mon.y + size) {
return true;
}
if (this.x >= mon.x && this.x <= mon.x + size
&& this.y + size >= mon.y
&& this.y + size <= mon.y + size) {
return true;
}
}
}
if (this.type == 1) {
// 如果敌人的方向是向下或者向上
if (GamePanel.myTank.direct == 0
|| GamePanel.myTank.direct == 2) {
// 我的上一点
if (this.x >= GamePanel.myTank.x
&& this.x <= GamePanel.myTank.x + size
&& this.y >= GamePanel.myTank.y
&& this.y <= GamePanel.myTank.y + size) {
return true;
}
// 下一点
if (this.x >= GamePanel.myTank.x
&& this.x <= GamePanel.myTank.x + size
&& this.y + size >= GamePanel.myTank.y
&& this.y + size <= GamePanel.myTank.y + size) {
return true;
}
}
if (GamePanel.myTank.direct == 3
|| GamePanel.myTank.direct == 1) {
// 上一点
if (this.x >= GamePanel.myTank.x
&& this.x <= GamePanel.myTank.x + size
&& this.y >= GamePanel.myTank.y
&& this.y <= GamePanel.myTank.y + size) {
return true;
}
if (this.x >= GamePanel.myTank.x
&& this.x <= GamePanel.myTank.x + size
&& this.y + size >= GamePanel.myTank.y
&& this.y + size <= GamePanel.myTank.y + size) {
return true;
}
}
}
}
break;
}
return false;
}
// 禁止几秒
private void StopSeconds() {
int olddirect = this.direct;
int waitingtime = r.nextInt(6);
try {
Thread.sleep(waitingtime);
} catch (Exception e) {
e.printStackTrace();
}
direct = olddirect;
}
public void run() {
while (true) {
try {
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
switch (this.direct) {
case 0:
// 说明坦克正在向上移动
for (int i = 0; i < movesteps; i++) {
// 判断坦克是否遇到障碍物,遇到就停止
if (!isTouchOtherTank(direct) && !isTouchBWall()
&& !isTouchCWall() && !isTouchBorder()) {
this.move();
// 判断坦克行走后是否遇到障碍物,遇到就停止并还原移动
if (isTouchOtherTank(direct) || isTouchBWall()
|| isTouchCWall() || isTouchBorder()) {
this.changtoOldDirect();
StopSeconds();
}
} else {
break;
}
try {
Thread.sleep(WaitingTime);
} catch (Exception e) {
e.printStackTrace();
}
}
break;
case 1:
// 向右
for (int i = 0; i < movesteps; i++) {
// 判断坦克是否遇到障碍物,遇到就停止
if (!isTouchOtherTank(direct) && !isTouchBWall()
&& !isTouchCWall() && !isTouchBorder()) {
this.move();
// 判断坦克行走后是否遇到障碍物,遇到就停止并还原移动
if (isTouchOtherTank(direct) || isTouchBWall()
|| isTouchCWall() || isTouchBorder()) {
this.changtoOldDirect();
StopSeconds();
}
} else {
break;
}
try {
Thread.sleep(WaitingTime);
} catch (Exception e) {
e.printStackTrace();
}
}
break;
case 2:
// 向下
for (int i = 0; i < movesteps; i++) {
// 判断坦克是否遇到障碍物,遇到就停止
if (!isTouchOtherTank(direct) && !isTouchBWall()
&& !isTouchCWall() && !isTouchBorder()) {
this.move();
// 判断坦克行走后是否遇到障碍物,遇到就停止并还原移动
if (isTouchOtherTank(direct) || isTouchBWall()
|| isTouchCWall() || isTouchBorder()) {
this.changtoOldDirect();
StopSeconds();
}
} else {
break;
}
try {
Thread.sleep(WaitingTime);
} catch (Exception e) {
e.printStackTrace();
}
}
break;
case 3:
// 向左
for (int i = 0; i < movesteps; i++) {
// 判断坦克是否遇到障碍物,遇到就停止
if (!isTouchOtherTank(direct) && !isTouchBWall()
&& !isTouchCWall() && !isTouchBorder()) {
this.move();
// 判断坦克行走后是否遇到障碍物,遇到就停止并还原移动
if (isTouchOtherTank(direct) || isTouchBWall()
|| isTouchCWall() || isTouchBorder()) {
this.changtoOldDirect();
StopSeconds();
}
} else {
break;
}
try {
Thread.sleep(WaitingTime);
} catch (Exception e) {
e.printStackTrace();
}
}
break;
}
// 添加子弹
AddBullets();
try {
// 让坦克随机产生一个随机的新的方向
Thread.sleep(WaitingforRandom);
this.direct = (int) (Math.random() * 4);
} catch (Exception e) {
e.printStackTrace();
}
// 判断敌人坦克是否死亡
if (this.isLive == false) {
// 让坦克死亡后退出线程
break;
}
}
}
private void AddBullets() {
// 判断是否需要给坦克加入新的子弹
if (true) {
if (isLive) {
if (bullets.size() < bulletsize) {
if (GamePanel.myTank.isLive && bullets.size() > 2) {
// 启动声音
Audio audio = new Audio("Shot.wav");
audio.start();
}
// 没有子弹,添加
Bullet bullet = null;
switch (direct) {
case 0:// 上
bullet = new Bullet(x + (size - 8) / 2, y, 0);
this.bullets.add(bullet);
break;
case 1:// 右
bullet = new Bullet(x + size / 2, y + (size - 6) / 2, 1);
this.bullets.add(bullet);
break;
case 2:// 下
bullet = new Bullet(x + (size - 10) / 2, y + size / 2,
2);
this.bullets.add(bullet);
break;
case 3:// 左
bullet = new Bullet(x, y + (size - 6) / 2, 3);
this.bullets.add(bullet);
break;
}
// 启动子弹线程
Thread t = new Thread(bullet);
t.start();
}
}
}
}
public boolean isTouchCWall() {
for (int i = 0; i < GamePanel.CWalls.size(); i++) {
CommentWall CWall = GamePanel.CWalls.get(i);
for (int j = 0; j < GamePanel.monster.size(); j++) {
Monster mon = GamePanel.monster.get(j);
if (CWall.getRect().intersects(mon.getRect())) {
return true;
}
}
}
return false;
}
public boolean isTouchBWall() {
for (int i = 0; i < GamePanel.BWalls_1.size(); i++) {
BlockWall BWall = GamePanel.BWalls_1.get(i);
for (int j = 0; j < GamePanel.monster.size(); j++) {
Monster mon = GamePanel.monster.get(j);
if (BWall.getRect().intersects(mon.getRect())) {
return true;
}
}
}
return false;
}
// 以下为成员变量的构造函数
public static int getTankNumber() {
return TankNumber;
}
public static void setTankNumber(int tankNumber) {
TankNumber = tankNumber;
}
// 调用父类的构造函数进行初始化
public Monster(int x, int y) {
super(x, y);
}
}
Tree.java
package TankWar;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
public class Tree {
// 定义墙的大小
public static int Treewidth = 36;
public static int Treeheight = 36;
// 初始化墙的图片
private static Image[] treeImags = null;
static {
treeImags = new Image[] { Toolkit.getDefaultToolkit().getImage(
Tree.class.getResource("/images/tree.gif")), };
}
// 定义坐标
public int x;
public int y;
// 定义普通墙的存在状态--永远存在的
// boolean isLive = true;
// 画出普通墙
public void drawTree(Graphics g) {
g.drawImage(treeImags[0], x, y, null);
}
// 构造函数
public Tree(int x, int y) {
this.x = x;
this.y = y;
}
public Rectangle getRect() {
return new Rectangle(x, y, Treewidth, Treeheight);
}
}