Hello All, This is suppose to be my finished code for my "PacMan Game". All this "game" is suppose to do is to allow a Pacman to eat dots that can be placed on the screen at any location by clicking the mouse. It has a main class and a dot class file. For some reason the PacMan is not eating the dots, any ideas? Any help will be greatly appreciated

Here are the codes for each.

Main Class:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class PacMan extends Applet {

 int xcoords = 0;
 int ycoords = 0;
 Dot[] dot = new Dot[1000];
 int dotct = 0;

 public void init () {

  resize (500,500);
  setBackground (Color.black);
  addMouseListener (new MouseAdapter ()

    public void mousePressed (MouseEvent e) {

    if (xcoords == 0){
    	xcoords = (e.getX()+50);
    	ycoords = (e.getY()+50);

    else {
	  dot[dotct] = new Dot();
	  dot[dotct].x = e.getX();
      dot[dotct].y = e.getY();
      dot[dotct].r = 5;


  addKeyListener (new KeyAdapter ()

     public void keyPressed (KeyEvent e) {

      if (e.getKeyCode()==KeyEvent.VK_UP)
        ycoords -= 5;
      if (e.getKeyCode()==KeyEvent.VK_DOWN)
        ycoords += 5;
      if (e.getKeyCode()==KeyEvent.VK_LEFT)
        xcoords -= 5;
      if (e.getKeyCode()==KeyEvent.VK_RIGHT)
        xcoords += 5;



 public void paint (Graphics g) {

 g.setColor (Color.blue);
    if ((ycoords + xcoords) == 0) g.drawString ("Please Click On Screen Twice",155,235);
 	if ((ycoords + xcoords) == 0) g.drawString ("First Click For Placement of PacMan",140,250);
 	if ((ycoords + xcoords) == 0) g.drawString ("Second Click For Placement of First Dot",130,265);

 g.setColor (Color.yellow);

 	if ((ycoords + xcoords)%2 == 0)
		g.fillArc (xcoords-50,ycoords-50,30,30,45,270);
   		g.fillArc (xcoords-50,ycoords-50,30,30,0,360);

 	for (int i=0; i < dotct; i++) {
 	  if (!dot[i].isEaten() )



public void EatDot() {

  for (int i=0; i < dotct; i++) {
 	if ((xcoords - dot[i].x <= 5) && (ycoords - dot[i].y <=5) && !dot[i].isEaten()) {

Dot Class:

import java.awt.*;

public class Dot {

	public double x = 0, y =0, r=0;

public void drawDot (Graphics g) {

  g.setColor (Color.pink);
  g.fillOval ((int)(x-r),(int)(y-r),(int)(2*r),(int)(2*r));


private boolean eaten = false;

public void setEaten (boolean beenEaten) {

	eaten = beenEaten;

public boolean isEaten() {

	return eaten;



Recommended Answers

All 3 Replies

Ah I just realised that you are not actually calling the eatDot() method anywhere in your code. I think it should probably be called immediately before the call to repaint()...

Yes, EatDot() needs to be called before repaint() in your KeyAdapter to evaluate if a dot is eaten.

Also, your collision function in EatDot() needs to check the absolute value of the coordinate differences

if ((Math.abs(xcoords - dot[i].x) <= 5) && ((Math.abs(ycoords - dot[i].y) <=5)) && !dot[i].isEaten()) {

and lastly you need to reevaluate your offsets from xcoords and ycoords in your placement and drawing of the pacman arc. Offsetting -50 from x and y are causing your pacman to be drawn in the wrong location relative to xcoords and ycoords values you are checking against the dot coordinates.

You are close. You just need to check your relative coordinate and bounding box usage to fine tune it.

You might want to also use an ArrayList for the Dots instead of a fixed 1000 element array and remove a dot when it is eaten, rather than leaving it in an array and continuing to check it after it's long gone.

commented: Good points all +1

Thanks for the help guys! I finally figured it out :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.