How to make Pacman eat dots?

I am trying to make my pacman object eat the pac dots but i am not sure how to with my program help?

public abstract class item
{
abstract void pacEatsDots();
}


public class Pacman extends item
{
  int x;
  int y;
  int radius = 15;
  int direction = 1;
  int direction2 = 0;

  public Pacman(int xn, int yn)
  {
    x = xn;
    y = yn;
  }

  void keyPressed() {//https://forum.processing.org/two/discussion/1573/spoof-of-pacman-game-for-project
    color white = color(255, 255, 255);
    color black = color(0, 0, 0);
    color c = pg.get(x+1, y);
    color d = pg.get(x, y+1);
    color e = pg.get(x-1, y);
    color f = pg.get(x, y-1);

    if (key == CODED)
    {
      if (e == white)
      {
        if (keyCode == LEFT) 
        {
          x = x - 2;
          direction = -1;
          direction2 = 0;
        }
      } 
      if ( c == white)
      {
        if (keyCode == RIGHT) 
        {  
          x = x + 2;
          direction = 1;
          direction2 = 0;
        }
      } 
       if ( f == white)
      {
         if (keyCode == UP) 
        {
          y = y - 2;
          direction = 0;
          direction2 = -1;
        }
      } 
      if ( d == white)
      {
         if (keyCode == DOWN) 
        { 
          y = y + 2;
          direction = 0;
          direction2 = 1;
        }
      }
    }
    render();
  }


  public void render() {//https://forum.processing.org/two/discussion/1573/spoof-of-pacman-game-for-project
    for ( int i=-1; i < 2; i++) {
      for ( int j=-1; j < 2; j++) {
        pushMatrix();
        translate(x +(i*width), y + (j*height) );
        if ( direction == -1) { 
          rotate(PI);
        }
        if ( direction2 == 1) { 
          rotate(HALF_PI);
        }
        if ( direction2 == -1) { 
          rotate( PI + HALF_PI );
        }

        arc(0, 0, radius*2.3, radius*2.3, map((millis() % 500), 0, 500, 0, 0.52), map((millis() % 500), 0, 500, TWO_PI, 5.76) );
        popMatrix();
        fill(255, 255, 0);
        // mouth movement //
      }
    }
  }




  void pacEatsDots()
  {

  }
  }



