Need a help for this Pacmen code
in
Programming Questions
•
1 year ago
Hi y' all,
I'm coding a bunch of Pacmen which are supposed to "munch" while bouncing back and forth in the display window. The problem is most of them munch way to fast, while a few munch slower to very slow. Here's my code:
// Code starts here
Pacman[] pac = new Pacman[50];
void setup() {
size(400, 200);
smooth();
for(int i = 0; i < pac.length; i++) {
pac[i] = new Pacman();
}
}
void draw() {
background(0);
for(int i = 0; i < pac.length; i++) {
pac[i].display();
pac[i].go();
pac[i].munch();
}
}
// Pacman class block starts here
class Pacman {
float x;
float y;
float radius;
float speed;
color col;
int direction;
Pacman() {
radius = random(10, 30);
x = random(radius, width-radius);
y = random(height);
speed = random(1.0, 4.0);
colorMode(HSB, 360, 100, 100);
col = color(random(20, 120), 100, random(80, 100));
direction = 1;
}
void display() {
noStroke();
fill(col);
ellipseMode(RADIUS);
}
void go() {
x += speed * direction;
if ((x > width - radius) || (x < radius)) {
direction = -direction;
}
}
// I know the problem lies in here, just what did I miss?
void munch() {
int n = int(x); // Cast float x to int n
if (direction == 1) { // If pacman is going to the right,
if (n%2 == 0) { // and that n%2 is 0,
ellipse(x, y, radius, radius); // draw a pacman with mouth closed
}
else {
arc(x, y, radius, radius, radians(35), radians(325)); // Else, draw a pacman with mouth open to the right.
}
}
if (direction == -1) { // If pacman is going back to the left,
if (n%2 == 0) { // and that n%2 == 0,
ellipse(x, y, radius, radius); // draw a pacman with mouth closed.
}
else {
arc(x, y, radius, radius, radians(215), radians(505)); // Else, draw a pacman with mouth open to the left.
}
}
}
}
// This code ends here
Your kind help would be very much appreciated! Thanks in advance.
I'm coding a bunch of Pacmen which are supposed to "munch" while bouncing back and forth in the display window. The problem is most of them munch way to fast, while a few munch slower to very slow. Here's my code:
// Code starts here
Pacman[] pac = new Pacman[50];
void setup() {
size(400, 200);
smooth();
for(int i = 0; i < pac.length; i++) {
pac[i] = new Pacman();
}
}
void draw() {
background(0);
for(int i = 0; i < pac.length; i++) {
pac[i].display();
pac[i].go();
pac[i].munch();
}
}
// Pacman class block starts here
class Pacman {
float x;
float y;
float radius;
float speed;
color col;
int direction;
Pacman() {
radius = random(10, 30);
x = random(radius, width-radius);
y = random(height);
speed = random(1.0, 4.0);
colorMode(HSB, 360, 100, 100);
col = color(random(20, 120), 100, random(80, 100));
direction = 1;
}
void display() {
noStroke();
fill(col);
ellipseMode(RADIUS);
}
void go() {
x += speed * direction;
if ((x > width - radius) || (x < radius)) {
direction = -direction;
}
}
// I know the problem lies in here, just what did I miss?
void munch() {
int n = int(x); // Cast float x to int n
if (direction == 1) { // If pacman is going to the right,
if (n%2 == 0) { // and that n%2 is 0,
ellipse(x, y, radius, radius); // draw a pacman with mouth closed
}
else {
arc(x, y, radius, radius, radians(35), radians(325)); // Else, draw a pacman with mouth open to the right.
}
}
if (direction == -1) { // If pacman is going back to the left,
if (n%2 == 0) { // and that n%2 == 0,
ellipse(x, y, radius, radius); // draw a pacman with mouth closed.
}
else {
arc(x, y, radius, radius, radians(215), radians(505)); // Else, draw a pacman with mouth open to the left.
}
}
}
}
// This code ends here
Your kind help would be very much appreciated! Thanks in advance.
1