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