无法弄清楚为什么我的碰撞检查不起作用

时间:2017-05-12 18:18:30

标签: java

我尝试了3种不同的方法来检查玩家和炸弹爆炸后的碰撞。我已经使用了一个带有球员点数的炸弹检查边界的点obj,玩家和炸弹都是矩形,并且我已经写了一条4行的语句来检查玩家的所有点与炸弹的所有点。

我只是将我的所有代码放在下面,这将是有用的。我也使用eclipse。

如果你有任何想法,为什么它还没有工作,我很乐意接受它们。 感谢所有帮助

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


/**
* Class Driver - write a description of the class here
* 
* @author (your name) 
* @version (a version number)
*/

public class Driver extends JApplet
private Timer timer= new Timer(10, null);
private Animation anime=new Animation(timer,500,500);

public void init()
{
    setSize(500,500);
    getContentPane().add(anime);
}

public void start()
{
  timer.start();   

}

public void stop()
{
    timer.stop();
}
}

/ *****动画****** /

import java.awt.*;
import javax.swing.*;  
import java.awt.event.*;
import java.util.HashSet;

public class Animation extends JPanel {
private Timer timer;
private Map Map;
private BomberMen P1;
private BomberMen P2;
private int width;
private int height;
private HashSet<Bomb> bombs = new HashSet<Bomb>();
private HashSet<Wall> walls = new HashSet<Wall>();
private HashSet<Bomb> toRemove = new HashSet<Bomb>();
private int gamestate;
private Menu menu;

public Animation(Timer cd, int w, int h) {
    this.width = w;
    this.height = h;
    setFocusable(true);
    this.timer = cd;
    P1 = new BomberMen(450,450);
    P2 = new BomberMen(0,0);
    timer.addActionListener(new DrawerListener());
    addKeyListener(new UserKeyListener());
    this.gamestate=1;
}

public void paintComponent(Graphics g) 
{
if (gamestate==1)
    {
        Menu.draw(g);
    }
 if (gamestate==2)
  {
    g.setColor(Color.white);
    g.fillRect(0, 0, 500, 500);
    P1.draw(g);
    P2.draw(g);
    for (Wall w : walls)
    {
        w.draw(g);
    }

    for (Bomb b : bombs)
    {
        b.draw(g);
    }
  }
 if (gamestate==3)
 {
     Menu.draw(g);
 }
}


private void MakeWalls() {
    for (int y = 0; y< 500; y += 60) {
        for (int x = 0; x < 200; x += 60) {
            walls.add(new Wall(x, y, 30, 30));
        }
    }
}

private boolean checkCollision() {

    for (Bomb b : bombs) {
        if (b.getExplode() == true) 
        { 

            if(P1.getBounds().intersects(b.getBoundsTall()) || P1.getBounds().intersects(b.getBoundsLong()))
            {
                return true;
            }

            else
            {
                return false;
            }






            /**
           if (P1.getX()>b.getX100() && P1.getX()<b.getX110() && P1.getY() > b.getY10() && P1.getY() < b.getY20() //top right point
            ||  P1.getX30()>b.getX100() && P1.getX30()<b.getX110() && P1.getY() > b.getY10() && P1.getY() < b.getY20() //top left point             
            ||  P1.getX30()>b.getX100() && P1.getX30()<b.getX110() && P1.getY30() > b.getY10() && P1.getY30() < b.getY20()  //bot left point
            ||  P1.getX()>b.getX100() && P1.getX()<b.getX110() && P1.getY30() > b.getY10() && P1.getY30() < b.getY20() //bot right point
                )
            {
                return true;
            }
            else
            {
                return false;
            }
        **/
            /**
            for (Point p : P1.getHitBox()) {
                if (b.getHitboxLong().contains(p) || b.getHitboxTall().contains(p)) 
                {
                    return true;
                }
                **/



        }
    }
    return false;
}

private class DrawerListener implements ActionListener {
    private int countdown = 0;

    public void actionPerformed(ActionEvent e) 
    {
        if (gamestate == 2) 
        {
            for (Bomb b : bombs)
            {
                b.update();
                if (b.getTime() >= 500)
                    toRemove.add(b);
            }
            bombs.removeAll(toRemove);
            toRemove = new HashSet<Bomb>();

            if (checkCollision() == true)
            {

                gamestate = 1;

            }
            repaint();
        }

    }

}

private class UserKeyListener implements KeyListener 
{

    @Override
    public void keyPressed(KeyEvent e)
    {
        if (gamestate==1)
        {
            if(e.getKeyCode()==37)
            {
                gamestate=2;
            }
        }

    if(gamestate==2)
    {
        if (e.getKeyCode() == 37) {//left
            P1.changeX(-30);
        } else if (e.getKeyCode() == 38) {//up
            P1.changeY(-30);
        } else if (e.getKeyCode() == 39) {//right
            P1.changeX(30);
        } else if (e.getKeyCode() == 40) {//down
            P1.changeY(30);
        } else if (e.getKeyCode() == 32) {//space
            bombs.add(new Bomb(P1.getX() + 10, P1.getY() + 10));

        }

        if(e.getKeyCode()==65)//left
        {
            P2.changeX(-30);
        }
        if(e.getKeyCode()==87)//up
        {
            P2.changeY(-30);
        }
        if(e.getKeyCode()==68)//right
        {
            P2.changeX(30);
        }
        if(e.getKeyCode()==83)//down
        {
            P2.changeY(30);
        }
        if(e.getKeyCode()==70)//F for bomb
        {
            bombs.add(new Bomb(P2.getX()+10,P2.getY()+10));
        }
    }

    }

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

    }

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

    }

}

}

