Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hello, I have two codes and i want to make them run on the same scetch using processing. When the pac man game ends, it

hello, I have two codes and i want to make them run on the same scetch using processing. When the pac man game ends, it should go to the clock animation. this is homework i am struggling on.
pac-man 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 *struggled to add ghosts.svg and eyes.svg because they froze and did not move so I had to remove it.
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 Pac-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);
}
clock code:
void setup(){
size(1000,1000);
smooth();
}
void draw(){
background(255);
translate(width/2, height/2);
drawClock();
}
void drawClock(){
// Draw clock face
noFill();
strokeWeight(2);
ellipse(0,0,300,300);
// Calculate angles for hour, minute, and second hands
float hoursAngle = map(hour()%12,0,12,0, TWO_PI)- HALF_PI;
float minutesAngle = map(minute(),0,60,0, TWO_PI)- HALF_PI;
float secondsAngle = map(second(),0,60,0, TWO_PI)- HALF_PI;
// hour hand
strokeWeight(4);
line(0,0,50* cos(hoursAngle),50* sin(hoursAngle));
// minute hand
strokeWeight(3);
line(0,0,70* cos(minutesAngle),70* sin(minutesAngle));
// second hand
stroke(80,0,0); // Brown color for seconds hand
strokeWeight(2);
line(0,0,90* cos(secondsAngle),90* sin(secondsAngle));
// center point
fill(0);
ellipse(0,0,10,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_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

Database And Expert Systems Applications 15th International Conference Dexa 2004 Zaragoza Spain August 30 September 3 2004 Proceedings Lncs 3180

Authors: Fernando Galindo ,Makoto Takizawa ,Roland Traunmuller

2004th Edition

3540229361, 978-3540229360

More Books

Students also viewed these Databases questions

Question

2. What are the components of IT infrastructure?

Answered: 1 week ago