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.