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