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?