public class Dots  {
  float x, y;
  boolean isVisible;
  boolean on=true;
  Dots(float ix, float iy) {
    x = ix;
    y = iy;
    isVisible = true;
  }
  void display() {
    fill(#E5DA99);


    ellipse(x,y,12,12);


  }

  void displayBigDots()//https://processing.org/discourse/beta/num_1261323787.html
  {

  if(frameCount% 8== 0) {
    if(on)
  fill(255);
    else
  fill(0);
    on = !on;
  }
  ellipse(x,y,20,20);
  }
  //public void setup(
}

ArrayList<Dots> dots = new ArrayList();
Pacman pac;
PImage img;
PImage startscreen;
PGraphics pg;
boolean path = false;
Dots dotUpperLeftCorner;
Dots dotUpperRightCorner;
Dots dotLowerLeftCorner;
Dots dotLowerRightCorner;
void setup()
{
  background(0);

  size(723, 800);
  int y=40;
  int x = 40;
  dotUpperLeftCorner = new Dots(40, 40);
  dotUpperRightCorner = new Dots(683, 40);
  dotLowerLeftCorner = new Dots(40, 610);
  dotLowerRightCorner = new Dots(680,610);
  img = loadImage("psb2.jpg");
  pac = new Pacman(362, 610);
  pg = createGraphics(723, 800);

  for (int i =0; i<28; i++)//long line left side
  {
    dots.add(new Dots(165, y+20)); 
    y+=23;
  }
  for(int i = 0;i<5;i++)
  {
    dots.add(new Dots(x,y-645));
    y+=22;
  }
  for(int i = 0; i<5;i++)
  {
    dots.add(new Dots(x,y-644));
    x+=26;
  }
  for(int i = 0;i<11;i++)
  {
    dots.add(new Dots(x-110,y-755));
    x+=26;
  }
  for(int i = 0;i<20;i++)
  {
    dots.add(new Dots(x-265,y-644));
    x+=26;
  }
   for(int i = 0;i<3;i++)
  {
    dots.add(new Dots(x-935,y-620));
    y+=26;
  }
   for(int i = 0;i<4;i++)
  {
    dots.add(new Dots(x-910,y-645.5));
    x+=26;
  }
  for(int i = 0;i<3;i++)
  {
    dots.add(new Dots(x-837,y-695));
    y+=26;
  }
  for(int i = 0;i<3;i++)
  {
    dots.add(new Dots(x-810,y-720));
    x+=26;
  }
  for(int i = 0;i<3;i++)
  {
    dots.add(new Dots(x-760,y-720));
    x+=26;
  }
  for(int i = 0;i<3;i++)
  {
    dots.add(new Dots(x-758,y-773));
    y+=26;
  }
  for(int i = 0;i<5;i++)
  {
    dots.add(new Dots(x-837,y-990));
    y+=23;
  }
  for(int i = 0;i<10;i++)
  {
    dots.add(new Dots(x-812,y-1105));
    x+=26;
  }
  for(int i = 0;i<4;i++)
  {
    dots.add(new Dots(x-811,y-1080));
    y+=23;
  }
  for(int i = 0;i<4;i++)
  {
    dots.add(new Dots(x-1175,y-1175));
    y+=23;
  }
  for(int i = 0;i<3;i++)
  {
    dots.add(new Dots(x-811,y-1155));
    y+=26;
  }
  for(int i = 0;i<4;i++)
  {
    dots.add(new Dots(x-915,y-1180));
    x+=26;
  }
  for(int i = 0;i<4;i++)
  {
    dots.add(new Dots(x-1046,y-1345));
    y+=23;
  }
  for(int i = 0;i<23;i++)
  {
    dots.add(new Dots(x-1044,y-1325));
    y+=23;
  }
  for(int i = 0;i<5;i++)
  {
    dots.add(new Dots(x-1015,y-1500));
    x+=25;
  }
  for(int i = 0;i<6;i++)
  {
    dots.add(new Dots(x-1320,y-1500));
    x+=25;
  }
  for(int i = 0;i<3;i++)
  {
    dots.add(new Dots(x-1470,y-1480));
    y+=23;
  }
  for(int i = 0;i<3;i++)
  {
    dots.add(new Dots(x-1555,y-1550));
    y+=23;
  }
  for(int i = 0;i<6;i++)
  {
    dots.add(new Dots(x-1685,y-1638));
    x+=26;
  }
  for(int i = 0;i<5;i++)
  {
    dots.add(new Dots(x-1990,y-1638));
    x+=26;
  }
  for(int i = 0;i<14;i++)
  {
    dots.add(new Dots(x-1970,y-1554));
    x+=26;
  }
  for(int i = 0;i<2;i++)
  {
   dots.add(new Dots(x-2484,y-1610));
    y+=26;
  }
  for(int i = 0;i<2;i++)
  {
    dots.add(new Dots(x-2460,y-1604));
    x+=26;
  }
  for(int i = 0;i<2;i++)
  {
   dots.add(new Dots(x-2485,y-1580));
    y+=26;
  }
  for(int i = 0;i<5;i++)
  {
    dots.add(new Dots(x-2535,y-1585));
    x+=26;
  }
  for(int i = 0;i<2;i++)
  {
   dots.add(new Dots(x-2665,y-1560));
    y+=26;
  }
  for(int i = 0;i<26;i++)
  {
    dots.add(new Dots(x-2665,y-1565));
    x+=26;
  }
  for(int i = 0;i<3;i++)
  {
   dots.add(new Dots(x-2692,y-1635));
    y+=26;
  }
  for(int i = 0;i<2;i++)
  {
   dots.add(new Dots(x-2978,y-1695));
    y+=26;
  }
  for(int i = 0;i<2;i++)
  {
   dots.add(new Dots(x-3065,y-1745));
    y+=26;
  }
  for(int i = 0;i<3;i++)
  {
    dots.add(new Dots(x-2976,y-1818));
    x+=26;
  }
  for(int i = 0;i<3;i++)
  {
    dots.add(new Dots(x-3196,y-1820));
    x+=26;
  }
  for(int i = 0;i<3;i++)
  {
   dots.add(new Dots(x-3063,y-1870));
    y+=26;
  }
  for(int i = 0;i<3;i++)
  {
   dots.add(new Dots(x-3294,y-1950));
    y+=26;
  }
  for(int i = 0;i<3;i++)
  {
   dots.add(new Dots(x-2910,y-2047));
    y+=26;
  }
  for(int i = 0;i<4;i++)
  {
    dots.add(new Dots(x-2964,y-2050));
    x+=28;
  }
  for(int i = 0;i<1;i++)
  {
    dots.add(new Dots(x-2994,y-2126));
    x+=28;
  }
  for(int i = 0;i<2;i++)
  {
   dots.add(new Dots(x-2995,y-2180));
    y+=28;
  }

}



void draw()
{
  stroke(0);
  image(img, 0, 0);
  fill(255);
  pg.beginDraw();
  pg.background(0);
  pg.stroke(255);
  pg.strokeWeight(21.0);
  pg.line(40, 40, 40, 225);//pathway
  pg.line(40, 225, 165, 225);//pathway
  pg.line(40, 40, 325, 40);//pathway
  pg.line(165, 40, 165, 685);
  pg.line(165, 685, 40, 685);
  pg.line(40, 685, 40, 755);
  pg.line(40, 755, 690, 755);
  pg.line(690, 755, 690, 685);
  pg.line(690, 685, 560, 685);
  pg.line(560, 685, 560, 40);
  pg.line(400, 40, 690, 40);
  pg.line(400, 40, 400, 145);
  pg.line(690, 40, 690, 145);
  pg.line(690, 145, 40, 145);
  pg.line(325, 40, 325, 145);
  pg.line(245, 145, 245, 225);
  pg.line(245, 225, 320, 225);
  pg.line(320, 225, 320, 300);
  pg.line(250, 300, 480, 300);
  pg.line(250, 300, 250, 375);
  pg.line(480, 300, 480, 375);
  pg.line(480, 375, 800, 375);
  pg.line(250, 375, 0, 375);
  pg.line(250, 375, 250, 525);
  pg.line(40, 525, 320, 525);
  pg.line(40, 525, 40, 610);
  pg.line(320, 525, 320, 610);
  pg.line(320, 610, 165, 610);
  pg.line(40, 610, 90, 610);
  pg.line(90, 610, 90, 685);
  pg.line(320, 610, 560, 610);
  pg.line(475, 610, 475, 685);
  pg.line(475, 685, 405, 685);
  pg.line(405, 685, 405, 755);
  pg.line(320, 755, 320, 680);
  pg.line(320, 680, 250, 680);
  pg.line(250, 680, 250, 610);
  pg.line(400, 610, 400, 525);
  pg.line(400, 525, 680, 525);
  pg.line(680, 525, 680, 610);
  pg.line(680, 610, 630, 610);
  pg.line(630, 610, 630, 685);
  pg.line(480, 375, 480, 525);
  pg.line(480, 455, 250, 455);
  pg.line(400, 300, 400, 225);
  pg.line(400, 225, 480, 225);
  pg.line(480, 225, 480, 145);
  pg.line(690, 145, 690, 220);
  pg.line(690, 220, 560, 220);
  pg.endDraw();
 //image(pg, 0, 0);


  for (int i =0; i<dots.size (); i++)
  {
    dots.get(i).display();
  }

  dotUpperLeftCorner.displayBigDots();
  dotUpperRightCorner.displayBigDots();
  dotLowerLeftCorner.displayBigDots();
  dotLowerRightCorner.displayBigDots();
  pac.keyPressed();

}
Tagged:

Answers

