Frogger Type游戏碰撞检测

时间:2013-12-06 22:48:30

标签: java swing collision-detection java-2d

我需要一些帮助来弄清楚如何在蛙人游戏中捕捉和存储碰撞。我已经设法使游戏运行得相当顺利,现在想要为游戏添加碰撞。经过一些研究,我发现矩形似乎是我游戏的可行选择,但是,我不知道我应该从哪里开始实际捕获“敌人”和“玩家”的碰撞。到目前为止,这是我的代码:

public class myJPanel0 extends JPanel implements KeyListener, ActionListener
   //This panel will contain the game
{ 
JButton menu;
Image myImage;
Graphics g;
myJPanel1 p1;
   // Border[] gameBorder = new Border[]{BorderFactory.createTitledBorder("Border types")};
 gameOverJPanel gp;

JButton lion = new JButton(new ImageIcon("images/download.jpg"));  
JButton yard = new JButton("50 Yardline");
JButton score = new JButton("Touchdown");
JButton start = new JButton("Start");
JButton scoreKeeper = new JButton("Your score is");
JButton lives = new JButton("Lives left = ");


Timer tim;
int delay = 100;
int x = 296;
int y = 685;
int counter = 0;

ButtonObject[] enemies = new ButtonObject[10];
ButtonObject[] enemies1 = new ButtonObject[10];

ImageIcon icon[] = new ImageIcon[10];

     public myJPanel0(myJPanel1 informedp1, gameOverJPanel informedgp)
     {
      super();
      setLayout(null);
      p1 = informedp1;
      gp = informedgp;
      setBackground(Color.MAGENTA);
      menu = new JButton("Menu");
      scoreKeeper.setBounds(16,80,200,55);
      lives.setBounds(417, 80,200,55);
      lion.setBounds(x,y,40,55);
      score.setBounds(16,135,601,55);
      yard.setBounds(16,410,601,55);
      start.setBounds(16,685,601,55);
      menu.setBounds(new Rectangle(250,5,80,30));
      add(menu);
      add(lives);
      add(lion);
      add(yard);
      add(start);
      add(scoreKeeper);
      add(score);

      setFocusable(true);
  addKeyListener(this);


  tim = new Timer(delay, this);
      tim.start();

        for (int i = 0; i <= 3; i++){ // loop that cycles through first half of enemy creation
        String text = String.valueOf(i);
        int y = 630 - (i * 55);
        int x = 16; 
        enemies[i] = new ButtonObject(text+"??", x, y, 40, 55); 
        enemies[i].setBounds(new Rectangle(x, y, 40,55));
        add(enemies[i]);


        }

      for (int i = 0; i <= 3; i++){ // second have enemy creation
        String text = String.valueOf(i);
        int y = 355 - (i * 55);
        int x = 16; 
        enemies1[i] = new ButtonObject(text+"??", x, y, 40, 55);
        enemies1[i].setBounds(new Rectangle(x, y, 40,55));
        add(enemies1[i]);
        }

    }

     public void paintComponent(Graphics g) 
{
        super.paintComponent(g);
       Image myLion = Toolkit.getDefaultToolkit().getImage("images/download.jpg");
       g.drawImage(myLion,296,355,40,55,this);

    if(p1.myImage1 == 1)
    {
    super.paintComponent(g); 
    Image myImage = Toolkit.getDefaultToolkit().getImage("images/snow.jpg");//Place holder for now, we can come up with our own image.
    g.drawImage(myImage, 0, 0,680,880, this); 
    requestFocusInWindow();
    }else if(p1.myImage1 == 2) 
       {
           super.paintComponent(g); 
           Image myImage = Toolkit.getDefaultToolkit().getImage("images/grass.jpg");//Place holder for now, we can come up with our own image.
           g.drawImage(myImage, 0, 0,680,880, this);  
           requestFocusInWindow();
       }
    else{
           super.paintComponent(g); 
           Image myImage = Toolkit.getDefaultToolkit().getImage("images/stone.jpg");//Place holder for now, we can come up with our own image.
           g.drawImage(myImage, 0, 0,680,880, this); 
           requestFocusInWindow();
    }

      }

           public void keyPressed(KeyEvent evt) 
        {
     System.out.println("Key pressed");
        int kk = evt.getKeyCode();
    if(kk ==  evt.VK_LEFT) {x=x-40;} 
    else if(kk ==  evt.VK_RIGHT) {x=x+40;}
    else if(kk ==  evt.VK_UP) {y=y-55;}
    else if(kk ==  evt.VK_DOWN) {y=y+55;}
    lion.setBounds(x,y,40,55);
    System.out.println(x);
    System.out.println(y);
    if(y <= 135){
    counter = counter + 1;
    scoreKeeper.setText("Your score is " + counter);
     y = 740;
    }


   }

   public void keyReleased(KeyEvent evt) {  }

   public void keyTyped(KeyEvent evt) {  }


    public void actionPerformed(ActionEvent event) {

    for (int i = 0; i <= 3; i++)
      {
          enemies[i].move();
      }
    for (int i = 0; i <= 3; i++)
      {
          enemies1[i].move();
      }
   }

}

0 个答案:

没有答案