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