Computer Science Canada

PacMan Help

Author:  Panphobia [ Mon Apr 01, 2013 8:08 pm ]
Post subject:  PacMan Help

I have been trying to make pacman in processing lately, just trying to get the pacman chomping in the direction of where he is headed, one thing that I am having trouble with is when the pacman stops moving he does not stay in the direction of where he was headed here is my code
Java:
int radius = 50, directionX = 1, directionY = 1;
float y = 250, x = 250, speed = 3;
float rotation = .1;
float limits1=0.7, limits2=5.3;
float stop=limits2, start=limits1;
boolean isMove = true;
float up=-4, down=4, left=-4, right=4;
float xDir=0, yDir;
boolean is = false;
int[]array=new int[8];
void setup()
{
  size (800, 600);
  noStroke();
  smooth();
  fill(255, 255, 0);
  ellipseMode(RADIUS);
  // frameRate(20);
}


void draw()
{
  background(0);

  stop+=rotation;
  start-=rotation;
  if (stop<=limits2||stop>limits2+0.7) {
    rotation *=-1;
  }
  x+=xDir;
  y+=yDir;
  if (x>width)x=0;
  if (x<0)x=width;
  if (y<0)y=height;
  if (y>height)y=0;
//    if (w && n) {
//      //start = 0.7-2*PI/3;
//      //stop = 5.3-2*PI/3;
//      arc(x, y, radius, radius, start-2*PI/3, stop-2*PI/3);
//    }
//    else if (w && s) {
//      //start = 0.7-2*PI/3-PI/2;
//     // stop = 5.3 -2*PI/3-PI/2;
//      arc(x, y, radius, radius, start -2*PI/3-PI/2, stop -2*PI/3-PI/2);
//    }
//    else if (e && s) {
//      //start =0.7-2*PI/3-PI;
//      //stop = 5.3-2*PI/3-PI;
//       arc(x, y, radius, radius, start-2*PI/3-PI, stop-2*PI/3-PI);
//    }
//    else if (e && n) {
//      //stop = 5.3-PI/3+0.5;
//      //start = 0.7-PI/3+0.5;
//       arc(x, y, radius, radius, start-PI/3+0.5, stop-PI/3+0.5);
//    }
    if (xDir == -4 && yDir == 0) {
     //start = 0.7-PI;
     // stop = 5.3-PI;
       arc(x, y, radius, radius, start-PI, stop-PI);
    }
    else if (xDir == 0 && yDir == -4) {
      //start=0.7-PI/2;
      //stop = 5.3-PI/2;
       arc(x, y, radius, radius, start-PI/2+0.1, stop-PI/2+0.1);
    }
    else if (xDir == 0 && yDir == 4) {
     // start=0.7+PI/2+0.1;
     // stop =5.3+PI/2+0.1;
      arc(x, y, radius, radius, start+PI/2+0.1, stop+PI/2+0.1);
 
   }else if((xDir==0&&yDir==0)||(xDir==4&&yDir==0)){
     arc(x, y, radius, radius, start, stop);
   }
}
void keyPressed() {
  switch(key) {
  case'w':
  case'W':
    yDir=up;
    xDir=0;
    break;
  case'a':
  case'A':
    xDir=left;
    yDir=0;
    break;
  case's':
  case'S':
    yDir=down;
    xDir=0;
    break;
  case'd':
  case'D':
    xDir=right;
    yDir=0;
    break;
  }
}
void keyReleased() {
  switch(key) {
  case'w':
  case'W':
    yDir=0;
    break;
  case'a':
  case'A':
    xDir=0;
    break;
  case's':
  case'S':
    yDir=0;
    break;
  case'd':
  case'D':
    xDir=0;
    break;
  }
}
now I know why it is not chomping in that direction now, but when I tryed to set the start and stop positions of the arc angle depending on where I am going and then draw the arc outside of the if statement, it basically freezes in that position, and I know that that is because it keeps resetting the value of the arc angle, but how would I make the pacman stay chomping in the same direction as it was moving when it stops, without this problem? and also whenever you move lets say left right quickly it kind of jerks and doesnt move for maybe 30 frames.

Author:  nullptr [ Mon Apr 01, 2013 11:04 pm ]
Post subject:  Re: PacMan Help

One way to do it is to store your previous direction and draw using that instead. In each frame you could put:

Java:

if (xDir != 0 || yDir != 0)
{
    prevX = xDir;
    prevY = yDir;
}


Then, in your draw() method, you would use prevX and prevY instead of xDir and yDir.

For the other problem, it depends how you're determining whether or not to draw the arc. From just looking at your code, it seems like you'll always draw an arc, unless I'm missing something, so there'd be no chomping at all.

Author:  Panphobia [ Tue Apr 02, 2013 1:48 am ]
Post subject:  RE:PacMan Help

Yea thanks, I got the game working, now all I need to do is add the walls.


: