Eric Xu / Mbed 2 deprecated Pac-Man

Dependencies:   4DGL-uLCD-SE PinDetect mbed

Fork of Pac-Man by Eric Xu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Pacman.h Source File

Pacman.h

00001 /******************************************************
00002  * Author: Yuanzhe Xu
00003  * Institution: Georgia Institute of Technology
00004  * Date: October 18, 2015
00005  *
00006  * This header file declares all the functions and
00007  * variables needed for the Pacman object. Specific
00008  * Pac-Man functionalities can be found in main.cpp.
00009  ******************************************************/
00010 
00011 // Pacman class
00012 class Pacman {
00013     public:
00014         // Pac-Man constructor
00015         Pacman(uLCD_4DGL& uLCD, Stage& stage)
00016             : uLCD(uLCD), stage(stage) {
00017         }
00018 
00019         // Initialize Pac-Man at the start of the game
00020         void initialize() {
00021             x = 64;
00022             y = 108;
00023             lives = 3;
00024             livesChanged = true;
00025             score = 0;
00026             scoreChanged = true;
00027             direction = FACEUP;
00028             mouthOpen = false;
00029             pacDotsEaten = 0;
00030             bigPacDotsEaten = 0;
00031             powerUp = 0;
00032             powerUpChanged = false;
00033             ghostsEaten = 0;
00034             dead = false;
00035             draw(x, y);
00036             displayStatus();
00037         }
00038 
00039         // Set the next direction
00040         void setNextDirection(int next) {
00041             nextDirection = next;
00042         }
00043 
00044         // Move Pac-Man and return whether the game is over
00045         bool move() {
00046             // If all the pac dots are eaten or Pac-Man is out of lives
00047             if (((pacDotsEaten == stage.pacDots) &&
00048                 (bigPacDotsEaten == stage.bigPacDots)) ||
00049                 (lives == 0)) {
00050                 // Return true that the game is over and exit out of the function
00051                 return true;
00052             }
00053 
00054             // If Pac-Man is not invincible
00055             if (powerUp == 0) {
00056                 // Reset the ghosts eaten counter
00057                 ghostsEaten = 0;
00058             }
00059 
00060             // If Pac-Man is invincible
00061             if (powerUp > 0) {
00062                 // Decrement the invincibility counter
00063                 powerUp--;
00064 
00065                 // If invincibility just ended or just started
00066                 if ((powerUp == 0) || (powerUp == 99)) {
00067                     // Changes the color of the ghosts
00068                     powerUpChanged = true;
00069                 // If invincibility did not just end or did not just start
00070                 } else {
00071                     // Set powerUpChanged to false
00072                     powerUpChanged = false;
00073                 }
00074             }
00075 
00076             // Move Pac-Man in his current direction
00077             switch (direction) {
00078                 case FACERIGHT:
00079                     moveRight();
00080                     break;
00081                 case FACEDOWN:
00082                     moveDown();
00083                     break;
00084                 case FACELEFT:
00085                     moveLeft();
00086                     break;
00087                 case FACEUP:
00088                     moveUp();
00089                     break;
00090                 default:
00091                     break;
00092             }
00093 
00094             // Return false that the game is over
00095             return false;
00096         }
00097 
00098         // Display score and number of lives remaining
00099         void displayStatus() {
00100             // If the number of lives just changed
00101             if (livesChanged == true) {
00102                 // Draw the corresponding number of lives
00103                 switch (lives) {
00104                     case 3:
00105                         drawLives(56, 12);
00106                         drawLives(64, 12);
00107                         drawLives(72, 12);
00108                         break;
00109                     case 2:
00110                         erase(72, 12);
00111                         drawLives(56, 12);
00112                         drawLives(64, 12);
00113                         break;
00114                     case 1:
00115                         erase(56, 12);
00116                         drawLives(64, 12);
00117                         break;
00118                     default:
00119                         break;
00120                 }
00121 
00122                 // Reset livesChanged
00123                 livesChanged = false;
00124             }
00125 
00126             // If the score just changed
00127             if (scoreChanged == true) {
00128                 // Print the new score
00129                 uLCD.locate(0, 1);
00130                 uLCD.printf("%d", score);
00131                 scoreChanged = false;
00132             }
00133 
00134             // Print the invincibility counter
00135             uLCD.locate(14, 1);
00136 
00137             if (powerUp == 100) {
00138                 uLCD.printf("100");
00139             } else if (powerUp >= 10) {
00140                 uLCD.printf(" %d", powerUp);
00141             } else {
00142                 uLCD.printf("  %d", powerUp);
00143             }
00144         }
00145 
00146         // Game over
00147         void gameOver() {
00148             // If all the pac dots have been eaten
00149             if ((pacDotsEaten == stage.pacDots) &&
00150                 (bigPacDotsEaten == stage.bigPacDots)) {
00151                 // Pac-Man wins
00152                 win();
00153             // If the number of lives is 0
00154             } else if (lives == 0) {
00155                 // Pac-Man loses
00156                 lose();
00157             }
00158         }
00159 
00160 
00161     private:
00162         // Current horizontal position
00163         int x;
00164         // Current vertical position
00165         int y;
00166         // Current direction
00167         int direction;
00168         // Next direction
00169         int nextDirection;
00170         // Number of lives
00171         int lives;
00172         // The lives display updates when livesChanged is true
00173         bool livesChanged;
00174         // Score
00175         int score;
00176         // The score display updates when scoreChanged is true
00177         bool scoreChanged;
00178         // Mouth open or closed for animation
00179         bool mouthOpen;
00180         // Number of pac dots eaten
00181         int pacDotsEaten;
00182         // Number of big pac dots eaten
00183         int bigPacDotsEaten;
00184         // Invincibility counter
00185         int powerUp;
00186         // Changes the color of the ghosts when powerUpChanged is true
00187         bool powerUpChanged;
00188         // Number of ghosts eaten
00189         int ghostsEaten;
00190         // Ghosts reset when dead is true
00191         bool dead;
00192         // uLCD display
00193         uLCD_4DGL& uLCD;
00194         // Stage reference
00195         Stage& stage;
00196         // Set Ghost as a friend class
00197         friend class Ghost;
00198 
00199         // Draw the Pac-Man to display lives
00200         void drawLives(int xCoordinate, int yCoordinate) {
00201             uLCD.filled_circle(xCoordinate, yCoordinate, 3, YELLOW);
00202             int x1 = xCoordinate - 2;
00203             int x2 = xCoordinate - 3;
00204             int y1 = yCoordinate - 1;
00205             int y2 = yCoordinate + 1;
00206             uLCD.rectangle(x1, y1, x2, y2, BLACK);
00207             uLCD.pixel(xCoordinate, yCoordinate, BLACK);
00208             uLCD.pixel(xCoordinate - 1, yCoordinate, BLACK);
00209         }
00210 
00211         // Draw Pac-Man based on his direction
00212         void draw(int xCoordinate, int yCoordinate) {
00213             // If his mouth is closed
00214             if (mouthOpen == false) {
00215                 // Draw Pac-Man with his mouth closed as a circle
00216                 uLCD.filled_circle(xCoordinate, yCoordinate, 3, YELLOW);
00217             // If his mouth is open
00218             } else {
00219                 // Draw Pac-Man with his mouth open based on his direction
00220                 int x1;
00221                 int x2;
00222                 int y1;
00223                 int y2;
00224 
00225                 switch (direction) {
00226                     case FACERIGHT:
00227                         uLCD.filled_circle(xCoordinate, yCoordinate, 3, YELLOW);
00228                         x1 = xCoordinate + 2;
00229                         x2 = xCoordinate + 3;
00230                         y1 = yCoordinate - 1;
00231                         y2 = yCoordinate + 1;
00232                         uLCD.rectangle(x1, y1, x2, y2, BLACK);            
00233                         uLCD.pixel(xCoordinate, yCoordinate, BLACK);
00234                         uLCD.pixel(xCoordinate + 1, yCoordinate, BLACK);
00235                         break;
00236                     case FACEDOWN:
00237                         uLCD.filled_circle(xCoordinate, yCoordinate, 3, YELLOW);
00238                         x1 = xCoordinate - 1;
00239                         x2 = xCoordinate + 1;
00240                         y1 = yCoordinate + 2;
00241                         y2 = yCoordinate + 3;
00242                         uLCD.rectangle(x1, y1, x2, y2, BLACK);
00243                         uLCD.pixel(xCoordinate, yCoordinate, BLACK);
00244                         uLCD.pixel(xCoordinate, yCoordinate + 1, BLACK);
00245                         break;
00246                     case FACELEFT:
00247                         uLCD.filled_circle(xCoordinate, yCoordinate, 3, YELLOW);
00248                         x1 = xCoordinate - 2;
00249                         x2 = xCoordinate - 3;
00250                         y1 = yCoordinate - 1;
00251                         y2 = yCoordinate + 1;
00252                         uLCD.rectangle(x1, y1, x2, y2, BLACK);
00253                         uLCD.pixel(xCoordinate, yCoordinate, BLACK);
00254                         uLCD.pixel(xCoordinate - 1, yCoordinate, BLACK);
00255                         break;
00256                     case FACEUP:
00257                         uLCD.filled_circle(xCoordinate, yCoordinate, 3, YELLOW);
00258                         x1 = xCoordinate - 1;
00259                         x2 = xCoordinate + 1;
00260                         y1 = yCoordinate - 2;
00261                         y2 = yCoordinate - 3;
00262                         uLCD.rectangle(x1, y1, x2, y2, BLACK);
00263                         uLCD.pixel(xCoordinate, yCoordinate, BLACK);
00264                         uLCD.pixel(xCoordinate, yCoordinate - 1, BLACK);
00265                         break;
00266                     default:
00267                         break;
00268                 }
00269             }
00270         }
00271 
00272         // Erase Pac-Man from the given position
00273         void erase(int xCoordinate, int yCoordinate) {
00274             uLCD.filled_circle(xCoordinate, yCoordinate, 3, BLACK);
00275         }
00276 
00277         // Move Pac-Man right
00278         void moveRight() {
00279             // If the space to the right is a valid positions and is not the rightmost space
00280             if ((stage.positions[x + 1][y] != 0) && (x != 124)) {
00281                 // Erase Pac-Man from his current position
00282                 erase(x, y);
00283                 // Increment Pac-Man's x-coordinate
00284                 x++;
00285                 // Draw Pac-Man in his new position
00286                 draw(x, y);
00287                 // Alternate Pac-Man's mouth for animation
00288                 mouthOpen = !mouthOpen;
00289 
00290                 // If Pac-Man passes partially across a big pac dot
00291                 if (stage.positions[x - 5][y] == 3) {
00292                     // Redraw the big pac dot part
00293                     uLCD.pixel(x - 4, y, YELLOW);
00294                 }
00295 
00296                 // Loop through Pac-Man's 7-pixel diameter
00297                 for (int i = x - 3; i <= x + 3; i++) {
00298                     // If a pac dot is eaten
00299                     if (stage.positions[i][y] == 2) {
00300                         // Increment the number of pac dots eaten
00301                         pacDotsEaten++;
00302                         // Add 10 to the score
00303                         score += 10;
00304                         // Set the coordinates to represent only a valid position
00305                         stage.positions[i][y] = 1;
00306                         // Changes the score display
00307                         scoreChanged = true;
00308                     // If a big pac dot is eaten
00309                     } else if (stage.positions[i][y] == 3) {
00310                         // Erase the rest of the big pac dot
00311                         uLCD.pixel(x + 4, y, BLACK);
00312                         // Increment the number of big pac dots eaten
00313                         bigPacDotsEaten++;
00314                         // Add 50 to the score
00315                         score += 50;
00316                         // Set the coordinates to represent only a valid position
00317                         stage.positions[i][y] = 1;
00318                         // Set the invincibility counter to 100
00319                         powerUp = 100;
00320                         // Changes the score display
00321                         scoreChanged = true;
00322                     }
00323                 }
00324             // If the space to the right is not a valid position
00325             } else if (stage.positions[x + 1][y] == 0) {
00326                 // Draw Pac-Man with his mouth open
00327                 uLCD.rectangle(x + 2, y - 1, x + 3, y + 1, BLACK);            
00328                 uLCD.pixel(x, y, BLACK);
00329                 uLCD.pixel(x + 1, y, BLACK);
00330                 wait(0.001);
00331             }
00332 
00333             // If the rightmost space is reached
00334             if (x == 124) {
00335                 // Erase Pac-Man from his current position
00336                 erase(x, y);
00337                 // Set Pac-Man's x-coordinate to 4
00338                 x = 4;
00339                 // Draw Pac-Man in his new positions
00340                 draw(x, y);
00341             }
00342 
00343             // Set Pac-Man's next direction
00344             switch (nextDirection) {
00345                 case FACEDOWN:
00346                     if (stage.positions[x][y + 1] != 0) {
00347                         direction = nextDirection;
00348                     }
00349 
00350                     break;
00351                 case FACELEFT:
00352                     if (stage.positions[x - 1][y] != 0) {
00353                         direction = nextDirection;
00354                     }
00355 
00356                     break;
00357                 case FACEUP:
00358                     if (stage.positions[x][y - 1] != 0) {
00359                         direction = nextDirection;
00360                     }
00361 
00362                     break;
00363                 default:
00364                     break;
00365             }
00366         }
00367 
00368         // Move Pac-Man down
00369         void moveDown() {
00370             // If the space in the downward direction is a valid position
00371             if (stage.positions[x][y + 1] != 0) {
00372                 // Erase Pac-Man from his current position
00373                 erase(x, y);
00374                 // Increment Pac-Man's y-coordinate
00375                 y++;
00376                 // Draw Pac-Man in his new position
00377                 draw(x, y);
00378                 // Alternate Pac-Man's mouth for animation
00379                 mouthOpen = !mouthOpen;
00380 
00381                 // If Pac-Man passes partially through a big pac dot
00382                 if (stage.positions[x][y - 5] == 3) {
00383                     // Redraw the big pac dot part
00384                     uLCD.pixel(x, y - 4, YELLOW);
00385                 }
00386 
00387                 // Loop through Pac-Man's 7-pixel diameter
00388                 for (int j = y - 3; j <= y + 3; j++) {
00389                     // If a pac dot is eaten
00390                     if (stage.positions[x][j] == 2) {
00391                         // Increment the number of pac dots eaten
00392                         pacDotsEaten++;
00393                         // Add 10 to the score
00394                         score += 10;
00395                         // Set the position to represent only a valid position
00396                         stage.positions[x][j] = 1;
00397                         // Changes the score display
00398                         scoreChanged = true;
00399                     // If a big pac dot is eaten
00400                     } else if (stage.positions[x][j] == 3) {
00401                         // Erase the rest of the big pac dot
00402                         uLCD.pixel(x, y + 4, BLACK);
00403                         // Increment the number of big pac dots eaten
00404                         bigPacDotsEaten++;
00405                         // Add 50 to the score
00406                         score += 50;
00407                         // Set the coordinates to represent only a valid position(1)
00408                         stage.positions[x][j] = 1;
00409                         // Set the invincibility counter to 100
00410                         powerUp = 100;
00411                         // Changes the score display
00412                         scoreChanged = true;
00413                     }
00414                 }
00415             // If the space in the downward direction is not a valid position
00416             } else {
00417                 // Draw Pac-Man with his mouth open
00418                 uLCD.rectangle(x - 1, y + 2, x + 1, y + 3, BLACK);
00419                 uLCD.pixel(x, y, BLACK);
00420                 uLCD.pixel(x, y + 1, BLACK);
00421                 wait(0.001);
00422             }
00423 
00424             // Set Pac-Man's next direction
00425             switch (nextDirection) {
00426                 case FACERIGHT:
00427                     if (stage.positions[x + 1][y] != 0) {
00428                         direction = nextDirection;
00429                     }
00430 
00431                     break;
00432                 case FACELEFT:
00433                     if (stage.positions[x - 1][y] != 0) {
00434                         direction = nextDirection;
00435                     }
00436 
00437                     break;
00438                 case FACEUP:
00439                     if (stage.positions[x][y - 1] != 0) {
00440                         direction = nextDirection;
00441                     }
00442 
00443                     break;
00444                 default:
00445                     break;
00446             }
00447         }
00448 
00449         // Move Pac-Man left
00450         void moveLeft() {
00451             // If the space to the left is a valid position and is not the leftmost space
00452             if ((stage.positions[x - 1][y] != 0) && (x != 4)) {
00453                 // Erase Pac-Man from his current position
00454                 erase(x, y);
00455                 // Decrement Pac-Man's x-coordinate
00456                 x--;
00457                 // Draw Pac-Man in his new position
00458                 draw(x, y);
00459                 // Alternate Pac-Man's mouth for animation
00460                 mouthOpen = !mouthOpen;
00461 
00462                 // If Pac-Man passes partially through a big pac dot
00463                 if (stage.positions[x + 5][y] == 3) {
00464                     // Redraw the big pac dot part
00465                     uLCD.pixel(x + 4, y, YELLOW);
00466                 }
00467 
00468                 // Loop through Pac-Man's 7-pixel diameter
00469                 for (int i = x + 3; i >= x - 3; i--) {
00470                     // If a pac dot is eaten
00471                     if (stage.positions[i][y] == 2) {
00472                         // Increment the number of pac dots eaten
00473                         pacDotsEaten++;
00474                         // Add 10 to the score
00475                         score += 10;
00476                         // Set the coordinates to represent only a valid position
00477                         stage.positions[i][y] = 1;
00478                         // Changes the score display
00479                         scoreChanged = true;
00480                     // If a big pac dot is eaten
00481                     } else if (stage.positions[i][y] == 3) {
00482                         // Erase the rest of the big pac dot
00483                         uLCD.pixel(x - 4, y, BLACK);
00484                         // Increment the number of big pac dots eaten
00485                         bigPacDotsEaten++;
00486                         // Add 50 to the score
00487                         score += 50;
00488                         // Set the coordinates to represent only a valid position
00489                         stage.positions[i][y] = 1;
00490                         // Set the invincibility counter to 100
00491                         powerUp = 100;
00492                         // Changes the score display
00493                         scoreChanged = true;
00494                     }
00495                 }
00496             // If the space to the left is not a valid position
00497             } else if (stage.positions[x - 1][y] == 0) {
00498                 // Draw Pac-Man with his mouth open
00499                 uLCD.rectangle(x - 2, y - 1, x - 1, y + 1, BLACK);
00500                 uLCD.pixel(x, y, BLACK);
00501                 uLCD.pixel(x - 1, y, BLACK);
00502                 wait(0.001);
00503             }
00504 
00505             // If the leftmost position is reached
00506             if (x == 4) {
00507                 // Erase Pac-Man from his current position
00508                 erase(x, y);
00509                 // Set Pac-Man's x-coordinate to 124
00510                 x = 124;
00511                 // Draw Pac-Man in his new position
00512                 draw(x, y);
00513             }
00514 
00515             // Set Pac-Man's next direction
00516             switch (nextDirection) {
00517                 case FACERIGHT:
00518                     if (stage.positions[x + 1][y] != 0) {
00519                         direction = nextDirection;
00520                     }
00521 
00522                     break;
00523                 case FACEDOWN:
00524                     if (stage.positions[x][y + 1] != 0) {
00525                         direction = nextDirection;
00526                     }
00527 
00528                     break;
00529                 case FACEUP:
00530                     if (stage.positions[x][y - 1] != 0) {
00531                         direction = nextDirection;
00532                     }
00533 
00534                     break;
00535                 default:
00536                     break;
00537             }
00538         }
00539 
00540         // Move Pac-Man up
00541         void moveUp() {
00542             // If the space in the upward direction is a valid position
00543             if (stage.positions[x][y - 1] != 0) {
00544                 // Erase Pac-Man from his current position
00545                 erase(x, y);
00546                 // Decrement Pac-Man's y-coordinate
00547                 y--;
00548                 // Draw Pac-Man in his new position
00549                 draw(x, y);
00550                 // Alternate Pac-Man's mouth for animation
00551                 mouthOpen = !mouthOpen;
00552 
00553                 // If Pac-Man passes partially through a big pac dot
00554                 if (stage.positions[x][y + 5] == 3) {
00555                     // Redraw the big pac dot part
00556                     uLCD.pixel(x, y + 4, YELLOW);
00557                 }
00558 
00559                 // Loop through Pac-Man's 7-pixel diameter
00560                 for (int j = y + 3; j >= y - 3; j--) {
00561                     // If a pac dot is eaten
00562                     if (stage.positions[x][j] == 2) {
00563                         // Increment the number of pac dots eaten
00564                         pacDotsEaten++;
00565                         // Add 10 to the score
00566                         score += 10;
00567                         // Set the coordinates to represent only a valid position
00568                         stage.positions[x][j] = 1;
00569                         // Changes the score display
00570                         scoreChanged = true;
00571                     // If a big pac dot is eaten
00572                     } else if (stage.positions[x][j] == 3) {
00573                         // Erase the reset of the big pac dot
00574                         uLCD.pixel(x, y - 4, BLACK);
00575                         // Increment the number of big pac dots eaten
00576                         bigPacDotsEaten++;
00577                         // Add 50 to the score
00578                         score += 50;
00579                         // Set the coordinates to represent only a valid position
00580                         stage.positions[x][j] = 1;
00581                         // Set the invincibility counter to 100
00582                         powerUp = 100;
00583                         // Changes the score display
00584                         scoreChanged = true;
00585                     }
00586                 }
00587             // If the space in the upward direction is not a valid position
00588             } else {
00589                 // Draw Pac-Man with his mouth open
00590                 uLCD.rectangle(x - 1, y - 2, x + 1, y - 3, BLACK);
00591                 uLCD.pixel(x, y, BLACK);
00592                 uLCD.pixel(x, y - 1, BLACK);
00593                 wait(0.001);
00594             }
00595 
00596             // Set Pac-Man's next direction
00597             switch (nextDirection) {
00598                 case FACERIGHT:
00599                     if (stage.positions[x + 1][y] != 0) {
00600                         direction = nextDirection;
00601                     }
00602 
00603                     break;
00604                 case FACEDOWN:
00605                     if (stage.positions[x][y + 1] != 0) {
00606                         direction = nextDirection;
00607                     }
00608 
00609                     break;
00610                 case FACELEFT:
00611                     if (stage.positions[x - 1][y] != 0) {
00612                         direction = nextDirection;
00613                     }
00614 
00615                     break;
00616                 default:
00617                     break;
00618             }
00619         }
00620 
00621         // Dying animation
00622         void dyingAnimation() {   
00623             erase(x, y);         
00624             draw(x, y);
00625             uLCD.rectangle(x - 1, y - 2, x + 1, y - 3, BLACK);
00626             uLCD.pixel(x, y, BLACK);
00627             uLCD.pixel(x, y - 1, BLACK);
00628 
00629             uLCD.pixel(x - 1, y, BLACK);
00630             uLCD.pixel(x - 1, y - 1, BLACK);
00631             uLCD.pixel(x - 2, y - 2, BLACK);
00632             uLCD.pixel(x + 1, y, BLACK);
00633             uLCD.pixel(x + 1, y - 1, BLACK);
00634             uLCD.pixel(x + 2, y - 2, BLACK);
00635             wait(0.15);
00636 
00637             uLCD.pixel(x - 2, y, BLACK);
00638             uLCD.pixel(x - 3, y, BLACK);
00639             uLCD.pixel(x - 2, y - 1, BLACK);
00640             uLCD.pixel(x - 3, y - 1, BLACK);
00641             uLCD.pixel(x + 2, y, BLACK);
00642             uLCD.pixel(x + 3, y, BLACK);
00643             uLCD.pixel(x + 2, y - 1, BLACK);
00644             uLCD.pixel(x + 3, y - 1, BLACK);
00645             wait(0.15);
00646 
00647             uLCD.pixel(x - 1, y + 1, BLACK);
00648             uLCD.pixel(x - 2, y + 1, BLACK);
00649             uLCD.pixel(x - 3, y + 1, BLACK);
00650             uLCD.pixel(x - 2, y + 2, BLACK);
00651             uLCD.pixel(x + 1, y + 1, BLACK);
00652             uLCD.pixel(x + 2, y + 1, BLACK);
00653             uLCD.pixel(x + 3, y + 1, BLACK);
00654             uLCD.pixel(x + 2, y + 2, BLACK);
00655             wait(0.15);
00656 
00657             uLCD.pixel(x - 1, y + 2, BLACK);
00658             uLCD.pixel(x - 1, y + 3, BLACK);
00659             uLCD.pixel(x + 1, y + 2, BLACK);
00660             uLCD.pixel(x + 1, y + 3, BLACK);
00661             wait(0.15);
00662 
00663             uLCD.pixel(x, y + 1, BLACK);
00664             uLCD.pixel(x, y + 2, BLACK);
00665             uLCD.pixel(x, y + 3, BLACK);
00666             wait(0.15);
00667         }
00668 
00669         // Reinitialize Pac-Man after losing a life
00670         void reinitialize() {
00671             // Set Pac-Man's x-coordinate to the starting point
00672             x = 64;
00673             // Set Pac-Man's y-coordinate to the starting point
00674             y = 108;
00675             // Redraw the pac dots on the stage
00676             stage.redrawPacDots();
00677             // Changes the lives display
00678             livesChanged = true;
00679             // Draw Pac-Man at his starting point
00680             draw(x, y);
00681             // Display the lives and score counter
00682             displayStatus();
00683         }
00684 
00685         // Pac-Man dies
00686         void die() {
00687             // Decrement the number of lives
00688             lives--;
00689             // Erase Pac-Man from his current position
00690             erase(x, y);
00691             // Reset Pac-Man's direction to be up
00692             direction = FACEUP;
00693             // Displays Pac-Man's dying animation
00694             dyingAnimation();
00695 
00696             // If the number of lives is 0
00697             if (lives == 0) {
00698                 // Break out of the function
00699                 return;
00700             }
00701 
00702             // Reinitialize Pac-Man
00703             reinitialize();
00704         }
00705 
00706         // Pac-Man wins
00707         void win() {
00708             uLCD.cls();
00709             uLCD.filled_rectangle(0, 0, 127, 127, BLACK);
00710             uLCD.locate(3, 7);
00711             uLCD.printf("Pac-Man wins.\n");
00712             uLCD.locate(3, 8);
00713             uLCD.printf("Score: %d", score);
00714         }
00715 
00716         // Pac-Man loses
00717         void lose() {
00718             uLCD.cls();
00719             uLCD.filled_rectangle(0, 0, 127, 127, BLACK);
00720             uLCD.locate(2, 7);
00721             uLCD.printf("Pac-Man loses.\n");
00722             uLCD.locate(3, 8);
00723             uLCD.printf("Score: %d", score);
00724         }
00725 };