Move PacMan Using Arrow Key

Hello, here is my attempt at it any help would be much appreciated.

//The variables that control everything known about the pacman
float pacmanX, pacmanY;         //The position of the pacman
final int PACMAN_DIAMETER = 50; //The diameter of the pacman
float pacmanAngle = 0.0;        //The direction the pacman is facing
float pacmanMouth = 0.0;        //The angle that the mouth is open

int directionX = 0;
int directionY = 0;

int speed = 3;

void setup() {
  size(500, 500);
  pacmanX = width/2;  //Start the pacman in the
  pacmanY = height/2; //centre of the canvas

void draw() {
  background(0); //draw a fresh frame each time
  movePacman();    //Move the pacman toward the mouse
  turnPacman();    //Turn it to face the mouse
  animateMouth();  //Make the mouth open and close
  drawPacman();    //And draw it

void drawPacman() {
  fill(255, 255, 0); //yellow pacman
  stroke(0);       //with a black outline
  strokeWeight(2); //that's a little thicker

    pacmanAngle+pacmanMouth/2, pacmanAngle+TWO_PI-pacmanMouth/2, PIE);

void animateMouth() {
  //This function changes the pacmanMouth variable so that it slowly
  //increases from 0 to PACMAN_MAX_MOUTH, then snaps closed to 0 again.
  final float PACMAN_MOUTH_SPEED = 0.08;
  final float PACMAN_MAX_MOUTH = 1.5;
  //Increase the mouth opening each time, but snap it shut at the maximum
  pacmanMouth = (pacmanMouth + PACMAN_MOUTH_SPEED)%PACMAN_MAX_MOUTH;

void movePacman() {
  //Change the pacman's position (paxmanX and pacmanY) by 1/Nth
  //of the distance from the pacman to the mouse, in both X and Y.
  // int N = 20; //Try moving 1/20th of the way each frame.
  // float xChange = (mouseX-pacmanX)/N;
  // float yChange = (mouseY-pacmanY)/N;
  //Move the pacman by those amounts.
  //  pacmanX = pacmanX + xChange;
  // pacmanY = pacmanY + yChange;
  pacmanX += speed;

  if (pacmanX > width) {  
    pacmanX = -PACMAN_DIAMETER;
  if (pacmanY > height) { 
    pacmanY = -PACMAN_DIAMETER;

void turnPacman() {
  //Set the pacmanAngle variable to be the angle from the pacman to the mouse
  //The atan2() function can be found in the Processing reference pages
  // pacmanAngle = atan2(pacmanY,pacmanX);

void keyPressed () {
  if (key == CODED);
  if (keyCode == RIGHT) {
    directionX += 0 ;

    if (keyCode == DOWN) {
      directionY += 1 ;
    if (keyCode == LEFT) {
      directionX += 2 ;
    if (keyCode == UP) {
      directionY += 3 ;

type or paste code here

I think the idea is that you have just ONE direction variable, and that one variable has the value of either 0, 1, 2, or 3. It will be 0 if Pacman is moving right. Or 1 if he is moving up. For left, the value will be 2. And for down, 3.

When a key press occurs, you will set the direction variable to the right value.

When you move Pacman, you will move him in one of four directions based on the value in that variable.


Take a close look at your brackets:

void keyPressed () 
  if (key == CODED);
  if (keyCode == RIGHT) 
    directionX += 0 ;

    if (keyCode == DOWN) 
      directionY += 1 ;
    if (keyCode == LEFT) 
      directionX += 2 ;
    if (keyCode == UP) 
      directionY += 3 ;

I lined them up so you can see the problem.

This line does not execute anything based on condition because you terminated with a semi-colon:
if (key == CODED);


You could add println() statements for debugging your code to see the status of your variables and to help understand the flow of your code.



Thank you, so should i initialize direction to a variable or assign it when i am putting it in keyPress.

Thank you ! but is the code the make the pacman move based on the arrow keys correct ?


What is the source of this code that you are asking about?

Do you have permission to post it to a public forum?

yes it is the code i posted previously