/ ******** ***********炸弹/

    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.util.ArrayList;
    import java.util.HashSet;

    public class Bomb 
    {
      private int countdown;
      private int x;
      private int y;
      private boolean explode;
      private Rectangle hitboxTall;
      private Rectangle hitboxLong;

      public Bomb(int x,int y)
      {
          this.explode = false;
          this.countdown=0;
          this.x=x;
          this.y=y;

      }

      public void draw(Graphics g)
      {
          if(explode == true)
          {
              g.drawRect(x-100,y-10,210,30);//horizontal
              g.drawRect(x-10,y-100,30,210);//vertical
          }
          else if (explode == false)
              g.fillRect(x, y, 10, 10);
      }

      public void update()
      {

         if (countdown == 300)
          {
             explode=true;
             hitboxLong = new Rectangle(x-100,y-10,210,30);//H
             hitboxTall = new Rectangle(x-10,y-100,30,210);//V

          }

         countdown++;
      }
      public int getTime()
      {
          return countdown;
      }
      public boolean getExplode()
      {
          return explode;
      }
    /**
    public Rectangle getHitboxTall() {
        return hitboxTall;
    }

    public void setHitboxTall(Rectangle hitboxTall) {
        this.hitboxTall = hitboxTall;
    }

    public Rectangle getHitboxLong() {
        return hitboxLong;
    }

    public void setHitboxLong(Rectangle hitboxLong) {
        this.hitboxLong = hitboxLong;
        }
    **/
    public int getX100()
    {
        // TODO Auto-generated method stub
        return x-100;
    }
    public int getX110()
    {
        return x+110;
    }
    public int getY10()
    {
        return y-10;
    }
    public int getY20()
    {
        return y+20;    
    }

    public Rectangle getBoundsTall() {

        return hitboxTall.getBounds();
    }
    public Rectangle getBoundsLong()
    {
        return hitboxLong.getBounds();
    }

    }

/ *************播放*********** /

        import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.HashSet;
    import javax.imageio.*;

    public class BomberMen
    {
        private int x;
        private int y;
        private BufferedImage img;
        private HashSet <Point> hitBox = new HashSet<Point>(); 
        private Point person;
        private Rectangle player; 

        public BomberMen(int x,int y)
        {
            this.x=x;
            this.y=y;
           player = new Rectangle(x,y,30,30);

        }

        public void update(int x,int y)
        {
            this.x+=x;
            this.y+=y;
        }

        public void changeX(int x)
        {
            update(x,0);

        }

        public void changeY(int y)
        {
            update(0,y);
        }

        public void draw(Graphics g)
        {  
        /** BufferedImage img = null;
          try {
                 img = ImageIO.read(new File("bombermanDude.jpg"));
                  } catch (IOException e) {
                  } 
              g.drawImage(img, 200, 200, null);
          **/ 

            g.setColor(Color.blue);
            g.drawRect(x,y,30,30);
        }
    /**
        public HashSet<Point> getHitBox() {
            return hitBox;
        }

        public void setHitBox(HashSet<Point> hitBox) {
            this.hitBox = hitBox;
        }

        public Point getPerson() {
            return person;
        }

        public void setPerson(Point person) {
            this.person = person;
        }

        private void fillSet()
        {
            hitBox.add(person);
            hitBox.add(new Point ((int) person.getX()+30, (int) person.getY()  ));
            hitBox.add(new Point ((int) person.getX()+30, (int) person.getY()+30));
            hitBox.add(new Point ((int) person.getX(),    (int) person.getY()+30));
        }
        **/
        public int getX() 
        {
            return x;
        }

        public int getY()
        {
            return y;
        }
        public int getX30()
        {
            return x+30;
        }
        public int getY30()
        {
            return y+30;
        }

        public Rectangle getBounds() 
        {
            return player.getBounds();

        }
    }

/ **********菜单************** /

    import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Menu extends JPanel
{
    private static Font myFont = new Font("Garamond",Font.BOLD,20);

    public static void draw(Graphics g)
    {   
        g.setColor(Color.blue);
        g.fillRect(0, 0,500,500);

        g.setColor(Color.black);
        g.setFont(myFont);
        g.drawString("Start Game", 200, 200);
        g.drawString("Press Enter", 200, 250);

        //JButton b1 = new JButton("Start Game, Y or N");
        //b1.setMnemonic(y);
        //b1.

        // TODO Auto-generated method stub

    }
    /**private class DrawerListener implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub

        }
    //  if ()
    }
    **/
}

/ ************壁**************** /     import java.awt。*;

public class Wall 
{
private int h=30;
private int w=30;
private int x;
private int y;

public Wall(int x,int y,int h, int w)
{
    this.x=x;
    this.y=y;
    this.h=h;
    this.w=w;
}

public void draw(Graphics g)
{
    g.setColor(Color.blue);
    g.fillRect(x,y,30,30);
}

}

0 个答案:

没有答案