# Need help coding pacman for university

**URL:** <https://discourse.processing.org/t/need-help-coding-pacman-for-university/44208>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [April 2, 2024, 8:43am UTC](https://discourse.processing.org/t/need-help-coding-pacman-for-university/44208 "2024-04-02T08:43:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hellafraid](https://avatars.discourse-cdn.com/v4/letter/h/ea666f/32.png) [@Hellafraid](https://discourse.processing.org/u/Hellafraid)\
**Post date:** [April 2, 2024, 8:43am UTC](https://discourse.processing.org/t/need-help-coding-pacman-for-university/44208/1 "2024-04-02T08:43:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ProcessingOrg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/processingorg/32/18341_2.png) [@ProcessingOrg](https://discourse.processing.org/u/ProcessingOrg)\
**Post date:** [April 2, 2024, 9:52am UTC](https://discourse.processing.org/t/need-help-coding-pacman-for-university/44208/2 "2024-04-02T09:52:03Z")

</div>

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](https://discourse.processing.org/t/guidelines-asking-questions/2147).

---

<div class="post-metadata">

**Author:** ![Hellafraid](https://avatars.discourse-cdn.com/v4/letter/h/ea666f/32.png) [@Hellafraid](https://discourse.processing.org/u/Hellafraid)\
**Post date:** [April 3, 2024, 4:45am UTC](https://discourse.processing.org/t/need-help-coding-pacman-for-university/44208/4 "2024-04-03T04:45:32Z")

</div>

Here is my code so far

```auto
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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 3, 2024, 8:35am UTC](https://discourse.processing.org/t/need-help-coding-pacman-for-university/44208/5 "2024-04-03T08:35:42Z")

</div>

> [@Hellafraid](#):
>
> trying to separate the track from pac-man as the “Fill” command

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
