
-----------------------------------
huskiesgoaler34
Tue Jun 07, 2011 12:40 pm

Collision Detection in Java Applet
-----------------------------------
Hey, I have one problem that I am having in my game. How do I check to see that there is a collision between the yellow circles and the Pac Man Sprite. I tried to add some code which returns the values of their coordinates but I don't know where to go from there. What I want the program to do is to replace the yellow circle with a black circle over it when the PacMan "eats" it (or in fact passes over it). Can someone help please. I will like help to get one collision for one circle. This will give my enough knowledge to finish the rest. Thanks in advance.

[code]
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Rectangle;
import java.awt.image.*;
import java.util.*;
import sun.audio.*;    //import the sun.audio package
import java.io.*;


public class PacMaze2 extends JApplet implements Runnable, KeyListener 
{
  static Font gameFont = new Font("SanSerif", Font.BOLD, 72);
  int width;
  int height;
  int xcordPink = 150;
  int ycordBlue = 133;
  int xcordOrange =253;
  ArrayList  walls;
  Image backgroundBuf;
  Image screenBuf;
  Image img;
  Image cherry;
  Image pinkGhost;
  Image blueGhost;
  Image orangeGhost;
  Image finishLineCongratulations;
  boolean collision;
  JLabel player;
  Thread runner;
  int x = 355;
  int y = 255;
  int speed = 10;
  Graphics2D background;
  Thread thread;
  
  public static void Music (){
  
//  InputStream in = new FileInputStream("Opening_Sound.mp3");
//// Create an AudioStream object from the input stream.
//AudioStream as = new AudioStream(in);         
//// Use the static class member "player" from class AudioPlayer to play
//// clip.
//AudioPlayer.player.start(as);            
//// Similarly, to stop the audio.
//AudioPlayer.player.stop(as); 

  }



  
  public void init ()
  {
    Music();
    collisions();
    img = getImage (getCodeBase(),"Animated_PacMan_Left.gif");
    pinkGhost = getImage (getCodeBase(),"Pink_Ghost.gif");
    blueGhost = getImage (getCodeBase(),"Blue_Ghost.gif");
    orangeGhost = getImage (getCodeBase (),"Orange_Ghost.gif");
    cherry = getImage (getCodeBase (),"Pac_Cherry.gif");
    finishLineCongratulations = getImage (getCodeBase(), "PacMan_Congrats.gif");
    player = new JLabel (new ImageIcon (img));
    player.setSize (img.getWidth (this), img.getHeight (this));
    addKeyListener(this); 
    setFocusable (true);
    width = this.getWidth();
    height = this.getHeight();
    walls = new ArrayList  ();
    buildWalls();
        
  }
  
  
  
  public void start ()
  {
    
    
    if (thread == null )
      thread = new Thread (this);
    thread.start();
   
  }
  
  public void stop ()
  {
   if (thread != null ) 
    thread.interrupt();
   thread = null;
  }
  

  
  
public void buildWalls( )
  
{   // create Rectangle objects and store in ArrayList walls
  
  int wallThickness = 10;
      
  walls.add (new Rectangle (0,0, width, wallThickness));//top border
  walls.add (new Rectangle (0,0, wallThickness, height)); //left border     /BORDERS AROUND APPLET FRAME
  walls.add (new Rectangle (width-wallThickness,0, wallThickness,height));//right border
  walls.add (new Rectangle (0, height-wallThickness,width,wallThickness));//down border
  
  walls.add( new Rectangle( 50,height-wallThickness-240, wallThickness, 65 ) ); //top space divider on left side
  walls.add( new Rectangle( 50,height-wallThickness-120, wallThickness, 70 ) );  //bottom space divider on left side      
  walls.add( new Rectangle( 50,height-wallThickness-50, width-60, wallThickness ) ); //bottom horizontal piece
  walls.add( new Rectangle( 50,height-wallThickness-250, width-200, wallThickness ) ); //top horizontal piece (left)
  walls.add( new Rectangle( 285,height-wallThickness-250, width-340, wallThickness ) ); //top horizontal piece (right)
  walls.add( new Rectangle( 180,height-wallThickness-150, width-20, wallThickness ) );//middle piece (across)
  walls.add( new Rectangle( 250,height-wallThickness-100, width-20, wallThickness ) ); //lower middle piece across
  walls.add( new Rectangle( 180,height-205, wallThickness, 100 ) );  //middle piece
  walls.add( new Rectangle( 120,height-205, wallThickness, 100 ) ); 
  }

public void paintWalls ()
{
  backgroundBuf = createImage (width, height);
  background = (Graphics2D) backgroundBuf.getGraphics();
  background.setColor (Color.black);
  background.fillRect (0,0,width, height);
  background.setColor (Color.RED);
  
  for (int i=0; i