| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1109 人关注过本帖
标题:初步编写的Java坦克大战代码,未实现敌人坦克的发射和爆炸效果,求解答?
取消只看楼主 加入收藏
萧阳
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-4-21
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
初步编写的Java坦克大战代码,未实现敌人坦克的发射和爆炸效果,求解答?
代码如下:
package t1;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;



public class Tank2 extends JFrame {
MyPanel mp=null;
public static void main(String[] args)
{
Tank2 t2=new Tank2();
}
public Tank2()
{
mp=new MyPanel();
this.add(mp);
this.addKeyListener(mp);
Thread t=new Thread(mp);
t.start();
this.setSize(400,300);
this.setLocation(300,280);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}



}
class MyPanel extends JPanel implements KeyListener,Runnable
{
MyTank mt=null;
Vector<DiTank>dtk=new Vector<DiTank>();//集合类多线程
Vector<Baozha>bzjh=new Vector<Baozha>();

int tksl=3;


Image tp1=null;
Image tp2=null;
Image tp3=null;

public MyPanel()
{
mt=new MyTank(140,232);
for(int i=0;i<tksl;i++)
{
DiTank dt=new DiTank((i)*181+2,0);
dt.setDir(2);

Thread t2=new Thread(dt);
t2.start();
Zidan zd=new Zidan(dt.x+10,dt.y+30,2);
dt.dzd.add(zd);
Thread t3=new Thread(zd);
t3.start();
dtk.add(dt);

}

tp1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("F:/java/eclipse/workspace/Student/src/bzxg1.gif"));
tp2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("F:/java/eclipse/workspace/Student/src/bzxg2.gif"));
tp3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("F:/java/eclipse/workspac/Student/src/bzxg3.gif"));

}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0,0,400,300);//绘制画布
this.drawTank(mt.getX(),mt.getY(),g,this.mt.dir,0);

for(int i=0;i<dtk.size();i++)
{
DiTank dt=dtk.get(i);
if(dt.shengming==true){

this.drawTank(dt.getX(),dt.getY(),g,dt.dir,1);
}

}
for(int i=0;i<mt.aa.size();i++)
{
Zidan zd=mt.aa.get(i);


if(mt.zd!=null&&mt.zd.shengming==true)
{
g.setColor(Color.white);
g.fill3DRect(mt.zd.x,mt.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)
{
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);

}
bz.scq();
if(bz.shengcunqi==0)
{
bzjh.remove(bz);
}
}



}
public void jzdf(Zidan zd,DiTank dt)
{
switch(dt.dir)
{
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;
}
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;
}
break;
}
}
public void drawTank(int x,int y,Graphics g,int dir,int leixing)
{
switch(leixing)
{
case 0://我的坦克
g.setColor(Color.red);
break;
case 1://敌人的坦克
g.setColor(Color.BLUE);
break;
}


switch(dir)
{
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;
default:
break;

}
this.repaint();
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_W)
{
this.mt.setDir(0);
this.mt.W();

}else if(e.getKeyCode()==KeyEvent.VK_A)
{
this.mt.setDir(1);
this.mt.A();
}else if(e.getKeyCode()==KeyEvent.VK_S)
{
this.mt.setDir(2);
this.mt.S();
}else if(e.getKeyCode()==KeyEvent.VK_D)
{
this.mt.setDir(3);
this.mt.D();
}
if(e.getKeyCode()==KeyEvent.VK_J)
{
if(this.mt.aa.size()<8)
{
this.mt.fszd();
}

}
this.repaint();


}
public void run()
{
while(true)
{
try{
Thread.sleep(100);
}
catch(Exception e){}
for(int i=0;i<mt.aa.size();i++)//这里用二层循环的目的是,让每发子弹去和每个敌人坦克比较
{
Zidan zd=mt.aa.get(i);
if(zd.shengming==true)
{
for(int j=0;j<dtk.size();j++)
{
DiTank dt=dtk.get(j);
if(dt.shengming==true)
{
this.jzdf(zd,dt);
}
}
}
}
this.repaint();
}

}
 


}
package t1;

import java.util.Vector;



