Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hello I made a game of pac man and the ghost models i used were not good enough. My teacher gave me this code to

hello I made a game of pac man and the ghost models i used were not good enough. My teacher gave me this code to replace the 2 ghost models but I dont know how to add it to my code.
MODEL FOR THE TWO GHOSTS:
PShape ghost, eyes;
void setup(){
size(500,200);
ghost=loadShape("ghost.svg");
eyes=loadShape("eyes.svg");
}
void draw(){
//Draw default svg ghost to left with reduced-size eyes on top
ghost.enableStyle();
shape(ghost,0,0);
shape(eyes,50,30,100,100);
//Disable style of ghost and draw same ghost and eyes to right
ghost.disableStyle();
shape(ghost,150,0);
shape(eyes,200,30,100,100);
//Change fill to red and draw same ghost and eyes to right
fill(255,0,0);
shape(ghost,300,0);
shape(eyes,350,30,100,100);
noFill();
}
GAME CODE:
int pacX, pacY; // Position of Pac-Man
int pacSize =40; // Size of Pac-Man
int dotSize =15; // Size of dots
int dotSpacing =50; // Spacing between dots
int numDots =20; // Number of dots
int score =0; // Score
int ghost1X, ghost1Y; // Position of Ghost 1
int ghost2X, ghost2Y; // Position of Ghost 2
int ghostSpeed =1; // Ghosts' movement speed
boolean mouthOpen = true; // Controls Pac-Man's chomping animation
void setup(){
size(1000,1000);
pacX = width /2;
pacY = height /2;
// Initialize ghost positions
ghost1X = width /4;
ghost1Y = height /2;
ghost2X =3* width /4;
ghost2Y = height /2;
}
void draw(){
background(0);
// Draw dots
for (int i =0; i < numDots; i++){
int dotX = i * dotSpacing + dotSpacing /2;
int dotY = height /2;
fill(250);
ellipse(dotX, dotY, dotSize, dotSize);
}
// Draw Ghosts
fill(300,0,0); // Red color for Ghost 1
ellipse(ghost1X, ghost1Y, pacSize, pacSize);
fill(0,0,300); // Blue color for Ghost 2
ellipse(ghost2X, ghost2Y, pacSize, pacSize);
// Draw Pac-Man with chomping animation
fill(255,255,0);
arc(pacX, pacY, pacSize, pacSize, mouthOpen ?0.2 : 0.1, PI *1.8);
// Chomping animation
if (frameCount %30==0){
mouthOpen =!mouthOpen;
}
// Moves Pac-Man
pacX =(pacX +1)% width;
// Moves Ghosts towards Pac-Man
if (ghost1X < pacX){
ghost1X += ghostSpeed;
} else {
ghost1X -= ghostSpeed;
}
if (ghost2X < pacX){
ghost2X += ghostSpeed;
} else {
ghost2X -= ghostSpeed;
}
// Checks for collisions
checkCollision();
// Shows score
displayScore();
}
void checkCollision(){
for (int i =0; i < numDots; i++){
int dotX = i * dotSpacing + dotSpacing /2;
int dotY = height /2;
float distance = dist(pacX, pacY, dotX, dotY);
// Check collision with PacP-Man
if (distance < pacSize /2+ dotSize /2){
score++;
}
// Check collision with Ghosts
float ghost1Distance = dist(pacX, pacY, ghost1X, ghost1Y);
float ghost2Distance = dist(pacX, pacY, ghost2X, ghost2Y);
if (ghost1Distance < pacSize /2|| ghost2Distance < pacSize /2){
// Game over text
println("Game Over! Your score: "+ score);
noLoop(); // Stop the draw loop
}
}
}
// Allows player to control the Pac-man with the 4 basic arrow keys
void keyPressed(){
if (keyCode == UP){
pacY -=10; // Shift Pac-Man up
} else if (keyCode == DOWN){
pacY +=10; // Shift Pac-Man down
} else if (keyCode == LEFT){
pacX -=10; // Shift Pac-Man left
} else if (keyCode == RIGHT){
pacX +=10; // Shift Pac-Man right
}
}
// Shows score
void displayScore(){
fill(255);
textSize(20);
text("Score: "+ score, 20,30);
}
(This is using processing code)

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_2

Step: 3

blur-text-image_3

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

Data Infrastructure For Medical Research In Databases

Authors: Thomas Heinis ,Anastasia Ailamaki

1st Edition

1680833480, 978-1680833485

More Books

Students also viewed these Databases questions

Question

The company has fair promotion/advancement policies.

Answered: 1 week ago