我现在进入写打出第一发子弹的阶段了但是很纠结啊···我的子弹始终在枪口,你动,后台输出子弹的坐标也已经在移动了但是就是不动、我的代码如下,球牛人指点啊啊啊啊啊啊//子弹类cl...
我现在进入写打出第一发子弹的阶段了 但是很纠结啊···我的子弹始终在枪口,你动,后台输出子弹的坐标也已经在移动了 但是就是不动、我的代码如下,球牛人指点啊啊啊啊啊啊//子弹类
class Shot implements Runnable{
int x;
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 getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
int y;
int direct;
int speed=1;
//是否还活着
boolean isLive=true;
public Shot(int x,int y,int direct)
{
this.x=x;
this.y=y;
this.direct=direct;
}
@Override
public void run() {
while(true)
{
try{
Thread.sleep(50);
}catch(Exception e){
// TODO Auto-generated method stub
}
while(true)
{
switch(direct)
{
case 0:
//子弹向上
y-=speed;
break;
case 1:
x+=speed;
break;
case 2:
y+=speed;
break;
case 3:
x-=speed;
break;
}
System.out.println("子弹坐标x"+x+"y="+y);
//子弹何时死亡?
//判断该子弹是否碰到边缘
if(x<0||x>400||y<0||y>300)
{
this.isLive=false;
break;
}
}
}
}
}
//我的坦克
class Hero extends Tank
{
//子弹
Shot s=null;
public Hero(int x,int y)
{
super(x,y);
}
//开火
public void shotEnemy()
{
switch(this.direct)
{
case 0:
s=new Shot(x+10,y,0);
break;
case 1:
s=new Shot(x+30,y+10,1);
break;
case 2:
s=new Shot(x+10,y+30,2);
break;
case 3:
s=new Shot(x,y+30,3);
break;
}
//启动子弹
Thread t=new Thread(s);
t.start();
}
展开