PacMan Boolean Problem Part Duex.

Thread Solved
Reply

Join Date: Oct 2007
Posts: 15
Reputation: jackskell26 is an unknown quantity at this point 
Solved Threads: 0
jackskell26 jackskell26 is offline Offline
Newbie Poster

PacMan Boolean Problem Part Duex.

 
0
  #1
Dec 6th, 2007
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:
  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class PacMan extends Applet {
  6.  
  7. int xcoords = 0;
  8. int ycoords = 0;
  9. Dot[] dot = new Dot[1000];
  10. int dotct = 0;
  11.  
  12.  
  13.  
  14. public void init () {
  15.  
  16. resize (500,500);
  17. setBackground (Color.black);
  18. addMouseListener (new MouseAdapter ()
  19.  
  20. {
  21. public void mousePressed (MouseEvent e) {
  22.  
  23. if (xcoords == 0){
  24. xcoords = (e.getX()+50);
  25. ycoords = (e.getY()+50);
  26. }
  27.  
  28. else {
  29. dot[dotct] = new Dot();
  30. dot[dotct].x = e.getX();
  31. dot[dotct].y = e.getY();
  32. dot[dotct].r = 5;
  33. dotct++;
  34. repaint();
  35.  
  36. }
  37. }
  38. }
  39. );
  40.  
  41. addKeyListener (new KeyAdapter ()
  42.  
  43. {
  44. public void keyPressed (KeyEvent e) {
  45.  
  46. if (e.getKeyCode()==KeyEvent.VK_UP)
  47. ycoords -= 5;
  48. if (e.getKeyCode()==KeyEvent.VK_DOWN)
  49. ycoords += 5;
  50. if (e.getKeyCode()==KeyEvent.VK_LEFT)
  51. xcoords -= 5;
  52. if (e.getKeyCode()==KeyEvent.VK_RIGHT)
  53. xcoords += 5;
  54.  
  55. repaint();
  56.  
  57. }
  58. }
  59. );
  60. }
  61.  
  62. public void paint (Graphics g) {
  63.  
  64. g.setColor (Color.blue);
  65. if ((ycoords + xcoords) == 0) g.drawString ("Please Click On Screen Twice",155,235);
  66. if ((ycoords + xcoords) == 0) g.drawString ("First Click For Placement of PacMan",140,250);
  67. if ((ycoords + xcoords) == 0) g.drawString ("Second Click For Placement of First Dot",130,265);
  68.  
  69. g.setColor (Color.yellow);
  70.  
  71. if ((ycoords + xcoords)%2 == 0)
  72. g.fillArc (xcoords-50,ycoords-50,30,30,45,270);
  73. else
  74. g.fillArc (xcoords-50,ycoords-50,30,30,0,360);
  75.  
  76. for (int i=0; i < dotct; i++) {
  77. if (!dot[i].isEaten() )
  78. dot[i].drawDot(g);
  79.  
  80. }
  81.  
  82. }
  83.  
  84. public void EatDot() {
  85.  
  86. for (int i=0; i < dotct; i++) {
  87. if ((xcoords - dot[i].x <= 5) && (ycoords - dot[i].y <=5) && !dot[i].isEaten()) {
  88. dot[i].setEaten(true);
  89. }
  90. }
  91. }
  92. }

Dot Class:
  1. import java.awt.*;
  2.  
  3. public class Dot {
  4.  
  5. public double x = 0, y =0, r=0;
  6.  
  7. public void drawDot (Graphics g) {
  8.  
  9. g.setColor (Color.pink);
  10. g.fillOval ((int)(x-r),(int)(y-r),(int)(2*r),(int)(2*r));
  11.  
  12. }
  13.  
  14. private boolean eaten = false;
  15.  
  16. public void setEaten (boolean beenEaten) {
  17.  
  18. eaten = beenEaten;
  19. }
  20.  
  21. public boolean isEaten() {
  22.  
  23. return eaten;
  24.  
  25. }
  26.  
  27. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 771
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 104
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: PacMan Boolean Problem Part Duex.

 
0
  #2
Dec 7th, 2007
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()...
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: PacMan Boolean Problem Part Duex.

 
1
  #3
Dec 7th, 2007
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
  1. 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.
Last edited by Ezzaral; Dec 7th, 2007 at 12:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 15
Reputation: jackskell26 is an unknown quantity at this point 
Solved Threads: 0
jackskell26 jackskell26 is offline Offline
Newbie Poster

Re: PacMan Boolean Problem Part Duex.

 
0
  #4
Dec 7th, 2007
Thanks for the help guys! I finally figured it out
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC