PacMan moving left and right with chomp, not game

Hello. Coding student here. Trying to understand how to make a Pacman character move across the screen while chomping. Once he hits the edge of the window, turns around and goes back the other way.
This is one example of what I have cobbled together so far, and yes it was pulled from the Processing book. Bear with me, I am trying to understand all of this…

float open = 0.05;

void setup() {
size(500, 150);
ellipseMode(RADIUS);
}

void draw() {
background(200, 20, 200);
x += speed * direction;
if ((x > width-radius) || (x < radius)) {
direction = -direction; // Flip direction
}
fill(254, 255, 0);
mouth0 += open;
if (mouth0 > .6 || mouth0 < .05){
open = open * -1;
}

if (direction == 1){
arc(x, 60, radius, radius, mouth0, TWO_PI -mouth0); // Face right

} else{

arc(x, 60, radius, radius,3.67, 8.9);  // Face left

}}

My other code I like better, because it is very short and works well, but same issue…how to turn around and chomp both ways:

int radius = 40;
float mouth = 0;
float x = 0;
float y = 2;
float xspeed = 2;
float open = 0.05;

void setup(){
size(300, 200);
ellipseMode(RADIUS);//draws circle from center of shape not edge
}

void draw(){
background(12, 7, 167);

x = x + xspeed;//makes ball move forward to the right   
if(x > width + radius || x < 0 -radius){
  xspeed = xspeed * -1;
  //should make direction change
  //this makes ball go to very edge of screen before bouncing back, which I wanted
}
fill(252, 252, 7);

mouth += open;
if (mouth > .6 || mouth < .05){
open = open * -1;
}
arc(x, 100, radius, radius, mouth, TWO_PI - mouth);
}

Help?

1 Like

Haha, I thought you mean jump instead of chomp!

:wink:

you have to combine the two Sketches

here is an example:

  • the key idea is that the variable direction knows in which direction we go, left or right (1 or 2, I made two constants for this - better readable)

  • depending on direction we draw a pac man facing left or right

  • Both directions use the variables mouth and open but have other angles in their arc() command

Chrisir

int radius = 40;
float mouth = 0;
float x;
float y = 2;
float xspeed = 2;
float open = 0.05;

final int directionRight = 1;   // a constant 
final int directionLeft  = 2; 
int direction=directionRight; 

void setup() {
  size(300, 200);

  x=width/2;

  ellipseMode(RADIUS); // draws circle from center of shape not edge
}

void draw() {
  background(12, 7, 167);

  x = x + xspeed;//makes ball move forward to the right   

  //should make direction change
  //this makes ball go to very edge of screen before bouncing back, which I wanted
  if (x > width - radius) {
    xspeed = abs(xspeed) * -1; // always neg 
    direction=directionLeft;
    mouth = 0;
    open = 0.05;
  } 

  if (x < radius-4) {
    xspeed = abs(xspeed); // always pos 
    direction=directionRight;
    mouth = 0;
    open = 0.05;
  }

  fill(252, 252, 7);

  mouth += open;

  if (mouth > .6 || mouth < .05) {
    open = open * -1;
  }

  if (direction == directionRight) {
    //right
    arc(x, 100, 
      radius, radius, 
      mouth, TWO_PI - mouth);
  } else if (direction == directionLeft) {
    // left
    arc(x, 100, 
      radius, radius, 
      radians(180) + mouth, radians(180) + TWO_PI - mouth);
  } else {
    println("Error");
  }
  //
}//func 
//

I have been staring at these two codes that make Pac move and chomp, feeling that the answer did not have to be a page long. Especially when just getting him to chomp and smoothly go right was so simple (in the amount of code it took).
I couldn’t figure out how to write the code that would differentiate between left and right, thank you!
Questions:
What does “abs” mean?
Why width/2?
Why radians(180)?

I rewrote from your information, taking out the
x=width/2;
and “abs”.
Thank you! I had previously tried to write the code with a direction = direction option, but did not know how to word it for left, only going right. I had not thought about placing Right and Left in there.
I had the second arc using TWO_PI once before, but as I could not turn Pac around, the 2nd arc never worked.
Could you tell me how the radians(180) affects the code? I see that it is my same arc used twice. How does radians(180) make the difference?

Pac finally goes left and right, turning around, and chomping the whole way!
The Pac code now reads:
float x;
float y = 2;
float xspeed = 2;
float open = 0.05;

int directionRight = 1;
int directionLeft = 2;
int direction = directionRight;

void setup() {
size(300, 200);
ellipseMode(RADIUS);
}

void draw() {
background(12, 7, 167);
x = x + xspeed;
if (x > width + radius || x < 0 -radius) {
xspeed = xspeed * -1;
direction = directionLeft;
mouth = 0;
open = 0.05;
//pac goes off screen to turn around, which I want
}
if (x < radius -4) {
xspeed = xspeed;
direction = directionRight;
mouth = 0;
open = 0.05;
}

fill(252, 252, 7);
mouth += open;

if (mouth > .6 || mouth < .05) {
open = open * -1;
}
if (direction == directionRight) {
arc(x, 100, radius, radius, mouth, TWO_PI - mouth);
} else if (direction == directionLeft) {
arc(x, 100, radius, radius, radians(180) + mouth, radians(180) + TWO_PI - mouth);
}
}

A jumping Pacman would be very fun to watch!

1 Like

Great!

Yeah, abs() is the absolute of a number so just the number without the minus sign. So just to make sure it’s positiv.

radians() gets the radians of a degree value.

So radians of 180 is just PI

It’s just important that direction knows where we go;
you could also use a boolean here.

The constant directionRight is just for better readability; 1 and 2 would also work.

Besides, speedx also carries the information of the direction

It just starts the yellow part of pacman on the left side, instead of on the right. Check reference for arc() command

I thought that was what I had xspeed doing, cool.
So, the line translated reads:
arc(x, 100, radius, radius, PI + mouth, PI + TWO_PI - mouth); ?

I am a little confused. “=” means equal , and “==” also means equal?

It’s good to keep xspeed and direction separated though. For example, you’d check xspeed against being 1 and -1. Okay. Later you want to increase the xspeed to 3. Then the check won’t work anymore and pacman would have the wrong mouth position. An error hard to find.
Separate concerns.

I think so! Does it work?

Good question!

  • “=” means assign. So a = 3 says, give / set “a” the value 3.
  • “==” means comparison and is only used in if statements or statements that check a value.
    So if(a==3) doesn’t change “a” but asks whether “a” has the value 3 at the moment.

Chrisir