坦克大战源代码Word文档下载推荐.docx
《坦克大战源代码Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《坦克大战源代码Word文档下载推荐.docx(45页珍藏版)》请在冰点文库上搜索。
退出"
jmi4.setActionCommand("
QUIT"
jmi4.addActionListener(this);
jm.add(jmi1);
jm.add(jmi2);
jm.add(jmi3);
jm.add(jmi4);
jmb.add(jm);
jmb.add(jm1);
//gp=newGamePanel();
//Threadt=newThread(gp);
//t.start();
////注册一下接收键盘的动作
//this.addKeyListener(this);
////设置界面
//this.add(gp);
sp=newStartPanel();
Threadt=newThread(sp);
t.start();
this.add(sp);
this.setJMenuBar(jmb);
this.setBounds(180,20,305,530);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicstaticvoidmain(String[]args){
TankGame1_0tg=newTankGame1_0();
@Override
publicvoidkeyPressed(KeyEventarg0){
if(!
Record.pause){
switch(arg0.getKeyCode()){
caseKeyEvent.VK_UP:
if(!
gp.mtk.isTouchTank()&
&
!
gp.mtk.isTouchRiver())
gp.mtk.moveUp();
gp.mtk.setDirect(0);
break;
caseKeyEvent.VK_DOWN:
gp.mtk.moveDown();
gp.mtk.setDirect
(2);
caseKeyEvent.VK_LEFT:
gp.mtk.moveLeft();
gp.mtk.setDirect(3);
caseKeyEvent.VK_RIGHT:
gp.mtk.moveRight();
gp.mtk.setDirect
(1);
}
if(arg0.getKeyCode()=='
A'
||arg0.getKeyCode()=='
a'
){
gp.mtk.kitEnemy();
gp.repaint();
publicvoidkeyReleased(KeyEventarg0){
//TODOAuto-generatedmethodstub
publicvoidkeyTyped(KeyEventarg0){
publicvoidactionPerformed(ActionEventarg0){
if(arg0.getActionCommand().equals("
)){
this.remove(sp);
gp=newGamePanel();
Threadt=newThread(gp);
t.start();
//注册一下接收键盘的动作
this.addKeyListener(this);
//设置界面
this.add(gp);
this.setVisible(true);
Record.pause=true;
Record.pause=false;
System.exit(0);
}
classGamePanelextendsJPanelimplementsRunnable{
//定义的我的坦克
MyTankmtk;
Riverriver=newRiver(50,300,200,30);
Vector<
SoilWall>
sw=newVector<
();
SoilWalls=null;
SteelWall>
stw=newVector<
SteelWallst=null;
Imageim1=null;
Imageim2=null;
Imageim3=null;
Imageim4=null;
Imageim5=null;
Bomb>
bb=newVector<
//定义敌人的坦克
intdrtssize=4;
EnemyTank>
drts;
EnemyTankdt=null;
GamePanel(){
//初始化爆炸的图片
im1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("
/bomb_1.gif"
));
im2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("
/bomb_2.gif"
im3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("
/bomb_3.gif"
im4=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("
/img2.jpg"
im5=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("
/img1.jpg"
//土墙的初始化
for(inti=5;
i<
24;
i++,i++){
s=newSoilWall(i*10,120);
sw.add(s);
//钢墙的初始化
st=newSteelWall(i*10,200);
stw.add(st);
//我的坦克初始化
mtk=newMyTank(135,350,0,0);
mtk.setStw(stw);
mtk.setSw(sw);
//敌人的坦克初始化
drts=newVector<
for(inti=0;
this.drtssize;
i++){
EnemyTankdt=newEnemyTank((i+1)*50,10,2,1);
EnemyTankdt1=newEnemyTank((i+1)*50,60,2,1);
Threadt=newThread(dt);
Threadt1=newThread(dt1);
t1.start();
dt.setEts(drts);
dt1.setEts(drts);
dt.setR(river);
dt1.setR(river);
dt.setSw(sw);
dt.setStw(stw);
mtk.setEts(drts);
mtk.setR(river);
drts.add(dt);
drts.add(dt1);
publicvoidpaint(Graphicsg){
super.paint(g);
//游戏的操作背景色
g.setColor(Color.BLACK);
//g.fillRect(0,0,300,400);
g.drawImage(im4,0,0,300,400,this);
//让tank接收到panel上的对象
mtk.setEts(drts);
if(river!
=null)
//画出河流
g.drawImage(im5,river.x,river.y,river.x1,river.y1,this);
//画出墙来
g.setColor(Color.yellow);
sw.size();
s=sw.get(i);
if(s.isLive)
g.fill3DRect(s.x,s.y,15,20,true);
else
sw.remove(s);
g.setColor(Color.WHITE);
stw.size();
st=stw.get(i);
if(st.isLive)
g.fill3DRect(st.x,st.y,15,20,true);
stw.remove(st);
//画出炸弹
for(inti=0;
bb.size();
Bombbomb=bb.get(i);
if(bomb.isLive==true){
bomb.times--;
if(bomb.times>
14)
g.drawImage(im1,bomb.x,bomb.y,30,30,this);
elseif(bomb.times>
7)
g.drawImage(im2,bomb.x,bomb.y,30,30,this);
0)
g.drawImage(im3,bomb.x,bomb.y,30,30,this);
if(bomb.times==0)
{bomb.isLive=false;
bb.remove(bomb);
}
//画我的坦克
if(Record.MyLife>
drawTank(mtk.getX(),mtk.getY(),mtk.getDirect(),0,g);
if(mtk.isLive==false)
{Record.MyLife--;
mtk=newMyTank(135,350,0,0);
mtk.setSw(sw);
mtk.setStw(stw);
//画我的坦克的子弹
g.setColor(Color.white);
mtk.bs.size();
Bulletb=mtk.bs.get(i);
if(b!
=null&
b.isLive==true)
g.drawOval(b.getX(),b.getY(),2,2);
if(b.isLive==false)
mtk.bs.remove(b);
//画出敌人的坦克
drts.size();
EnemyTankdt=drts.get(i);
if(dt.isLive==true&
dt!
drawTank(dt.getX(),dt.getY(),dt.getDirect(),dt.getType(),g);
if(dt.isLive==false){
drts.remove(dt);
Record.EnemyLife--;
if(Record.EnemyLife>
7){
dt=newEnemyTank(120,10,2,1);
dt.setSw(sw);
dt.setStw(stw);
Threadt=newThread(dt);
t.start();
drts.add(dt);
}
//画出敌人的坦克的子弹
EnemyTankdt=drts.get(i);
if(dt.isLive==true)
for(intj=0;
j<
dt.bs.size();
j++){
Bulletb=dt.bs.get(j);
if(b.isLive)
g.drawOval(b.getX(),b.getY(),2,2);
if(b.isLive==false)
dt.bs.remove(b);
//显示游戏结束画面
if(Record.EnemyLife<
=0||Record.MyLife<
=0)
{
g.setColor(Color.YELLOW);
g.setFont(newFont("
宋体"
Font.BOLD,20));
g.drawString("
GAMEOVER!
"
100,100);
Record.pause=true;
//显示游戏中的各种提示信息
showInfo(g);
//显示信息的方法
publicvoidshowInfo(Graphicsg){
Colorc=null;
c=g.getColor();
this.drawTank(50,420,0,0,g);
this.drawTank(150,420,0,1,g);
黑体"
g.drawString(Record.MyLife+"
80,435);
g.drawString(Record.EnemyLife+"
185,435);
g.setColor(c);
//用于绘制坦克
publicvoiddrawTank(intx,inty,intdirect,inttype,Graphicsg){
switch(type){
case0:
g.setColor(Color.YELLOW);
case1:
g.setColor(Color.green);
switch(direct){
g.fill3DRect(x,y,5,30,false);
g.draw3DRect(x+5,y+5,10,20,true);
g.fill3DRect(x+15,y,5,30,false);
g.fillOval(x+7,y+8,6,13);
g.drawRect(x+10,y-4,1,17);
case2:
g.drawRect(x+10,y+13,1,17);
g.fill3DRect(x,y,30,5,false);
g.draw3DRect(x+5,y+5,20,10,true);
g.fill3DRect(x,y+15,30,5,false);
g.fillOval(x+9,y+7,13,6);
g.drawRect(x+15,y+9,17,1);
case3:
g.drawRect(x-2,y+9,17,1);
publicvoidkill(Tankt,Bulletb){
switch(t.direct){
if(b.x>
t.x&
b.y>
t.y&
b.x<
t.x+20&
b.y<
t.y+30)
{t.isLive=false;
b.isLive=false;
this.bb.add(newBomb(t.getX(),t.getY()));
t.x+30&
t.y+20)
publicvoidkillWall(Wallw,Bulletb){
if(b.x>
=w.x&
=w.y&
=w.x+15&
=w.y+20)
{
w.life--;
if(w.life<
w.isLive=false;
publicvoidrun(){
while(true){
this.repaint();
try{
Thread.sleep(10);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
if(mtk!
for(inti=0;
Bulletb=mtk.bs.get(i);
if(b.isLive){
for(intj=0;
Tankt=drts.get(j);
if(t.isLive){
this.kill(t,b);
}
}
Tanket=drts.get(i);
if(et.isLive){
et.bs.size();
Bulletb=et.bs.get(j);
if(b.isLive&
b!
=null){
this.kill(mtk,b);
for(