Java游戏中的碰撞检测

时间:2018-04-13 15:54:20

标签: java

我正在用java编写我的第一个高中项目游戏,显然我遇到了一些问题。 我的游戏是关于龙珠(所以一场战斗游戏)和我需要帮助解决两个角色的碰撞。 我尝试过使用矩形,但似乎坐标永远不会改变(因此碰撞只发生在起点上)。

这是控制碰撞的类:

public class RettangoloC 
{

int X,Y,dy,dx;
Rectangle2D r1;
RettangoloV rettV;

public RettangoloC()
{

initRett();
}

这些是"悟空"的初始坐标。字符;

 private void initRett() 
 {
 X = 950;
 Y = 0; 
 r1 =  new Rectangle(X,Y, 60,100);

}

 public void Move()
{
X += dx;
Y += dy;
 }

这是应该检测到碰撞的地方;

public boolean checkCollisions() {

rettV=new RettangoloV();
    if (r1.intersects(rettV.getRett())) 
    {
        return true;

    }
    else
        return false;
    }

 public Rectangle2D getRett()
{
  return r1;
 }

每次角色移动时,矩形也会移动;

  public void keyPressed(KeyEvent e) {
   int key = e.getKeyCode();


if (key == KeyEvent.VK_LEFT) 
{
    dx = - 4;
}

if (key == KeyEvent.VK_RIGHT) {

    dx = 4;
}

if (key == KeyEvent.VK_UP) {
    dy = -2;
}

if (key == KeyEvent.VK_DOWN) {
    dy = +1;
}

if (key == KeyEvent.VK_NUMPAD1) {

    dx-=1;

}

if (key == KeyEvent.VK_NUMPAD2) {
    dx-=1;

}

if (key == KeyEvent.VK_NUMPAD3) {
    dx-=1;
}


}

public void keyReleased(KeyEvent e) {

int key = e.getKeyCode();


if (key == KeyEvent.VK_LEFT) {
   dx = 0;
}

if (key == KeyEvent.VK_RIGHT) {

   dx = 0;
}

if (key == KeyEvent.VK_UP) {
    dy = 0;
}

if (key == KeyEvent.VK_DOWN) {
    dy = 0;
}

if (key == KeyEvent.VK_NUMPAD1) {
    dx=0;

}

if (key == KeyEvent.VK_NUMPAD3) {
    dx=0;
}
}
}

角色的矩形" Vegeta"几乎相同,变量的名称不同。

public int getY() {
    return y;
}

public Image getImage() {
    return imageT;
}

public void setImage()
{

    if(a==1)
    {
        imageT=image2;
    }
    if(a==0)
    {   

        imageT=image1;
    }
    if(a==2)
    {
        imageT=image3;

    }

    if(a==4)
    {
    imageT=image4;

    }
    if(a==3)
    {
        imageT=image3r;
    }
    if(a==6)
    {
        imageT=image2r;
    }
    if(a==7)
    {
        imageT=image4r;
    }
}

使用" a"悟空的形象会改变;

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    rett=new RettangoloC();

    if (key == KeyEvent.VK_LEFT) 
    {
        tx=getX();

        if(tx<=0) //Blocco
        {
            dx=0;
        }
        else
        {
            if(tx<=0)
            {
                dx=0;
            }
            else
        dx = - 4;
        }

        a=2;
        setImage();
        getImage();

    }

    if (key == KeyEvent.VK_RIGHT) {
        tx=getX();

        if(tx>=950)
        {
            dx=0;
        }
        else
        {
            if(tx>=950)
            {
                dx=0;
            }
            else
        dx = 4;
        }
        a=1;
        setImage();
        getImage();
    }

    if (key == KeyEvent.VK_UP) {
        ty=getY();
        if(ty<=4)
        {
            dy=-0;
        }
        else
        {
            if(ty<=2)
            {
                dy=-0;
            }
            else
        dy = -2;
        }
        a=4;
        setImage();
        getImage();
    }

    if (key == KeyEvent.VK_DOWN) {
        ty=getY();
        if(ty>=650)
        {
            dy=-0;
        }
        else
        {
            if(ty>=600)
            {
                dy=-0;
            }
            else
        dy = +1;
        }
        a=4;
        setImage();
        getImage();
    }

    if (key == KeyEvent.VK_NUMPAD0) {
        a=10;
        setImage();
        getImage();
    }

