java 坦克大战游戏教程完整代码.docx
《java 坦克大战游戏教程完整代码.docx》由会员分享,可在线阅读,更多相关《java 坦克大战游戏教程完整代码.docx(28页珍藏版)》请在冰豆网上搜索。
![java 坦克大战游戏教程完整代码.docx](https://file1.bdocx.com/fileroot1/2023-4/16/b836d59f-3c77-4c96-9350-a6d80b63f354/b836d59f-3c77-4c96-9350-a6d80b63f3541.gif)
java坦克大战游戏教程完整代码
项目:
用java做的坦克大战
效果图:
进入《坦克大战》游戏主界面
点击开始菜单,即可进入游戏:
使用W/A/S/D控制坦克移动,使用J发射子弹
消灭所有坦克游戏获得胜利,被击中后,游戏将会结束:
完整代码见下文,需要准备一个进入坦克大战的音乐声音,放在TankVs.java代码文件的同路径的shengyin文件夹下。
(没有也可以)
代码运行方法:
1,在以及配置好javaJDK的情况下,将下面的完整的代码复制到并粘贴到记事本内,并将后缀.txt改为.java,文件命名为TankVs.java。
2,运行cmd,使用javacTankVs.java命令编译该java文件,编译成功后,使用javaTankVs命令即可运行游戏。
//以下为完整的坦克游戏代码
importjava.awt.*;
importjavax.swing.*;
importjava.awt.event.*;
importjava.util.*;
importjava.io.*;
importjavax.sound.sampled.*;//导入声音类包
publicclassTankVsextendsJFrameimplementsActionListener,Runnable
{
OverPanelop=null;
WinPanelwp=null;
FirstPanelfmp=null;
MyPanelmp=null;
JMenuBarjmb=null;
JMenujm=null;
JMenuItemjmi=null;
publicstaticvoidmain(String[]args)
{
TankVstk1=newTankVs();
}
publicTankVs()
{
fmp=newFirstPanel();
jmb=newJMenuBar();
jm=newJMenu("开始");
jmi=newJMenuItem("开始游戏");
jm.add(jmi);
jmb.add(jm);
this.setJMenuBar(jmb);
this.add(fmp);
jmi.addActionListener(this);//添加单击事件监听
this.setSize(1000,800);
this.setLocation(200,100);
this.setTitle("坦克大战");
//文件的路径自己改一下
this.setIconImage(newImageIcon("D:
\\JAVA\\高级\\坦克大战\\image\\tank.jpg").getImage());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
Threadt=newThread(fmp);
t.start();
}
publicvoidactionPerformed(ActionEvente)
{
if(fmp!
=null){this.remove(fmp);}
if(op!
=null){this.remove(op);}
if(wp!
=null){this.remove(wp);}
mp=newMyPanel();
mp.repaint();
this.addKeyListener(mp);//添加键盘事件监听
this.add(mp);
Threadt=newThread(mp);
t.start();
this.setVisible(true);
Threadt1=newThread(this);
t1.start();
shengyinsy=newshengyin(".\\shengyin\\tank.wav");
sy.start();
}
publicvoidrun()
{
while(true)
{
try
{
Thread.sleep(50);
}
catch(Exceptione){}
if(this.mp.dtk.size()==0)
{
try
{
Thread.sleep(1000);
}
catch(Exceptione){}
this.wp=newWinPanel();
this.add(wp);
this.remove(mp);
this.setVisible(true);
}
if(this.mp.mt.shengming==false)
{
try
{
Thread.sleep(1000);
}
catch(Exceptione){}
this.op=newOverPanel();
this.add(op);
this.remove(mp);
this.setVisible(true);
}
}
}
}
classFirstPanelextendsJPanelimplementsRunnable
{
inttimes=0;
publicvoidpaint(Graphicsg)
{
super.paint(g);
g.fillRect(0,0,800,600);
if(times%2==0)
{
g.setColor(Color.yellow);
FontmyFont=newFont("华文行楷",Font.BOLD,80);
g.setFont(myFont);
g.drawString("坦克大战",230,300);
}
}
publicvoidrun()
{
while(true)
{
try
{
Thread.sleep(500);
}
catch(Exceptione){}
times++;
this.repaint();
}
}
}
classOverPanelextendsJPanel
{
publicvoidpaint(Graphicsg)
{
super.paint(g);
g.fillRect(0,0,800,600);
g.setColor(Color.yellow);
g.setFont(newFont("Cambria",Font.BOLD,80));
g.drawString("GAMEOVER",200,300);
}
}
classWinPanelextendsJPanel
{
publicvoidpaint(Graphicsg)
{
super.paint(g);
g.fillRect(0,0,800,600);
g.setColor(Color.yellow);
g.setFont(newFont("Cambria",Font.BOLD,80));
g.drawString("YOUWIN",200,300);
}
}
classMyPanelextendsJPanelimplementsKeyListener,Runnable
{
WoTankmt=null;
Vectordtk=newVector();//集合类+泛型,Vector适用与多线程,
inttks=12;
publicMyPanel()
{
mt=newWoTank(260,500);
mt.setSudu(8);
for(inti=0;i{
DiTankdt=newDiTank((750/(tks-1))*i,20);
dt.setSudu
(2);
dtk.add(dt);
Threadt=newThread(dt);//启动敌人坦克线程
t.start();
}
}
publicvoidpaint(Graphicsg)
{
super.paint(g);
g.fillRect(0,0,800,600);
if(mt.shengming==true)
{
this.drawTank(mt.getX(),mt.getY(),g,mt.getFangxiang(),0);//绘制我方坦克
}
for(inti=0;i{
DiTankdt=dtk.get(i);
this.drawTank(dt.getX(),dt.getY(),g,dt.getFangxiang(),1);
if(dt.zd.shengming==true)
{
g.setColor(Color.white);
g.fill3DRect(dt.zd.x,dt.zd.y,3,3,false);//绘制敌方坦克
}
}
for(inti=0;i{
Zidanzd=mt.aa.get(i);
if(zd.shengming==true&&mt.shengming==true)
{
g.setColor(Color.red);
g.fill3DRect(zd.x,zd.y,3,3,false);
}
if(zd.shengming==false)
{
mt.aa.remove(zd);
}
}
}
publicvoiddrawTank(intx,inty,Graphicsg,intfangxiang,intleixing)
{
switch(leixing)
{
case0:
//我的坦克
g.setColor(Color.yellow);
break;
case1:
//敌人的坦克
g.setColor(Color.green);
break;
}
switch(fangxiang)
{
case0:
//上方向
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;
case1:
//左方向
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;
case2:
//下方向
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;
case3:
//右方向
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;
}
}
publicvoidkeyTyped(KeyEvente){}
publicvoidkeyReleased(KeyEvente){}
publicvoidkeyPressed(KeyEvente)
{
if(mt.shengming==true)
{
if(e.getKeyCode()==KeyEvent.VK_W)
{
this.mt.setFangxiang(0);
this.mt.xiangshang();
}
elseif(e.getKeyCode()==KeyEvent.VK_A)
{
this.mt.setFangxiang
(1);
this.mt.xiangzuo();
}
elseif(e.getKeyCode()==KeyEvent.VK_S)
{
this.mt.setFangxiang
(2);
this.mt.xiangxia();
}
elseif(e.getKeyCode()==KeyEvent.VK_D)
{
this.mt.setFangxiang(3);
this.mt.xiangyou();
}
if(e.getKeyCode()==KeyEvent.VK_J)
{
if(mt.aa.size()<8)//允许我的坦克同时存在8颗子弹
{
this.mt.fszd();
Threadt=newThread(this);//启动画板线程
t.start();
}
}
}
this.repaint();
}
publicvoidrun()
{
while(mt.zd.shengming==true||dtk.size()>0)
{
try
{
Thread.sleep(50);
}
catch(Exceptione){}
for(inti=0;i{
Zidanzd=mt.aa.get(i);
for(intj=0;j{
DiTankdt=dtk.get(j);
this.jzdf(zd,dt);
}
}
for(inti=0;i{
DiTankdt=dtk.get(i);
this.jzwf(dt.zd,mt);
}
this.repaint();
//System.out.println("线程1");
}
}
publicvoidjzdf(Zidanzd,DiTankdt)//击中敌方
{
switch(dt.fangxiang)
{
case0:
case2:
if(zd.x>dt.getX()&&zd.x
dt.getY()&&zd.y{
zd.shengming=false;
dt.shengming=false;
mt.aa.remove(zd);
dtk.remove(dt);
}
break;
case1:
case3:
if(zd.x>dt.getX()&&zd.x
dt.getY()&&zd.y{
zd.shengming=false;
dt.shengming=false;
mt.aa.remove(zd);
dtk.remove(dt);
}
break;
}
}
publicvoidjzwf(Zidanzd,WoTankmt)//击中我方
{
if(mt.shengming==true)
{
switch(mt.fangxiang)
{
case0:
case2:
if(zd.x>mt.getX()&&zd.xmt.getY()&&zd.y{
zd.shengming=false;
mt.shengming=false;
}
break;
case1:
case3:
if(zd.x>mt.getX()&&zd.xmt.getY()&&zd.y{
zd.shengming=false;
mt.shengming=false;
}
break;
}
}
}
}
classTank
{
privateintx=0,y=0;
intfangxiang=0;
intsudu=1;
booleanshengming=true;
publicintgetSudu()
{
returnsudu;
}
publicvoidsetSudu(intsudu)
{
this.sudu=sudu;
}
publicintgetFangxiang()
{
returnfangxiang;
}
publicvoidsetFangxiang(intfangxiang)
{
this.fangxiang=fangxiang;
}
publicintgetX()
{
returnx;
}
publicintgetY()
{
returny;
}
publicvoidsetX(intx)
{
this.x=x;
}
publicvoidsetY(inty)
{
this.y=y;
}
publicTank(intx,inty)
{
this.x=x;
this.y=y;
}
}
classWoTankextendsTank
{
Zidanzd=newZidan();
Vectoraa=newVector();
publicWoTank(intx,inty)
{
super(x,y);
}
publicvoidxiangshang()
{
if(getY()-sudu>0)
{
this.setY(this.getY()-sudu);
}
}
publicvoidxiangzuo()
{
if(getX()-sudu>0)
{
this.setX(this.getX()-sudu);
}
}
publicvoidxiangxia()
{
if(getY()+sudu<520)
{
this.setY(this.getY()+sudu);
}
}
publicvoidxiangyou()
{
if(getX()+sudu<750)
{
this.setX(this.getX()+sudu);
}
}
publicvoidfszd()
{
switch(this.fangxiang)
{
case0:
zd=newZidan(super.getX()+10,super.getY(),super.getFangxiang(),true);
break;
case1:
zd=newZidan(super.getX(),super.getY()+10,super.getFangxiang(),true);
break;
case2:
zd=newZidan(super.getX()+10,super.getY()+30,super.getFangxiang(),true);
break;
case3:
zd=newZidan(super.getX()+30,super.getY()+10,super.getFangxiang(),true);
break;
}
aa.add(zd);
Threadt=newThread(zd);//启动子弹线程
t.start();
}
}
classZidanimplementsRunnable
{
intx;
inty;
intfangxiang;
intsudu=8;
publicbooleanshengming=false;
publicZidan()
{
}
publicZidan(intx,inty,intfangxiang,booleanshengming)
{
this.x=x;
this.y=y;
this.fangxiang=fangxiang;
this.shengming=shengming;
}
publicvoidrun()
{
while(true)
{