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
Ghost.h@2:d35fde2d82cd, 2015-12-04 (annotated)
- Committer:
- soapy12312
- Date:
- Fri Dec 04 23:03:08 2015 +0000
- Revision:
- 2:d35fde2d82cd
- Parent:
- 1:2a7e2f5aeda4
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 Ghost players in Pac-Man. |
soapy12312 | 0:218d3fb75950 | 4 | ******************************************************/ |
soapy12312 | 0:218d3fb75950 | 5 | |
soapy12312 | 0:218d3fb75950 | 6 | |
soapy12312 | 0:218d3fb75950 | 7 | // Ghost class |
soapy12312 | 0:218d3fb75950 | 8 | class Ghost { |
soapy12312 | 0:218d3fb75950 | 9 | public: |
soapy12312 | 0:218d3fb75950 | 10 | // Ghost constructor |
soapy12312 | 1:2a7e2f5aeda4 | 11 | Ghost(int color, Pacman& pacman, PinDetect &ghostRight, PinDetect &ghostDown, PinDetect &ghostLeft, PinDetect &ghostUp, Stage& stage, uLCD_4DGL& uLCD) : |
soapy12312 | 1:2a7e2f5aeda4 | 12 | color(color), pacman(pacman), ghostRight(ghostRight), ghostDown(ghostDown), ghostLeft(ghostLeft), ghostUp(ghostUp), stage(stage), uLCD(uLCD) { |
soapy12312 | 0:218d3fb75950 | 13 | // Use internal pullups |
soapy12312 | 0:218d3fb75950 | 14 | ghostRight.mode(PullUp); |
soapy12312 | 0:218d3fb75950 | 15 | ghostDown.mode(PullUp); |
soapy12312 | 0:218d3fb75950 | 16 | ghostLeft.mode(PullUp); |
soapy12312 | 0:218d3fb75950 | 17 | ghostUp.mode(PullUp); |
soapy12312 | 0:218d3fb75950 | 18 | wait(0.01); |
soapy12312 | 0:218d3fb75950 | 19 | // Interrupt callback functions |
soapy12312 | 0:218d3fb75950 | 20 | ghostRight.attach_deasserted(this, &Ghost::ghostRightTrigger); |
soapy12312 | 0:218d3fb75950 | 21 | ghostDown.attach_deasserted(this, &Ghost::ghostDownTrigger); |
soapy12312 | 0:218d3fb75950 | 22 | ghostLeft.attach_deasserted(this, &Ghost::ghostLeftTrigger); |
soapy12312 | 0:218d3fb75950 | 23 | ghostUp.attach_deasserted(this, &Ghost::ghostUpTrigger); |
soapy12312 | 0:218d3fb75950 | 24 | wait(0.01); |
soapy12312 | 0:218d3fb75950 | 25 | // Set sampling frequency |
soapy12312 | 0:218d3fb75950 | 26 | ghostRight.setSampleFrequency(); |
soapy12312 | 0:218d3fb75950 | 27 | ghostDown.setSampleFrequency(); |
soapy12312 | 0:218d3fb75950 | 28 | ghostLeft.setSampleFrequency(); |
soapy12312 | 0:218d3fb75950 | 29 | ghostUp.setSampleFrequency(); |
soapy12312 | 0:218d3fb75950 | 30 | wait(1); |
soapy12312 | 0:218d3fb75950 | 31 | } |
soapy12312 | 0:218d3fb75950 | 32 | |
soapy12312 | 0:218d3fb75950 | 33 | // Initialize the ghost at the beginning of the game |
soapy12312 | 0:218d3fb75950 | 34 | void initialize(int xCoordinate, int yCoordinate, int facingDirection) { |
soapy12312 | 0:218d3fb75950 | 35 | startingX = xCoordinate; |
soapy12312 | 0:218d3fb75950 | 36 | startingY = yCoordinate; |
soapy12312 | 0:218d3fb75950 | 37 | x = xCoordinate; |
soapy12312 | 0:218d3fb75950 | 38 | y = yCoordinate; |
soapy12312 | 0:218d3fb75950 | 39 | direction = facingDirection; |
soapy12312 | 0:218d3fb75950 | 40 | immobile = false; |
soapy12312 | 0:218d3fb75950 | 41 | draw(x, y); |
soapy12312 | 0:218d3fb75950 | 42 | } |
soapy12312 | 0:218d3fb75950 | 43 | |
soapy12312 | 0:218d3fb75950 | 44 | // Move the ghost |
soapy12312 | 0:218d3fb75950 | 45 | void move() { |
soapy12312 | 0:218d3fb75950 | 46 | // If Pac-Man died |
soapy12312 | 0:218d3fb75950 | 47 | if (pacman.dead == true) { |
soapy12312 | 0:218d3fb75950 | 48 | // Reset the ghost |
soapy12312 | 0:218d3fb75950 | 49 | erase(x, y); |
soapy12312 | 0:218d3fb75950 | 50 | reinitialize(); |
soapy12312 | 0:218d3fb75950 | 51 | pacman.dead = false; |
soapy12312 | 0:218d3fb75950 | 52 | } |
soapy12312 | 0:218d3fb75950 | 53 | |
soapy12312 | 0:218d3fb75950 | 54 | // If the ghost is not moving |
soapy12312 | 0:218d3fb75950 | 55 | if (immobile == true) { |
soapy12312 | 0:218d3fb75950 | 56 | // Reset the ghost |
soapy12312 | 0:218d3fb75950 | 57 | erase(x, y); |
soapy12312 | 0:218d3fb75950 | 58 | stage.redrawPacDots(); |
soapy12312 | 0:218d3fb75950 | 59 | reinitialize(); |
soapy12312 | 0:218d3fb75950 | 60 | } |
soapy12312 | 0:218d3fb75950 | 61 | |
soapy12312 | 0:218d3fb75950 | 62 | // Move the ghost in its current direction |
soapy12312 | 0:218d3fb75950 | 63 | switch (direction) { |
soapy12312 | 0:218d3fb75950 | 64 | case FACERIGHT: |
soapy12312 | 0:218d3fb75950 | 65 | moveRight(); |
soapy12312 | 0:218d3fb75950 | 66 | break; |
soapy12312 | 0:218d3fb75950 | 67 | case FACEDOWN: |
soapy12312 | 0:218d3fb75950 | 68 | moveDown(); |
soapy12312 | 0:218d3fb75950 | 69 | break; |
soapy12312 | 0:218d3fb75950 | 70 | case FACELEFT: |
soapy12312 | 0:218d3fb75950 | 71 | moveLeft(); |
soapy12312 | 0:218d3fb75950 | 72 | break; |
soapy12312 | 0:218d3fb75950 | 73 | case FACEUP: |
soapy12312 | 0:218d3fb75950 | 74 | moveUp(); |
soapy12312 | 0:218d3fb75950 | 75 | break; |
soapy12312 | 0:218d3fb75950 | 76 | default: |
soapy12312 | 0:218d3fb75950 | 77 | break; |
soapy12312 | 0:218d3fb75950 | 78 | } |
soapy12312 | 0:218d3fb75950 | 79 | |
soapy12312 | 0:218d3fb75950 | 80 | // If Pac-Man and the ghost occupy the same position |
soapy12312 | 0:218d3fb75950 | 81 | if ((abs(pacman.x - x) < 5) && (abs(pacman.y - y) < 5)) { |
soapy12312 | 0:218d3fb75950 | 82 | // If Pac-Man is not invincible |
soapy12312 | 0:218d3fb75950 | 83 | if (pacman.powerUp == 0) { |
soapy12312 | 0:218d3fb75950 | 84 | // Reset Pac-Man and the ghost |
soapy12312 | 0:218d3fb75950 | 85 | pacman.ghostsEaten = 0; |
soapy12312 | 0:218d3fb75950 | 86 | pacman.erase(pacman.x, pacman.y); |
soapy12312 | 0:218d3fb75950 | 87 | erase(x, y); |
soapy12312 | 0:218d3fb75950 | 88 | pacman.draw(pacman.x, pacman.y); |
soapy12312 | 0:218d3fb75950 | 89 | wait(0.5); |
soapy12312 | 0:218d3fb75950 | 90 | pacman.die(); |
soapy12312 | 0:218d3fb75950 | 91 | pacman.dead = true; |
soapy12312 | 0:218d3fb75950 | 92 | reinitialize(); |
soapy12312 | 0:218d3fb75950 | 93 | // If Pac-Man is invincible |
soapy12312 | 0:218d3fb75950 | 94 | } else { |
soapy12312 | 0:218d3fb75950 | 95 | // Reset the ghost |
soapy12312 | 0:218d3fb75950 | 96 | pacman.ghostsEaten++; |
soapy12312 | 0:218d3fb75950 | 97 | pacman.score += pacman.ghostsEaten * 200; |
soapy12312 | 0:218d3fb75950 | 98 | pacman.scoreChanged = true; |
soapy12312 | 0:218d3fb75950 | 99 | erase(x, y); |
soapy12312 | 0:218d3fb75950 | 100 | stage.redrawPacDots(); |
soapy12312 | 0:218d3fb75950 | 101 | pacman.draw(pacman.x, pacman.y); |
soapy12312 | 0:218d3fb75950 | 102 | wait(0.5); |
soapy12312 | 0:218d3fb75950 | 103 | reinitialize(); |
soapy12312 | 0:218d3fb75950 | 104 | } |
soapy12312 | 0:218d3fb75950 | 105 | } |
soapy12312 | 0:218d3fb75950 | 106 | } |
soapy12312 | 0:218d3fb75950 | 107 | |
soapy12312 | 0:218d3fb75950 | 108 | |
soapy12312 | 0:218d3fb75950 | 109 | private: |
soapy12312 | 0:218d3fb75950 | 110 | int color; |
soapy12312 | 0:218d3fb75950 | 111 | Pacman& pacman; |
soapy12312 | 0:218d3fb75950 | 112 | PinDetect &ghostRight; |
soapy12312 | 0:218d3fb75950 | 113 | PinDetect &ghostDown; |
soapy12312 | 0:218d3fb75950 | 114 | PinDetect &ghostLeft; |
soapy12312 | 0:218d3fb75950 | 115 | PinDetect &ghostUp; |
soapy12312 | 0:218d3fb75950 | 116 | Stage& stage; |
soapy12312 | 0:218d3fb75950 | 117 | uLCD_4DGL& uLCD; |
soapy12312 | 0:218d3fb75950 | 118 | int startingX; |
soapy12312 | 0:218d3fb75950 | 119 | int startingY; |
soapy12312 | 0:218d3fb75950 | 120 | int x; |
soapy12312 | 0:218d3fb75950 | 121 | int y; |
soapy12312 | 0:218d3fb75950 | 122 | volatile int previousDirection; |
soapy12312 | 0:218d3fb75950 | 123 | int direction; |
soapy12312 | 0:218d3fb75950 | 124 | volatile int nextDirection; |
soapy12312 | 0:218d3fb75950 | 125 | bool immobile; |
soapy12312 | 0:218d3fb75950 | 126 | |
soapy12312 | 0:218d3fb75950 | 127 | // Set the next direction to be right |
soapy12312 | 0:218d3fb75950 | 128 | void ghostRightTrigger() { |
soapy12312 | 0:218d3fb75950 | 129 | previousDirection = (direction + 2) % 4; |
soapy12312 | 0:218d3fb75950 | 130 | |
soapy12312 | 0:218d3fb75950 | 131 | if (previousDirection != FACERIGHT) { |
soapy12312 | 0:218d3fb75950 | 132 | nextDirection = FACERIGHT; |
soapy12312 | 0:218d3fb75950 | 133 | } |
soapy12312 | 0:218d3fb75950 | 134 | } |
soapy12312 | 0:218d3fb75950 | 135 | |
soapy12312 | 0:218d3fb75950 | 136 | // Set the next direction to be down |
soapy12312 | 0:218d3fb75950 | 137 | void ghostDownTrigger() { |
soapy12312 | 0:218d3fb75950 | 138 | previousDirection = (direction + 2) % 4; |
soapy12312 | 0:218d3fb75950 | 139 | |
soapy12312 | 0:218d3fb75950 | 140 | if (previousDirection != FACEDOWN) { |
soapy12312 | 0:218d3fb75950 | 141 | nextDirection = FACEDOWN; |
soapy12312 | 0:218d3fb75950 | 142 | } |
soapy12312 | 0:218d3fb75950 | 143 | } |
soapy12312 | 0:218d3fb75950 | 144 | |
soapy12312 | 0:218d3fb75950 | 145 | // Set the next direction to be left |
soapy12312 | 0:218d3fb75950 | 146 | void ghostLeftTrigger() { |
soapy12312 | 0:218d3fb75950 | 147 | previousDirection = (direction + 2) % 4; |
soapy12312 | 0:218d3fb75950 | 148 | |
soapy12312 | 0:218d3fb75950 | 149 | if (previousDirection != FACELEFT) { |
soapy12312 | 0:218d3fb75950 | 150 | nextDirection = FACELEFT; |
soapy12312 | 0:218d3fb75950 | 151 | } |
soapy12312 | 0:218d3fb75950 | 152 | } |
soapy12312 | 0:218d3fb75950 | 153 | |
soapy12312 | 0:218d3fb75950 | 154 | // Set the next direction to be up |
soapy12312 | 0:218d3fb75950 | 155 | void ghostUpTrigger() { |
soapy12312 | 0:218d3fb75950 | 156 | previousDirection = (direction + 2) % 4; |
soapy12312 | 0:218d3fb75950 | 157 | |
soapy12312 | 0:218d3fb75950 | 158 | if (previousDirection != FACEUP) { |
soapy12312 | 0:218d3fb75950 | 159 | nextDirection = FACEUP; |
soapy12312 | 0:218d3fb75950 | 160 | } |
soapy12312 | 0:218d3fb75950 | 161 | } |
soapy12312 | 0:218d3fb75950 | 162 | |
soapy12312 | 0:218d3fb75950 | 163 | // Reset function for each ghost |
soapy12312 | 0:218d3fb75950 | 164 | void reinitialize() { |
soapy12312 | 0:218d3fb75950 | 165 | // Red ghost reset |
soapy12312 | 0:218d3fb75950 | 166 | if (color == RED) { |
soapy12312 | 0:218d3fb75950 | 167 | initialize(startingX, startingY, FACELEFT); |
soapy12312 | 0:218d3fb75950 | 168 | // Yellow ghost reset |
soapy12312 | 0:218d3fb75950 | 169 | } else if (color == YELLOW) { |
soapy12312 | 0:218d3fb75950 | 170 | initialize(startingX, startingY, FACERIGHT); |
soapy12312 | 0:218d3fb75950 | 171 | } |
soapy12312 | 0:218d3fb75950 | 172 | } |
soapy12312 | 0:218d3fb75950 | 173 | |
soapy12312 | 0:218d3fb75950 | 174 | // Draw the ghost based on its direction |
soapy12312 | 0:218d3fb75950 | 175 | void draw(int xCoordinate, int yCoordinate) { |
soapy12312 | 0:218d3fb75950 | 176 | // Outer bound rectangular coordinates of the ghost |
soapy12312 | 0:218d3fb75950 | 177 | int x1 = xCoordinate - 3; |
soapy12312 | 0:218d3fb75950 | 178 | int x2 = xCoordinate + 3; |
soapy12312 | 0:218d3fb75950 | 179 | int y1 = yCoordinate - 3; |
soapy12312 | 0:218d3fb75950 | 180 | int y2 = yCoordinate + 3; |
soapy12312 | 0:218d3fb75950 | 181 | |
soapy12312 | 0:218d3fb75950 | 182 | // If Pac-Man is currently invincible |
soapy12312 | 0:218d3fb75950 | 183 | if (pacman.powerUp > 0) { |
soapy12312 | 0:218d3fb75950 | 184 | // Draw the ghost in blue |
soapy12312 | 0:218d3fb75950 | 185 | uLCD.filled_rectangle(x1, y1, x2, y2, BLUE); |
soapy12312 | 0:218d3fb75950 | 186 | // If Pac-Man is not currently invincible |
soapy12312 | 0:218d3fb75950 | 187 | } else { |
soapy12312 | 0:218d3fb75950 | 188 | // Draw the ghost in its original color |
soapy12312 | 0:218d3fb75950 | 189 | uLCD.filled_rectangle(x1, y1, x2, y2, color); |
soapy12312 | 0:218d3fb75950 | 190 | } |
soapy12312 | 0:218d3fb75950 | 191 | |
soapy12312 | 0:218d3fb75950 | 192 | // Specify the pixels |
soapy12312 | 0:218d3fb75950 | 193 | uLCD.pixel(x1, y1, BLACK); |
soapy12312 | 0:218d3fb75950 | 194 | uLCD.pixel(x2, y1, BLACK); |
soapy12312 | 0:218d3fb75950 | 195 | uLCD.pixel(xCoordinate - 2, y2, BLACK); |
soapy12312 | 0:218d3fb75950 | 196 | uLCD.pixel(xCoordinate, y2, BLACK); |
soapy12312 | 0:218d3fb75950 | 197 | uLCD.pixel(xCoordinate + 2, y2, BLACK); |
soapy12312 | 0:218d3fb75950 | 198 | uLCD.pixel(x1, yCoordinate - 1, WHITE); |
soapy12312 | 0:218d3fb75950 | 199 | uLCD.pixel(xCoordinate - 2, yCoordinate - 2, WHITE); |
soapy12312 | 0:218d3fb75950 | 200 | uLCD.pixel(xCoordinate - 2, yCoordinate - 1, WHITE); |
soapy12312 | 0:218d3fb75950 | 201 | uLCD.pixel(xCoordinate - 2, yCoordinate, WHITE); |
soapy12312 | 0:218d3fb75950 | 202 | uLCD.pixel(xCoordinate - 1, yCoordinate - 1, WHITE); |
soapy12312 | 0:218d3fb75950 | 203 | uLCD.pixel(x2, yCoordinate - 1, WHITE); |
soapy12312 | 0:218d3fb75950 | 204 | uLCD.pixel(xCoordinate + 2, yCoordinate - 2, WHITE); |
soapy12312 | 0:218d3fb75950 | 205 | uLCD.pixel(xCoordinate + 2, yCoordinate - 1, WHITE); |
soapy12312 | 0:218d3fb75950 | 206 | uLCD.pixel(xCoordinate + 2, yCoordinate, WHITE); |
soapy12312 | 0:218d3fb75950 | 207 | uLCD.pixel(xCoordinate + 1, yCoordinate - 1, WHITE); |
soapy12312 | 0:218d3fb75950 | 208 | |
soapy12312 | 0:218d3fb75950 | 209 | // Draw the ghost's eyes based on its current direction |
soapy12312 | 0:218d3fb75950 | 210 | switch (direction) { |
soapy12312 | 0:218d3fb75950 | 211 | case FACERIGHT: |
soapy12312 | 0:218d3fb75950 | 212 | uLCD.pixel(xCoordinate - 1, yCoordinate - 1, BLACK); |
soapy12312 | 0:218d3fb75950 | 213 | uLCD.pixel(x2, yCoordinate - 1, BLACK); |
soapy12312 | 0:218d3fb75950 | 214 | break; |
soapy12312 | 0:218d3fb75950 | 215 | case FACEDOWN: |
soapy12312 | 0:218d3fb75950 | 216 | uLCD.pixel(xCoordinate - 2, yCoordinate, BLACK); |
soapy12312 | 0:218d3fb75950 | 217 | uLCD.pixel(xCoordinate + 2, yCoordinate, BLACK); |
soapy12312 | 0:218d3fb75950 | 218 | break; |
soapy12312 | 0:218d3fb75950 | 219 | case FACELEFT: |
soapy12312 | 0:218d3fb75950 | 220 | uLCD.pixel(x1, yCoordinate - 1, BLACK); |
soapy12312 | 0:218d3fb75950 | 221 | uLCD.pixel(xCoordinate + 1, yCoordinate - 1, BLACK); |
soapy12312 | 0:218d3fb75950 | 222 | break; |
soapy12312 | 0:218d3fb75950 | 223 | case FACEUP: |
soapy12312 | 0:218d3fb75950 | 224 | uLCD.pixel(xCoordinate - 2, yCoordinate - 2, BLACK); |
soapy12312 | 0:218d3fb75950 | 225 | uLCD.pixel(xCoordinate + 2, yCoordinate - 2, BLACK); |
soapy12312 | 0:218d3fb75950 | 226 | break; |
soapy12312 | 0:218d3fb75950 | 227 | default: |
soapy12312 | 0:218d3fb75950 | 228 | break; |
soapy12312 | 0:218d3fb75950 | 229 | } |
soapy12312 | 0:218d3fb75950 | 230 | } |
soapy12312 | 0:218d3fb75950 | 231 | |
soapy12312 | 0:218d3fb75950 | 232 | // Erase the ghost from the given position |
soapy12312 | 0:218d3fb75950 | 233 | void erase(int xCoordinate, int yCoordinate) { |
soapy12312 | 0:218d3fb75950 | 234 | int x1 = xCoordinate - 3; |
soapy12312 | 0:218d3fb75950 | 235 | int x2 = xCoordinate + 3; |
soapy12312 | 0:218d3fb75950 | 236 | int y1 = yCoordinate - 3; |
soapy12312 | 0:218d3fb75950 | 237 | int y2 = yCoordinate + 3; |
soapy12312 | 0:218d3fb75950 | 238 | uLCD.filled_rectangle(x1, y1, x2, y2, BLACK); |
soapy12312 | 0:218d3fb75950 | 239 | } |
soapy12312 | 0:218d3fb75950 | 240 | |
soapy12312 | 0:218d3fb75950 | 241 | // Move the ghost right |
soapy12312 | 0:218d3fb75950 | 242 | void moveRight() { |
soapy12312 | 0:218d3fb75950 | 243 | // If the space to the right is a valid position and is not the rightmost space |
soapy12312 | 0:218d3fb75950 | 244 | if ((stage.positions[x + 1][y] != 0) && (x != 124)) { |
soapy12312 | 0:218d3fb75950 | 245 | // The ghost is mobile |
soapy12312 | 0:218d3fb75950 | 246 | immobile = false; |
soapy12312 | 0:218d3fb75950 | 247 | // Erase the ghost from its current position |
soapy12312 | 0:218d3fb75950 | 248 | erase(x, y); |
soapy12312 | 0:218d3fb75950 | 249 | // Increment the ghost's x-coordinate |
soapy12312 | 0:218d3fb75950 | 250 | x++; |
soapy12312 | 0:218d3fb75950 | 251 | |
soapy12312 | 0:218d3fb75950 | 252 | // If passed over a pac dot |
soapy12312 | 0:218d3fb75950 | 253 | if (stage.positions[x - 4][y] == 2) { |
soapy12312 | 0:218d3fb75950 | 254 | // Redraw the pac dot |
soapy12312 | 0:218d3fb75950 | 255 | uLCD.pixel(x - 4, y, YELLOW); |
soapy12312 | 0:218d3fb75950 | 256 | // If passed over a big pac dot |
soapy12312 | 0:218d3fb75950 | 257 | } else { |
soapy12312 | 0:218d3fb75950 | 258 | // Redraw the big pac dot |
soapy12312 | 0:218d3fb75950 | 259 | for (int i = x - 2; i >= x - 5; i--) { |
soapy12312 | 0:218d3fb75950 | 260 | if (stage.positions[i][y] == 3) { |
soapy12312 | 0:218d3fb75950 | 261 | uLCD.filled_circle(i, y, 1, YELLOW); |
soapy12312 | 0:218d3fb75950 | 262 | } |
soapy12312 | 0:218d3fb75950 | 263 | } |
soapy12312 | 0:218d3fb75950 | 264 | } |
soapy12312 | 0:218d3fb75950 | 265 | |
soapy12312 | 0:218d3fb75950 | 266 | // Draw the ghost in its new position |
soapy12312 | 0:218d3fb75950 | 267 | draw(x, y); |
soapy12312 | 0:218d3fb75950 | 268 | // If the space to the right is not a valid positions |
soapy12312 | 0:218d3fb75950 | 269 | } else if (stage.positions[x + 1][y] == 0) { |
soapy12312 | 0:218d3fb75950 | 270 | // The ghost is immobile |
soapy12312 | 0:218d3fb75950 | 271 | immobile = true; |
soapy12312 | 0:218d3fb75950 | 272 | wait(0.03); |
soapy12312 | 0:218d3fb75950 | 273 | } |
soapy12312 | 0:218d3fb75950 | 274 | |
soapy12312 | 0:218d3fb75950 | 275 | // If the rightmost position is reached |
soapy12312 | 0:218d3fb75950 | 276 | if (x == 124) { |
soapy12312 | 0:218d3fb75950 | 277 | // The ghost is mobile |
soapy12312 | 0:218d3fb75950 | 278 | immobile = false; |
soapy12312 | 0:218d3fb75950 | 279 | // Erase the ghost from its current position |
soapy12312 | 0:218d3fb75950 | 280 | erase(x, y); |
soapy12312 | 0:218d3fb75950 | 281 | // Set the ghost's x-coordinate to 4 |
soapy12312 | 0:218d3fb75950 | 282 | x = 4; |
soapy12312 | 0:218d3fb75950 | 283 | |
soapy12312 | 0:218d3fb75950 | 284 | // If passed over a pac dot |
soapy12312 | 0:218d3fb75950 | 285 | if (stage.positions[124][68] == 2) { |
soapy12312 | 0:218d3fb75950 | 286 | // Redraw the pac dot |
soapy12312 | 0:218d3fb75950 | 287 | uLCD.pixel(124, 68, YELLOW); |
soapy12312 | 0:218d3fb75950 | 288 | } |
soapy12312 | 0:218d3fb75950 | 289 | |
soapy12312 | 0:218d3fb75950 | 290 | // Draw the ghost in its new position |
soapy12312 | 0:218d3fb75950 | 291 | draw(x, y); |
soapy12312 | 0:218d3fb75950 | 292 | } |
soapy12312 | 0:218d3fb75950 | 293 | |
soapy12312 | 0:218d3fb75950 | 294 | // If Pac-Man's invincibility status changed |
soapy12312 | 0:218d3fb75950 | 295 | if (pacman.powerUpChanged == true) { |
soapy12312 | 0:218d3fb75950 | 296 | // Change the color of the ghost |
soapy12312 | 0:218d3fb75950 | 297 | draw(x, y); |
soapy12312 | 0:218d3fb75950 | 298 | // Reset Pac-Man's invincibility status change |
soapy12312 | 0:218d3fb75950 | 299 | pacman.powerUpChanged = false; |
soapy12312 | 0:218d3fb75950 | 300 | } |
soapy12312 | 0:218d3fb75950 | 301 | |
soapy12312 | 0:218d3fb75950 | 302 | // Set the ghost's next direction |
soapy12312 | 0:218d3fb75950 | 303 | switch (nextDirection) { |
soapy12312 | 0:218d3fb75950 | 304 | case FACEDOWN: |
soapy12312 | 0:218d3fb75950 | 305 | if (stage.positions[x][y + 1] != 0) { |
soapy12312 | 0:218d3fb75950 | 306 | direction = nextDirection; |
soapy12312 | 0:218d3fb75950 | 307 | } |
soapy12312 | 0:218d3fb75950 | 308 | |
soapy12312 | 0:218d3fb75950 | 309 | break; |
soapy12312 | 0:218d3fb75950 | 310 | case FACELEFT: |
soapy12312 | 0:218d3fb75950 | 311 | if (stage.positions[x - 1][y] != 0) { |
soapy12312 | 0:218d3fb75950 | 312 | direction = nextDirection; |
soapy12312 | 0:218d3fb75950 | 313 | } |
soapy12312 | 0:218d3fb75950 | 314 | |
soapy12312 | 0:218d3fb75950 | 315 | break; |
soapy12312 | 0:218d3fb75950 | 316 | case FACEUP: |
soapy12312 | 0:218d3fb75950 | 317 | if (stage.positions[x][y - 1] != 0) { |
soapy12312 | 0:218d3fb75950 | 318 | direction = nextDirection; |
soapy12312 | 0:218d3fb75950 | 319 | } |
soapy12312 | 0:218d3fb75950 | 320 | |
soapy12312 | 0:218d3fb75950 | 321 | break; |
soapy12312 | 0:218d3fb75950 | 322 | default: |
soapy12312 | 0:218d3fb75950 | 323 | break; |
soapy12312 | 0:218d3fb75950 | 324 | } |
soapy12312 | 0:218d3fb75950 | 325 | } |
soapy12312 | 0:218d3fb75950 | 326 | |
soapy12312 | 0:218d3fb75950 | 327 | // Move the ghost down |
soapy12312 | 0:218d3fb75950 | 328 | void moveDown() { |
soapy12312 | 0:218d3fb75950 | 329 | // If the space in the downward direction is a valid position |
soapy12312 | 0:218d3fb75950 | 330 | if (stage.positions[x][y + 1] != 0) { |
soapy12312 | 0:218d3fb75950 | 331 | // The ghost is mobile |
soapy12312 | 0:218d3fb75950 | 332 | immobile = false; |
soapy12312 | 0:218d3fb75950 | 333 | // Erase the ghost from its current position |
soapy12312 | 0:218d3fb75950 | 334 | erase(x, y); |
soapy12312 | 0:218d3fb75950 | 335 | // Increment the ghost's y-coordinate |
soapy12312 | 0:218d3fb75950 | 336 | y++; |
soapy12312 | 0:218d3fb75950 | 337 | |
soapy12312 | 0:218d3fb75950 | 338 | // If passed over a pac dot |
soapy12312 | 0:218d3fb75950 | 339 | if (stage.positions[x][y - 4] == 2) { |
soapy12312 | 0:218d3fb75950 | 340 | // Redraw the pac dot |
soapy12312 | 0:218d3fb75950 | 341 | uLCD.pixel(x, y - 4, YELLOW); |
soapy12312 | 0:218d3fb75950 | 342 | // If passed over a big pac dot |
soapy12312 | 0:218d3fb75950 | 343 | } else { |
soapy12312 | 0:218d3fb75950 | 344 | // Redraw the big pac dot |
soapy12312 | 0:218d3fb75950 | 345 | for (int j = y - 2; j >= y - 5; j--) { |
soapy12312 | 0:218d3fb75950 | 346 | if (stage.positions[x][j] == 3) { |
soapy12312 | 0:218d3fb75950 | 347 | uLCD.filled_circle(x, j, 1, YELLOW); |
soapy12312 | 0:218d3fb75950 | 348 | } |
soapy12312 | 0:218d3fb75950 | 349 | } |
soapy12312 | 0:218d3fb75950 | 350 | } |
soapy12312 | 0:218d3fb75950 | 351 | |
soapy12312 | 0:218d3fb75950 | 352 | // Draw the ghost in its new position |
soapy12312 | 0:218d3fb75950 | 353 | draw(x, y); |
soapy12312 | 0:218d3fb75950 | 354 | // If the space in the downward direction is not a valid position |
soapy12312 | 0:218d3fb75950 | 355 | } else { |
soapy12312 | 0:218d3fb75950 | 356 | // The ghost is immobile |
soapy12312 | 0:218d3fb75950 | 357 | immobile = true; |
soapy12312 | 0:218d3fb75950 | 358 | wait(0.03); |
soapy12312 | 0:218d3fb75950 | 359 | } |
soapy12312 | 0:218d3fb75950 | 360 | |
soapy12312 | 0:218d3fb75950 | 361 | // If Pac-Man's invincibility status changed |
soapy12312 | 0:218d3fb75950 | 362 | if (pacman.powerUpChanged == true) { |
soapy12312 | 0:218d3fb75950 | 363 | // Change the color of the ghost |
soapy12312 | 0:218d3fb75950 | 364 | draw(x, y); |
soapy12312 | 0:218d3fb75950 | 365 | // Reset Pac-Man's invincibility status change |
soapy12312 | 0:218d3fb75950 | 366 | pacman.powerUpChanged = false; |
soapy12312 | 0:218d3fb75950 | 367 | } |
soapy12312 | 0:218d3fb75950 | 368 | |
soapy12312 | 0:218d3fb75950 | 369 | // Set the ghost's next direction |
soapy12312 | 0:218d3fb75950 | 370 | switch (nextDirection) { |
soapy12312 | 0:218d3fb75950 | 371 | case FACERIGHT: |
soapy12312 | 0:218d3fb75950 | 372 | if (stage.positions[x + 1][y] != 0) { |
soapy12312 | 0:218d3fb75950 | 373 | direction = nextDirection; |
soapy12312 | 0:218d3fb75950 | 374 | } |
soapy12312 | 0:218d3fb75950 | 375 | |
soapy12312 | 0:218d3fb75950 | 376 | break; |
soapy12312 | 0:218d3fb75950 | 377 | case FACELEFT: |
soapy12312 | 0:218d3fb75950 | 378 | if (stage.positions[x - 1][y] != 0) { |
soapy12312 | 0:218d3fb75950 | 379 | direction = nextDirection; |
soapy12312 | 0:218d3fb75950 | 380 | } |
soapy12312 | 0:218d3fb75950 | 381 | |
soapy12312 | 0:218d3fb75950 | 382 | break; |
soapy12312 | 0:218d3fb75950 | 383 | case FACEUP: |
soapy12312 | 0:218d3fb75950 | 384 | if (stage.positions[x][y - 1] != 0) { |
soapy12312 | 0:218d3fb75950 | 385 | direction = nextDirection; |
soapy12312 | 0:218d3fb75950 | 386 | } |
soapy12312 | 0:218d3fb75950 | 387 | |
soapy12312 | 0:218d3fb75950 | 388 | break; |
soapy12312 | 0:218d3fb75950 | 389 | default: |
soapy12312 | 0:218d3fb75950 | 390 | break; |
soapy12312 | 0:218d3fb75950 | 391 | } |
soapy12312 | 0:218d3fb75950 | 392 | } |
soapy12312 | 0:218d3fb75950 | 393 | |
soapy12312 | 0:218d3fb75950 | 394 | // Move the ghost left |
soapy12312 | 0:218d3fb75950 | 395 | void moveLeft() { |
soapy12312 | 0:218d3fb75950 | 396 | // If the space to the left is a valid position and is not the leftmost space |
soapy12312 | 0:218d3fb75950 | 397 | if ((stage.positions[x - 1][y] != 0) && (x != 4)) { |
soapy12312 | 0:218d3fb75950 | 398 | // The ghost is mobile |
soapy12312 | 0:218d3fb75950 | 399 | immobile = false; |
soapy12312 | 0:218d3fb75950 | 400 | // Erase the ghost from its current position |
soapy12312 | 0:218d3fb75950 | 401 | erase(x, y); |
soapy12312 | 0:218d3fb75950 | 402 | // Decrement the ghost's x-coordinate |
soapy12312 | 0:218d3fb75950 | 403 | x--; |
soapy12312 | 0:218d3fb75950 | 404 | |
soapy12312 | 0:218d3fb75950 | 405 | // If passed over a pac dot |
soapy12312 | 0:218d3fb75950 | 406 | if (stage.positions[x + 4][y] == 2) { |
soapy12312 | 0:218d3fb75950 | 407 | // Redraw the pac dot |
soapy12312 | 0:218d3fb75950 | 408 | uLCD.pixel(x + 4, y, YELLOW); |
soapy12312 | 0:218d3fb75950 | 409 | // If passed over a big pac dot |
soapy12312 | 0:218d3fb75950 | 410 | } else { |
soapy12312 | 0:218d3fb75950 | 411 | // Redraw the big pac dot |
soapy12312 | 0:218d3fb75950 | 412 | for (int i = x + 2; i <= x + 5; i++) { |
soapy12312 | 0:218d3fb75950 | 413 | if (stage.positions[i][y] == 3) { |
soapy12312 | 0:218d3fb75950 | 414 | uLCD.filled_circle(i, y, 1, YELLOW); |
soapy12312 | 0:218d3fb75950 | 415 | } |
soapy12312 | 0:218d3fb75950 | 416 | } |
soapy12312 | 0:218d3fb75950 | 417 | } |
soapy12312 | 0:218d3fb75950 | 418 | |
soapy12312 | 0:218d3fb75950 | 419 | // Draw the ghost in its new position |
soapy12312 | 0:218d3fb75950 | 420 | draw(x, y); |
soapy12312 | 0:218d3fb75950 | 421 | // If the space to the left is not a valid position |
soapy12312 | 0:218d3fb75950 | 422 | } else if (stage.positions[x - 1][y] == 0) { |
soapy12312 | 0:218d3fb75950 | 423 | // The ghost is immobile |
soapy12312 | 0:218d3fb75950 | 424 | immobile = true; |
soapy12312 | 0:218d3fb75950 | 425 | wait(0.03); |
soapy12312 | 0:218d3fb75950 | 426 | } |
soapy12312 | 0:218d3fb75950 | 427 | |
soapy12312 | 0:218d3fb75950 | 428 | // If the leftmost position is reached |
soapy12312 | 0:218d3fb75950 | 429 | if (x == 4) { |
soapy12312 | 0:218d3fb75950 | 430 | // The ghost is mobile |
soapy12312 | 0:218d3fb75950 | 431 | immobile = false; |
soapy12312 | 0:218d3fb75950 | 432 | // Erase the ghost from its current position |
soapy12312 | 0:218d3fb75950 | 433 | erase(x, y); |
soapy12312 | 0:218d3fb75950 | 434 | // Set the ghost's x-coordinate to 124 |
soapy12312 | 0:218d3fb75950 | 435 | x = 124; |
soapy12312 | 0:218d3fb75950 | 436 | |
soapy12312 | 0:218d3fb75950 | 437 | // If passed over a pac dot |
soapy12312 | 0:218d3fb75950 | 438 | if (stage.positions[4][68] == 2) { |
soapy12312 | 0:218d3fb75950 | 439 | // Redraw the pac dot |
soapy12312 | 0:218d3fb75950 | 440 | uLCD.pixel(4, 68, YELLOW); |
soapy12312 | 0:218d3fb75950 | 441 | } |
soapy12312 | 0:218d3fb75950 | 442 | |
soapy12312 | 0:218d3fb75950 | 443 | // Draw the ghost in its new position |
soapy12312 | 0:218d3fb75950 | 444 | draw(x, y); |
soapy12312 | 0:218d3fb75950 | 445 | } |
soapy12312 | 0:218d3fb75950 | 446 | |
soapy12312 | 0:218d3fb75950 | 447 | // If Pac-Man's invincibility status changed |
soapy12312 | 0:218d3fb75950 | 448 | if (pacman.powerUpChanged == true) { |
soapy12312 | 0:218d3fb75950 | 449 | // Change the color of the ghost |
soapy12312 | 0:218d3fb75950 | 450 | draw(x, y); |
soapy12312 | 0:218d3fb75950 | 451 | // Reset Pac-Man's invincibility status change |
soapy12312 | 0:218d3fb75950 | 452 | pacman.powerUpChanged = false; |
soapy12312 | 0:218d3fb75950 | 453 | } |
soapy12312 | 0:218d3fb75950 | 454 | |
soapy12312 | 0:218d3fb75950 | 455 | // Set the ghost's next direction |
soapy12312 | 0:218d3fb75950 | 456 | switch (nextDirection) { |
soapy12312 | 0:218d3fb75950 | 457 | case FACERIGHT: |
soapy12312 | 0:218d3fb75950 | 458 | if (stage.positions[x + 1][y] != 0) { |
soapy12312 | 0:218d3fb75950 | 459 | direction = nextDirection; |
soapy12312 | 0:218d3fb75950 | 460 | } |
soapy12312 | 0:218d3fb75950 | 461 | |
soapy12312 | 0:218d3fb75950 | 462 | break; |
soapy12312 | 0:218d3fb75950 | 463 | case FACEDOWN: |
soapy12312 | 0:218d3fb75950 | 464 | if (stage.positions[x][y + 1] != 0) { |
soapy12312 | 0:218d3fb75950 | 465 | direction = nextDirection; |
soapy12312 | 0:218d3fb75950 | 466 | } |
soapy12312 | 0:218d3fb75950 | 467 | |
soapy12312 | 0:218d3fb75950 | 468 | break; |
soapy12312 | 0:218d3fb75950 | 469 | case FACEUP: |
soapy12312 | 0:218d3fb75950 | 470 | if (stage.positions[x][y - 1] != 0) { |
soapy12312 | 0:218d3fb75950 | 471 | direction = nextDirection; |
soapy12312 | 0:218d3fb75950 | 472 | } |
soapy12312 | 0:218d3fb75950 | 473 | |
soapy12312 | 0:218d3fb75950 | 474 | break; |
soapy12312 | 0:218d3fb75950 | 475 | default: |
soapy12312 | 0:218d3fb75950 | 476 | break; |
soapy12312 | 0:218d3fb75950 | 477 | } |
soapy12312 | 0:218d3fb75950 | 478 | } |
soapy12312 | 0:218d3fb75950 | 479 | |
soapy12312 | 0:218d3fb75950 | 480 | // Move the ghost up |
soapy12312 | 0:218d3fb75950 | 481 | void moveUp() { |
soapy12312 | 0:218d3fb75950 | 482 | // If the space in the upward direction is a valid position |
soapy12312 | 0:218d3fb75950 | 483 | if (stage.positions[x][y - 1] != 0) { |
soapy12312 | 0:218d3fb75950 | 484 | // The ghost is mobile |
soapy12312 | 0:218d3fb75950 | 485 | immobile = false; |
soapy12312 | 0:218d3fb75950 | 486 | // Erase the ghost from its current position |
soapy12312 | 0:218d3fb75950 | 487 | erase(x, y); |
soapy12312 | 0:218d3fb75950 | 488 | // Decrement the ghost's y-coordinate |
soapy12312 | 0:218d3fb75950 | 489 | y--; |
soapy12312 | 0:218d3fb75950 | 490 | |
soapy12312 | 0:218d3fb75950 | 491 | // If passed over a pac dot |
soapy12312 | 0:218d3fb75950 | 492 | if (stage.positions[x][y + 4] == 2) { |
soapy12312 | 0:218d3fb75950 | 493 | // Redraw the pac dot |
soapy12312 | 0:218d3fb75950 | 494 | uLCD.pixel(x, y + 4, YELLOW); |
soapy12312 | 0:218d3fb75950 | 495 | // If passed over a big pac dot |
soapy12312 | 0:218d3fb75950 | 496 | } else { |
soapy12312 | 0:218d3fb75950 | 497 | // Redraw the big pac dot |
soapy12312 | 0:218d3fb75950 | 498 | for (int j = y + 2; j <= y + 5; j++) { |
soapy12312 | 0:218d3fb75950 | 499 | if (stage.positions[x][j] == 3) { |
soapy12312 | 0:218d3fb75950 | 500 | uLCD.filled_circle(x, j, 1, YELLOW); |
soapy12312 | 0:218d3fb75950 | 501 | } |
soapy12312 | 0:218d3fb75950 | 502 | } |
soapy12312 | 0:218d3fb75950 | 503 | } |
soapy12312 | 0:218d3fb75950 | 504 | |
soapy12312 | 0:218d3fb75950 | 505 | // Draw the ghost in its new position |
soapy12312 | 0:218d3fb75950 | 506 | draw(x, y); |
soapy12312 | 0:218d3fb75950 | 507 | // If the space in the upward direction is not a valid position |
soapy12312 | 0:218d3fb75950 | 508 | } else { |
soapy12312 | 0:218d3fb75950 | 509 | // The ghost is immobile |
soapy12312 | 0:218d3fb75950 | 510 | immobile = true; |
soapy12312 | 0:218d3fb75950 | 511 | wait(0.03); |
soapy12312 | 0:218d3fb75950 | 512 | } |
soapy12312 | 0:218d3fb75950 | 513 | |
soapy12312 | 0:218d3fb75950 | 514 | // If Pac-Man's invincibility status changed |
soapy12312 | 0:218d3fb75950 | 515 | if (pacman.powerUpChanged == true) { |
soapy12312 | 0:218d3fb75950 | 516 | // Change the color of the ghost |
soapy12312 | 0:218d3fb75950 | 517 | draw(x, y); |
soapy12312 | 0:218d3fb75950 | 518 | // Reset Pac-Man's invincibility status change |
soapy12312 | 0:218d3fb75950 | 519 | pacman.powerUpChanged = false; |
soapy12312 | 0:218d3fb75950 | 520 | } |
soapy12312 | 0:218d3fb75950 | 521 | |
soapy12312 | 0:218d3fb75950 | 522 | // Set the ghost's next direction |
soapy12312 | 0:218d3fb75950 | 523 | switch (nextDirection) { |
soapy12312 | 0:218d3fb75950 | 524 | case FACERIGHT: |
soapy12312 | 0:218d3fb75950 | 525 | if (stage.positions[x + 1][y] != 0) { |
soapy12312 | 0:218d3fb75950 | 526 | direction = nextDirection; |
soapy12312 | 0:218d3fb75950 | 527 | } |
soapy12312 | 0:218d3fb75950 | 528 | |
soapy12312 | 0:218d3fb75950 | 529 | break; |
soapy12312 | 0:218d3fb75950 | 530 | case FACEDOWN: |
soapy12312 | 0:218d3fb75950 | 531 | if (stage.positions[x][y + 1] != 0) { |
soapy12312 | 0:218d3fb75950 | 532 | direction = nextDirection; |
soapy12312 | 0:218d3fb75950 | 533 | } |
soapy12312 | 0:218d3fb75950 | 534 | |
soapy12312 | 0:218d3fb75950 | 535 | break; |
soapy12312 | 0:218d3fb75950 | 536 | case FACELEFT: |
soapy12312 | 0:218d3fb75950 | 537 | if (stage.positions[x - 1][y] != 0) { |
soapy12312 | 0:218d3fb75950 | 538 | direction = nextDirection; |
soapy12312 | 0:218d3fb75950 | 539 | } |
soapy12312 | 0:218d3fb75950 | 540 | |
soapy12312 | 0:218d3fb75950 | 541 | break; |
soapy12312 | 0:218d3fb75950 | 542 | default: |
soapy12312 | 0:218d3fb75950 | 543 | break; |
soapy12312 | 0:218d3fb75950 | 544 | } |
soapy12312 | 0:218d3fb75950 | 545 | } |
soapy12312 | 0:218d3fb75950 | 546 | }; |