Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have an issue with my code, It is about pac man but the ghosts wont follow the pacman and there are not enough dots

I have an issue with my code, It is about pac man but the ghosts wont follow the pacman and there are not enough dots on the screen. After i get a few dots, they do not respawn and the game does not end when the pac man colides with the ghosts despite me making a code for it.
here is my code for the game:
PShape ghost1, ghost2, eyes1, eyes2;
int pacX, pacY;
int pacSize =40;
int dotSize =15;
int dotSpacing =50;
int numDots =20;
int score =0;
int ghost1X, ghost1Y;
int ghost2X, ghost2Y;
int ghostSpeed =1;
boolean mouthOpen = true;
void setup(){
size(1000,1000);
pacX = width /2;
pacY = height /2;
ghost1X = width /4;
ghost1Y = height /2;
ghost2X =3* width /4;
ghost2Y = height /2;
ghost1= loadShape("ghost.svg");
eyes1= loadShape("eyes.svg");
ghost2= loadShape("ghost.svg");
eyes2= loadShape("eyes.svg");
}
void draw(){
background(0);
for (int i =0; i < numDots; i++){
int dotX = i * dotSpacing + dotSpacing /2;
int dotY = height /2;
fill(250);
ellipse(dotX, dotY, dotSize, dotSize);
}
drawGhost(ghost1, eyes1, ghost1X, ghost1Y);
drawGhost(ghost2, eyes2, ghost2X, ghost2Y);
fill(255,255,0);
arc(pacX, pacY, pacSize, pacSize, mouthOpen ?0.2 : PI *1.8, mouthOpen ? TWO_PI -0.2 : PI *2.2);
if (frameCount %30==0){
mouthOpen =!mouthOpen;
}
moveGhostTowardsPacman(ghost1X, ghost1Y);
moveGhostTowardsPacman(ghost2X, ghost2Y);
checkCollision();
displayScore();
}
void drawGhost(PShape ghost, PShape eyes, int x, int y){
ghost.enableStyle();
shape(ghost, x, y);
shape(eyes, x +50, y +30);
ghost.disableStyle();
shape(ghost, x +150, y);
shape(eyes, x +200, y +30);
fill(255,0,0);
shape(ghost, x +300, y);
shape(eyes, x +350, y +30);
noFill();
}
void moveGhostTowardsPacman(int ghostX, int ghostY){
if (ghostX < pacX){
ghostX += ghostSpeed;
} else {
ghostX -= ghostSpeed;
}
}
void checkCollision(){
if (dist(pacX, pacY, ghost1X, ghost1Y)< pacSize /2){
gameOver();
}
if (dist(pacX, pacY, ghost2X, ghost2Y)< pacSize /2){
gameOver();
}
for (int i =0; i < numDots; i++){
int dotX = i * dotSpacing + dotSpacing /2;
int dotY = height /2;
if (dist(pacX, pacY, dotX, dotY)< pacSize /2){
score++;
numDots--;
}
}
}
void gameOver(){
println("Game over!");
}
void displayScore(){
fill(255);
textSize(20);
text("Score: "+ score, 20,30);
}
void keyPressed(){
if (keyCode == UP){
pacY -=10;
} else if (keyCode == DOWN){
pacY +=10;
} else if (keyCode == LEFT){
pacX -=10;
} else if (keyCode == RIGHT){
pacX +=10;
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions