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