数字是攻击。我只用NUMPAD1尝试了碰撞;

    if (key == KeyEvent.VK_NUMPAD1) {
        if(b1==true)
        {
            a=3;
        }
        else
        a=0;

        x-=1;
        setImage();
        getImage();
    }

    if (key == KeyEvent.VK_NUMPAD2) {
        a=6;
        x-=1;
        setImage();
        getImage();
    }

    if (key == KeyEvent.VK_NUMPAD3) {
        a=7;
        x-=1;
        setImage();
        getImage();
    }


}

public void keyReleased(KeyEvent e) {

    int key = e.getKeyCode();
    a=0;


    if (key == KeyEvent.VK_LEFT) {
       dx = 0;

        setImage();
        getImage();
    }

    if (key == KeyEvent.VK_RIGHT) {

       dx = 0;
        a=0;
        setImage();
        getImage();
    }

    if (key == KeyEvent.VK_UP) {
        dy = 0;
        a=0;
        setImage();
        getImage();
    }

    if (key == KeyEvent.VK_DOWN) {
        dy = 0;
        a=0;
        setImage();
        getImage();
    }

    if (key == KeyEvent.VK_NUMPAD1) {
        a=0;
        setImage();
        getImage();

    }

    if (key == KeyEvent.VK_NUMPAD3) {
        a=0;
        setImage();
        getImage();
    }
}

然后有一个班级管理所有其他人;

  private void initBoard() {

    addKeyListener(new TAdapter());
    setFocusable(true);

    sfondo=new Sfondo();
    craft = new Craft();
    vegeta= new Vegeta();
    rettc=new RettangoloC();
    rettv=new RettangoloV();

    timer1 = new Timer(DELAY,new Somma());
    timer1.start();
    timer2 = new Timer(DELAY,new Meno());
    timer2.start(); 
    timer3 = new Timer(DELAY,new RettCraft());
    timer3.start(); 
    timer4 = new Timer(DELAY,new RettVeg());
    timer4.start(); 
}



public void paintComponent(Graphics g) {
    super.paintComponent(g);

    doDrawing(g);

    Toolkit.getDefaultToolkit().sync();
}

private void doDrawing(Graphics g) {

    Graphics2D g2s = (Graphics2D) g;
    g2s.drawImage(sfondo.getSfondo(),sfondo.getXS(), sfondo.getYS(), this);

    Graphics2D g2g = (Graphics2D) g;
    g2g.drawImage(craft.getImage(),craft.getX(), craft.getY(), this);

    Graphics2D g2v = (Graphics2D) g;
    g2v.drawImage(vegeta.getImage2(),vegeta.getX2(), vegeta.getY2(), this); 
}



   class Somma implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
        craft.move();
        craft.setImage();
        craft.getImage();

        repaint(); 
    }
}

   class Meno implements ActionListener
   {
    public void actionPerformed(ActionEvent e)
    {    vegeta.move2();
        vegeta.setImage2();
        vegeta.getImage2();


        repaint(); 
     }
   }

类RettCraft实现了ActionListener     {

public void actionPerformed(ActionEvent e) 
{
     rettc.Move();  
     craft.b1=rettc.checkCollisions();
}
}

   class RettVeg implements ActionListener
   {

      public void actionPerformed(ActionEvent e) 
      {
     rettv.Move();  
     vegeta.b1=rettv.checkCollisions();
      }
    }

private class TAdapter extends KeyAdapter {


    public void keyReleased(KeyEvent e) {
        craft.keyReleased(e);
        vegeta.keyReleased(e);
        rettc.keyReleased(e);
        rettv.keyReleased(e);

    }


    public void keyPressed(KeyEvent e) {
        craft.keyPressed(e);
        vegeta.keyPressed(e);
        rettc.keyReleased(e);
        rettv.keyReleased(e);
    }

    }




} 

我知道这是很多代码,而且我也知道代码写得不好,但我会&#34;清楚&#34;当项目完成时。 提前致谢(如果帖子中有问题请随时告诉我,这是我第一次使用SOF)。

0 个答案:

没有答案