/**
* @author strive
* 功能:坦克大战1.0版本
*/
package com.Tank;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.util.*;
public class TankGame extends JFrame implements ActionListener{
//定义控件
My_Panel mp=null;
StartPanel sp=null;
//菜单栏
JMenuBar jmb=null;
//菜单
JMenu jm=null;
//子菜单
JMenuItem jmi1=null;
JMenuItem jmi2=null;
JMenuItem jmi3=null;
JMenuItem jmi4=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
TankGame tg=new TankGame();
}
//构造函数
public TankGame()
{
//创建游戏开始面板
sp=new StartPanel();
Thread tsp=new Thread(sp);
//创建菜单栏
jmb=new JMenuBar();
//创建菜单及子菜单
jm=new JMenu("游戏");
jmi1=new JMenuItem("开始新游戏");
jmi2=new JMenuItem("存档退出游戏");
jmi3=new JMenuItem("续上局存档");
jmi4=new JMenuItem("退出游戏");
//添加消息源
jmi1.addActionListener(this);
jmi1.setActionCommand("start");
jmi2.addActionListener(this);
jmi2.setActionCommand("saveExit");
jmi3.addActionListener(this);
jmi3.setActionCommand("continue");
jmi4.addActionListener(this);
jmi4.setActionCommand("exit");
//子菜单加入菜单中
jm.add(jmi1);
jm.add(jmi2);
jm.add(jmi3);
jm.add(jmi4);
//菜单加入菜单栏
jmb.add(jm);
//菜单栏加入面板
this.setJMenuBar(jmb);
this.add(sp);
tsp.start();
this.setSize(600,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
//消息响应
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("start"))
{
//创建游戏界面面板
mp=new My_Panel("newGames");
mp.setSize(600, 500);
//关闭start线程
this.remove(sp);
//启动mp线程
Thread th=new Thread(mp);
th.start();
this.add(mp);
this.addKeyListener(mp);
}else if(e.getActionCommand().equals("saveExit"))
{
Recorder.setEts(mp.enemys);
Recorder.saveGame();
System.exit(0);
}else if(e.getActionCommand().equals("continue"))
{
//创建游戏界面面板
mp=new My_Panel("continue");
mp.setSize(600, 500);
//关闭start线程
this.remove(sp);
//启动mp线程
Thread th=new Thread(mp);
th.start();
this.add(mp);
this.addKeyListener(mp);
}else if(e.getActionCommand().equals("exit"))
{
System.exit(0);
}
}
}
//定义游戏开始(关卡显示)面盘
class StartPanel extends JPanel implements Runnable
{
//闪光效果
int on=0;
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
if(on%2==0)
{
g.setColor(Color.yellow);
g.setFont(new Font("华文新魏",Font.BOLD,30));
g.drawString("Stage : 1", 150, 150);
}
}
public void run() {
// TODO Auto-generated method stub
while(true)
{
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.on++;
if(on==10)
{
on=0;
}
//System.out.println(on);
this.repaint();
}
}
}
//定义我的panel类,画板
class My_Panel extends JPanel implements KeyListener,Runnable
{
//表示击毁第几辆坦克
Thread []thEnemy=null;
Thread []thEShot=null;
//定义一变量,表示当前游戏是启动还是暂停
boolean run=true;
//定义一个我的坦克
Hero hero=null;
//定义敌军坦克组
Vector<Enemy> enemys=new Vector<Enemy>();
//限制敌军每次坦克数量
int ey_size=6;
//定义一个炸弹集合
Vector<Bomb> bombs=new Vector<Bomb>();
//定义三张图片(爆炸过程)
Image image1=null;
Image image2=null;
Image image3=null;
//构造函数
public My_Panel(String flag)
{
Vector<Node> nodes=new Vector<Node>();
//创建我军坦克
hero=new Hero(200, 250);
hero.setColor(0);
if(flag=="newGames")
{
thEnemy=new Thread[ey_size];
thEShot=new Thread[ey_size];
//创建敌军坦克组
for(int i=0;i<ey_size;i++)
{
//创建敌军坦克
Enemy e=new Enemy((i+1)*50, 0);
e.getEnemys(enemys);
//给敌军坦克注入子弹
Shot enemyShot=new Shot(e.x+13, e.y+25, 2);
//加入到子弹集合
e.ss.add(enemyShot);
//开始敌军坦克线程
thEnemy[i]=new Thread(e);
thEnemy[i].start();
//开启子弹线程
thEShot[i]=new Thread(enemyShot);
thEShot[i].start();
e.setColor(1);
e.setDirect(2);
enemys.add(e);
}
}else
{
nodes=Recorder.getTanGame();
thEnemy=new Thread[nodes.size()];
thEShot=new Thread[nodes.size()];
//创建敌军坦克组
for(int i=0;i<nodes.size();i++)
{
Node node=nodes.get(i);
//创建敌军坦克
Enemy e=new Enemy(node.x,node.y);
e.getEnemys(enemys);
//给敌军坦克注入子弹
Shot enemyShot=new Shot(e.x+13, e.y+25, 2);
//加入到子弹集合
e.ss.add(enemyShot);
//开始敌军坦克线程
thEnemy[i]=new Thread(e);
thEnemy[i].start();
//开启子弹线程
thEShot[i]=new Thread(enemyShot);
thEShot[i].start();
e.setColor(1);
e.setDirect(node.dircet);
enemys.add(e);
}
}
//音频文件
AePlayWave ap=new AePlayWave(".\\111.wav");
ap.start();
//图片初始化
// image1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
// image2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
// image3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));
try {
image1=ImageIO.read(new File("bomb_1.gif"));
image2=ImageIO.read(new File("bomb_2.gif"));
image3=ImageIO.read(new File("bomb_3.gif"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//画板游戏信息
public void ShowIo(Graphics g)
{
//敌军坦克剩余数量统计
this.drawTank(60, 350, g, 0, 1);
g.setColor(Color.black);
g.drawString(Recorder.getEnemyNo()+"", 100, 370);
//hero生命值
this.drawTank(140, 350, g, 0, 0);
g.setColor(Color.black);
g.drawString(Recorder.getHeroLife()+"", 180, 370);
//记录玩家成绩
g.setColor(Color.red);
g.setFont(new Font("华文隶书",Font.BOLD,18));
g.drawString("您的总成绩为", 400, 40);
this.drawTank(420, 70, g, 0, 1);
g.setColor(Color.black);
g.drawString(Recorder.getHitenemyNo()+"", 480, 90);
this.drawTank(420, 120, g, 0, 0);
g.setColor(Color.black);
g.drawString(Recorder.getDeadHero()+"", 480, 140);
}
//画出墙面阻碍物
public void build1(Graphics g,int x,int y)
{
g.setColor(Color.red);
g.fill3DRect(x, y, 10, 5, false);
}
public void build2(Graphics g,int x,int y)
{
g.setColor(Color.red);
g.fill3DRect(x, y, 5, 10, false);
}
//覆盖paint函数
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 400, 300);
//画墙面(HELLO)
for(int h=0;h<9;h++)
{
for(int d=0;d<2;d++)
{
int x=20+d*5;
int y=100+h*10;
this.build2(g, x, y);
}
}
for(int d=0;d<4;d++)
{
for(int h=0;h<2;h++)
{
int x=30+d*10;
int y=140+h*5;
this.build1(g, x, y);
}
}
for(int h=0;h<9;h++)
{
for(int d=0;d<2;d++)
{
int x=70+d*5;
int y=100+h*10;
this.build2(g, x, y);
}
}
for(int d=0;d<5;d++)
{
for(int h=0;h<2;h++)
{
int x=100+10*d;
int y=100+5*h;
this.build1(g, x, y);
}
}
for(int h=0;h<9;h++)
{
for(int d=0;d<2;d++)
{
int x=100+5*d;
int y=100+10*h;
this.build2(g, x, y);
}
}
for(int d=0;d<5;d++)
{
for(int h=0;h<2;h++)
{
int x=100+10*d;
int y=140+5*h;
this.build1(g, x, y);
}
}
for(int d=0;d<5;d++)
{
for(int h=0;h<2;h++)
{
int x=100+10*d;
int y=180+5*h;
this.build1(g, x, y);
}
}