| | |
PacMan Boolean Problem Part Duex.
Thread Solved
![]() |
•
•
Join Date: Oct 2007
Posts: 15
Reputation:
Solved Threads: 0
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:
Dot Class:
Here are the codes for each.
Main Class:
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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; } }
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 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.
Also, your collision function in EatDot() needs to check the absolute value of the coordinate differences
Java Syntax (Toggle Plain Text)
if ((Math.abs(xcoords - dot[i].x) <= 5) && ((Math.abs(ycoords - dot[i].y) <=5)) && !dot[i].isEaten()) {
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.
Last edited by Ezzaral; Dec 7th, 2007 at 12:18 pm.
![]() |
Other Threads in the Java Forum
- Previous Thread: help with java please.
- Next Thread: Need help with a very simple Java program
| Thread Tools | Search this Thread |
account android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer homework html ide image inheritance integer integration intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux loop method midlethttpconnection migrate mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program property ria scanner server set sharepoint smart sms smsspam sourcelabs splash sql sqlite subclass support swing testautomation textfield threads tree trolltech unlimited utility windows






