My Pacman Game issues

I have a problem about when I want to press a key, up, down, right and left, change the image of my Pacman, depending on its position, with different animated sprites, which are in different images, according to their position, but my Pacman character does not it is shown, and in my video game it does not show me any error in the console.

I need to fix this.

This is the code link of my videogame: https://editor.p5js.org/CodeOscar/sketches/HyfW-17g4

1 Like

Try using only == with your conditionals (if) instead of ===

Btw- on mobile so I can not test it. Just something that stuck out to me.

@CodeOscar in draw() you say pacman.animate(data, rightImage); , which will only animate the rightImage and ignore anything that happens inside the keyPressed function. I recommend making another image, imag or something and in function keyPressed do:

function keyPressed() {
 if (keyCode === RIGHT_ARROW) {
   	imag = rightImage;
   //pacman.animate(data, rightImage);
   pacman.dir(1, 0);
 } else if (keyCode === LEFT_ARROW) {
   	imag = leftImage;
   //pacman.animate(data, leftImage);
   pacman.dir(-1, 0);
 } else if (keyCode === UP_ARROW) {
   	imag = upImage;
   //pacman.animate(data, upImage);
   pacman.dir(0, -1);
 } else if (keyCode === DOWN_ARROW) {
   	imag = downImage;
   //pacman.animate(data, downImage);
   pacman.dir(0, 1);
 }
}

In draw you can now say: pacman.animate(data, imag); and it should update with that image
now it still does not animate properly, but that is because in the pacman.js on line 18:

      animation.push(img);

will only add extra images which makes the array of animations longer and longer. you need to clear that out.
Probably when you want to select another image (so in function keyPressed()).
I’m not a p5.js master so I can’t guarantee this will make it work, or tell you how to, but this is definitely some things that are wrong.

2 Likes

I would not have images being passed around as variables at all! Instead, make all your images globals, load them in setup, and then use conditional statements inside your pacman class to determine which way pacman is moving & use the appropriate image.

if( dx  > 0 ){
  image( pacRight, pacX, pacY );
} else if ( dx < 0 ){
  image( pacLeft, pacX, pacY );
} else // ... etc
1 Like

@TfGuy44 and @Whahahahaha Also if you want you can download my code for more details,

For download my code go to my code link of my videogame > File > Download

Pacman

1 Like

I did Pacman back in 2015. I have no need to download your version.

1 Like

@TfGuy44 Thank you very much for the link, I go to check the code, and execute them, for add features to my code :wink: