public class Tank1 extends JFrame implements ActionListener {
MyPanel mp = null;
GuanKaPanel gkmb = null;
JMenuBar cdt = null;
JMenu cd = null;
JMenuItem cdx1 = null;
public static void main(String[] args) {
Tank1 t1 = new Tank1();// 主函数的调用
}
public Tank1() {
cdt = new JMenuBar();
cd = new JMenu("游戏(G)");
cd.setMnemonic('G');
cdx1 = new JMenuItem("新游戏(N)");
cdx1.addActionListener(this);
cdx1.setActionCommand("xyx");
cd.add(cdx1);
cdt.add(cd);
JMenuItem menuItem = new JMenuItem("\u9000\u51FA\u6E38\u620F");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JiLu.bcjl();
System.exit(0);
}
});
cd.add(menuItem);
/*
* mp = new MyPanel(); this.add(mp); this.addKeyListener(mp); Thread t =
* new Thread(mp);// 开启线程 t.start();
*/
gkmb = new GuanKaPanel();
getContentPane().add(gkmb);
gkmb.setLayout(null);
Thread t1 = new Thread(gkmb);
t1.start();
this.setTitle("张宇的坦克大战");
this.setJMenuBar(cdt);
this.setSize(1080, 820);
this.setLocation(250, 150);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("xyx")) {
mp = new MyPanel();
this.remove(gkmb);
getContentPane().add(mp);
this.addKeyListener(mp);
Thread t = new Thread(mp);// 开启线程
t.start();
this.setVisible(true);
}
}
}
class MyPanel extends JPanel implements KeyListener, Runnable {
MyTank mt = null;
// 敌方坦克集合类,arr单线程 vec多线程。
Vector<BaoZha> bzjh = new Vector<BaoZha>();// 爆炸类集合。
Vector<DiTank> dtk = new Vector<DiTank>();
int tksl = 18;// 敌方坦克数量
Image tp1 = null;
Image tp2 = null;
Image tp3 = null;
public void tjsj(Graphics g) {
/*
* this.drawTank(330, 660, g, 0, 0); g.setColor(Color.black);
* g.drawString(JiLu.getMtsl()+"" ,360, 680);
*/
this.drawTank(330, 660, g, 0, 1);
g.setColor(Color.black);
g.drawString(JiLu.getDtsl() + "", 360, 680);
g.drawString(JiLu.getSdtj() + "", 980, 300);
this.drawTank(900, 200, g, 0, 1);
g.setColor(Color.black);
JiLu.dqjl();
Font f = new Font("楷体", Font.BOLD, 20);
g.setFont(f);
g.drawString("你消灭的坦克总数", 800, 300);
// g.drawString("消灭坦克999辆有惊喜", 800, 600);
}
public MyPanel() {
mt = new MyTank(330, 530);
for (int i = 0; i < tksl; i++) {
DiTank dt = new DiTank((i) * 45 + 5, 10);
Thread t = new Thread(dt);// 启动敌方坦克线程
t.start();
dtk.add(dt);
dt.setDtk(dtk);// 修改敌方坦克的集合类
/*
* ZiDan zd=new ZiDan(dt.x,dt.y+0, 2); dt.dzd.add(zd);//装子弹 Thread
* dzdfs =new Thread(zd); dzdfs.start();//坦克初始子弹。
*/
// 装敌方坦克
}
tp1 = Toolkit.getDefaultToolkit().getImage(
Panel.class.getResource("/bzxg1.gif"));
tp2 = Toolkit.getDefaultToolkit().getImage(
Panel.class.getResource("/bzxg2.gif"));
tp3 = Toolkit.getDefaultToolkit().getImage(
Panel.class.getResource("/bzxg3.gif"));
Shengyin sy = new Shengyin("./newmagua.wav");
// Shengyin sy=new Shengyin("./tank.wav");
sy.start();
}
public void paint(Graphics g) {// 注意方法名,不要搞混了。
super.paint(g);
this.tjsj(g);
g.fillRect(0, 0, 800, 600);
if (mt.shengming) {
this.drawTank(mt.getX(), mt.getY(), g, mt.getFangxiang(), 0);// 控制方向
// 画我方坦克
}
for (int i = 0; i < dtk.size(); i++) {
DiTank dt = dtk.get(i);
if (dt.shengming) {
this.drawTank(dt.getX(), dt.getY(), g, dt.getFangxiang(), 1);
for (int j = 0; j < dt.dzd.size(); j++) {
ZiDan dtzd = dt.dzd.get(j);
if (dtzd.shengming)// 敌方坦克子弹
{
g.setColor(Color.white);
g.fill3DRect(dtzd.x + 8, dtzd.y, 3, 3, false);
} else {
dt.dzd.remove(dtzd);
}
}
this.repaint();
// 画敌方坦克
}
}
// 子弹
for (int i = 0; i < mt.aa.size(); i++) {
ZiDan zd = mt.aa.get(i);
if (zd != null && zd.shengming == true) {
g.setColor(Color.red);
g.fill3DRect(zd.x, zd.y, 3, 3, false);
}
if (zd.shengming == false) {
mt.aa.remove(zd);
}
}
// 爆炸
for (int i = 0; i < bzjh.size(); i++) {
BaoZha bz = bzjh.get(i);
if (bz.shengcunqi > 6) {
System.out.println("666");
g.drawImage(tp1, bz.x, bz.y, 30, 30, this);
} else if (bz.shengcunqi > 3) {
g.drawImage(tp2, bz.x, bz.y, 30, 30, this);
} else {
g.drawImage(tp3, bz.x, bz.y, 30, 30, this);
}
try {
Thread.sleep(30);
this.repaint();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.repaint();
bz.scqjs();
if (bz.shengcunqi == 0) {
bzjh.remove(bz);
}
}
}
public void jzwf() {
for (int i = 0; i < dtk.size(); i++) {
DiTank dt = dtk.get(i);
for (int j = 0; j < dt.dzd.size(); j++) {
ZiDan zd = dt.dzd.get(j);
this.jzsf(zd, mt);
}
}
}
public void jzdf() {
for (int i = 0; i < mt.aa.size(); i++) {
// 每发子弹和每个坦克
ZiDan zd = mt.aa.get(i);// 装子弹
// System.out.println("1");
if (zd.shengming) {
for (int j = 0; j < dtk.size(); j++) {
DiTank dt = dtk.get(j);// 装坦克
// System.out.println("2");
if (dt.shengming) {
if (this.jzsf(zd, dt)) {
JiLu.dtjs();// 敌方坦克数量减少
JiLu.sdtj();// 记录杀敌数
}
}
}
}
this.repaint();
}
}
public boolean jzsf(ZiDan zd, Tank dt) {
Boolean b = false;
switch (dt.fangxiang) {
case 0:
case 2:
if (zd.x > dt.x && zd.x < dt.x + 20 && zd.y > dt.y
&& zd.y < dt.y + 30) {
zd.shengming = false;
dt.shengming = false;
b = true;
BaoZha bz = new BaoZha(dt.x, dt.y);
bzjh.add(bz);
}
// System.out.println("4");
break;
case 1:
case 3:
if (zd.x > dt.x && zd.x < dt.x + 30 & zd.y > dt.y
&& zd.y < dt.y + 20) {
zd.shengming = false;
dt.shengming = false;
b = true;
BaoZha bz = new BaoZha(dt.x, dt.y);
bzjh.add(bz);
}
// System.out.println("4");
break;
}
return b;
}
public void drawTank(int x, int y, Graphics g, int fangxiang, int leixing) {// 高封装度
// 任意定义敌我坦克
switch (leixing) {
case 0:// 我的坦克
g.setColor(Color.yellow);
break;
case 1:// 敌方的坦克
g.setColor(Color.green);
break;
default:
break;
}
switch (fangxiang) {
case 0:
g.fill3DRect(x, y, 5, 30, false);
g.fill3DRect(x + 15, y, 5, 30, false);
g.fill3DRect(x + 5, y + 5, 10, 20, false);
g.fillOval(x + 5, y + 10, 10, 10);
g.drawLine(x + 10, y + 15, x + 10, y - 3);
break;
case 1:
g.fill3DRect(x, y, 30, 5, false);
g.fill3DRect(x, y + 15, 30, 5, false);
g.fill3DRect(x + 5, y + 5, 20, 10, false);
g.fillOval(x + 10, y + 5, 10, 10);
g.drawLine(x + 15, y + 10, x - 3, y + 10);
break;
case 2:
g.fill3DRect(x, y, 5, 30, false);
g.fill3DRect(x + 15, y, 5, 30, false);
g.fill3DRect(x + 5, y + 5, 10, 20, false);
g.fillOval(x + 5, y + 10, 10, 10);
g.drawLine(x + 10, y + 15, x + 10, y + 33);
break;
case 3:
g.fill3DRect(x, y, 30, 5, false);
g.fill3DRect(x, y + 15, 30, 5, false);
g.fill3DRect(x + 5, y + 5, 20, 10, false);
g.fillOval(x + 10, y + 5, 10, 10);
g.drawLine(x + 15, y + 10, x + 33, y + 10);
break;
}
}
public void keyPressed(KeyEvent e) {// 键盘监听
if (e.getKeyCode() == KeyEvent.VK_W) {
mt.setFangxiang(0);
mt.xiangshang();
}
else if (e.getKeyCode() == KeyEvent.VK_A) {
mt.setFangxiang(1);
mt.xiangzuo();
}
else if (e.getKeyCode() == KeyEvent.VK_S) {
mt.setFangxiang(2);
mt.xiangxia();
}
else if (e.getKeyCode() == KeyEvent.VK_D) {
mt.setFangxiang(3);
mt.xiangyou();
}
if (e.getKeyCode() == KeyEvent.VK_J) {
if (mt.aa.size() < 8) {
mt.fszd();// 启动子弹
}
}
this.repaint();
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void run() {
while (true) {// 节约cpu,内存资源。
try {
Thread.sleep(50);
// System.out.println("麻瓜");
}
catch (InterruptedException e) {
System.out.println("爆炸");
}
// System.out.println("0");
this.jzdf();
this.jzwf();
/*
* 检错:
*
* 1、代码的错误 2、逻辑的错误 3、变量的错误
*/
}
}
}
package t1;
import java.util.Vector;
class Tank
{
int x=0,y=0;//定义坦克的坐标
int fangxiang=2;//定义方向
int sudu=3;//定义速度
boolean shengming=true;
public int getFangxiang() {
return fangxiang;
}
public void setFangxiang(int fangxiang) {
this.fangxiang = fangxiang;
}
public int getSudu() {
return sudu;
}
public void setSudu(int sudu) {
this.sudu = sudu;
}
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 Tank(int x, int y) {
super();
this.x = x;
this.y = y;
}
}
package t1;
class ZiDan implements Runnable
{
int x,y;//定义坐标
int fangxiang;//定义方向
int sudu=5;//行走速度
boolean shengming=true;
public ZiDan(int x, int y,int fangxiang) {
super();
this.x = x;
this.y = y;
this.fangxiang=fangxiang;
}
public void run() {
// TODO Auto-generated method stub
while(true)
{//死循环需要睡眠
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
switch (this.fangxiang) {
case 0:
y-=sudu;
break;
case 1:
x-=sudu;
break;
case 2:
y+=sudu;
break;
case 3:
x+=sudu;
break;
}
if(x<0x>800y<0y>600)
{
shengming=false;
break;
}
}
}
}
点击查看更多内容
8人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