| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 537 人关注过本帖
标题:能帮我看看这个子弹的坐标哪里错了吗
只看楼主 加入收藏
hsjjgm
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:106
专家分:189
注 册:2013-4-27
结帖率:88.89%
收藏
已结贴  问题点数:40 回复次数:3 
能帮我看看这个子弹的坐标哪里错了吗
我的这个坦克哪里错了啊
运行坦克可以正常移动,但是子弹的坐标老是错误,子弹也飞不出去,到底是哪里错了啊,检查很久了
package unfinished;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TryTank
{
    MyPanel my;
    JFrame jf;
    public static void main(String[] args)
    {
        TryTank tr = new TryTank();
    }
    public TryTank()
    {
        jf = new JFrame("我的坦克大战");
        my = new MyPanel();
        jf.addKeyListener(my);
        Thread t2 = new Thread(my);
        t2.start();
        my.setBackground(Color.black);
        jf.add(my);
        jf.setVisible(true);
        jf.setSize(400,300);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

class MyPanel extends JPanel implements KeyListener,Runnable
{
    Mine t;
    public MyPanel()
    {
        t = new Mine(10,10);
    }
    public void paint(Graphics g)
    {
        super.paint(g);
        t.setType(2);
        t.setSpeed(4);
        this.paintTank(t.getX(), t.getY(), g, t.getDirect(), t.getType());
        if(t.b != null&&t.b.isAlive == true)
        {
            t.b.setSpeed(2);
            g.fillOval(t.b.getX(), t.b.getY(), 2, 2);
        }
    }
   
    //画坦克
    public void paintTank(int x, int y, Graphics g, int direct, int type)
    {
        //判断颜色,1是敌人,黄色。2是自己,红色
        if(type == 1)
        {
            g.setColor(Color.YELLOW);
            switch(direct)
            {
                case 1:
                    g.fill3DRect(x, y,5,30,false);
                    g.fill3DRect(x+5,y+5,10,20,false);
                    g.fill3DRect(x+15,y,5,30,false);
                    g.fillOval(x+4,y+10,10,10);
                    g.drawLine(x+9,y+15,x+9,y-4);
                    break;
                case 2:
                    g.fill3DRect(x, y,30,5,false);
                    g.fill3DRect(x+5,y+5,20,10,false);
                    g.fill3DRect(x,y+15,30,5,false);
                    g.fillOval(x+10,y+5,10,10);
                    g.drawLine(x+15,y+10,x+34,y+10);
                    break;
                case 3:
                    g.fill3DRect(x, y,5,30,false);
                    g.fill3DRect(x+5,y+5,10,20,false);
                    g.fill3DRect(x+15,y,5,30,false);
                    g.fillOval(x+4,y+10,10,10);
                    g.drawLine(x+9,y+15,x+9,y+34);
                    break;   
                case 4:
                    g.fill3DRect(x, y,30,5,false);
                    g.fill3DRect(x+5,y+5,20,10,false);
                    g.fill3DRect(x,y+15,30,5,false);
                    g.fillOval(x+10,y+5,10,10);
                    g.drawLine(x+15,y+10,x-4,y+10);        
            }
        }
        else if(type == 2)
        {
            g.setColor(Color.RED);
            switch(direct)
            {
                case 1:
                    g.fill3DRect(x, y,5,30,false);
                    g.fill3DRect(x+5,y+5,10,20,false);
                    g.fill3DRect(x+15,y,5,30,false);
                    g.fillOval(x+4,y+10,10,10);
                    g.drawLine(x+9,y+15,x+9,y-4);
                    break;
                case 2:
                    g.fill3DRect(x, y,30,5,false);
                    g.fill3DRect(x+5,y+5,20,10,false);
                    g.fill3DRect(x,y+15,30,5,false);
                    g.fillOval(x+10,y+5,10,10);
                    g.drawLine(x+15,y+10,x+34,y+10);
                    break;
                case 3:
                    g.fill3DRect(x, y,5,30,false);
                    g.fill3DRect(x+5,y+5,10,20,false);
                    g.fill3DRect(x+15,y,5,30,false);
                    g.fillOval(x+4,y+10,10,10);
                    g.drawLine(x+9,y+15,x+9,y+34);
                    break;   
                case 4:
                    g.fill3DRect(x, y,30,5,false);
                    g.fill3DRect(x+5,y+5,20,10,false);
                    g.fill3DRect(x,y+15,30,5,false);
                    g.fillOval(x+10,y+5,10,10);
                    g.drawLine(x+15,y+10,x-4,y+10);        
            }
        }
    }
   
    @Override
    public void keyTyped(KeyEvent e)
    {
    }
    @Override
    public void keyPressed(KeyEvent e)
    {
        //设置WASD为移动
        if(e.getKeyCode() == KeyEvent.VK_W)
        {
            t.setDirect(1);
            t.moveUp();
        }
        else if(e.getKeyCode() == KeyEvent.VK_D)
        {
            t.setDirect(2);
            t.moveRight();
        }
        else if(e.getKeyCode() == KeyEvent.VK_S)
        {
            t.setDirect(3);
            t.moveDown();
        }
        else if(e.getKeyCode() == KeyEvent.VK_A)
        {
            t.setDirect(4);
            t.moveLeft();
        }
        if(e.getKeyCode() == KeyEvent.VK_J)
        {
            t.fired();
        }
        this.repaint();
    }
    @Override
    public void keyReleased(KeyEvent e)
    {
    }
    @Override
    public void run()
    {
        try
        {
            Thread.sleep(100);
        } catch (InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
   
    this.repaint();
}

class Tank
{
    int x = 10, y = 10;
   
    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 Mine extends Tank
{
    int type = 0,direct = 1,speed =1;
    Bullet b;
    public Mine(int x, int y)
    {
        super(x,y);
    }
   
    public int getType()
    {
        return type;
    }

    public void setType(int type)
    {
        this.type = type;
    }

    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;
    }
   
    //坦克移动
    public void moveUp()
    {
        y-=this.getSpeed();
    }
   
    public void moveRight()
    {
        x+=this.getSpeed();
    }
   
    public void moveDown()
    {
        y+=this.getSpeed();
    }
   
    public void moveLeft()
    {
        x-=this.getSpeed();
    }
   
    //开火
    public void fired()
    {
        switch(this.direct)
        {
            case 1:
                b = new Bullet(x+10,y,1);
            case 2:
                b = new Bullet(x+30,y+10,2);
            case 3:
                b = new Bullet(x+10,y+30,3);
            case 4:
                b = new Bullet(x,y+10,4);
        }
        Thread t1 = new Thread(b);
        t1.start();
    }
}

class Bullet implements Runnable
{
    int x = 10, y = 10,speed = 1,direct = 1;
    boolean isAlive = true;
   
    public Bullet(int x, int y,int direct)
    {
        this.x = x;
        this.y = y;
        this.direct = 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 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 void run()
    {
        while(true)
        {
            try
            {
                Thread.sleep(50);
            } catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            switch(this.direct)
            {
                case 1:
                    y-=this.getSpeed();
                    break;
                case 2:
                    x+=this.getSpeed();
                    break;
                case 3:
                    y+=this.getSpeed();
                    break;
                case 4:
                    x-=this.getSpeed();
                    break;
            }
            System.out.println("["+x+","+y+"]");
            
//            if(x<0||x>400||y<0||y>300);
//            {
//                isAlive = false;
//                break;
//            }
        }
    }
}
}

搜索更多相关主题的帖子: import public package 
2013-05-27 14:47
Kingbox_tang
Rank: 7Rank: 7Rank: 7
来 自:天津师范大学
等 级:黑侠
威 望:3
帖 子:146
专家分:677
注 册:2012-11-27
收藏
得分:5 
同学,我没明白你的意思,是发子弹就是有位置输出 么?
还是什么?你这个runnable的实现不对吧,还有,direct那个貌似调用不到啊。

旨在提高编程水平,学有所用,学有所成,学有所为。
2013-05-27 18:28
ren829
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:15
帖 子:255
专家分:1174
注 册:2006-3-11
收藏
得分:35 
错误不是很多,多注意一下就好了,子弹类没有问题,问题出在画图上。

有几个错误
1. public void fired()
    {
        switch(this.direct)
        {
            case 1:
                b = new Bullet(x+10,y,1);
            case 2:
                b = new Bullet(x+30,y+10,2);
            case 3:
                b = new Bullet(x+10,y+30,3);
            case 4:
                b = new Bullet(x,y+10,4);
        }
        Thread t1 = new Thread(b);
        t1.start();
    }         没写break 吧,穿了吧。子弹位置当然不准确。而且坐标也不对,子弹的坐标应该是坦克炮筒那个线的一个点,正确的坐标是这样的。
case 1:
                b = new Bullet(x+9,y-4,1);
                break;
            case 2:
                b = new Bullet(x+34,y+10,2);
                break;
            case 3:
                b = new Bullet(x+9,y+34,3);
                break;
            case 4:
                b = new Bullet(x-4,y+10,4);
                break;


2、你重画动作居然是要用按键来实现,怎么想的?按一下 重画一下,应该是线程控制不停的控制。
 public void run()
    {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            this.repaint();
        }
}

再把keyPressed里的repaint()删除掉。改完这两个错误就好了,你试试。
2013-05-27 21:57
hsjjgm
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:106
专家分:189
注 册:2013-4-27
收藏
得分:0 
回复 3楼 ren829
万分感谢
刚调试了下好了,主要原因是少了break和MyPanel面板忘记持续刷新了
那个炮弹位置还好,还算正常
那个线程放在按键J里主要是为了实现只有一颗子弹,但是要想发就发的效果,即一个子弹消失之前,如果再按发射,第一颗子弹会消失

2013-05-29 09:15
快速回复:能帮我看看这个子弹的坐标哪里错了吗
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042047 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved