大柚菠萝和西瓜 2017-06-17 15:57 采纳率: 0%
浏览 931

java坦克大战问题,求大神帮忙看下代码

原来自己已经写好了摁J键发射子弹并运行成功,但是想进行下一步子弹打中敌人后敌人消失时,代码写好了后子弹却发射不了了

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
import javax.swing.*;
public class Mytank1 extends JFrame{
Mypanle mp=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
Mytank1 mytank1=new Mytank1();
}
public Mytank1()
{
mp=new Mypanle();
Thread t=new Thread(mp);
t.start();
this.addKeyListener(mp);
this.add(mp);
this.setTitle("6");
this.setSize(400, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

}
//面板
class Mypanle extends JPanel implements KeyListener,Runnable
{
Hero hero=null;
Vector ets=new Vector();
int badsize=3;
public Mypanle()
{
//创建自己的tanke
hero=new Hero(100,100);
//创建敌人坦克
//int badsize=3;
for(int i=0;i<badsize;i++)
{
Badtank et=new Badtank((i+1)*50, 0);
et.setColor(0);
ets.add(et);
}
}
public void paint(Graphics g)
{
super.paint(g);
g.fillRect(0, 0, 400, 500);
this.drawtank(hero.getX(), hero.getY(), g, hero.direction, 1);
//画出敌人坦克
for(int i=0;i<ets.size();i++)
{
Badtank et=ets.get(i);
if(et.islive)
{
this.drawtank(et.getX(), et.getY(), g, et.direction, 0);
}
}
//画出子弹
for(int i=0;i<hero.ss.size();i++)
{
Shot myshot=hero.ss.get(i);
if(myshot!=null&&myshot.islive==true)
{
g.draw3DRect(myshot.x,myshot.y, 1,1, false);
}

    if(myshot.islive==false)
        {
            hero.ss.remove(myshot);
        }
    }
}
//判断子弹是否打中坦克
/*public void hittank(Shot s,Badtank et)
{
    //判断坦克方向
    switch(et.direction)
    {
    case 0:
    case 3:
    //击中坦克
        if(s.x>et.x&&s.x<et.x+20&&s.y>et.y&&s.y<et.y+30)
        {
        s.islive=false;
        et.islive=false;
        }
    case 1:
    case 2:
        if(s.x>et.x&&s.x<et.x+30&&s.y>et.y&&s.y<et.y+20)
        {
        s.islive=false;
        et.islive=false;
        }

    }
}*/

public void drawtank(int x,int y,Graphics g,int direction,int type)
{
    switch(type)
    {
    case 0:
        g.setColor(Color.YELLOW);
        break;
    case 1:
        g.setColor(Color.CYAN);
        break;
    }
    switch(direction)
    {
    //上
    case 0:
        //g.setColor(Color.CYAN);
        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, 9, 9);
        g.drawLine(x+9,y+10,x+9,y);
        break;
    //右
    case 1:
        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, 9, 9);
        g.drawLine(x+15,y+10,x+30,y+10);
        break;
    //左
    case 2:
        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, 9, 9);
        g.drawLine(x,y+10,x+13,y+10);
        break;
    //下
    case 3:
        //g.setColor(Color.CYAN);
        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, 9, 9);
        g.drawLine(x+9,y+13,x+9,y+30);
        break;


    }
}
@Override
public void keyPressed(KeyEvent e){
    // TODO Auto-generated method stub
    if(e.getKeyCode()==KeyEvent.VK_DOWN)
    {
        hero.moveDown();
        //这两种方法都可;
        //hero.y+=10;
        hero.directd();
    }
    else if(e.getKeyCode()==KeyEvent.VK_UP)
    {
        hero.moveUp();
        hero.directup();
    }
    else if(e.getKeyCode()==KeyEvent.VK_LEFT)
    {
        hero.moveLeft();
        hero.directl();
    }
    else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
    {
        hero.moveRight();
        hero.directR();
    }

    if(e.getKeyCode()==KeyEvent.VK_J)
    {
        if(this.hero.ss.size()<=4)
        {
        this.hero.fireshot();
        }
    }
    repaint();  

}
@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}
@Override
public void run() {
    // TODO Auto-generated method stub
    while(true)
    {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //判断是否被击中
        /*for(int i=0;i<hero.ss.size();i++)
        {
            //取出子弹
            Shot myshot=hero.ss.get(i);
            if(myshot.islive)
            {
                for(int j=0;j<ets.size();i++)
                {
                    //取出敌人坦克
                    Badtank et=ets.get(j);
                    if(et.islive)
                    {
                        this.hittank(myshot, et);
                    }
                }
            }   
        }*/

        this.repaint();
    }   
}

}
//子弹
class Shot implements Runnable
{
int x=0;
int y=0;
int speed=5;
int direct;
boolean islive=true;
public Shot(int x,int y,int direct)
{
this.x=x;
this.y=y;
this.direct=direct;
}
public void run()
{
while(true)
{
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch(direct)
{
case 0:
y-=speed;
break;
case 1:
x+=speed;
break;
case 2:
x-=speed;
break;
case 3:
y+=speed;
break;
}
if(x400||y>500||y<0)
{
islive=false;
break;
}
}
}
}
//敌人坦克
class Badtank extends Tank
{
boolean islive=true;
public Badtank(int x,int y)
{
super(x,y);
}
}
//坦克
class Tank
{
int x=0;
int y=0;
int color;
int direction=3;
int speed=10;
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}

public int getDirect() {
    return direction;
}
public void setDirect(int direct) {
    this.direction = direct;
}

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 Tank(int x,int y)
{
    this.x=x;
    this.y=y;
}
//移动
/*public void moveUp(){  
    y-=speed;  
}  
public void moveDown(){  
    y+=speed;  
}  
public void moveLeft(){  
    x-=speed;  
}  
public void moveRight(){  
    x+=speed;  
} 
//方向
public void directup()
{
    //向上
    this.direction=0;
}
public void directR()
{
    //向右
    this.direction=1;
}
public void directl()
{
    //向左
    this.direction=2;
}
public void directd()
{
    this.direction=3;
}*/

}
//我的坦克
class Hero extends Tank
{
Vector ss=new Vector();
Shot s=null;
public Hero(int x, int y) {
super(x, y);
// TODO Auto-generated constructor stub
}

public void fireshot()
{
    switch(this.direction)
    {
    case 0:
        s=new Shot(x+11,y,0);
        ss.add(s);
        break;
    case 1:
        s=new Shot(x+31,y+11,1);
        ss.add(s);
        break;
    case 2:
        s=new Shot(x,y+11,2);
        ss.add(s);
        break;
    case 3:
        s=new Shot(x+10,y+31,3);
        ss.add(s);
        break;  
    } 
    Thread t=new Thread(s);
    t.start();      
}
//移动
        public void moveUp(){  
            y-=speed;  
        }  
        public void moveDown(){  
            y+=speed;  
        }  
        public void moveLeft(){  
            x-=speed;  
        }  
        public void moveRight(){  
            x+=speed;  
        } 
        //方向
        public void directup()
        {
            //向上
            this.direction=0;
        }
        public void directR()
        {
            //向右
            this.direction=1;
        }
        public void directl()
        {
            //向左
            this.direction=2;
        }
        public void directd()
        {
            this.direction=3;
        }

}

  • 写回答

2条回答 默认 最新

  • devmiao 2017-06-17 16:02
    关注
    评论

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站