hb649596168 2016-09-30 02:16 采纳率: 86.7%
浏览 1139
已采纳

求助。为什么我坦克大战刚按J子弹没看到,敌方坦克就消失,子弹的坐标能显示,但看不到子弹。。。

求助。为什么我坦克大战刚按J子弹没看到,敌方坦克就消失,子弹的坐标能显示,但看不到子弹。。。

package com.test3;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class MyTankGame3 extends JFrame{

    MyPanel mp=null;
    public static void main(String[] args) {
            // TODO Auto-generated method stub
            MyTankGame3 mtg=new MyTankGame3();

    }

    //构造函数
    public MyTankGame3()
    {
        mp=new MyPanel();
        //启动mp线程
        Thread t=new Thread(mp);
        t.start();

        this.add(mp);
        //注册监听
        this.addKeyListener(mp);

        this.setSize(400,300);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

}

//我的面板
class MyPanel extends JPanel implements KeyListener,Runnable
{
    //定义一个我的坦克
    Hero hero=null;

    //定义敌人的坦克组
    Vector<EnemyTank> ets=new Vector<EnemyTank>();
    int enSize=3;


    //构造函数
    public MyPanel()
    {
        //初始化我的坦克
        hero=new Hero(100,100); 
        //初始化敌人的坦克
        for(int i=0;i<enSize;i++)
        {
                //创建坦克
                EnemyTank et=new EnemyTank((i+1)*50, 0);
                et.setDirect(2);//修改方向
                et.setColor(0);//修改颜色
                //加入到Vector中
                ets.add(et);
        }
    }


    //重写paint
    public void paint(Graphics g)
    {
            super.paint(g);
            g.fillRect(0, 0, 400, 300);//画出跟画板一样大的矩形,当做填充背景。默认为黑色

            //调用坦克函数,画出自己的坦克
            this.drawTank(hero.getX(), hero.getY(), g, this.hero.direct, 1);

            //从ss中取出每颗子弹,并画出
            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)
                    {
                            //从ss中删除该死亡的子弹
                            hero.ss.remove(myShot);
                    }
            }

            //画出敌人的坦克
            for(int i=0;i<ets.size();i++)
            {
                    EnemyTank et=ets.get(i);
                    if(et.isLive)
            {
                    this.drawTank(et.getX(), et.getY()+15, g, et.getDirect(), 0);
            }
    }
}


//写一个函数专门判断子弹是否击中敌人坦克
public void hitTank(Shot s,EnemyTank et)
{
    //判断该坦克的方向
    switch(et.direct)
    {
            //如果此时敌人坦克的方向是上后者下
            case 0:
            case 2:
                    if(s.x>(et.x-10)&&s.x<(et.x+10)&&s.y>(et.y-15)&&s.y<(et.y+15));
                    {
                            //击中
                            //子弹死亡
                            s.isLive=false;
                            //敌人坦克死亡
                            et.isLive=false;
                    }
                    case 1:
                    case 3:
                            if(s.x>(et.x-15)&&s.x<(et.x+15)&&s.y>(et.y-10)&&s.y<(et.y+10));
                    {
                            //击中
                            //子弹死亡
                            s.isLive=false;
                            //敌人坦克死亡
                            et.isLive=false;
                        }
        }
}


//画出坦克的函数
public void drawTank(int x,int y,Graphics g,int direct,int type)
//direct为坦克的方向,type为坦克的类型
{
    //判断什么类型的坦克
    switch(type)
    {
    case 0://若为0,则是我的坦克
    g.setColor(Color.cyan);
    break;
    case 1://为1,则是敌方的坦克
    g.setColor(Color.yellow);
    break;
    }

    //判断方向
    switch(direct)
    {
    case 0://向上
            //1.画出左边的填充矩形..用3D矩形使效果更明显
            g.fill3DRect(x-10,y-15,5,30,false);
            //2.画出右边的矩形
            g.fill3DRect(x+5,y-15,5,30,false);
            //3.画出中间矩形
            g.fill3DRect(x-5,y-10,10,20,false);
            //4.画出圆形
            g.fillOval(x-5, y-5, 10, 10);
            //5.画出线
            g.drawLine(x, y, x, y-15);
            break;
    case 1://方向向右
        //1.画出上边的填充矩形..用3D矩形使效果更明显
            g.fill3DRect(x-15,y-10,30,5,false);
            //2.画出下边的矩形
            g.fill3DRect(x-15,y+5,30,5,false);
            //3.画出中间矩形
            g.fill3DRect(x-10,y-5,20,10,false);
            //4.画出圆形
            g.fillOval(x-5, y-5, 10, 10);
            //5.画出线
            g.drawLine(x, y, x+15, y);
            break;
    case 2://向下
            //画出我的坦克
            //1.画出左边的填充矩形..用3D矩形使效果更明显
            g.fill3DRect(x-10,y-15,5,30,false);
            //2.画出右边的矩形
            g.fill3DRect(x+5,y-15,5,30,false);
            //3.画出中间矩形
            g.fill3DRect(x-5,y-10,10,20,false);
            //4.画出圆形
            g.fillOval(x-5, y-5, 10, 10);
            //5.画出线
            g.drawLine(x, y, x, y+15);
            break;
    case 3://方向向左
        //1.画出上边的填充矩形..用3D矩形使效果更明显
            g.fill3DRect(x-15,y-10,30,5,false);
            //2.画出下边的矩形
            g.fill3DRect(x-15,y+5,30,5,false);
            //3.画出中间矩形
            g.fill3DRect(x-10,y-5,20,10,false);
            //4.画出圆形
            g.fillOval(x-5, y-5, 10, 10);
            //5.画出线
            g.drawLine(x, y, x-15, y);
            break;
        }
}


