Hello, I am writing a VERY simple version of Pacman. In this "game" the user can click on the screen to select where the Pacman will start and any other clicks will add dots to the screen. The pacman will then be controlled by the arrows keys. The pacman should be able to eat the dots when he gets within a reasonable range of the dot. However, I have been trying to think of a why to do this, and I am quite stumped. I believe that the best way to do this would be to use a boolean method to represent whether the dots have been eaten or not and just to print the whens that haven't been eaten. Below are the two classes that I am using, the main class the class that stores the information for the dots. Any information on how I could do this would be extremely helpful. Thank you

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;
      dotct++;
      repaint();

      }
     }
   }
  );

  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;

      repaint();

     }
   }
  );
 }

 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);
   	else
   		g.fillArc (xcoords-50,ycoords-50,30,30,0,360);

 	for (int i=0; i < dotct; i++)
   		dot[i].drawDot(g);

 }

}

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

	}


}

I think I would do something like this in your Dot class:

private boolean eaten = false;
// call this next method to set the Dot to eaten
public void setEaten(boolean beenEaten) {
eaten = beenEaten;
}
// call this next method to check if the Dot is eaten when drawing it
public boolean isEaten() {
return eaten;
}

Then in your main class, you need a method to check whether pacman is close to not eaten dots and set to eaten if he is. In your paint method you need to check whether each Dot is eaten before drawing it.

Thanks for the help. However, I'm not quite sure if I understand what I should use to check whether pacman is with in the eating range. Like say if pacman is within 5 pixels of the dot, I would like him to eat it.

You know pacman's coordinates because you draw him. The Dots are drawn at the start so you know their coords as well. A simple method to check whether the nearest uneaten Dot is within 5 pixels of pacman should do the trick.

To Do:
-- find nearest Dot to Pacman's current coordinates
-- is this dot eaten?
-- if not, is this Dot's coords close enough to eat?

This is what I have been working on.

public void EatDot() {

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

 	 }

Not sure if this is what you meant for me to do. But I'm trying to call the isEaten method in the Dot class. However, I get some errors when I try to compile

I would do something more like this:

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[i].setEaten(true);
         }
  }
}

Also, you only want to eat one dot at a time. And you might need some sort of abs function (see Math.abs in java documentation) for your checks in the if-statement.

Thanks once again. I'm trying to work out how to have the paint check to see if it's been eaten. Would it be more sensible to have it check in the Dot class? Such as if the boolean is true it prints those dots?

Sorry, I just editted the post above to include a check for whether the dot is eaten. It's been added the if-statement you wrote before.

So in your paint method you would write something like:

if ( ! dot[i].isEaten() ) {
// paint the dot
}

I don't think Pacman wants to eat food :P Still not working for some reason, any ideas?

I don't think Pacman wants to eat food :P Still not working for some reason, any ideas?

Here is the code now,

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;
	  dotct++;
	  repaint();

	  }
	 }
   }
  );

  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;

	  repaint();

	 }
   }
  );
 }

 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);
   	else
   		g.fillArc (xcoords-50,ycoords-50,30,30,0,360);

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

	 }

	}

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[i].setEaten(true);
		 	}
		  }
	}
}

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;

	}

}

I'm sorry jackskell26, I can't see what's wrong. Maybe someone else can help.

Thanks anyways, you've helped me out alot :)

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.