This program simulates a game of Pac-Man that allows for multiplayer. One player controls Pac-Man, while two other players control a red and yellow ghost using different tactile switches.

Dependencies:   4DGL-uLCD-SE PinDetect mbed

Fork of Pac-Man by Eric Xu

Committer:
soapy12312
Date:
Mon Oct 19 17:47:39 2015 +0000
Revision:
0:8b6686f2d4be
Multiplayer Pac-Man.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
soapy12312 0:8b6686f2d4be 1 /******************************************************
soapy12312 0:8b6686f2d4be 2 * Author: Yuanzhe Xu
soapy12312 0:8b6686f2d4be 3 * Institution: Georgia Institute of Technology
soapy12312 0:8b6686f2d4be 4 * Date: October 18, 2015
soapy12312 0:8b6686f2d4be 5 *
soapy12312 0:8b6686f2d4be 6 * This header file declares all the functions and
soapy12312 0:8b6686f2d4be 7 * variables needed for the Pacman object. Specific
soapy12312 0:8b6686f2d4be 8 * Pac-Man functionalities can be found in main.cpp.
soapy12312 0:8b6686f2d4be 9 ******************************************************/
soapy12312 0:8b6686f2d4be 10
soapy12312 0:8b6686f2d4be 11 // Pacman class
soapy12312 0:8b6686f2d4be 12 class Pacman {
soapy12312 0:8b6686f2d4be 13 public:
soapy12312 0:8b6686f2d4be 14 // Pac-Man constructor
soapy12312 0:8b6686f2d4be 15 Pacman(uLCD_4DGL& uLCD, Stage& stage)
soapy12312 0:8b6686f2d4be 16 : uLCD(uLCD), stage(stage) {
soapy12312 0:8b6686f2d4be 17 }
soapy12312 0:8b6686f2d4be 18
soapy12312 0:8b6686f2d4be 19 // Initialize Pac-Man at the start of the game
soapy12312 0:8b6686f2d4be 20 void initialize() {
soapy12312 0:8b6686f2d4be 21 x = 64;
soapy12312 0:8b6686f2d4be 22 y = 108;
soapy12312 0:8b6686f2d4be 23 lives = 3;
soapy12312 0:8b6686f2d4be 24 livesChanged = true;
soapy12312 0:8b6686f2d4be 25 score = 0;
soapy12312 0:8b6686f2d4be 26 scoreChanged = true;
soapy12312 0:8b6686f2d4be 27 direction = FACEUP;
soapy12312 0:8b6686f2d4be 28 mouthOpen = false;
soapy12312 0:8b6686f2d4be 29 pacDotsEaten = 0;
soapy12312 0:8b6686f2d4be 30 bigPacDotsEaten = 0;
soapy12312 0:8b6686f2d4be 31 powerUp = 0;
soapy12312 0:8b6686f2d4be 32 powerUpChanged = false;
soapy12312 0:8b6686f2d4be 33 ghostsEaten = 0;
soapy12312 0:8b6686f2d4be 34 dead = false;
soapy12312 0:8b6686f2d4be 35 draw(x, y);
soapy12312 0:8b6686f2d4be 36 displayStatus();
soapy12312 0:8b6686f2d4be 37 }
soapy12312 0:8b6686f2d4be 38
soapy12312 0:8b6686f2d4be 39 // Set the next direction
soapy12312 0:8b6686f2d4be 40 void setNextDirection(int next) {
soapy12312 0:8b6686f2d4be 41 nextDirection = next;
soapy12312 0:8b6686f2d4be 42 }
soapy12312 0:8b6686f2d4be 43
soapy12312 0:8b6686f2d4be 44 // Move Pac-Man and return whether the game is over
soapy12312 0:8b6686f2d4be 45 bool move() {
soapy12312 0:8b6686f2d4be 46 // If all the pac dots are eaten or Pac-Man is out of lives
soapy12312 0:8b6686f2d4be 47 if (((pacDotsEaten == stage.pacDots) &&
soapy12312 0:8b6686f2d4be 48 (bigPacDotsEaten == stage.bigPacDots)) ||
soapy12312 0:8b6686f2d4be 49 (lives == 0)) {
soapy12312 0:8b6686f2d4be 50 // Return true that the game is over and exit out of the function
soapy12312 0:8b6686f2d4be 51 return true;
soapy12312 0:8b6686f2d4be 52 }
soapy12312 0:8b6686f2d4be 53
soapy12312 0:8b6686f2d4be 54 // If Pac-Man is not invincible
soapy12312 0:8b6686f2d4be 55 if (powerUp == 0) {
soapy12312 0:8b6686f2d4be 56 // Reset the ghosts eaten counter
soapy12312 0:8b6686f2d4be 57 ghostsEaten = 0;
soapy12312 0:8b6686f2d4be 58 }
soapy12312 0:8b6686f2d4be 59
soapy12312 0:8b6686f2d4be 60 // If Pac-Man is invincible
soapy12312 0:8b6686f2d4be 61 if (powerUp > 0) {
soapy12312 0:8b6686f2d4be 62 // Decrement the invincibility counter
soapy12312 0:8b6686f2d4be 63 powerUp--;
soapy12312 0:8b6686f2d4be 64
soapy12312 0:8b6686f2d4be 65 // If invincibility just ended or just started
soapy12312 0:8b6686f2d4be 66 if ((powerUp == 0) || (powerUp == 99)) {
soapy12312 0:8b6686f2d4be 67 // Changes the color of the ghosts
soapy12312 0:8b6686f2d4be 68 powerUpChanged = true;
soapy12312 0:8b6686f2d4be 69 // If invincibility did not just end or did not just start
soapy12312 0:8b6686f2d4be 70 } else {
soapy12312 0:8b6686f2d4be 71 // Set powerUpChanged to false
soapy12312 0:8b6686f2d4be 72 powerUpChanged = false;
soapy12312 0:8b6686f2d4be 73 }
soapy12312 0:8b6686f2d4be 74 }
soapy12312 0:8b6686f2d4be 75
soapy12312 0:8b6686f2d4be 76 // Move Pac-Man in his current direction
soapy12312 0:8b6686f2d4be 77 switch (direction) {
soapy12312 0:8b6686f2d4be 78 case FACERIGHT:
soapy12312 0:8b6686f2d4be 79 moveRight();
soapy12312 0:8b6686f2d4be 80 break;
soapy12312 0:8b6686f2d4be 81 case FACEDOWN:
soapy12312 0:8b6686f2d4be 82 moveDown();
soapy12312 0:8b6686f2d4be 83 break;
soapy12312 0:8b6686f2d4be 84 case FACELEFT:
soapy12312 0:8b6686f2d4be 85 moveLeft();
soapy12312 0:8b6686f2d4be 86 break;
soapy12312 0:8b6686f2d4be 87 case FACEUP:
soapy12312 0:8b6686f2d4be 88 moveUp();
soapy12312 0:8b6686f2d4be 89 break;
soapy12312 0:8b6686f2d4be 90 default:
soapy12312 0:8b6686f2d4be 91 break;
soapy12312 0:8b6686f2d4be 92 }
soapy12312 0:8b6686f2d4be 93
soapy12312 0:8b6686f2d4be 94 // Return false that the game is over
soapy12312 0:8b6686f2d4be 95 return false;
soapy12312 0:8b6686f2d4be 96 }
soapy12312 0:8b6686f2d4be 97
soapy12312 0:8b6686f2d4be 98 // Display score and number of lives remaining
soapy12312 0:8b6686f2d4be 99 void displayStatus() {
soapy12312 0:8b6686f2d4be 100 // If the number of lives just changed
soapy12312 0:8b6686f2d4be 101 if (livesChanged == true) {
soapy12312 0:8b6686f2d4be 102 // Draw the corresponding number of lives
soapy12312 0:8b6686f2d4be 103 switch (lives) {
soapy12312 0:8b6686f2d4be 104 case 3:
soapy12312 0:8b6686f2d4be 105 drawLives(56, 12);
soapy12312 0:8b6686f2d4be 106 drawLives(64, 12);
soapy12312 0:8b6686f2d4be 107 drawLives(72, 12);
soapy12312 0:8b6686f2d4be 108 break;
soapy12312 0:8b6686f2d4be 109 case 2:
soapy12312 0:8b6686f2d4be 110 erase(72, 12);
soapy12312 0:8b6686f2d4be 111 drawLives(56, 12);
soapy12312 0:8b6686f2d4be 112 drawLives(64, 12);
soapy12312 0:8b6686f2d4be 113 break;
soapy12312 0:8b6686f2d4be 114 case 1:
soapy12312 0:8b6686f2d4be 115 erase(56, 12);
soapy12312 0:8b6686f2d4be 116 drawLives(64, 12);
soapy12312 0:8b6686f2d4be 117 break;
soapy12312 0:8b6686f2d4be 118 default:
soapy12312 0:8b6686f2d4be 119 break;
soapy12312 0:8b6686f2d4be 120 }
soapy12312 0:8b6686f2d4be 121
soapy12312 0:8b6686f2d4be 122 // Reset livesChanged
soapy12312 0:8b6686f2d4be 123 livesChanged = false;
soapy12312 0:8b6686f2d4be 124 }
soapy12312 0:8b6686f2d4be 125
soapy12312 0:8b6686f2d4be 126 // If the score just changed
soapy12312 0:8b6686f2d4be 127 if (scoreChanged == true) {
soapy12312 0:8b6686f2d4be 128 // Print the new score
soapy12312 0:8b6686f2d4be 129 uLCD.locate(0, 1);
soapy12312 0:8b6686f2d4be 130 uLCD.printf("%d", score);
soapy12312 0:8b6686f2d4be 131 scoreChanged = false;
soapy12312 0:8b6686f2d4be 132 }
soapy12312 0:8b6686f2d4be 133
soapy12312 0:8b6686f2d4be 134 // Print the invincibility counter
soapy12312 0:8b6686f2d4be 135 uLCD.locate(14, 1);
soapy12312 0:8b6686f2d4be 136
soapy12312 0:8b6686f2d4be 137 if (powerUp == 100) {
soapy12312 0:8b6686f2d4be 138 uLCD.printf("100");
soapy12312 0:8b6686f2d4be 139 } else if (powerUp >= 10) {
soapy12312 0:8b6686f2d4be 140 uLCD.printf(" %d", powerUp);
soapy12312 0:8b6686f2d4be 141 } else {
soapy12312 0:8b6686f2d4be 142 uLCD.printf(" %d", powerUp);
soapy12312 0:8b6686f2d4be 143 }
soapy12312 0:8b6686f2d4be 144 }
soapy12312 0:8b6686f2d4be 145
soapy12312 0:8b6686f2d4be 146 // Game over
soapy12312 0:8b6686f2d4be 147 void gameOver() {
soapy12312 0:8b6686f2d4be 148 // If all the pac dots have been eaten
soapy12312 0:8b6686f2d4be 149 if ((pacDotsEaten == stage.pacDots) &&
soapy12312 0:8b6686f2d4be 150 (bigPacDotsEaten == stage.bigPacDots)) {
soapy12312 0:8b6686f2d4be 151 // Pac-Man wins
soapy12312 0:8b6686f2d4be 152 win();
soapy12312 0:8b6686f2d4be 153 // If the number of lives is 0
soapy12312 0:8b6686f2d4be 154 } else if (lives == 0) {
soapy12312 0:8b6686f2d4be 155 // Pac-Man loses
soapy12312 0:8b6686f2d4be 156 lose();
soapy12312 0:8b6686f2d4be 157 }
soapy12312 0:8b6686f2d4be 158 }
soapy12312 0:8b6686f2d4be 159
soapy12312 0:8b6686f2d4be 160
soapy12312 0:8b6686f2d4be 161 private:
soapy12312 0:8b6686f2d4be 162 // Current horizontal position
soapy12312 0:8b6686f2d4be 163 int x;
soapy12312 0:8b6686f2d4be 164 // Current vertical position
soapy12312 0:8b6686f2d4be 165 int y;
soapy12312 0:8b6686f2d4be 166 // Current direction
soapy12312 0:8b6686f2d4be 167 int direction;
soapy12312 0:8b6686f2d4be 168 // Next direction
soapy12312 0:8b6686f2d4be 169 int nextDirection;
soapy12312 0:8b6686f2d4be 170 // Number of lives
soapy12312 0:8b6686f2d4be 171 int lives;
soapy12312 0:8b6686f2d4be 172 // The lives display updates when livesChanged is true
soapy12312 0:8b6686f2d4be 173 bool livesChanged;
soapy12312 0:8b6686f2d4be 174 // Score
soapy12312 0:8b6686f2d4be 175 int score;
soapy12312 0:8b6686f2d4be 176 // The score display updates when scoreChanged is true
soapy12312 0:8b6686f2d4be 177 bool scoreChanged;
soapy12312 0:8b6686f2d4be 178 // Mouth open or closed for animation
soapy12312 0:8b6686f2d4be 179 bool mouthOpen;
soapy12312 0:8b6686f2d4be 180 // Number of pac dots eaten
soapy12312 0:8b6686f2d4be 181 int pacDotsEaten;
soapy12312 0:8b6686f2d4be 182 // Number of big pac dots eaten
soapy12312 0:8b6686f2d4be 183 int bigPacDotsEaten;
soapy12312 0:8b6686f2d4be 184 // Invincibility counter
soapy12312 0:8b6686f2d4be 185 int powerUp;
soapy12312 0:8b6686f2d4be 186 // Changes the color of the ghosts when powerUpChanged is true
soapy12312 0:8b6686f2d4be 187 bool powerUpChanged;
soapy12312 0:8b6686f2d4be 188 // Number of ghosts eaten
soapy12312 0:8b6686f2d4be 189 int ghostsEaten;
soapy12312 0:8b6686f2d4be 190 // Ghosts reset when dead is true
soapy12312 0:8b6686f2d4be 191 bool dead;
soapy12312 0:8b6686f2d4be 192 // uLCD display
soapy12312 0:8b6686f2d4be 193 uLCD_4DGL& uLCD;
soapy12312 0:8b6686f2d4be 194 // Stage reference
soapy12312 0:8b6686f2d4be 195 Stage& stage;
soapy12312 0:8b6686f2d4be 196 // Set Ghost as a friend class
soapy12312 0:8b6686f2d4be 197 friend class Ghost;
soapy12312 0:8b6686f2d4be 198
soapy12312 0:8b6686f2d4be 199 // Draw the Pac-Man to display lives
soapy12312 0:8b6686f2d4be 200 void drawLives(int xCoordinate, int yCoordinate) {
soapy12312 0:8b6686f2d4be 201 uLCD.filled_circle(xCoordinate, yCoordinate, 3, YELLOW);
soapy12312 0:8b6686f2d4be 202 int x1 = xCoordinate - 2;
soapy12312 0:8b6686f2d4be 203 int x2 = xCoordinate - 3;
soapy12312 0:8b6686f2d4be 204 int y1 = yCoordinate - 1;
soapy12312 0:8b6686f2d4be 205 int y2 = yCoordinate + 1;
soapy12312 0:8b6686f2d4be 206 uLCD.rectangle(x1, y1, x2, y2, BLACK);
soapy12312 0:8b6686f2d4be 207 uLCD.pixel(xCoordinate, yCoordinate, BLACK);
soapy12312 0:8b6686f2d4be 208 uLCD.pixel(xCoordinate - 1, yCoordinate, BLACK);
soapy12312 0:8b6686f2d4be 209 }
soapy12312 0:8b6686f2d4be 210
soapy12312 0:8b6686f2d4be 211 // Draw Pac-Man based on his direction
soapy12312 0:8b6686f2d4be 212 void draw(int xCoordinate, int yCoordinate) {
soapy12312 0:8b6686f2d4be 213 // If his mouth is closed
soapy12312 0:8b6686f2d4be 214 if (mouthOpen == false) {
soapy12312 0:8b6686f2d4be 215 // Draw Pac-Man with his mouth closed as a circle
soapy12312 0:8b6686f2d4be 216 uLCD.filled_circle(xCoordinate, yCoordinate, 3, YELLOW);
soapy12312 0:8b6686f2d4be 217 // If his mouth is open
soapy12312 0:8b6686f2d4be 218 } else {
soapy12312 0:8b6686f2d4be 219 // Draw Pac-Man with his mouth open based on his direction
soapy12312 0:8b6686f2d4be 220 int x1;
soapy12312 0:8b6686f2d4be 221 int x2;
soapy12312 0:8b6686f2d4be 222 int y1;
soapy12312 0:8b6686f2d4be 223 int y2;
soapy12312 0:8b6686f2d4be 224
soapy12312 0:8b6686f2d4be 225 switch (direction) {
soapy12312 0:8b6686f2d4be 226 case FACERIGHT:
soapy12312 0:8b6686f2d4be 227 uLCD.filled_circle(xCoordinate, yCoordinate, 3, YELLOW);
soapy12312 0:8b6686f2d4be 228 x1 = xCoordinate + 2;
soapy12312 0:8b6686f2d4be 229 x2 = xCoordinate + 3;
soapy12312 0:8b6686f2d4be 230 y1 = yCoordinate - 1;
soapy12312 0:8b6686f2d4be 231 y2 = yCoordinate + 1;
soapy12312 0:8b6686f2d4be 232 uLCD.rectangle(x1, y1, x2, y2, BLACK);
soapy12312 0:8b6686f2d4be 233 uLCD.pixel(xCoordinate, yCoordinate, BLACK);
soapy12312 0:8b6686f2d4be 234 uLCD.pixel(xCoordinate + 1, yCoordinate, BLACK);
soapy12312 0:8b6686f2d4be 235 break;
soapy12312 0:8b6686f2d4be 236 case FACEDOWN:
soapy12312 0:8b6686f2d4be 237 uLCD.filled_circle(xCoordinate, yCoordinate, 3, YELLOW);
soapy12312 0:8b6686f2d4be 238 x1 = xCoordinate - 1;
soapy12312 0:8b6686f2d4be 239 x2 = xCoordinate + 1;
soapy12312 0:8b6686f2d4be 240 y1 = yCoordinate + 2;
soapy12312 0:8b6686f2d4be 241 y2 = yCoordinate + 3;
soapy12312 0:8b6686f2d4be 242 uLCD.rectangle(x1, y1, x2, y2, BLACK);
soapy12312 0:8b6686f2d4be 243 uLCD.pixel(xCoordinate, yCoordinate, BLACK);
soapy12312 0:8b6686f2d4be 244 uLCD.pixel(xCoordinate, yCoordinate + 1, BLACK);
soapy12312 0:8b6686f2d4be 245 break;
soapy12312 0:8b6686f2d4be 246 case FACELEFT:
soapy12312 0:8b6686f2d4be 247 uLCD.filled_circle(xCoordinate, yCoordinate, 3, YELLOW);
soapy12312 0:8b6686f2d4be 248 x1 = xCoordinate - 2;
soapy12312 0:8b6686f2d4be 249 x2 = xCoordinate - 3;
soapy12312 0:8b6686f2d4be 250 y1 = yCoordinate - 1;
soapy12312 0:8b6686f2d4be 251 y2 = yCoordinate + 1;
soapy12312 0:8b6686f2d4be 252 uLCD.rectangle(x1, y1, x2, y2, BLACK);
soapy12312 0:8b6686f2d4be 253 uLCD.pixel(xCoordinate, yCoordinate, BLACK);
soapy12312 0:8b6686f2d4be 254 uLCD.pixel(xCoordinate - 1, yCoordinate, BLACK);
soapy12312 0:8b6686f2d4be 255 break;
soapy12312 0:8b6686f2d4be 256 case FACEUP:
soapy12312 0:8b6686f2d4be 257 uLCD.filled_circle(xCoordinate, yCoordinate, 3, YELLOW);
soapy12312 0:8b6686f2d4be 258 x1 = xCoordinate - 1;
soapy12312 0:8b6686f2d4be 259 x2 = xCoordinate + 1;
soapy12312 0:8b6686f2d4be 260 y1 = yCoordinate - 2;
soapy12312 0:8b6686f2d4be 261 y2 = yCoordinate - 3;
soapy12312 0:8b6686f2d4be 262 uLCD.rectangle(x1, y1, x2, y2, BLACK);
soapy12312 0:8b6686f2d4be 263 uLCD.pixel(xCoordinate, yCoordinate, BLACK);
soapy12312 0:8b6686f2d4be 264 uLCD.pixel(xCoordinate, yCoordinate - 1, BLACK);
soapy12312 0:8b6686f2d4be 265 break;
soapy12312 0:8b6686f2d4be 266 default:
soapy12312 0:8b6686f2d4be 267 break;
soapy12312 0:8b6686f2d4be 268 }
soapy12312 0:8b6686f2d4be 269 }
soapy12312 0:8b6686f2d4be 270 }
soapy12312 0:8b6686f2d4be 271
soapy12312 0:8b6686f2d4be 272 // Erase Pac-Man from the given position
soapy12312 0:8b6686f2d4be 273 void erase(int xCoordinate, int yCoordinate) {
soapy12312 0:8b6686f2d4be 274 uLCD.filled_circle(xCoordinate, yCoordinate, 3, BLACK);
soapy12312 0:8b6686f2d4be 275 }
soapy12312 0:8b6686f2d4be 276
soapy12312 0:8b6686f2d4be 277 // Move Pac-Man right
soapy12312 0:8b6686f2d4be 278 void moveRight() {
soapy12312 0:8b6686f2d4be 279 // If the space to the right is a valid positions and is not the rightmost space
soapy12312 0:8b6686f2d4be 280 if ((stage.positions[x + 1][y] != 0) && (x != 124)) {
soapy12312 0:8b6686f2d4be 281 // Erase Pac-Man from his current position
soapy12312 0:8b6686f2d4be 282 erase(x, y);
soapy12312 0:8b6686f2d4be 283 // Increment Pac-Man's x-coordinate
soapy12312 0:8b6686f2d4be 284 x++;
soapy12312 0:8b6686f2d4be 285 // Draw Pac-Man in his new position
soapy12312 0:8b6686f2d4be 286 draw(x, y);
soapy12312 0:8b6686f2d4be 287 // Alternate Pac-Man's mouth for animation
soapy12312 0:8b6686f2d4be 288 mouthOpen = !mouthOpen;
soapy12312 0:8b6686f2d4be 289
soapy12312 0:8b6686f2d4be 290 // If Pac-Man passes partially across a big pac dot
soapy12312 0:8b6686f2d4be 291 if (stage.positions[x - 5][y] == 3) {
soapy12312 0:8b6686f2d4be 292 // Redraw the big pac dot part
soapy12312 0:8b6686f2d4be 293 uLCD.pixel(x - 4, y, YELLOW);
soapy12312 0:8b6686f2d4be 294 }
soapy12312 0:8b6686f2d4be 295
soapy12312 0:8b6686f2d4be 296 // Loop through Pac-Man's 7-pixel diameter
soapy12312 0:8b6686f2d4be 297 for (int i = x - 3; i <= x + 3; i++) {
soapy12312 0:8b6686f2d4be 298 // If a pac dot is eaten
soapy12312 0:8b6686f2d4be 299 if (stage.positions[i][y] == 2) {
soapy12312 0:8b6686f2d4be 300 // Increment the number of pac dots eaten
soapy12312 0:8b6686f2d4be 301 pacDotsEaten++;
soapy12312 0:8b6686f2d4be 302 // Add 10 to the score
soapy12312 0:8b6686f2d4be 303 score += 10;
soapy12312 0:8b6686f2d4be 304 // Set the coordinates to represent only a valid position
soapy12312 0:8b6686f2d4be 305 stage.positions[i][y] = 1;
soapy12312 0:8b6686f2d4be 306 // Changes the score display
soapy12312 0:8b6686f2d4be 307 scoreChanged = true;
soapy12312 0:8b6686f2d4be 308 // If a big pac dot is eaten
soapy12312 0:8b6686f2d4be 309 } else if (stage.positions[i][y] == 3) {
soapy12312 0:8b6686f2d4be 310 // Erase the rest of the big pac dot
soapy12312 0:8b6686f2d4be 311 uLCD.pixel(x + 4, y, BLACK);
soapy12312 0:8b6686f2d4be 312 // Increment the number of big pac dots eaten
soapy12312 0:8b6686f2d4be 313 bigPacDotsEaten++;
soapy12312 0:8b6686f2d4be 314 // Add 50 to the score
soapy12312 0:8b6686f2d4be 315 score += 50;
soapy12312 0:8b6686f2d4be 316 // Set the coordinates to represent only a valid position
soapy12312 0:8b6686f2d4be 317 stage.positions[i][y] = 1;
soapy12312 0:8b6686f2d4be 318 // Set the invincibility counter to 100
soapy12312 0:8b6686f2d4be 319 powerUp = 100;
soapy12312 0:8b6686f2d4be 320 // Changes the score display
soapy12312 0:8b6686f2d4be 321 scoreChanged = true;
soapy12312 0:8b6686f2d4be 322 }
soapy12312 0:8b6686f2d4be 323 }
soapy12312 0:8b6686f2d4be 324 // If the space to the right is not a valid position
soapy12312 0:8b6686f2d4be 325 } else if (stage.positions[x + 1][y] == 0) {
soapy12312 0:8b6686f2d4be 326 // Draw Pac-Man with his mouth open
soapy12312 0:8b6686f2d4be 327 uLCD.rectangle(x + 2, y - 1, x + 3, y + 1, BLACK);
soapy12312 0:8b6686f2d4be 328 uLCD.pixel(x, y, BLACK);
soapy12312 0:8b6686f2d4be 329 uLCD.pixel(x + 1, y, BLACK);
soapy12312 0:8b6686f2d4be 330 wait(0.001);
soapy12312 0:8b6686f2d4be 331 }
soapy12312 0:8b6686f2d4be 332
soapy12312 0:8b6686f2d4be 333 // If the rightmost space is reached
soapy12312 0:8b6686f2d4be 334 if (x == 124) {
soapy12312 0:8b6686f2d4be 335 // Erase Pac-Man from his current position
soapy12312 0:8b6686f2d4be 336 erase(x, y);
soapy12312 0:8b6686f2d4be 337 // Set Pac-Man's x-coordinate to 4
soapy12312 0:8b6686f2d4be 338 x = 4;
soapy12312 0:8b6686f2d4be 339 // Draw Pac-Man in his new positions
soapy12312 0:8b6686f2d4be 340 draw(x, y);
soapy12312 0:8b6686f2d4be 341 }
soapy12312 0:8b6686f2d4be 342
soapy12312 0:8b6686f2d4be 343 // Set Pac-Man's next direction
soapy12312 0:8b6686f2d4be 344 switch (nextDirection) {
soapy12312 0:8b6686f2d4be 345 case FACEDOWN:
soapy12312 0:8b6686f2d4be 346 if (stage.positions[x][y + 1] != 0) {
soapy12312 0:8b6686f2d4be 347 direction = nextDirection;
soapy12312 0:8b6686f2d4be 348 }
soapy12312 0:8b6686f2d4be 349
soapy12312 0:8b6686f2d4be 350 break;
soapy12312 0:8b6686f2d4be 351 case FACELEFT:
soapy12312 0:8b6686f2d4be 352 if (stage.positions[x - 1][y] != 0) {
soapy12312 0:8b6686f2d4be 353 direction = nextDirection;
soapy12312 0:8b6686f2d4be 354 }
soapy12312 0:8b6686f2d4be 355
soapy12312 0:8b6686f2d4be 356 break;
soapy12312 0:8b6686f2d4be 357 case FACEUP:
soapy12312 0:8b6686f2d4be 358 if (stage.positions[x][y - 1] != 0) {
soapy12312 0:8b6686f2d4be 359 direction = nextDirection;
soapy12312 0:8b6686f2d4be 360 }
soapy12312 0:8b6686f2d4be 361
soapy12312 0:8b6686f2d4be 362 break;
soapy12312 0:8b6686f2d4be 363 default:
soapy12312 0:8b6686f2d4be 364 break;
soapy12312 0:8b6686f2d4be 365 }
soapy12312 0:8b6686f2d4be 366 }
soapy12312 0:8b6686f2d4be 367
soapy12312 0:8b6686f2d4be 368 // Move Pac-Man down
soapy12312 0:8b6686f2d4be 369 void moveDown() {
soapy12312 0:8b6686f2d4be 370 // If the space in the downward direction is a valid position
soapy12312 0:8b6686f2d4be 371 if (stage.positions[x][y + 1] != 0) {
soapy12312 0:8b6686f2d4be 372 // Erase Pac-Man from his current position
soapy12312 0:8b6686f2d4be 373 erase(x, y);
soapy12312 0:8b6686f2d4be 374 // Increment Pac-Man's y-coordinate
soapy12312 0:8b6686f2d4be 375 y++;
soapy12312 0:8b6686f2d4be 376 // Draw Pac-Man in his new position
soapy12312 0:8b6686f2d4be 377 draw(x, y);
soapy12312 0:8b6686f2d4be 378 // Alternate Pac-Man's mouth for animation
soapy12312 0:8b6686f2d4be 379 mouthOpen = !mouthOpen;
soapy12312 0:8b6686f2d4be 380
soapy12312 0:8b6686f2d4be 381 // If Pac-Man passes partially through a big pac dot
soapy12312 0:8b6686f2d4be 382 if (stage.positions[x][y - 5] == 3) {
soapy12312 0:8b6686f2d4be 383 // Redraw the big pac dot part
soapy12312 0:8b6686f2d4be 384 uLCD.pixel(x, y - 4, YELLOW);
soapy12312 0:8b6686f2d4be 385 }
soapy12312 0:8b6686f2d4be 386
soapy12312 0:8b6686f2d4be 387 // Loop through Pac-Man's 7-pixel diameter
soapy12312 0:8b6686f2d4be 388 for (int j = y - 3; j <= y + 3; j++) {
soapy12312 0:8b6686f2d4be 389 // If a pac dot is eaten
soapy12312 0:8b6686f2d4be 390 if (stage.positions[x][j] == 2) {
soapy12312 0:8b6686f2d4be 391 // Increment the number of pac dots eaten
soapy12312 0:8b6686f2d4be 392 pacDotsEaten++;
soapy12312 0:8b6686f2d4be 393 // Add 10 to the score
soapy12312 0:8b6686f2d4be 394 score += 10;
soapy12312 0:8b6686f2d4be 395 // Set the position to represent only a valid position
soapy12312 0:8b6686f2d4be 396 stage.positions[x][j] = 1;
soapy12312 0:8b6686f2d4be 397 // Changes the score display
soapy12312 0:8b6686f2d4be 398 scoreChanged = true;
soapy12312 0:8b6686f2d4be 399 // If a big pac dot is eaten
soapy12312 0:8b6686f2d4be 400 } else if (stage.positions[x][j] == 3) {
soapy12312 0:8b6686f2d4be 401 // Erase the rest of the big pac dot
soapy12312 0:8b6686f2d4be 402 uLCD.pixel(x, y + 4, BLACK);
soapy12312 0:8b6686f2d4be 403 // Increment the number of big pac dots eaten
soapy12312 0:8b6686f2d4be 404 bigPacDotsEaten++;
soapy12312 0:8b6686f2d4be 405 // Add 50 to the score
soapy12312 0:8b6686f2d4be 406 score += 50;
soapy12312 0:8b6686f2d4be 407 // Set the coordinates to represent only a valid position(1)
soapy12312 0:8b6686f2d4be 408 stage.positions[x][j] = 1;
soapy12312 0:8b6686f2d4be 409 // Set the invincibility counter to 100
soapy12312 0:8b6686f2d4be 410 powerUp = 100;
soapy12312 0:8b6686f2d4be 411 // Changes the score display
soapy12312 0:8b6686f2d4be 412 scoreChanged = true;
soapy12312 0:8b6686f2d4be 413 }
soapy12312 0:8b6686f2d4be 414 }
soapy12312 0:8b6686f2d4be 415 // If the space in the downward direction is not a valid position
soapy12312 0:8b6686f2d4be 416 } else {
soapy12312 0:8b6686f2d4be 417 // Draw Pac-Man with his mouth open
soapy12312 0:8b6686f2d4be 418 uLCD.rectangle(x - 1, y + 2, x + 1, y + 3, BLACK);
soapy12312 0:8b6686f2d4be 419 uLCD.pixel(x, y, BLACK);
soapy12312 0:8b6686f2d4be 420 uLCD.pixel(x, y + 1, BLACK);
soapy12312 0:8b6686f2d4be 421 wait(0.001);
soapy12312 0:8b6686f2d4be 422 }
soapy12312 0:8b6686f2d4be 423
soapy12312 0:8b6686f2d4be 424 // Set Pac-Man's next direction
soapy12312 0:8b6686f2d4be 425 switch (nextDirection) {
soapy12312 0:8b6686f2d4be 426 case FACERIGHT:
soapy12312 0:8b6686f2d4be 427 if (stage.positions[x + 1][y] != 0) {
soapy12312 0:8b6686f2d4be 428 direction = nextDirection;
soapy12312 0:8b6686f2d4be 429 }
soapy12312 0:8b6686f2d4be 430
soapy12312 0:8b6686f2d4be 431 break;
soapy12312 0:8b6686f2d4be 432 case FACELEFT:
soapy12312 0:8b6686f2d4be 433 if (stage.positions[x - 1][y] != 0) {
soapy12312 0:8b6686f2d4be 434 direction = nextDirection;
soapy12312 0:8b6686f2d4be 435 }
soapy12312 0:8b6686f2d4be 436
soapy12312 0:8b6686f2d4be 437 break;
soapy12312 0:8b6686f2d4be 438 case FACEUP:
soapy12312 0:8b6686f2d4be 439 if (stage.positions[x][y - 1] != 0) {
soapy12312 0:8b6686f2d4be 440 direction = nextDirection;
soapy12312 0:8b6686f2d4be 441 }
soapy12312 0:8b6686f2d4be 442
soapy12312 0:8b6686f2d4be 443 break;
soapy12312 0:8b6686f2d4be 444 default:
soapy12312 0:8b6686f2d4be 445 break;
soapy12312 0:8b6686f2d4be 446 }
soapy12312 0:8b6686f2d4be 447 }
soapy12312 0:8b6686f2d4be 448
soapy12312 0:8b6686f2d4be 449 // Move Pac-Man left
soapy12312 0:8b6686f2d4be 450 void moveLeft() {
soapy12312 0:8b6686f2d4be 451 // If the space to the left is a valid position and is not the leftmost space
soapy12312 0:8b6686f2d4be 452 if ((stage.positions[x - 1][y] != 0) && (x != 4)) {
soapy12312 0:8b6686f2d4be 453 // Erase Pac-Man from his current position
soapy12312 0:8b6686f2d4be 454 erase(x, y);
soapy12312 0:8b6686f2d4be 455 // Decrement Pac-Man's x-coordinate
soapy12312 0:8b6686f2d4be 456 x--;
soapy12312 0:8b6686f2d4be 457 // Draw Pac-Man in his new position
soapy12312 0:8b6686f2d4be 458 draw(x, y);
soapy12312 0:8b6686f2d4be 459 // Alternate Pac-Man's mouth for animation
soapy12312 0:8b6686f2d4be 460 mouthOpen = !mouthOpen;
soapy12312 0:8b6686f2d4be 461
soapy12312 0:8b6686f2d4be 462 // If Pac-Man passes partially through a big pac dot
soapy12312 0:8b6686f2d4be 463 if (stage.positions[x + 5][y] == 3) {
soapy12312 0:8b6686f2d4be 464 // Redraw the big pac dot part
soapy12312 0:8b6686f2d4be 465 uLCD.pixel(x + 4, y, YELLOW);
soapy12312 0:8b6686f2d4be 466 }
soapy12312 0:8b6686f2d4be 467
soapy12312 0:8b6686f2d4be 468 // Loop through Pac-Man's 7-pixel diameter
soapy12312 0:8b6686f2d4be 469 for (int i = x + 3; i >= x - 3; i--) {
soapy12312 0:8b6686f2d4be 470 // If a pac dot is eaten
soapy12312 0:8b6686f2d4be 471 if (stage.positions[i][y] == 2) {
soapy12312 0:8b6686f2d4be 472 // Increment the number of pac dots eaten
soapy12312 0:8b6686f2d4be 473 pacDotsEaten++;
soapy12312 0:8b6686f2d4be 474 // Add 10 to the score
soapy12312 0:8b6686f2d4be 475 score += 10;
soapy12312 0:8b6686f2d4be 476 // Set the coordinates to represent only a valid position
soapy12312 0:8b6686f2d4be 477 stage.positions[i][y] = 1;
soapy12312 0:8b6686f2d4be 478 // Changes the score display
soapy12312 0:8b6686f2d4be 479 scoreChanged = true;
soapy12312 0:8b6686f2d4be 480 // If a big pac dot is eaten
soapy12312 0:8b6686f2d4be 481 } else if (stage.positions[i][y] == 3) {
soapy12312 0:8b6686f2d4be 482 // Erase the rest of the big pac dot
soapy12312 0:8b6686f2d4be 483 uLCD.pixel(x - 4, y, BLACK);
soapy12312 0:8b6686f2d4be 484 // Increment the number of big pac dots eaten
soapy12312 0:8b6686f2d4be 485 bigPacDotsEaten++;
soapy12312 0:8b6686f2d4be 486 // Add 50 to the score
soapy12312 0:8b6686f2d4be 487 score += 50;
soapy12312 0:8b6686f2d4be 488 // Set the coordinates to represent only a valid position
soapy12312 0:8b6686f2d4be 489 stage.positions[i][y] = 1;
soapy12312 0:8b6686f2d4be 490 // Set the invincibility counter to 100
soapy12312 0:8b6686f2d4be 491 powerUp = 100;
soapy12312 0:8b6686f2d4be 492 // Changes the score display
soapy12312 0:8b6686f2d4be 493 scoreChanged = true;
soapy12312 0:8b6686f2d4be 494 }
soapy12312 0:8b6686f2d4be 495 }
soapy12312 0:8b6686f2d4be 496 // If the space to the left is not a valid position
soapy12312 0:8b6686f2d4be 497 } else if (stage.positions[x - 1][y] == 0) {
soapy12312 0:8b6686f2d4be 498 // Draw Pac-Man with his mouth open
soapy12312 0:8b6686f2d4be 499 uLCD.rectangle(x - 2, y - 1, x - 1, y + 1, BLACK);
soapy12312 0:8b6686f2d4be 500 uLCD.pixel(x, y, BLACK);
soapy12312 0:8b6686f2d4be 501 uLCD.pixel(x - 1, y, BLACK);
soapy12312 0:8b6686f2d4be 502 wait(0.001);
soapy12312 0:8b6686f2d4be 503 }
soapy12312 0:8b6686f2d4be 504
soapy12312 0:8b6686f2d4be 505 // If the leftmost position is reached
soapy12312 0:8b6686f2d4be 506 if (x == 4) {
soapy12312 0:8b6686f2d4be 507 // Erase Pac-Man from his current position
soapy12312 0:8b6686f2d4be 508 erase(x, y);
soapy12312 0:8b6686f2d4be 509 // Set Pac-Man's x-coordinate to 124
soapy12312 0:8b6686f2d4be 510 x = 124;
soapy12312 0:8b6686f2d4be 511 // Draw Pac-Man in his new position
soapy12312 0:8b6686f2d4be 512 draw(x, y);
soapy12312 0:8b6686f2d4be 513 }
soapy12312 0:8b6686f2d4be 514
soapy12312 0:8b6686f2d4be 515 // Set Pac-Man's next direction
soapy12312 0:8b6686f2d4be 516 switch (nextDirection) {
soapy12312 0:8b6686f2d4be 517 case FACERIGHT:
soapy12312 0:8b6686f2d4be 518 if (stage.positions[x + 1][y] != 0) {
soapy12312 0:8b6686f2d4be 519 direction = nextDirection;
soapy12312 0:8b6686f2d4be 520 }
soapy12312 0:8b6686f2d4be 521
soapy12312 0:8b6686f2d4be 522 break;
soapy12312 0:8b6686f2d4be 523 case FACEDOWN:
soapy12312 0:8b6686f2d4be 524 if (stage.positions[x][y + 1] != 0) {
soapy12312 0:8b6686f2d4be 525 direction = nextDirection;
soapy12312 0:8b6686f2d4be 526 }
soapy12312 0:8b6686f2d4be 527
soapy12312 0:8b6686f2d4be 528 break;
soapy12312 0:8b6686f2d4be 529 case FACEUP:
soapy12312 0:8b6686f2d4be 530 if (stage.positions[x][y - 1] != 0) {
soapy12312 0:8b6686f2d4be 531 direction = nextDirection;
soapy12312 0:8b6686f2d4be 532 }
soapy12312 0:8b6686f2d4be 533
soapy12312 0:8b6686f2d4be 534 break;
soapy12312 0:8b6686f2d4be 535 default:
soapy12312 0:8b6686f2d4be 536 break;
soapy12312 0:8b6686f2d4be 537 }
soapy12312 0:8b6686f2d4be 538 }
soapy12312 0:8b6686f2d4be 539
soapy12312 0:8b6686f2d4be 540 // Move Pac-Man up
soapy12312 0:8b6686f2d4be 541 void moveUp() {
soapy12312 0:8b6686f2d4be 542 // If the space in the upward direction is a valid position
soapy12312 0:8b6686f2d4be 543 if (stage.positions[x][y - 1] != 0) {
soapy12312 0:8b6686f2d4be 544 // Erase Pac-Man from his current position
soapy12312 0:8b6686f2d4be 545 erase(x, y);
soapy12312 0:8b6686f2d4be 546 // Decrement Pac-Man's y-coordinate
soapy12312 0:8b6686f2d4be 547 y--;
soapy12312 0:8b6686f2d4be 548 // Draw Pac-Man in his new position
soapy12312 0:8b6686f2d4be 549 draw(x, y);
soapy12312 0:8b6686f2d4be 550 // Alternate Pac-Man's mouth for animation
soapy12312 0:8b6686f2d4be 551 mouthOpen = !mouthOpen;
soapy12312 0:8b6686f2d4be 552
soapy12312 0:8b6686f2d4be 553 // If Pac-Man passes partially through a big pac dot
soapy12312 0:8b6686f2d4be 554 if (stage.positions[x][y + 5] == 3) {
soapy12312 0:8b6686f2d4be 555 // Redraw the big pac dot part
soapy12312 0:8b6686f2d4be 556 uLCD.pixel(x, y + 4, YELLOW);
soapy12312 0:8b6686f2d4be 557 }
soapy12312 0:8b6686f2d4be 558
soapy12312 0:8b6686f2d4be 559 // Loop through Pac-Man's 7-pixel diameter
soapy12312 0:8b6686f2d4be 560 for (int j = y + 3; j >= y - 3; j--) {
soapy12312 0:8b6686f2d4be 561 // If a pac dot is eaten
soapy12312 0:8b6686f2d4be 562 if (stage.positions[x][j] == 2) {
soapy12312 0:8b6686f2d4be 563 // Increment the number of pac dots eaten
soapy12312 0:8b6686f2d4be 564 pacDotsEaten++;
soapy12312 0:8b6686f2d4be 565 // Add 10 to the score
soapy12312 0:8b6686f2d4be 566 score += 10;
soapy12312 0:8b6686f2d4be 567 // Set the coordinates to represent only a valid position
soapy12312 0:8b6686f2d4be 568 stage.positions[x][j] = 1;
soapy12312 0:8b6686f2d4be 569 // Changes the score display
soapy12312 0:8b6686f2d4be 570 scoreChanged = true;
soapy12312 0:8b6686f2d4be 571 // If a big pac dot is eaten
soapy12312 0:8b6686f2d4be 572 } else if (stage.positions[x][j] == 3) {
soapy12312 0:8b6686f2d4be 573 // Erase the reset of the big pac dot
soapy12312 0:8b6686f2d4be 574 uLCD.pixel(x, y - 4, BLACK);
soapy12312 0:8b6686f2d4be 575 // Increment the number of big pac dots eaten
soapy12312 0:8b6686f2d4be 576 bigPacDotsEaten++;
soapy12312 0:8b6686f2d4be 577 // Add 50 to the score
soapy12312 0:8b6686f2d4be 578 score += 50;
soapy12312 0:8b6686f2d4be 579 // Set the coordinates to represent only a valid position
soapy12312 0:8b6686f2d4be 580 stage.positions[x][j] = 1;
soapy12312 0:8b6686f2d4be 581 // Set the invincibility counter to 100
soapy12312 0:8b6686f2d4be 582 powerUp = 100;
soapy12312 0:8b6686f2d4be 583 // Changes the score display
soapy12312 0:8b6686f2d4be 584 scoreChanged = true;
soapy12312 0:8b6686f2d4be 585 }
soapy12312 0:8b6686f2d4be 586 }
soapy12312 0:8b6686f2d4be 587 // If the space in the upward direction is not a valid position
soapy12312 0:8b6686f2d4be 588 } else {
soapy12312 0:8b6686f2d4be 589 // Draw Pac-Man with his mouth open
soapy12312 0:8b6686f2d4be 590 uLCD.rectangle(x - 1, y - 2, x + 1, y - 3, BLACK);
soapy12312 0:8b6686f2d4be 591 uLCD.pixel(x, y, BLACK);
soapy12312 0:8b6686f2d4be 592 uLCD.pixel(x, y - 1, BLACK);
soapy12312 0:8b6686f2d4be 593 wait(0.001);
soapy12312 0:8b6686f2d4be 594 }
soapy12312 0:8b6686f2d4be 595
soapy12312 0:8b6686f2d4be 596 // Set Pac-Man's next direction
soapy12312 0:8b6686f2d4be 597 switch (nextDirection) {
soapy12312 0:8b6686f2d4be 598 case FACERIGHT:
soapy12312 0:8b6686f2d4be 599 if (stage.positions[x + 1][y] != 0) {
soapy12312 0:8b6686f2d4be 600 direction = nextDirection;
soapy12312 0:8b6686f2d4be 601 }
soapy12312 0:8b6686f2d4be 602
soapy12312 0:8b6686f2d4be 603 break;
soapy12312 0:8b6686f2d4be 604 case FACEDOWN:
soapy12312 0:8b6686f2d4be 605 if (stage.positions[x][y + 1] != 0) {
soapy12312 0:8b6686f2d4be 606 direction = nextDirection;
soapy12312 0:8b6686f2d4be 607 }
soapy12312 0:8b6686f2d4be 608
soapy12312 0:8b6686f2d4be 609 break;
soapy12312 0:8b6686f2d4be 610 case FACELEFT:
soapy12312 0:8b6686f2d4be 611 if (stage.positions[x - 1][y] != 0) {
soapy12312 0:8b6686f2d4be 612 direction = nextDirection;
soapy12312 0:8b6686f2d4be 613 }
soapy12312 0:8b6686f2d4be 614
soapy12312 0:8b6686f2d4be 615 break;
soapy12312 0:8b6686f2d4be 616 default:
soapy12312 0:8b6686f2d4be 617 break;
soapy12312 0:8b6686f2d4be 618 }
soapy12312 0:8b6686f2d4be 619 }
soapy12312 0:8b6686f2d4be 620
soapy12312 0:8b6686f2d4be 621 // Dying animation
soapy12312 0:8b6686f2d4be 622 void dyingAnimation() {
soapy12312 0:8b6686f2d4be 623 erase(x, y);
soapy12312 0:8b6686f2d4be 624 draw(x, y);
soapy12312 0:8b6686f2d4be 625 uLCD.rectangle(x - 1, y - 2, x + 1, y - 3, BLACK);
soapy12312 0:8b6686f2d4be 626 uLCD.pixel(x, y, BLACK);
soapy12312 0:8b6686f2d4be 627 uLCD.pixel(x, y - 1, BLACK);
soapy12312 0:8b6686f2d4be 628
soapy12312 0:8b6686f2d4be 629 uLCD.pixel(x - 1, y, BLACK);
soapy12312 0:8b6686f2d4be 630 uLCD.pixel(x - 1, y - 1, BLACK);
soapy12312 0:8b6686f2d4be 631 uLCD.pixel(x - 2, y - 2, BLACK);
soapy12312 0:8b6686f2d4be 632 uLCD.pixel(x + 1, y, BLACK);
soapy12312 0:8b6686f2d4be 633 uLCD.pixel(x + 1, y - 1, BLACK);
soapy12312 0:8b6686f2d4be 634 uLCD.pixel(x + 2, y - 2, BLACK);
soapy12312 0:8b6686f2d4be 635 wait(0.15);
soapy12312 0:8b6686f2d4be 636
soapy12312 0:8b6686f2d4be 637 uLCD.pixel(x - 2, y, BLACK);
soapy12312 0:8b6686f2d4be 638 uLCD.pixel(x - 3, y, BLACK);
soapy12312 0:8b6686f2d4be 639 uLCD.pixel(x - 2, y - 1, BLACK);
soapy12312 0:8b6686f2d4be 640 uLCD.pixel(x - 3, y - 1, BLACK);
soapy12312 0:8b6686f2d4be 641 uLCD.pixel(x + 2, y, BLACK);
soapy12312 0:8b6686f2d4be 642 uLCD.pixel(x + 3, y, BLACK);
soapy12312 0:8b6686f2d4be 643 uLCD.pixel(x + 2, y - 1, BLACK);
soapy12312 0:8b6686f2d4be 644 uLCD.pixel(x + 3, y - 1, BLACK);
soapy12312 0:8b6686f2d4be 645 wait(0.15);
soapy12312 0:8b6686f2d4be 646
soapy12312 0:8b6686f2d4be 647 uLCD.pixel(x - 1, y + 1, BLACK);
soapy12312 0:8b6686f2d4be 648 uLCD.pixel(x - 2, y + 1, BLACK);
soapy12312 0:8b6686f2d4be 649 uLCD.pixel(x - 3, y + 1, BLACK);
soapy12312 0:8b6686f2d4be 650 uLCD.pixel(x - 2, y + 2, BLACK);
soapy12312 0:8b6686f2d4be 651 uLCD.pixel(x + 1, y + 1, BLACK);
soapy12312 0:8b6686f2d4be 652 uLCD.pixel(x + 2, y + 1, BLACK);
soapy12312 0:8b6686f2d4be 653 uLCD.pixel(x + 3, y + 1, BLACK);
soapy12312 0:8b6686f2d4be 654 uLCD.pixel(x + 2, y + 2, BLACK);
soapy12312 0:8b6686f2d4be 655 wait(0.15);
soapy12312 0:8b6686f2d4be 656
soapy12312 0:8b6686f2d4be 657 uLCD.pixel(x - 1, y + 2, BLACK);
soapy12312 0:8b6686f2d4be 658 uLCD.pixel(x - 1, y + 3, BLACK);
soapy12312 0:8b6686f2d4be 659 uLCD.pixel(x + 1, y + 2, BLACK);
soapy12312 0:8b6686f2d4be 660 uLCD.pixel(x + 1, y + 3, BLACK);
soapy12312 0:8b6686f2d4be 661 wait(0.15);
soapy12312 0:8b6686f2d4be 662
soapy12312 0:8b6686f2d4be 663 uLCD.pixel(x, y + 1, BLACK);
soapy12312 0:8b6686f2d4be 664 uLCD.pixel(x, y + 2, BLACK);
soapy12312 0:8b6686f2d4be 665 uLCD.pixel(x, y + 3, BLACK);
soapy12312 0:8b6686f2d4be 666 wait(0.15);
soapy12312 0:8b6686f2d4be 667 }
soapy12312 0:8b6686f2d4be 668
soapy12312 0:8b6686f2d4be 669 // Reinitialize Pac-Man after losing a life
soapy12312 0:8b6686f2d4be 670 void reinitialize() {
soapy12312 0:8b6686f2d4be 671 // Set Pac-Man's x-coordinate to the starting point
soapy12312 0:8b6686f2d4be 672 x = 64;
soapy12312 0:8b6686f2d4be 673 // Set Pac-Man's y-coordinate to the starting point
soapy12312 0:8b6686f2d4be 674 y = 108;
soapy12312 0:8b6686f2d4be 675 // Redraw the pac dots on the stage
soapy12312 0:8b6686f2d4be 676 stage.redrawPacDots();
soapy12312 0:8b6686f2d4be 677 // Changes the lives display
soapy12312 0:8b6686f2d4be 678 livesChanged = true;
soapy12312 0:8b6686f2d4be 679 // Draw Pac-Man at his starting point
soapy12312 0:8b6686f2d4be 680 draw(x, y);
soapy12312 0:8b6686f2d4be 681 // Display the lives and score counter
soapy12312 0:8b6686f2d4be 682 displayStatus();
soapy12312 0:8b6686f2d4be 683 }
soapy12312 0:8b6686f2d4be 684
soapy12312 0:8b6686f2d4be 685 // Pac-Man dies
soapy12312 0:8b6686f2d4be 686 void die() {
soapy12312 0:8b6686f2d4be 687 // Decrement the number of lives
soapy12312 0:8b6686f2d4be 688 lives--;
soapy12312 0:8b6686f2d4be 689 // Erase Pac-Man from his current position
soapy12312 0:8b6686f2d4be 690 erase(x, y);
soapy12312 0:8b6686f2d4be 691 // Reset Pac-Man's direction to be up
soapy12312 0:8b6686f2d4be 692 direction = FACEUP;
soapy12312 0:8b6686f2d4be 693 // Displays Pac-Man's dying animation
soapy12312 0:8b6686f2d4be 694 dyingAnimation();
soapy12312 0:8b6686f2d4be 695
soapy12312 0:8b6686f2d4be 696 // If the number of lives is 0
soapy12312 0:8b6686f2d4be 697 if (lives == 0) {
soapy12312 0:8b6686f2d4be 698 // Break out of the function
soapy12312 0:8b6686f2d4be 699 return;
soapy12312 0:8b6686f2d4be 700 }
soapy12312 0:8b6686f2d4be 701
soapy12312 0:8b6686f2d4be 702 // Reinitialize Pac-Man
soapy12312 0:8b6686f2d4be 703 reinitialize();
soapy12312 0:8b6686f2d4be 704 }
soapy12312 0:8b6686f2d4be 705
soapy12312 0:8b6686f2d4be 706 // Pac-Man wins
soapy12312 0:8b6686f2d4be 707 void win() {
soapy12312 0:8b6686f2d4be 708 uLCD.cls();
soapy12312 0:8b6686f2d4be 709 uLCD.filled_rectangle(0, 0, 127, 127, BLACK);
soapy12312 0:8b6686f2d4be 710 uLCD.locate(3, 7);
soapy12312 0:8b6686f2d4be 711 uLCD.printf("Pac-Man wins.\n");
soapy12312 0:8b6686f2d4be 712 uLCD.locate(3, 8);
soapy12312 0:8b6686f2d4be 713 uLCD.printf("Score: %d", score);
soapy12312 0:8b6686f2d4be 714 }
soapy12312 0:8b6686f2d4be 715
soapy12312 0:8b6686f2d4be 716 // Pac-Man loses
soapy12312 0:8b6686f2d4be 717 void lose() {
soapy12312 0:8b6686f2d4be 718 uLCD.cls();
soapy12312 0:8b6686f2d4be 719 uLCD.filled_rectangle(0, 0, 127, 127, BLACK);
soapy12312 0:8b6686f2d4be 720 uLCD.locate(2, 7);
soapy12312 0:8b6686f2d4be 721 uLCD.printf("Pac-Man loses.\n");
soapy12312 0:8b6686f2d4be 722 uLCD.locate(3, 8);
soapy12312 0:8b6686f2d4be 723 uLCD.printf("Score: %d", score);
soapy12312 0:8b6686f2d4be 724 }
soapy12312 0:8b6686f2d4be 725 };