坦克大战java源代码精编版.docx

上传人:b****9 文档编号:26388170 上传时间:2023-06-18 格式:DOCX 页数:35 大小:20.05KB
下载 相关 举报
坦克大战java源代码精编版.docx_第1页
第1页 / 共35页
坦克大战java源代码精编版.docx_第2页
第2页 / 共35页
坦克大战java源代码精编版.docx_第3页
第3页 / 共35页
坦克大战java源代码精编版.docx_第4页
第4页 / 共35页
坦克大战java源代码精编版.docx_第5页
第5页 / 共35页
点击查看更多>>
下载资源
资源描述

坦克大战java源代码精编版.docx

《坦克大战java源代码精编版.docx》由会员分享,可在线阅读,更多相关《坦克大战java源代码精编版.docx(35页珍藏版)》请在冰豆网上搜索。

坦克大战java源代码精编版.docx

坦克大战java源代码精编版

有些图片路径会出错要注意

packagecom.tankgame;

importjava.util.Vector;

//坦克类

classTank

{

intx=0;

inty=0;

intcolor=0;

intspeed=1;

intdirect=0;

booleanisLive=true;

publicTank(intx,inty)

{

this.x=x;

this.y=y;

}

publicintgetX(){

returnx;

}

publicvoidsetX(intx){

this.x=x;

}

publicintgetY(){

returny;

}

publicvoidsetY(inty){

this.y=y;

}

publicintgetDirect(){

returndirect;

}

publicvoidsetDirect(intdirect){

this.direct=direct;

}

publicintgetColor(){

returncolor;

}

publicvoidsetColor(intcolor){

this.color=color;

}

}

//我的坦克

classHeroextendsTank

{

Shotshot=null;

Vectorshotm=newVector();

publicHero(intx,inty)

{

super(x,y);

this.color=5;

}

//坦克具有一个打击敌人的方法

publicvoidshotenemy(intx,inty,intdirect)

{

switch(direct)

{

case0:

shot=newShot(this.x+10,this.y,0);

shotm.add(shot);

break;

case1:

shot=newShot(this.x+30,this.y+10,1);

shotm.add(shot);

break;

case2:

shot=newShot(this.x+10,this.y+30,2);

shotm.add(shot);

break;

case3:

shot=newShot(this.x,this.y+10,3);

shotm.add(shot);

break;

}

Threadth=newThread(shot);

th.start();

}

//调整速度

publicvoidmoveup()

{

y-=speed;

}

publicvoidmoveright()

{

x+=speed;

}

publicvoidmovedown()

{

y+=speed;

}

publicvoidmoveleft()

{

x-=speed;

}

}

//敌人的坦克

classEnemyTankextendsTankimplementsRunnable

{

Vectorensh=newVector();

Vectorets=newVector();

publicEnemyTank(intx,inty)

{

super(x,y);

this.setColor

(2);

this.setDirect

(2);

}

//获取MPanel上的敌人坦克

publicvoidsetets(Vectorvv)

{

this.ets=vv;

}

//判断敌人的坦克是否碰撞

publicbooleanisTouch()

{

booleanb=false;

EnemyTanket=null;

switch(direct)

{

case0:

for(inti=0;i

{

et=ets.get(i);

if(et!

=this)

{

if(et.direct==0||et.direct==2)

{

if(this.x>=et.x&&this.x<=et.x+20&&this.y<=et.y+30&&this.y>et.y)

{

returntrue;

}

if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y<=et.y+30&&this.y>et.y)

{

returntrue;

}

}

if(et.direct==1||et.direct==3)

{

if(this.x>=et.x&&this.x<=et.x+30&&this.y<=et.y+20&&this.y>et.y)

{

returntrue;

}

if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y<=et.y+20&&this.y>et.y)

{

returntrue;

}

}

}

}

break;

case1:

for(inti=0;i

{

et=ets.get(i);

if(et!

=this)

{

if(et.direct==0||et.direct==2)

{

if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y<=et.y+30&&this.y>et.y)

{

returntrue;

}

if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20<=et.y+30&&this.y+20>et.y)

{

returntrue;

}

}

if(et.direct==1||et.direct==3)

{

if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y<=et.y+20&&this.y>et.y)

{

returntrue;

}

if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20<=et.y+20&&this.y+20>et.y)

{

returntrue;

}

}

}

}

break;

case2:

for(inti=0;i

{

et=ets.get(i);

if(et!

=this)

{

if(et.direct==0||et.direct==2)

{

if(this.x>=et.x&&this.x<=et.x+20&&this.y+30<=et.y+30&&this.y+30>et.y)

{

returntrue;

}

if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y+30<=et.y+30&&this.y+30>et.y)

{

returntrue;

}

}

if(et.direct==1||et.direct==3)

{

if(this.x>=et.x&&this.x<=et.x+30&&this.y+30<=et.y+20&&this.y+30>et.y)

{

returntrue;

}

if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y+30<=et.y+20&&this.y+30>et.y)

{

returntrue;

}

}

}

}

break;

case3:

for(inti=0;i

{

et=ets.get(i);

if(et!

=this)

{

if(et.direct==0||et.direct==2)

{

if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y<=et.y+30&&this.y>et.y)

{

returntrue;

}

if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20<=et.y+30&&this.y+20>et.y)

{

returntrue;

}

}

if(et.direct==1||et.direct==3)

{

if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y<=et.y+20&&this.y>et.y)

{

returntrue;

}

if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20<=et.y+20&&this.y+20>et.y)

{

returntrue;

}

}

}

}

break;

}

returnb;

}

publicvoidrun(){

while(true)

{

switch(this.direct)

{

case0:

for(inti=0;i<30;i++)

{

if(y>0&&this.isTouch()==false)y-=this.speed;

try{

Thread.sleep(50);

}catch(InterruptedExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

break;

case1:

for(inti=0;i<30;i++)

{

if(x<365&&this.isTouch()==false)x+=this.speed;

try{

Thread.sleep(50);

}catch(InterruptedExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

break;

case2:

for(inti=0;i<30;i++)

{

if(y<270&&this.isTouch()==false)y+=this.speed;

try{

Thread.sleep(50);

}catch(InterruptedExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

break;

case3:

for(inti=0;i<30;i++)

{

if(x>0&&this.isTouch()==false)x-=this.speed;

try{

Thread.sleep(50);

}catch(InterruptedExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

break;

}

this.direct=(int)(Math.random()*4);

if(this.isLive==false)

{

break;

}

if(ensh.size()<5)

{

Shotes=null;

switch(this.direct)

{

case0:

es=newShot(this.getX()+10,this.getY(),0);

ensh.add(es);

break;

case1:

es=newShot(this.getX()+30,this.getY()+10,1);

ensh.add(es);

break;

case2:

es=newShot(this.getX()+10,this.getY()+30,2);

ensh.add(es);

break;

case3:

es=newShot(this.getX(),this.getY()+10,3);

ensh.add(es);

break;

}

Threadth=newThread(es);

th.start();

}

}

}

}

//炸弹类

classBomb

{

intx;

inty;

intlift=9;

booleanisLive=true;

publicBomb(intx,inty)

{

this.x=x;

this.y=y;

}

//炸弹的生命值

publicvoidliftdown()

{

if(lift>0)

{

lift--;

}else

{

isLive=false;

}

}

}

//子弹类

classShotimplementsRunnable

{

intshotX;

intshotY;

intdirect;

intshotspeed=1;

booleanisLive=true;

publicShot(intx,inty,intdirect)

{

this.shotX=x;

this.shotY=y;

this.direct=direct;

}

publicintgetShotX(){

returnshotX;

}

publicvoidsetShotX(intshotX){

this.shotX=shotX;

}

publicintgetShotY(){

returnshotY;

}

publicvoidsetShotY(intshotY){

this.shotY=shotY;

}

publicintgetShotspeed(){

returnshotspeed;

}

publicvoidsetShotspeed(intshotspeed){

this.shotspeed=shotspeed;

}

publicvoidrun(){

while(true)

{

try{

Thread.sleep(50);

}catch(InterruptedExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

switch(direct)

{

case0:

shotY-=shotspeed;

break;

case1:

shotX+=shotspeed;

break;

case2:

shotY+=shotspeed;

break;

case3:

shotX-=shotspeed;

break;

}

if(shotX<0||shotX>400||shotY<0||shotY>300)

{

isLive=false;

break;

}

}

}

}

/**

*功能:

坦克大战4.0

*/

packagecom.tankgame4;

importjava.awt.*;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.KeyEvent;

importjava.awt.event.KeyListener;

importjava.util.Vector;

importjava.io.*;

importjavax.swing.*;

importjavax.imageio.*;

publicclassMyTankGame4extendsJFrameimplementsActionListener{

MyPanelmp=null;

MyStartPanelmsp=null;

//菜单定义

JMenuBarjmb=null;

JMenujm1=null;

JMenuItemjmi1=null;

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

MyTankGame4mtg=newMyTankGame4();

}

//构造函数

publicMyTankGame4()

{

//创建菜单

jmb=newJMenuBar();

jm1=newJMenu("Game(G)");

jm1.setMnemonic('G');

jmi1=newJMenuItem("NewGame(N)");

jmi1.setMnemonic('N');

jmi1.addActionListener(this);

jmi1.setActionCommand("NewGame");

jm1.add(jmi1);

jmb.add(jm1);

this.setJMenuBar(jmb);

msp=newMyStartPanel();

Threadst=newThread(msp);

st.start();

this.add(msp);

this.setTitle("坦克大战");

this.setSize(400,300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

publicvoidactionPerformed(ActionEventarg0){

if(arg0.getActionCommand().equals("NewGame"))

{

this.remove(msp);

mp=newMyPanel();

Threadmt=newThread(mp);

mt.start();

this.add(mp);

this.addKeyListener(mp);

this.setVisible(true);

}

}

}

//游戏开始面板

classMyStartPanelextendsJPanelimplementsRunnable

{

inttimes=0;

publicvoidpaint(Graphicsg)

{

super.paint(g);

g.fillRect(0,0,400,300);

if(times%2==0)

{

Fontmyfont=newFont("行楷",Font.BOLD,30);

g.setFont(myfont);

g.setColor(Color.yellow);

g.drawString("第一关",130,130);

}

this.repaint();

}

publicvoidrun(){

while(true)

{

try{

Thread.sleep(300);

times++;

}catch(Interru

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 自我管理与提升

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1