class Tank
{
int x=0,y=0;
int sudu=2;
    int dir;
    boolean shengming=true;
int getDir() {
return dir;
}

public void setDir(int dir) {
this.dir = dir;
}


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)
{
this.x=x;
this.y=y;
}



}
class DiTank extends Tank implements Runnable
{
int dir=2;
int time=0;


Vector<Zidan> dzd=new Vector<Zidan>();

public DiTank(int x,int y)
{
super(x,y);

}
public void run()
{
while(true)
{

switch(this.dir)
{
case 0:
for(int i=0;i<30;i++)
{
if(y>0)
y-=sudu;
try{
Thread.sleep(50);
}catch(Exception e){}
}

break;
case 1:
for(int i=0;i<30;i++)
{
if(x>0)
x-=sudu;
try{
Thread.sleep(50);
}catch(Exception e){}
}

break;
case 2:
for(int i=0;i<30;i++)
{
if(y<230)
y+=sudu;
try{
Thread.sleep(50);
}catch(Exception e){}
}

break;
case 3:
for(int i=0;i<30;i++)
{
if(x<350)
x+=sudu;
try{
Thread.sleep(50);
}catch(Exception e){}
}

break;

}

this.dir=(int)(Math.random()*4);//随机函数:随机出现0到1之间的数字
if(this.shengming==false)
{
break;
}
this.time++;
if(time%2==0)
{
if(shengming)
{
if(dzd.size()<5)
{
Zidan zd=null;
switch(dir)
{
case 0:
zd=new Zidan(x+10,y,0);
dzd.add(zd);
break;
case 1:
zd=new Zidan(x,y+10,1);
dzd.add(zd);
break;
case 2:
zd=new Zidan(x+10,y+30,2);
dzd.add(zd);
break;
case 3:
zd=new Zidan(x+30,y+10,3);
dzd.add(zd);
break;
}
Thread t5=new Thread(zd);
t5.start();
}

}
}
}


}

}
class MyTank extends Tank//因为子弹是跟着坦克走的,所以子弹的功能大多都和坦克类有关
{
Vector<Zidan>aa=new Vector<Zidan>();
Zidan zd=null;
public MyTank(int x,int y)
{
super(x,y);
}
public void fszd()
{
switch(this.dir)
{
case 0:
zd=new Zidan(x+9,y-10,0);
aa.add(zd);
break;
case 1:
zd=new Zidan(x-10,y+9,1);
aa.add(zd);
break;
case 2:
zd=new Zidan(x+9,y+30,2);
aa.add(zd);
break;
case 3:
zd=new Zidan(x+30,y+9,3);
aa.add(zd);
break;

}
Thread t2=new Thread(zd);
t2.start();
}
public void W()
{
y-=sudu;
}
public void A()
{

x-=sudu;
}
public void S()
{
y+=sudu;

}
public void D()
{
x+=sudu;
}
}
class Zidan implements Runnable
{
int x;
int y;
int dir;
int sudu=5;
boolean shengming=true;

public Zidan(int x,int y,int dir)
{
this.x=x;
this.y=y;
this.dir=dir;
}
public void run()
{
while(true)//凡是线程下面的死循环里都要有个睡眠
{
try{
Thread.sleep(50);
}catch(Exception e){}
switch(dir)
{
case 0:
y-=sudu;
break;
case 1:
x-=sudu;
break;
case 2:
y+=sudu;
break;
case 3:
x+=sudu;
break;
//default:
// break;
}
if(x<0||x>400||y<0||y>300)
{
this.shengming=false;
break;
}
}
}
}
class Baozha
{
int x,y;
int shengcunqi=9;
boolean shengming=true;
public Baozha(int x,int y)
{
this.x=x;
this.y=y;
}
public void scq()
{
if(shengcunqi>0)
{
shengcunqi--;
}
else{
this.shengming=false;
}
}
}
搜索更多相关主题的帖子: package 坦克大战 public import null 
2015-04-21 22:44
萧阳
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-4-21
收藏
得分:0 
受教了,谢谢!我会注意的......
2015-04-22 11:22
快速回复:初步编写的Java坦克大战代码,未实现敌人坦克的发射和爆炸效果,求解答 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030326 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved