Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hi , I have an error in my Processing code can you please help identify the error? code: int currentAnimation = 0 ; / /

hi, I have an error in my Processing code can you please help identify the error?
code:
int currentAnimation =0; // Variable to keep track of the current animation
float animationStartTime; // Variable to store the start time of each animation
void setup(){
size(400,400);
animationStartTime = millis(); // Initialize the start time
}
void draw(){
background(255);
// Check if it's time to switch to the next animation
if (millis()- animationStartTime >=30000){
currentAnimation++; // Move to the next animation
if (currentAnimation >2){
currentAnimation =0; // Reset to the first animation if we've reached the end
}
animationStartTime = millis(); // Reset the start time for the new animation
}
// Draw the current animation
switch (currentAnimation){
case 0:
drawAnimation1();
break;
case 1:
drawAnimation2();
break;
case 2:
drawAnimation3();
break;
}
}
void drawAnimation1(){
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);
}
}

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

Database Systems On GPUs In Databases

Authors: Johns Paul ,Shengliang Lu ,Bingsheng He

1st Edition

1680838482, 978-1680838480

More Books

Students also viewed these Databases questions

Question

Always have the dignity of the other or others as a backdrop.

Answered: 1 week ago