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