  • What condition must be true for Pacman to eat a dot?

  • @TfGuyy44 are you referring to the isVisible variable? or in general

  • In general.

  • @TfGuy44 If the pacman object touches the dots then the dots become black

    public void pacEatsDots()
    {
      if(xpac == x && ypac == y)//x and y are the positions of the dots, xpac and ypac are the position of Pacman
      {
        for (int i =0; i<dots.size (); i++)
      {
        fill(0);
        dots.get(i).display();
      }
      }
    }
    

    i thought itd be something like this but it didn't work...

  • Simplify your problem.

    Forget about making Pacman eat ALL the dots.

    Let's consider ONE Pacman trying to eat ONE dot.

    You've got the idea to compare their positions. This is good, but do the positions really need to be EXACTLY the same?

    What happens to a dot when it is eaten?

  • edited June 2017

    @TfGuy44 the dots should "disappear" or turn from yellowish to black right? and if they arent the same position then how could i make it disappear?

  • edited June 2017
      for (int i =0; i<28; i++)//long line left side, this makes some of the dots
      {
        dots.add(new Dots(x+125, y+20)); 
        y+=23;
    if(xpax == x && ypac == y)
    {
    fill(0);
    }
      }
    

    this doesnt seem right but something like this??

  • edited June 2017

    Stop trying to work out the steps by using code.

    Work out the steps first, in English, then write the code.

    Consider:

    -- PROCESS TO DETERMINE WHICH DOTS ARE EATEN --
    Do this for every dot:
      See how far that dot is from Pacman's position.
      If they are close,
        mark this dot as eaten.
      That's all you have to do if the are close.
    That's all you need to do for every dot.
    -- END OF PROCESS --
    NOTE: This process should happen every frame.
    

    Now convert that logic into code. Make it as its own function!

  • @TfGuy44 so i understand this except for the frame, I don't really have something that keeps track of each frame, what do you mean by frame?

  • Each time draw() runs it generates one frame - or image - of animation for your sketch. Basically, I mean that this process should happen once every time draw() runs.

    (Note: Some games may use their own time-step that is not coupled with draw(). In that case, this sort of thing should happen each time the minimum time interval for your game "engine" is simulated.)

  • edited June 2017 Answer ✓

    i think there are more fundamental things wrong with the above code. specifically this

    ArrayList<Dots> dots = new ArrayList();
    

    which means you have to iterate through the entire list every move, just to see if you've eaten another dot. using a grid and an underlying 2d array has got to be a better option.

Sign In or Register to comment.