java大战坦克代码_坦克大战java源代码.doc

这个博客介绍了Java实现的坦克大战游戏的核心类,包括坦克类`Tank`、我的坦克类`Hero`和敌人坦克类`EnemyTank`。坦克类包含位置、颜色、速度和方向等属性,以及相关的方法。`Hero`类增加了射击方法和移动方法,而`EnemyTank`类实现了`Runnable`接口,可能涉及到多线程控制。博客中还提到了子弹类`Shot`和碰撞检测的逻辑,但没有提供完整的图片路径处理细节。
摘要由CSDN通过智能技术生成

坦克大战java源代码

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

package com.tankgame;

import java.util.Vector;

//坦克类

class Tank

{

int x=0;

int y=0;

int color=0;

int speed=1;

int direct=0;

boolean isLive=true;

public Tank(int x,int y)

{

this.x=x;

this.y=y;

}

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 int getDirect() {

return direct;

}

public void setDirect(int direct) {

this.direct = direct;

}

public int getColor() {

return color;

}

public void setColor(int color) {

this.color = color;

}

}

//我的坦克

class Hero extends Tank

{

Shot shot=null;

Vector shotm=new Vector();

public Hero(int x,int y)

{

super(x,y);

this.color=5;

}

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

public void shotenemy(int x,int y,int direct)

{

switch(direct)

{

case 0:

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

shotm.add(shot);

break;

case 1:

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

shotm.add(shot);

break;

case 2:

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

shotm.add(shot);

break;

case 3:

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

shotm.add(shot);

break;

}

Thread th=new Thread(shot);

th.start();

}

//调整速度

public void moveup()

{

y-=speed;

}

public void moveright()

{

x+=speed;

}

public void movedown()

{

y+=speed;

}

public void moveleft()

{

x-=speed;

}

}

//敌人的坦克

class EnemyTank extends Tank implements Runnable

{

Vectorensh=new Vector();

Vectorets=new Vector();

public EnemyTank(int x, int y)

{

super(x, y);

this.setColor(2);

this.setDirect(2);

}

//获取MPanel上的敌人坦克

public void setets(Vector vv)

{

this.ets=vv;

}

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

public boolean isTouch()

{

boolean b=false;

EnemyTank et=null;

switch(direct)

{

case 0:

for(int i=0;i

{

et=ets.get(i);

if(et!=this)

{

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

{

if(this.x>=et.x&&thi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值