There's a much simpler way to build our Pacman using transform() and rotate(), but we have to draw our packman with it's center at 0,0
PROCESSING
Custom Two-Directional Pacman Example Improved
// Better Pacman
// Simplified movement with transform() and rotate()
int myRadius = 50;
float xVal = myRadius; // Start at left edge of screen
float mySpeed = 2.5; // Positive value to go from left to right
float mouthAngle1 = 330;
float mouthAngle2 = 30;
float mouthSpeed = 2;
void setup() {
size(500 ,200);
ellipseMode(RADIUS);
fill(255,255,0);
}
void draw(){
background(0);
xVal += mySpeed; // Move pacman
translate(xVal,height/2);
// Rotate pacman 180 degrees if it's going left
if (mySpeed < 0) {
rotate(radians(180));
}
// Draw pacman
arc(0, 0, myRadius, myRadius, radians(mouthAngle2), radians(mouthAngle1));
// Increment and decrement the mouth angle
// Top goes from 330-360 (additive), bottom from 30-0 (subtractive)
mouthAngle1 += mouthSpeed;
mouthAngle2 -= mouthSpeed;
// Animate mouth
if (mouthAngle1 >= 360 || mouthAngle1 <= 320) {
mouthSpeed = -mouthSpeed;
}
// Switch direction when you hit an edge
if (xVal >= width-myRadius || xVal <= myRadius) {
mySpeed = -mySpeed; // invert speed from + to -
}
}