//键按下处理    a表示向左     s表示向下   d表示向右    w表示向上
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_W)
{
//设置我的坦克的方向,向上
this.hero.setDirect(0);
this.hero.moveUp();
}else if(e.getKeyCode()==KeyEvent.VK_D)
{
//向右
this.hero.setDirect(1);
this.hero.moveRight();
}else if(e.getKeyCode()==KeyEvent.VK_S)
{
//向下
this.hero.setDirect(2);
this.hero.moveDown();
}else if(e.getKeyCode()==KeyEvent.VK_A)
{
//向左
this.hero.setDirect(3);
this.hero.moveLeft();
}

//判断玩家是否按下J
if(e.getKeyCode()==KeyEvent.VK_J)
{
//设置子弹可发射的数量
if(this.hero.ss.size()<5)
{
//开火
this.hero.shotEnemy();
}
}

//必须重新绘制Panel
this.repaint();

}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub

}


public void run() {
//每个100毫秒去重绘
while(true)
{
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception
}


//判断是否击中
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();j++)
{
//取出坦克
EnemyTank et=ets.get(j);

if(et.isLive)
{
this.hitTank(myShot, et);
}
}
}
}


this.repaint();
}
}
}











//子弹类
class Shot implements Runnable
{
int x;
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;
}

public void run() {
while(true)
{
try {
Thread.sleep(50);
} catch (Exception e) {

}

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 Tank
{
        //表示坦克的横坐标、纵坐标
        int x=0;
        int y=0;

        //坦克的方向  0表示上   1表示右  2表示下  3表示左
        int direct=0;

        //坦克的颜色
        int color;

        //设置坦克的速度
        int speed=1;

//封装颜色、坐标、方向、速度
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
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;
}
}

//敌人的坦克
class EnemyTank extends Tank
{
        boolean isLive=true;//判断敌人活着
        public EnemyTank(int x,int y)
        {
                super(x,y);
        }
}



//我的坦克的
class Hero extends Tank
{
        //子弹
        Vector<Shot> ss=new Vector<Shot>();
        Shot s=null;//该行只能让子弹发射一次
        public Hero(int x,int y)
        {
                super(x,y);
        }

        //开火(设置子弹的初始位置)
        public void shotEnemy()
        {

        switch(this.direct)
        {
        case 0:
                //创建一颗子弹
                s=new Shot(x,y-15,0);
                //把子弹加入向量
                ss.add(s);
                break;
        case 1:
                s=new Shot(x+15,y,1);
                ss.add(s);
                break;
        case 2:
                s=new Shot(x,y+15,2);
                ss.add(s);
                break;
        case 3:
                s=new Shot(x-15,y,3);
                ss.add(s);
                break;
        }
        //启动子弹线程
        Thread t=new Thread(s);
        t.start();
        }

        //坦克向上移动
        public void moveUp()
        {
                y-=speed;
        }
        //坦克向右移动
        public void moveRight()
        {
                x+=speed;
        }//坦克向下移动
        public void moveDown()
        {
                y+=speed;
        }
        //坦克向左移动
        public void moveLeft()
        {
                x-=speed;
        }
        }



求助~~我查看了好久~就是不知道错在哪

  • 写回答

1条回答 默认 最新

  • ClareQi 2016-09-30 08:15
    关注

    if(s.x>(et.x-10)&&s.x<(et.x+10)&&s.y>(et.y-15)&&s.y<(et.y+15));
    {
    //击中
    //子弹死亡
    s.isLive=false;
    //敌人坦克死亡
    et.isLive=false;
    }
    case 1:
    case 3:
    if(s.x>(et.x-15)&&s.x<(et.x+15)&&s.y>(et.y-10)&&s.y<(et.y+10));
    {
    //击中
    //子弹死亡
    s.isLive=false;
    //敌人坦克死亡
    et.isLive=false;
    }

                                                if判断 怎么有;号??去掉就可以了吧。
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试