Need help coding pacman for university

Can someone help me with creating a processing program that animates PacMan that can move left to right across the screen and can change directions if the spacebar is pressed. and appear from the other side once it reaches one side in the center of a track made with two blue lines that are 10 pixels in width in a black 800 by 200 pixel screen. Thank you

Hello @Hellafraid! We’re happy to help, but it’s important for you to first try solving the issue yourself. Please share your attempts so the community can guide you towards the solution. And if you haven’t already, make sure to read our guidelines for asking questions.

1 Like

Here is my code so far

int pacSize = 10;
int direction = 1; 
int direction2 = 0;
float x = 20;
float  y = 300;
float speed = 1.5;
boolean halt = true;


void setup() {
  size(800, 200);
  smooth();
  noStroke();
  ellipseMode(RADIUS);
 drawTrack();
 
 }
  void drawTrack() {
    stroke(0, 0, 255); // Blue color for track
  strokeWeight(10);
  line(0, height / 2, width, height / 2); // Draw upper track
  line(0, height / 2 + 30, width, height / 2 + 30); // Draw lower track
}



void draw() {
  simulate();
  render();                                                
}
void simulate() {
  if ( !halt) {
    x += speed * direction;
    y += speed * direction2;
    x = (x + width) % width;
    y = (y + height) % height;
  }

}
void render() { 
  background(0);
  fill(255, 255, 0);
  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, pacSize, pacSize, map((millis() % 500), 0, 500, 0, 0.52), map((millis() % 500), 0, 500, TWO_PI, 5.76) );
      popMatrix();
    }
  }
}

void keyPressed() {
  if (keyCode == 32) { // Spacebar
    direction *= -1; // Change direction
      direction = 1;
      direction2 = 0;
      halt = false;
  }
}
void keyReleased() {
      if (keyCode == 32) { // Spacebar
    direction *= 1; // Change direction
      direction = -1;
      direction2 = 0;
      halt = false;
      }
}

I am having troubles trying to separate the track from pac-man as the “Fill” command also goes onto him. Also cant figure out how to make it so i only need to press the spacebar once to change directions.

I am surprised you draw track in setup since you use background later.
You can use drawTrack after background

You can use the fill command multiple times,
once before you draw the tracks and
once before you draw pacman itself

1 Like