0

I need some help figuring out how to capture and store collisions in a frogger game. I've managed to get the game to run fairly smoothly and now want to add collisions to the game. After some research I've found that rectangles seem like a viable option for my game, however, I'm not sure where I should start to actually capture a collision from an "enemy" and a "player". Here is my code so far:

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();
      }
   }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1566796
  • 35
  • 2
  • 9
  • 1
    Use Java-2D for [containment](http://stackoverflow.com/a/20296572/418556) & [collision](http://stackoverflow.com/a/14575043/418556) by `Area`. – Andrew Thompson Dec 06 '13 at 23:14
  • 1) A single blank line of white space in source code is *always* enough. Blank lines after `{` or before `}` are also typically redundant. 2) For Swing, typically use key bindings over the AWT based, lower level, `KeyListener`. See [How to Use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) for details on how to use them. 3) For better help sooner, post an [SSCCE](http://sscce.org/). Add imports and a `main` with top level window like `JFrame` or `JOptionPane` to make an SSCCE. – Andrew Thompson Dec 07 '13 at 00:47
  • 4) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Dec 07 '13 at 01:05

0 Answers0